Skip to content

Commit

Permalink
fix: action as @id if it's not refined (#4193)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed May 17, 2024
1 parent 0ca80ac commit 47b07d0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VALUE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ACTION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ACTION_TYPE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_AND_CONSTRAINT_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNEE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNER_ATTRIBUTE;
Expand Down Expand Up @@ -248,12 +247,16 @@ private JsonObject visitAction(@Nullable Action action) {
if (action == null) {
return actionBuilder.build();
}
actionBuilder.add(ODRL_ACTION_TYPE_ATTRIBUTE, action.getType());
if (action.getIncludedIn() != null) {
actionBuilder.add(ODRL_INCLUDED_IN_ATTRIBUTE, action.getIncludedIn());
}
if (action.getConstraint() != null) {
actionBuilder.add(ODRL_REFINEMENT_ATTRIBUTE, action.getConstraint().accept(this));
if (action.getIncludedIn() != null || action.getConstraint() != null) {
actionBuilder.add(ODRL_ACTION_ATTRIBUTE, jsonFactory.createObjectBuilder().add(ID, action.getType()));
if (action.getIncludedIn() != null) {
actionBuilder.add(ODRL_INCLUDED_IN_ATTRIBUTE, action.getIncludedIn());
}
if (action.getConstraint() != null) {
actionBuilder.add(ODRL_REFINEMENT_ATTRIBUTE, action.getConstraint().accept(this));
}
} else {
actionBuilder.add(ID, action.getType());
}
return actionBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.Optional;

import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ACTION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ACTION_TYPE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_INCLUDED_IN_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_REFINEMENT_ATTRIBUTE;
Expand All @@ -48,8 +49,11 @@ public JsonObjectToActionTransformer() {
}

private void transformProperties(String key, JsonValue value, Action.Builder builder, TransformerContext context) {
// we read the type for backward compatibility
if (ODRL_ACTION_TYPE_ATTRIBUTE.equals(key)) {
transformString(value, builder::type, context);
} else if (ODRL_ACTION_ATTRIBUTE.equals(key)) {
transformString(value, builder::type, context);
} else if (ODRL_INCLUDED_IN_ATTRIBUTE.equals(key)) {
transformString(value, builder::includedIn, context);
} else if (ODRL_REFINEMENT_ATTRIBUTE.equals(key)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VALUE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ACTION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ACTION_TYPE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_AND_CONSTRAINT_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNEE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNER_ATTRIBUTE;
Expand Down Expand Up @@ -176,7 +175,7 @@ void transform_actionWithAllAttributes_returnJsonObject() {
assertThat(permissionJson.getJsonObject(ODRL_ACTION_ATTRIBUTE)).isNotNull();

var actionJson = permissionJson.getJsonObject(ODRL_ACTION_ATTRIBUTE);
assertThat(actionJson.getJsonString(ODRL_ACTION_TYPE_ATTRIBUTE).getString()).isEqualTo(action.getType());
assertThat(actionJson.getJsonObject(ODRL_ACTION_ATTRIBUTE).getString(ID)).isEqualTo(action.getType());
assertThat(actionJson.getJsonString(ODRL_INCLUDED_IN_ATTRIBUTE).getString()).isEqualTo(action.getIncludedIn());
assertThat(actionJson.getJsonObject(ODRL_REFINEMENT_ATTRIBUTE)).isNotNull();

Expand Down Expand Up @@ -262,7 +261,7 @@ void transform_prohibitionWithConstraint_returnJsonObject() {

assertThat(prohibitionJson.getJsonArray(ODRL_REMEDY_ATTRIBUTE)).hasSize(1).first()
.extracting(JsonValue::asJsonObject).satisfies(remedyJson -> {
assertThat(remedyJson.getJsonObject(ODRL_ACTION_ATTRIBUTE).getString(ODRL_ACTION_TYPE_ATTRIBUTE))
assertThat(remedyJson.getJsonObject(ODRL_ACTION_ATTRIBUTE).getString(ID))
.isEqualTo("remedyAction");
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void transform_onlyId_returnAction() {
}

@Test
void transform_allAttributes_returnAction() {
void transform_allAttributesWithType_returnAction() {
var constraint = AtomicConstraint.Builder.newInstance()
.leftExpression(new LiteralExpression("left"))
.operator(Operator.EQ)
Expand All @@ -130,6 +130,32 @@ void transform_allAttributes_returnAction() {
verify(context, times(1)).transform(any(JsonObject.class), eq(Constraint.class));
}

@Test
void transform_allAttributes_returnAction() {
var constraint = AtomicConstraint.Builder.newInstance()
.leftExpression(new LiteralExpression("left"))
.operator(Operator.EQ)
.rightExpression(new LiteralExpression("right"))
.build();
when(context.transform(any(JsonObject.class), eq(Constraint.class))).thenReturn(constraint);

var action = jsonFactory.createObjectBuilder()
.add(ODRL_ACTION_ATTRIBUTE, "use")
.add(ODRL_INCLUDED_IN_ATTRIBUTE, "includedIn")
.add(ODRL_REFINEMENT_ATTRIBUTE, jsonFactory.createObjectBuilder().build())
.build();

var result = transformer.transform(TestInput.getExpanded(action), context);

assertThat(result).isNotNull();
assertThat(result.getType()).isEqualTo("use");
assertThat(result.getIncludedIn()).isEqualTo("includedIn");
assertThat(result.getConstraint()).isEqualTo(constraint);

verify(context, never()).reportProblem(anyString());
verify(context, times(1)).transform(any(JsonObject.class), eq(Constraint.class));
}

@Test
void transform_requiredAttributesMissing_reportProblem() {
var action = jsonFactory.createObjectBuilder().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public interface PropertyAndTypeNames {
String ODRL_PROHIBITION_ATTRIBUTE = ODRL_SCHEMA + "prohibition";
String ODRL_OBLIGATION_ATTRIBUTE = ODRL_SCHEMA + "obligation";
String ODRL_ACTION_ATTRIBUTE = ODRL_SCHEMA + "action";
@Deprecated(since = "0.7.0")
String ODRL_ACTION_TYPE_ATTRIBUTE = ODRL_SCHEMA + "type";
String ODRL_CONSEQUENCE_ATTRIBUTE = ODRL_SCHEMA + "consequence";
String ODRL_REMEDY_ATTRIBUTE = ODRL_SCHEMA + "remedy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void shouldStorePolicyDefinition() {
.body(CONTEXT, hasEntry(EDC_PREFIX, EDC_NAMESPACE))
.body(CONTEXT, hasEntry(ODRL_PREFIX, ODRL_SCHEMA))
.body("policy.'odrl:permission'.'odrl:constraint'.'odrl:operator'.@id", is("odrl:eq"))
.body("policy.'odrl:prohibition'.'odrl:remedy'.'odrl:action'.'odrl:type'", is(ODRL_SCHEMA + "anonymize"))
.body("policy.'odrl:prohibition'.'odrl:remedy'.'odrl:action'.@id", is("odrl:anonymize"))
.body("policy.'odrl:obligation'.'odrl:consequence'.size()", is(2));
}

Expand Down

0 comments on commit 47b07d0

Please sign in to comment.