Changesets · Branches · Tags · Compare
| Changeset | a74f4e2d1913b52b68bdb092ae354268c78a190b02dbff4d93e809bbbd2bfc67 |
|---|---|
| Author | ndr_brt <andrea.bertagnolli@gmail.com> · 2024-07-23 12:05 +02:00 |
| Committer | GitHub <noreply@github.com> · 2024-07-23 12:05 +02:00 |
| Parents | 70f714776e15 |
2 files changed
modified extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/controlplane/api/management/policy/validation/PolicyDefinitionValidator.java +9 −1
| @@ -146,7 +146,15 @@ | ||
| 146 | 146 | |
| 147 | 147 | @Override |
| 148 | 148 | public ValidationResult validate(JsonObject input) { |
| 149 | - if (LOGICAL_CONSTRAINTS.stream().anyMatch(input::containsKey)) { | |
| 149 | + var logicalOperands = input.keySet().stream().filter(LOGICAL_CONSTRAINTS::contains).toList(); | |
| 150 | + | |
| 151 | + if (logicalOperands.size() > 1) { | |
| 152 | + return ValidationResult.failure( | |
| 153 | + violation("Cannot define multiple operands in a Logical Constraint: %s".formatted(logicalOperands), | |
| 154 | + path.toString())); | |
| 155 | + } | |
| 156 | + | |
| 157 | + if (logicalOperands.size() == 1) { | |
| 150 | 158 | return ValidationResult.success(); |
| 151 | 159 | } |
| 152 | 160 |
modified extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/controlplane/api/management/policy/validation/PolicyDefinitionValidatorTest.java +26 −0
| @@ -34,6 +34,7 @@ | ||
| 34 | 34 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_CONSTRAINT_ATTRIBUTE; |
| 35 | 35 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_LEFT_OPERAND_ATTRIBUTE; |
| 36 | 36 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_OPERATOR_ATTRIBUTE; |
| 37 | +import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_OR_CONSTRAINT_ATTRIBUTE; | |
| 37 | 38 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PERMISSION_ATTRIBUTE; |
| 38 | 39 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_SET; |
| 39 | 40 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROFILE_ATTRIBUTE; |
| @@ -202,6 +203,31 @@ | ||
| 202 | 203 | assertThat(result).isSucceeded(); |
| 203 | 204 | } |
| 204 | 205 | |
| 206 | + @Test | |
| 207 | + void shouldFail_whenLogicalConstraintWithMultipleOperandsIsPresent() { | |
| 208 | + var permission = createArrayBuilder().add(createObjectBuilder() | |
| 209 | + .add(ODRL_ACTION_ATTRIBUTE, createValidAction()) | |
| 210 | + .add(ODRL_CONSTRAINT_ATTRIBUTE, createArrayBuilder().add(createObjectBuilder() | |
| 211 | + .add(ODRL_AND_CONSTRAINT_ATTRIBUTE, createArrayBuilder()) | |
| 212 | + .add(ODRL_OR_CONSTRAINT_ATTRIBUTE, createArrayBuilder()) | |
| 213 | + ))); | |
| 214 | + var policy = createObjectBuilder() | |
| 215 | + .add(TYPE, policySet()) | |
| 216 | + .add(ODRL_PERMISSION_ATTRIBUTE, permission); | |
| 217 | + var policyDefinition = createObjectBuilder() | |
| 218 | + .add(EDC_POLICY_DEFINITION_POLICY, createArrayBuilder().add(policy)) | |
| 219 | + .build(); | |
| 220 | + | |
| 221 | + var result = validator.validate(policyDefinition); | |
| 222 | + | |
| 223 | + assertThat(result).isFailed().extracting(ValidationFailure::getViolations).asInstanceOf(list(Violation.class)) | |
| 224 | + .hasSize(1) | |
| 225 | + .anySatisfy(violation -> { | |
| 226 | + assertThat(violation.message()).contains("multiple operands"); | |
| 227 | + assertThat(violation.path()).endsWith(ODRL_CONSTRAINT_ATTRIBUTE); | |
| 228 | + }); | |
| 229 | + } | |
| 230 | + | |
| 205 | 231 | @Test |
| 206 | 232 | void shouldSucceed_whenProfileIsPresent() { |
| 207 | 233 | var policy = createObjectBuilder() |