Skip to content

Commit

Permalink
feat: filter remedies in prohibitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Apr 5, 2024
1 parent eff6a21 commit 5c992a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public ScopeFilter(RuleBindingRegistry registry) {
}

public Policy applyScope(Policy policy, String scope) {
var filteredObligations = policy.getObligations().stream().map(d -> applyScope(d, scope)).filter(Objects::nonNull).collect(toList());
var filteredPermissions = policy.getPermissions().stream().map(p -> applyScope(p, scope)).filter(Objects::nonNull).collect(toList());
var filteredProhibitions = policy.getProhibitions().stream().map(p -> applyScope(p, scope)).filter(Objects::nonNull).collect(toList());
var filteredObligations = policy.getObligations().stream().map(d -> applyScope(d, scope)).filter(Objects::nonNull).toList();
var filteredPermissions = policy.getPermissions().stream().map(p -> applyScope(p, scope)).filter(Objects::nonNull).toList();
var filteredProhibitions = policy.getProhibitions().stream().map(p -> applyScope(p, scope)).filter(Objects::nonNull).toList();
return Policy.Builder.newInstance()
.type(policy.getType())
.assignee(policy.getAssignee())
Expand Down Expand Up @@ -112,10 +112,15 @@ Prohibition applyScope(Prohibition prohibition, String scope) {
return null;
}
var filteredConstraints = applyScope(prohibition.getConstraints(), scope);
var filteredRemedies = prohibition.getRemedies().stream()
.map(remedy -> applyScope(remedy, scope))
.filter(Objects::nonNull)
.toList();

return Prohibition.Builder.newInstance()
.action(prohibition.getAction())
.constraints(filteredConstraints)
.remedies(filteredRemedies)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,19 @@ void verifyFiltersProhibitionType() {
.action(REPORT_ACTION)
.constraint(BOUND_CONSTRAINT)
.constraint(UNBOUND_CONSTRAINT)
.remedy(Duty.Builder.newInstance().constraint(BOUND_CONSTRAINT).build())
.remedy(Duty.Builder.newInstance().constraint(UNBOUND_CONSTRAINT).build())
.build();

var filteredPermission = scopeFilter.applyScope(prohibition, BOUND_SCOPE);

assertThat(filteredPermission).isNotNull();
assertThat(filteredPermission.getAction()).isNotNull();
assertThat(filteredPermission.getConstraints().size()).isEqualTo(1); // verify that the unbound constraint was removed
assertThat(filteredPermission.getConstraints()).hasSize(1); // verify that the unbound constraint was removed
assertThat(filteredPermission.getConstraints()).contains(BOUND_CONSTRAINT);
assertThat(filteredPermission.getRemedies()).hasSize(2)
.anyMatch(it -> it.getConstraints().contains(BOUND_CONSTRAINT))
.anyMatch(it -> it.getConstraints().isEmpty());
}

@Test
Expand Down

0 comments on commit 5c992a0

Please sign in to comment.