Skip to content

Commit

Permalink
feat: fail validation if muliple operands in a Logical Constraint are…
Browse files Browse the repository at this point in the history
… found (#4371)
  • Loading branch information
ndr-brt authored Jul 23, 2024
1 parent 4e14377 commit f9f4d18
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,15 @@ private record ConstraintValidator(JsonLdPath path) implements Validator<JsonObj

@Override
public ValidationResult validate(JsonObject input) {
if (LOGICAL_CONSTRAINTS.stream().anyMatch(input::containsKey)) {
var logicalOperands = input.keySet().stream().filter(LOGICAL_CONSTRAINTS::contains).toList();

if (logicalOperands.size() > 1) {
return ValidationResult.failure(
violation("Cannot define multiple operands in a Logical Constraint: %s".formatted(logicalOperands),
path.toString()));
}

if (logicalOperands.size() == 1) {
return ValidationResult.success();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_CONSTRAINT_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_LEFT_OPERAND_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_OPERATOR_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_OR_CONSTRAINT_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PERMISSION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_SET;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROFILE_ATTRIBUTE;
Expand Down Expand Up @@ -202,6 +203,31 @@ void shouldSucceed_whenLogicalConstraintIsPresent() {
assertThat(result).isSucceeded();
}

@Test
void shouldFail_whenLogicalConstraintWithMultipleOperandsIsPresent() {
var permission = createArrayBuilder().add(createObjectBuilder()
.add(ODRL_ACTION_ATTRIBUTE, createValidAction())
.add(ODRL_CONSTRAINT_ATTRIBUTE, createArrayBuilder().add(createObjectBuilder()
.add(ODRL_AND_CONSTRAINT_ATTRIBUTE, createArrayBuilder())
.add(ODRL_OR_CONSTRAINT_ATTRIBUTE, createArrayBuilder())
)));
var policy = createObjectBuilder()
.add(TYPE, policySet())
.add(ODRL_PERMISSION_ATTRIBUTE, permission);
var policyDefinition = createObjectBuilder()
.add(EDC_POLICY_DEFINITION_POLICY, createArrayBuilder().add(policy))
.build();

var result = validator.validate(policyDefinition);

assertThat(result).isFailed().extracting(ValidationFailure::getViolations).asInstanceOf(list(Violation.class))
.hasSize(1)
.anySatisfy(violation -> {
assertThat(violation.message()).contains("multiple operands");
assertThat(violation.path()).endsWith(ODRL_CONSTRAINT_ATTRIBUTE);
});
}

@Test
void shouldSucceed_whenProfileIsPresent() {
var policy = createObjectBuilder()
Expand Down

0 comments on commit f9f4d18

Please sign in to comment.