Skip to content

Commit

Permalink
changed signature of Policy.isSemanticallySameAs to check for another…
Browse files Browse the repository at this point in the history
… Policy

Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed Sep 21, 2022
1 parent 3011222 commit 12dff0f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
Expand Up @@ -438,9 +438,9 @@ public Stream<PolicyEntry> stream() {
}

@Override
public boolean isSemanticallySameAs(final Collection<PolicyEntry> otherPolicyEntries) {
public boolean isSemanticallySameAs(final Policy otherPolicy) {
// sorting via the policy entry label is required in order to not depend on the order:
final Iterator<PolicyEntry> others = otherPolicyEntries.stream()
final Iterator<PolicyEntry> others = otherPolicy.stream()
.sorted(Comparator.comparing(e -> e.getLabel().toString()))
.collect(Collectors.toCollection(LinkedHashSet::new))
.iterator();
Expand Down
Expand Up @@ -14,7 +14,6 @@

import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;

import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -367,16 +366,14 @@ Optional<EffectedPermissions> getEffectedPermissionsFor(CharSequence label, Subj
Stream<PolicyEntry> stream();

/**
* Checks if the passed {@code otherPolicyEntries} are semantically the same as the in this policy contained policy
* entries.
* Checks if the passed {@code otherPolicy} is semantically the same as this policy.
* I.e. that those contain the same policy entries with the same subject ids having the same resources.
*
* @param otherPolicyEntries the other policy entries to check against.
* @return {@code true} if the other policy entries are semantically the same as the in this policy contained policy
* entries.
* @param otherPolicy the other policy to check against.
* @return {@code true} if the other policy is semantically the same as this policy.
* @since 3.0.0
*/
boolean isSemanticallySameAs(Collection<PolicyEntry> otherPolicyEntries);
boolean isSemanticallySameAs(Policy otherPolicy);

/**
* Returns a JSON object representation of this policy to embed in another JSON object.
Expand Down
Expand Up @@ -355,7 +355,7 @@ public void ensureTwoPoliciesAreSemanticallyTheSameIfOnlySubjectPayloadDiffers()
.setResource(resource1)
.build();

assertThat(policyA.isSemanticallySameAs(policyB.getEntriesSet())).isTrue();
assertThat(policyA.isSemanticallySameAs(policyB)).isTrue();
}

@Test
Expand Down Expand Up @@ -383,7 +383,7 @@ public void ensureTwoPoliciesAreSemanticallyDifferentIfSubjectIdsDiffer() {
.setResource(resource2)
.build();

assertThat(policyA.isSemanticallySameAs(policyB.getEntriesSet())).isFalse();
assertThat(policyA.isSemanticallySameAs(policyB)).isFalse();
}

@Test
Expand Down Expand Up @@ -412,7 +412,7 @@ public void ensureTwoPoliciesAreSemanticallyDifferentIfResourcesDiffer() {
.setResource(resource3)
.build();

assertThat(policyA.isSemanticallySameAs(policyB.getEntriesSet())).isFalse();
assertThat(policyA.isSemanticallySameAs(policyB)).isFalse();
}

@Test
Expand Down Expand Up @@ -442,7 +442,7 @@ public void ensureTwoPoliciesAreSemanticallyDifferentIfOneContainsMoreEntries()
.setResource(resource2)
.build();

assertThat(policyA.isSemanticallySameAs(policyB.getEntriesSet())).isFalse();
assertThat(policyA.isSemanticallySameAs(policyB)).isFalse();
}

}
Expand Up @@ -190,7 +190,7 @@ protected void publishEvent(@Nullable final Policy previousEntity, final PolicyE

if (null != previousEntity && null != entity) {
if (previousEntity.hasLifecycle(PolicyLifecycle.DELETED) || entityExistsAsDeleted() ||
!previousEntity.isSemanticallySameAs(entity.getEntriesSet())) {
!previousEntity.isSemanticallySameAs(entity)) {
// however only publish the PolicyTag when the Policy semantically changed
// (e.g. not when only a "subject" payload changed but not the subjectId itself)
publishPolicyTag(event);
Expand Down

0 comments on commit 12dff0f

Please sign in to comment.