Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fail validation if muliple operands in a Logical Constraint are found #4371

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading