From 886ef9abf0401aed23cbe4b76cb8133adb95a2c3 Mon Sep 17 00:00:00 2001 From: I037636 Date: Sun, 29 Oct 2023 11:44:31 +0100 Subject: [PATCH 1/9] feat: add private properties to policy definition entity --- .../policy/PolicyDefinitionApiExtension.java | 8 +++++- ...ObjectFromPolicyDefinitionTransformer.java | 10 ++++++- ...onObjectToPolicyDefinitionTransformer.java | 6 +++++ ...ctFromPolicyDefinitionTransformerTest.java | 3 ++- .../policy-definition-store-sql/docs/er.puml | 1 + .../docs/schema.sql | 1 + .../schema/BaseSqlDialectStatements.java | 2 ++ .../schema/SqlPolicyStoreStatements.java | 4 +++ .../policy/spi/PolicyDefinition.java | 27 ++++++++++++++++++- 9 files changed, 58 insertions(+), 4 deletions(-) diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/PolicyDefinitionApiExtension.java b/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/PolicyDefinitionApiExtension.java index 1f3d228dd1e..3bc4295a441 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/PolicyDefinitionApiExtension.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/PolicyDefinitionApiExtension.java @@ -24,6 +24,7 @@ import org.eclipse.edc.runtime.metamodel.annotation.Inject; import org.eclipse.edc.spi.system.ServiceExtension; import org.eclipse.edc.spi.system.ServiceExtensionContext; +import org.eclipse.edc.spi.types.TypeManager; import org.eclipse.edc.transform.spi.TypeTransformerRegistry; import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry; import org.eclipse.edc.web.spi.WebService; @@ -31,6 +32,7 @@ import java.util.Map; import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_TYPE; +import static org.eclipse.edc.spi.CoreConstants.JSON_LD; @Extension(value = PolicyDefinitionApiExtension.NAME) @@ -53,6 +55,9 @@ public class PolicyDefinitionApiExtension implements ServiceExtension { @Inject private JsonObjectValidatorRegistry validatorRegistry; + @Inject + private TypeManager typeManager; + @Override public String name() { return NAME; @@ -62,7 +67,8 @@ public String name() { public void initialize(ServiceExtensionContext context) { var jsonBuilderFactory = Json.createBuilderFactory(Map.of()); transformerRegistry.register(new JsonObjectToPolicyDefinitionTransformer()); - transformerRegistry.register(new JsonObjectFromPolicyDefinitionTransformer(jsonBuilderFactory)); + var mapper = typeManager.getMapper(JSON_LD); + transformerRegistry.register(new JsonObjectFromPolicyDefinitionTransformer(jsonBuilderFactory, mapper)); validatorRegistry.register(EDC_POLICY_DEFINITION_TYPE, PolicyDefinitionValidator.instance()); diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformer.java b/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformer.java index 5a08c5ad894..00a8677eb99 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformer.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformer.java @@ -14,6 +14,7 @@ package org.eclipse.edc.connector.api.management.policy.transform; +import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.json.JsonBuilderFactory; import jakarta.json.JsonObject; import org.eclipse.edc.connector.policy.spi.PolicyDefinition; @@ -29,11 +30,13 @@ public class JsonObjectFromPolicyDefinitionTransformer extends AbstractJsonLdTransformer { + private final ObjectMapper mapper; private final JsonBuilderFactory jsonFactory; - public JsonObjectFromPolicyDefinitionTransformer(JsonBuilderFactory jsonFactory) { + public JsonObjectFromPolicyDefinitionTransformer(JsonBuilderFactory jsonFactory, ObjectMapper jsonLdMapper) { super(PolicyDefinition.class, JsonObject.class); this.jsonFactory = jsonFactory; + this.mapper = jsonLdMapper; } @Override @@ -46,6 +49,11 @@ public JsonObjectFromPolicyDefinitionTransformer(JsonBuilderFactory jsonFactory) var policy = context.transform(input.getPolicy(), JsonObject.class); objectBuilder.add(EDC_POLICY_DEFINITION_POLICY, policy); + if (!input.getPrivateProperties().isEmpty()) { + var privatePropBuilder = jsonFactory.createObjectBuilder(); + transformProperties(input.getPrivateProperties(), privatePropBuilder, mapper, context); + objectBuilder.add(PolicyDefinition.EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES, privatePropBuilder); + } return objectBuilder.build(); } diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectToPolicyDefinitionTransformer.java b/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectToPolicyDefinitionTransformer.java index 7c4c6d22a81..815e4fb8304 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectToPolicyDefinitionTransformer.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectToPolicyDefinitionTransformer.java @@ -24,6 +24,7 @@ import org.jetbrains.annotations.Nullable; import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_POLICY; +import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES; public class JsonObjectToPolicyDefinitionTransformer extends AbstractJsonLdTransformer { @@ -42,6 +43,11 @@ public JsonObjectToPolicyDefinitionTransformer() { private void transformProperty(String key, JsonValue value, PolicyDefinition.Builder builder, TransformerContext context) { if (key.equals(EDC_POLICY_DEFINITION_POLICY)) { transformArrayOrObject(value, Policy.class, builder::policy, context); + } else if (key.equals(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES)) { + var props = value.asJsonArray().getJsonObject(0); + visitProperties(props, (k, val) -> transformProperty(k, val, builder, context)); + } else { + builder.privateProperty(key, value); } } } diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java index 4d9351b6bb5..a05d32b2304 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java @@ -27,6 +27,7 @@ import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_TYPE; import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID; import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; +import static org.eclipse.edc.jsonld.util.JacksonJsonLd.createObjectMapper; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; @@ -35,7 +36,7 @@ class JsonObjectFromPolicyDefinitionTransformerTest { - private final JsonObjectFromPolicyDefinitionTransformer transformer = new JsonObjectFromPolicyDefinitionTransformer(Json.createBuilderFactory(emptyMap())); + private final JsonObjectFromPolicyDefinitionTransformer transformer = new JsonObjectFromPolicyDefinitionTransformer(Json.createBuilderFactory(emptyMap()), createObjectMapper()); private final TransformerContext context = mock(TransformerContext.class); @Test diff --git a/extensions/control-plane/store/sql/policy-definition-store-sql/docs/er.puml b/extensions/control-plane/store/sql/policy-definition-store-sql/docs/er.puml index 130df29eb5d..539137d840a 100644 --- a/extensions/control-plane/store/sql/policy-definition-store-sql/docs/er.puml +++ b/extensions/control-plane/store/sql/policy-definition-store-sql/docs/er.puml @@ -23,5 +23,6 @@ entity edc_policydefinitions { target: string type: string extensible_properties: string <> + private_properties: string <> } @enduml \ No newline at end of file diff --git a/extensions/control-plane/store/sql/policy-definition-store-sql/docs/schema.sql b/extensions/control-plane/store/sql/policy-definition-store-sql/docs/schema.sql index 92e16957fe0..d4ef81277d7 100644 --- a/extensions/control-plane/store/sql/policy-definition-store-sql/docs/schema.sql +++ b/extensions/control-plane/store/sql/policy-definition-store-sql/docs/schema.sql @@ -27,6 +27,7 @@ CREATE TABLE IF NOT EXISTS edc_policydefinitions assignee VARCHAR, target VARCHAR, policy_type VARCHAR NOT NULL, + private_properties JSON, PRIMARY KEY (policy_id) ); diff --git a/extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/store/sql/policydefinition/store/schema/BaseSqlDialectStatements.java b/extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/store/sql/policydefinition/store/schema/BaseSqlDialectStatements.java index 74970ac7e21..2911781bde2 100644 --- a/extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/store/sql/policydefinition/store/schema/BaseSqlDialectStatements.java +++ b/extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/store/sql/policydefinition/store/schema/BaseSqlDialectStatements.java @@ -40,6 +40,7 @@ public String getInsertTemplate() { .column(getTargetColumn()) .column(getTypeColumn()) .column(getCreatedAtColumn()) + .jsonColumn(getPrivatePropertiesColumn()) .insertInto(getPolicyTable()); } @@ -55,6 +56,7 @@ public String getUpdateTemplate() { .column(getAssigneeColumn()) .column(getTargetColumn()) .column(getTypeColumn()) + .jsonColumn(getPrivatePropertiesColumn()) .update(getPolicyTable(), getPolicyIdColumn()); } diff --git a/extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/store/sql/policydefinition/store/schema/SqlPolicyStoreStatements.java b/extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/store/sql/policydefinition/store/schema/SqlPolicyStoreStatements.java index cf78ada26a4..5daf02a2fd9 100644 --- a/extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/store/sql/policydefinition/store/schema/SqlPolicyStoreStatements.java +++ b/extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/store/sql/policydefinition/store/schema/SqlPolicyStoreStatements.java @@ -94,5 +94,9 @@ default String getCreatedAtColumn() { return "created_at"; } + default String getPrivatePropertiesColumn() { + return "private_properties"; + } + SqlQueryStatement createQuery(QuerySpec querySpec); } diff --git a/spi/control-plane/policy-spi/src/main/java/org/eclipse/edc/connector/policy/spi/PolicyDefinition.java b/spi/control-plane/policy-spi/src/main/java/org/eclipse/edc/connector/policy/spi/PolicyDefinition.java index 7a0ca7f8387..6e220736690 100644 --- a/spi/control-plane/policy-spi/src/main/java/org/eclipse/edc/connector/policy/spi/PolicyDefinition.java +++ b/spi/control-plane/policy-spi/src/main/java/org/eclipse/edc/connector/policy/spi/PolicyDefinition.java @@ -19,7 +19,10 @@ import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import org.eclipse.edc.policy.model.Policy; import org.eclipse.edc.spi.entity.Entity; +import org.jetbrains.annotations.NotNull; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; import java.util.UUID; @@ -39,7 +42,9 @@ public class PolicyDefinition extends Entity { public static final String EDC_POLICY_DEFINITION_TYPE = EDC_NAMESPACE + "PolicyDefinition"; public static final String EDC_POLICY_DEFINITION_POLICY = EDC_NAMESPACE + "policy"; + public static final String EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES = EDC_NAMESPACE + "privateProperties"; private Policy policy; + private final Map privateProperties = new HashMap<>(); private PolicyDefinition() { } @@ -50,7 +55,7 @@ public Policy getPolicy() { @Override public int hashCode() { - return Objects.hash(Objects.hash(id), policy.hashCode()); + return Objects.hash(Objects.hash(id), policy.hashCode(), privateProperties); } @Override @@ -69,6 +74,15 @@ public String getUid() { return id; } + @NotNull + public Map getPrivateProperties() { + return privateProperties; + } + + public Object getPrivateProperty(String key) { + return privateProperties.get(key); + } + @JsonPOJOBuilder(withPrefix = "") public static final class Builder extends Entity.Builder { @@ -86,6 +100,17 @@ public Builder policy(Policy policy) { return this; } + public Builder privateProperty(String key, Object value) { + entity.privateProperties.put(key, value); + return this; + } + + public Builder privateProperties(Map privateProperties) { + Objects.requireNonNull(privateProperties); + entity.privateProperties.putAll(privateProperties); + return this; + } + @Override public Builder self() { return this; From 3ad63a02400d24c4b9f4270247659187914fe9ff Mon Sep 17 00:00:00 2001 From: I037636 Date: Sun, 29 Oct 2023 22:37:30 +0100 Subject: [PATCH 2/9] feat: add private properties to policy definition entity - Add unit tests --- ...onObjectToPolicyDefinitionTransformer.java | 22 +++-- ...ctFromPolicyDefinitionTransformerTest.java | 25 ++++++ ...jectToPolicyDefinitionTransformerTest.java | 56 ++++++++++++- .../PolicyDefinitionSerializationTest.java | 2 + .../spi/testfixtures/TestFunctions.java | 11 +++ .../store/PolicyDefinitionStoreTestBase.java | 84 +++++++++++++++++++ 6 files changed, 188 insertions(+), 12 deletions(-) diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectToPolicyDefinitionTransformer.java b/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectToPolicyDefinitionTransformer.java index 815e4fb8304..a57abef150f 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectToPolicyDefinitionTransformer.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectToPolicyDefinitionTransformer.java @@ -36,18 +36,22 @@ public JsonObjectToPolicyDefinitionTransformer() { public @Nullable PolicyDefinition transform(@NotNull JsonObject input, @NotNull TransformerContext context) { var builder = PolicyDefinition.Builder.newInstance(); builder.id(nodeId(input)); - visitProperties(input, (key, value) -> transformProperty(key, value, builder, context)); + visitProperties(input, (key, value) -> transformProperties(key, value, builder, context)); return builder.build(); } - private void transformProperty(String key, JsonValue value, PolicyDefinition.Builder builder, TransformerContext context) { - if (key.equals(EDC_POLICY_DEFINITION_POLICY)) { - transformArrayOrObject(value, Policy.class, builder::policy, context); - } else if (key.equals(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES)) { - var props = value.asJsonArray().getJsonObject(0); - visitProperties(props, (k, val) -> transformProperty(k, val, builder, context)); - } else { - builder.privateProperty(key, value); + private void transformProperties(String key, JsonValue jsonValue, PolicyDefinition.Builder builder, TransformerContext context) { + switch (key) { + case EDC_POLICY_DEFINITION_POLICY -> + transformArrayOrObject(jsonValue, Policy.class, builder::policy, context); + case EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES -> { + var props = jsonValue.asJsonArray().getJsonObject(0); + visitProperties(props, (k, val) -> transformProperties(k, val, builder, context)); + } + default -> { + builder.privateProperty(key, transformGenericProperty(jsonValue, context)); + } } + } } diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java index a05d32b2304..75d39ee4537 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java @@ -15,15 +15,19 @@ package org.eclipse.edc.connector.api.management.policy.transform; import jakarta.json.Json; +import jakarta.json.JsonBuilderFactory; import jakarta.json.JsonObject; import org.eclipse.edc.connector.policy.spi.PolicyDefinition; import org.eclipse.edc.policy.model.Policy; import org.eclipse.edc.transform.spi.TransformerContext; import org.junit.jupiter.api.Test; +import java.util.Map; + import static java.util.Collections.emptyMap; import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_POLICY; +import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES; import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_TYPE; import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID; import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; @@ -39,6 +43,10 @@ class JsonObjectFromPolicyDefinitionTransformerTest { private final JsonObjectFromPolicyDefinitionTransformer transformer = new JsonObjectFromPolicyDefinitionTransformer(Json.createBuilderFactory(emptyMap()), createObjectMapper()); private final TransformerContext context = mock(TransformerContext.class); + + private final JsonBuilderFactory jsonFactory = Json.createBuilderFactory(Map.of()); + + @Test void types() { assertThat(transformer.getInputType()).isEqualTo(PolicyDefinition.class); @@ -61,4 +69,21 @@ void transform() { verify(context).transform(policy, JsonObject.class); } + @Test + void transform_withPrivateProperties_simpleTypes() { + var policy = Policy.Builder.newInstance().build(); + var input = PolicyDefinition.Builder.newInstance().id("definitionId").policy(policy).privateProperty("some-key", "some-value").build(); + var policyJson = Json.createObjectBuilder().build(); + when(context.transform(any(), eq(JsonObject.class))).thenReturn(policyJson); + + var result = transformer.transform(input, context); + + assertThat(result).isNotNull(); + assertThat(result.getString(ID)).isEqualTo("definitionId"); + assertThat(result.getString(TYPE)).isEqualTo(EDC_POLICY_DEFINITION_TYPE); + assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_POLICY)).isSameAs(policyJson); + + assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES).getJsonString("some-key").getString()).isEqualTo("some-value"); + verify(context).transform(policy, JsonObject.class); + } } diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectToPolicyDefinitionTransformerTest.java b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectToPolicyDefinitionTransformerTest.java index f3932dc5f02..ccda69f0c82 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectToPolicyDefinitionTransformerTest.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectToPolicyDefinitionTransformerTest.java @@ -14,18 +14,27 @@ package org.eclipse.edc.connector.api.management.policy.transform; -import jakarta.json.Json; import jakarta.json.JsonObject; +import jakarta.json.JsonObjectBuilder; import org.eclipse.edc.connector.policy.spi.PolicyDefinition; +import org.eclipse.edc.jsonld.TitaniumJsonLd; import org.eclipse.edc.policy.model.Policy; +import org.eclipse.edc.spi.monitor.Monitor; import org.eclipse.edc.transform.spi.TransformerContext; import org.junit.jupiter.api.Test; +import static jakarta.json.Json.createArrayBuilder; +import static jakarta.json.Json.createObjectBuilder; import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_POLICY; +import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES; import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_TYPE; +import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.CONTEXT; import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID; import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; +import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VOCAB; +import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE; +import static org.eclipse.edc.spi.CoreConstants.EDC_PREFIX; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; @@ -36,6 +45,7 @@ class JsonObjectToPolicyDefinitionTransformerTest { private final JsonObjectToPolicyDefinitionTransformer transformer = new JsonObjectToPolicyDefinitionTransformer(); private final TransformerContext context = mock(TransformerContext.class); + private final TitaniumJsonLd jsonLd = new TitaniumJsonLd(mock(Monitor.class)); @Test void types() { @@ -45,10 +55,10 @@ void types() { @Test void transform() { - var policyJson = Json.createObjectBuilder().build(); + var policyJson = createObjectBuilder().build(); var policy = Policy.Builder.newInstance().build(); when(context.transform(any(), eq(Policy.class))).thenReturn(policy); - var json = Json.createObjectBuilder() + var json = createObjectBuilder() .add(ID, "definitionId") .add(TYPE, EDC_POLICY_DEFINITION_TYPE) .add(EDC_POLICY_DEFINITION_POLICY, policyJson) @@ -62,4 +72,44 @@ void transform() { verify(context).transform(policyJson, Policy.class); } + @Test + void transform_withPrivateProperties() { + when(context.transform(any(), eq(Object.class))).thenReturn("test-val"); + var policyJson = createObjectBuilder().build(); + var policy = Policy.Builder.newInstance().build(); + when(context.transform(any(), eq(Policy.class))).thenReturn(policy); + var json = createObjectBuilder() + .add(CONTEXT, createContextBuilder().addNull(EDC_PREFIX).build()) + .add(ID, "definitionId") + .add(TYPE, EDC_POLICY_DEFINITION_TYPE) + .add(EDC_POLICY_DEFINITION_POLICY, policyJson) + .add(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES, + createArrayBuilder() + .add(createObjectBuilder() + .add("test-prop", "test-val") + .build()) + .build()) + .build(); + var jsonObj = expand(json); + var result = transformer.transform(jsonObj, context); + + assertThat(result).isNotNull(); + assertThat(result.getId()).isEqualTo("definitionId"); + assertThat(result.getPolicy()).isSameAs(policy); + verify(context).transform(policyJson, Policy.class); + assertThat(result.getPrivateProperties()) + .hasSize(1) + .containsEntry(EDC_NAMESPACE + "test-prop", "test-val"); + } + + private JsonObject expand(JsonObject jsonObject) { + return jsonLd.expand(jsonObject).orElseThrow(f -> new AssertionError(f.getFailureDetail())); + } + + private JsonObjectBuilder createContextBuilder() { + return createObjectBuilder() + .add(VOCAB, EDC_NAMESPACE) + .add(EDC_PREFIX, EDC_NAMESPACE); + } + } diff --git a/spi/control-plane/policy-spi/src/test/java/org/eclipse/edc/connector/policy/spi/PolicyDefinitionSerializationTest.java b/spi/control-plane/policy-spi/src/test/java/org/eclipse/edc/connector/policy/spi/PolicyDefinitionSerializationTest.java index eb9514c0d0b..d61adf9c4f5 100644 --- a/spi/control-plane/policy-spi/src/test/java/org/eclipse/edc/connector/policy/spi/PolicyDefinitionSerializationTest.java +++ b/spi/control-plane/policy-spi/src/test/java/org/eclipse/edc/connector/policy/spi/PolicyDefinitionSerializationTest.java @@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test; import java.util.List; +import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; @@ -37,6 +38,7 @@ void verifySerialization() { .id("test-policy-id") .createdAt(12345L) .policy(createPolicy()) + .privateProperties(Map.of("key1", "value2")) .build(); var json = typeManager.writeValueAsString(policyDef); diff --git a/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/TestFunctions.java b/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/TestFunctions.java index 7c9ed532cb8..666dabeb039 100644 --- a/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/TestFunctions.java +++ b/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/TestFunctions.java @@ -27,6 +27,7 @@ import java.time.Clock; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -46,6 +47,16 @@ public static PolicyDefinition createPolicy(String id, String target) { .build(); } + public static PolicyDefinition createPolicy(String id, String target, Map privateProperties) { + var policy = createPolicyBuilder(id).target(target).build(); + + return PolicyDefinition.Builder.newInstance() + .createdAt(Clock.systemUTC().millis()) + .id(id) + .policy(policy) + .privateProperties(privateProperties) + .build(); + } public static List createPolicies(int count) { return IntStream.range(0, count).mapToObj(i -> createPolicy("policyDef" + i)).collect(Collectors.toList()); diff --git a/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java b/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java index 8f992d9b067..b39480f5e49 100644 --- a/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java +++ b/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java @@ -30,6 +30,7 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -95,6 +96,21 @@ void alreadyExists() { .satisfies(policy -> assertThat(policy.getPolicy().getTarget()).isEqualTo("Target1")) .extracting(PolicyDefinition::getCreatedAt).isEqualTo(policy1.getCreatedAt()); } + + @Test + @DisplayName("Save a single policy that not exists ") + void notExisting_with_privateProperties() { + var policy = createPolicy(getRandomId(), null, Map.of("key1", "value1", "key2", "value2")); + + getPolicyDefinitionStore().create(policy); + + var policyFromDb = getPolicyDefinitionStore().findById(policy.getUid()); + assertThat(policy).usingRecursiveComparison().isEqualTo(policyFromDb); + + assertThat(policyFromDb.getPrivateProperties()).hasSize(2); + assertThat(policyFromDb.getPrivateProperties().get("key1")).usingRecursiveComparison().isEqualTo("value1"); + assertThat(policyFromDb.getPrivateProperties().get("key2")).usingRecursiveComparison().isEqualTo("value2"); + } } @Nested @@ -132,6 +148,74 @@ void policyExists() { assertThat(result.getContent()).usingRecursiveComparison().isEqualTo(newPolicy); } + @Test + @DisplayName("Update an Policy that exists, adding a property") + void policyExists_addProperty() { + var id = getRandomId(); + var policy = createPolicyDef(id, "target"); + + var store = getPolicyDefinitionStore(); + store.create(policy); + + var spec = QuerySpec.Builder.newInstance().build(); + var policyFromDb = store.findAll(spec); + + assertThat(policyFromDb).hasSize(1).first(); + policy.getPrivateProperties().put("newKey", "newValue"); + var updated = getPolicyDefinitionStore().update(policy); + assertThat(updated).isNotNull(); + + var definitionFound = getPolicyDefinitionStore().findById(id); + + assertThat(definitionFound).isNotNull(); + assertThat(definitionFound).usingRecursiveComparison().isEqualTo(policy); + + } + + @Test + @DisplayName("Update an Policy that exists, remove a property") + void policyExists_removeProperty() { + var id = getRandomId(); + var policy = createPolicyDef(id, "target"); + policy.getPrivateProperties().put("newKey", "newValue"); + var created = getPolicyDefinitionStore().create(policy); + assertThat(created).isNotNull(); + + var definitionFound = getPolicyDefinitionStore().findById(id); + + assertThat(definitionFound).isNotNull(); + assertThat(definitionFound).usingRecursiveComparison().isEqualTo(policy); + + policy.getPrivateProperties().remove("newKey"); + var updated = getPolicyDefinitionStore().update(policy); + assertThat(updated).isNotNull(); + assertThat(definitionFound).isNotNull(); + assertThat(definitionFound).usingRecursiveComparison().isEqualTo(policy); + assertThat(definitionFound.getPrivateProperties()).doesNotContainKey("newKey"); + } + + @Test + @DisplayName("Update an Policy that exists, replace a property") + void policyExists_replaceProperty() { + var id = getRandomId(); + var policy = createPolicyDef(id, "target"); + policy.getPrivateProperties().put("newKey", "originalValue"); + var created = getPolicyDefinitionStore().create(policy); + assertThat(created).isNotNull(); + + var definitionFound = getPolicyDefinitionStore().findById(id); + + assertThat(definitionFound).isNotNull(); + assertThat(definitionFound).usingRecursiveComparison().isEqualTo(policy); + + policy.getPrivateProperties().put("newKey", "newValue"); + var updated = getPolicyDefinitionStore().update(policy); + assertThat(updated).isNotNull(); + assertThat(definitionFound).isNotNull(); + assertThat(definitionFound).usingRecursiveComparison().isEqualTo(policy); + assertThat(definitionFound.getPrivateProperties()).containsEntry("newKey", "newValue"); + } + @Test void whenPolicyNotExists() { var updated = createPolicy("another-id"); From 76a7bb77938773a790f586375ea38fd834a7baf5 Mon Sep 17 00:00:00 2001 From: I037636 Date: Mon, 30 Oct 2023 09:23:23 +0100 Subject: [PATCH 3/9] feat: add private properties to policy definition entity - Add unit tests --- .../store/PolicyDefinitionStoreTestBase.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java b/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java index b39480f5e49..52ce5e660dd 100644 --- a/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java +++ b/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java @@ -591,6 +591,19 @@ void whenNoFiltersWithLimitAndOffset() { assertThat(list).hasSize(2).usingRecursiveFieldByFieldElementComparator().isSubsetOf(policy1, policy2, policy3); } + @Test + @DisplayName("find all a with property ") + void findAll_with_privateProperties() { + var policy1 = createPolicy(getRandomId(), null, Map.of("key1", "value1", "key2", "value2")); + var policy2 = createPolicy(getRandomId(), null, Map.of("key3", "value3", "key4", "value4")); + + getPolicyDefinitionStore().create(policy1); + getPolicyDefinitionStore().create(policy2); + + var list = getPolicyDefinitionStore().findAll(QuerySpec.Builder.newInstance().limit(3).offset(0).build()).collect(Collectors.toList()); + assertThat(list).hasSize(2).usingRecursiveFieldByFieldElementComparator().isSubsetOf(policy1, policy2); + } + @Test void whenEqualFilter() { var policy1 = createPolicy(getRandomId()); @@ -660,6 +673,19 @@ void whenExists() { assertThat(store.findById(policy.getUid())).isNull(); } + @Test + @DisplayName("Delete existing policy with properties") + void whenExists_WithProperties() { + var policy = createPolicy(getRandomId(), null, Map.of("key1", "value1", "key2", "value2")); + var store = getPolicyDefinitionStore(); + store.create(policy); + + var result = store.delete(policy.getUid()); + assertThat(result.succeeded()).isTrue(); + assertThat(result.getContent()).usingRecursiveComparison().isEqualTo(policy); + assertThat(store.findById(policy.getUid())).isNull(); + } + @Test @DisplayName("Delete a non existing policy") void whenNonexistent() { From 7a2a898fd2d54344260ff83dc0d03a0a01ca99dd Mon Sep 17 00:00:00 2001 From: I037636 Date: Mon, 30 Oct 2023 09:47:47 +0100 Subject: [PATCH 4/9] feat: add private properties to policy definition entity - Add unit tests --- .../spi/testfixtures/store/PolicyDefinitionStoreTestBase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java b/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java index 52ce5e660dd..ef9a97d24fc 100644 --- a/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java +++ b/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java @@ -674,7 +674,7 @@ void whenExists() { } @Test - @DisplayName("Delete existing policy with properties") + @DisplayName("Delete an existing policy with properties") void whenExists_WithProperties() { var policy = createPolicy(getRandomId(), null, Map.of("key1", "value1", "key2", "value2")); var store = getPolicyDefinitionStore(); From 8f4ee6c127a50bc7b173588d3d62e8a7a9ad0ca7 Mon Sep 17 00:00:00 2001 From: I037636 Date: Mon, 30 Oct 2023 10:13:17 +0100 Subject: [PATCH 5/9] feat: add private properties to policy definition entity --- .../policydefinition/store/SqlPolicyDefinitionStore.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/store/sql/policydefinition/store/SqlPolicyDefinitionStore.java b/extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/store/sql/policydefinition/store/SqlPolicyDefinitionStore.java index ddd10b1cd85..57e67168c0d 100644 --- a/extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/store/sql/policydefinition/store/SqlPolicyDefinitionStore.java +++ b/extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/store/sql/policydefinition/store/SqlPolicyDefinitionStore.java @@ -10,6 +10,7 @@ * Contributors: * ZF Friedrichshafen AG - Initial API and Implementation * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - improvements + * SAP SE - add private properties to contract definition * */ @@ -57,6 +58,9 @@ public class SqlPolicyDefinitionStore extends AbstractSqlStore implements Policy private final TypeReference> extensiblePropertiesType = new TypeReference<>() { }; + private static final TypeReference> PRIVATE_PROPERTIES_TYPE = new TypeReference<>() { + }; + public SqlPolicyDefinitionStore(DataSourceRegistry dataSourceRegistry, String dataSourceName, TransactionContext transactionContext, ObjectMapper objectMapper, SqlPolicyStoreStatements sqlPolicyStoreStatements, QueryExecutor queryExecutor) { super(dataSourceRegistry, dataSourceName, transactionContext, objectMapper, queryExecutor); @@ -149,7 +153,8 @@ private void insert(PolicyDefinition def) { policy.getAssignee(), policy.getTarget(), toJson(policy.getType(), policyType), - def.getCreatedAt()); + def.getCreatedAt(), + toJson(def.getPrivateProperties())); } catch (Exception e) { throw new EdcPersistenceException(e.getMessage(), e); } @@ -171,6 +176,7 @@ private void updateInternal(PolicyDefinition def) { policy.getAssignee(), policy.getTarget(), toJson(policy.getType(), policyType), + toJson(def.getPrivateProperties()), id); } catch (Exception e) { throw new EdcPersistenceException(e.getMessage(), e); @@ -195,6 +201,7 @@ private PolicyDefinition mapResultSet(ResultSet resultSet) throws SQLException { .id(resultSet.getString(statements.getPolicyIdColumn())) .policy(policy) .createdAt(resultSet.getLong(statements.getCreatedAtColumn())) + .privateProperties(fromJson(resultSet.getString(statements.getPrivatePropertiesColumn()), PRIVATE_PROPERTIES_TYPE)) .build(); } From d0e04e1b303adad8123c900cf0d33d8674a0137f Mon Sep 17 00:00:00 2001 From: I037636 Date: Mon, 6 Nov 2023 15:29:53 +0100 Subject: [PATCH 6/9] fix for component tests fails --- .../spi/testfixtures/store/PolicyDefinitionStoreTestBase.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java b/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java index ef9a97d24fc..0982f3dfac5 100644 --- a/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java +++ b/spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java @@ -188,6 +188,7 @@ void policyExists_removeProperty() { policy.getPrivateProperties().remove("newKey"); var updated = getPolicyDefinitionStore().update(policy); + definitionFound = getPolicyDefinitionStore().findById(id); assertThat(updated).isNotNull(); assertThat(definitionFound).isNotNull(); assertThat(definitionFound).usingRecursiveComparison().isEqualTo(policy); @@ -210,6 +211,7 @@ void policyExists_replaceProperty() { policy.getPrivateProperties().put("newKey", "newValue"); var updated = getPolicyDefinitionStore().update(policy); + definitionFound = getPolicyDefinitionStore().findById(id); assertThat(updated).isNotNull(); assertThat(definitionFound).isNotNull(); assertThat(definitionFound).usingRecursiveComparison().isEqualTo(policy); From 0b9b234485b71199af302c3e5a5ed2ea5ae27b19 Mon Sep 17 00:00:00 2001 From: I037636 Date: Fri, 17 Nov 2023 10:21:23 +0100 Subject: [PATCH 7/9] review comments, add test for complex types --- ...ctFromPolicyDefinitionTransformerTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java index 75d39ee4537..3b533e6e3c1 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java @@ -86,4 +86,26 @@ void transform_withPrivateProperties_simpleTypes() { assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES).getJsonString("some-key").getString()).isEqualTo("some-value"); verify(context).transform(policy, JsonObject.class); } + + + @Test + void transform_withPrivateProperties_complexTypes() { + var policy = Policy.Builder.newInstance().build(); + var input = PolicyDefinition.Builder.newInstance().id("definitionId").policy(policy).privateProperty("root", Map.of("key1", "value1", "nested1", Map.of("key2", "value2", "key3", Map.of("theKey", "theValue, this is what we're looking for")))).build(); + var policyJson = Json.createObjectBuilder().build(); + when(context.transform(any(), eq(JsonObject.class))).thenReturn(policyJson); + + var result = transformer.transform(input, context); + + assertThat(result).isNotNull(); + assertThat(result.getString(ID)).isEqualTo("definitionId"); + assertThat(result.getString(TYPE)).isEqualTo(EDC_POLICY_DEFINITION_TYPE); + assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_POLICY)).isSameAs(policyJson); + + assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES).getJsonObject("root").getJsonString("key1").getString()).isEqualTo("value1"); + assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES).getJsonObject("root").getJsonObject("nested1").getJsonString("key2").getString()).isEqualTo("value2"); + assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES).getJsonObject("root").getJsonObject("nested1").getJsonObject("key3").getJsonString("theKey").getString()).isEqualTo("theValue, this is what we're looking for"); + + verify(context).transform(policy, JsonObject.class); + } } From 99925577567e5663b78b0dbdf601f887810612f5 Mon Sep 17 00:00:00 2001 From: I037636 Date: Thu, 23 Nov 2023 10:53:07 +0100 Subject: [PATCH 8/9] checkstyle and endtoend tests --- ...ctFromPolicyDefinitionTransformerTest.java | 28 ++++++++++-- .../PolicyDefinitionApiEndToEndTest.java | 43 +++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java index 3b533e6e3c1..1cb209d6d00 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/JsonObjectFromPolicyDefinitionTransformerTest.java @@ -91,7 +91,12 @@ void transform_withPrivateProperties_simpleTypes() { @Test void transform_withPrivateProperties_complexTypes() { var policy = Policy.Builder.newInstance().build(); - var input = PolicyDefinition.Builder.newInstance().id("definitionId").policy(policy).privateProperty("root", Map.of("key1", "value1", "nested1", Map.of("key2", "value2", "key3", Map.of("theKey", "theValue, this is what we're looking for")))).build(); + var input = PolicyDefinition + .Builder.newInstance() + .id("definitionId") + .policy(policy) + .privateProperty("root", Map.of("key1", "value1", "nested1", Map.of("key2", "value2", "key3", Map.of("theKey", "theValue, this is what we're looking for")))) + .build(); var policyJson = Json.createObjectBuilder().build(); when(context.transform(any(), eq(JsonObject.class))).thenReturn(policyJson); @@ -102,9 +107,24 @@ void transform_withPrivateProperties_complexTypes() { assertThat(result.getString(TYPE)).isEqualTo(EDC_POLICY_DEFINITION_TYPE); assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_POLICY)).isSameAs(policyJson); - assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES).getJsonObject("root").getJsonString("key1").getString()).isEqualTo("value1"); - assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES).getJsonObject("root").getJsonObject("nested1").getJsonString("key2").getString()).isEqualTo("value2"); - assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES).getJsonObject("root").getJsonObject("nested1").getJsonObject("key3").getJsonString("theKey").getString()).isEqualTo("theValue, this is what we're looking for"); + assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES) + .getJsonObject("root") + .getJsonString("key1") + .getString()) + .isEqualTo("value1"); + assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES) + .getJsonObject("root") + .getJsonObject("nested1") + .getJsonString("key2") + .getString()) + .isEqualTo("value2"); + assertThat(result.getJsonObject(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES) + .getJsonObject("root") + .getJsonObject("nested1") + .getJsonObject("key3") + .getJsonString("theKey") + .getString()) + .isEqualTo("theValue, this is what we're looking for"); verify(context).transform(policy, JsonObject.class); } diff --git a/system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/PolicyDefinitionApiEndToEndTest.java b/system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/PolicyDefinitionApiEndToEndTest.java index 1794e3abf50..055973dbe5c 100644 --- a/system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/PolicyDefinitionApiEndToEndTest.java +++ b/system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/PolicyDefinitionApiEndToEndTest.java @@ -40,6 +40,7 @@ @EndToEndTest public class PolicyDefinitionApiEndToEndTest extends BaseManagementApiEndToEndTest { + @Test void shouldStorePolicyDefinition() { var requestBody = createObjectBuilder() @@ -74,6 +75,48 @@ void shouldStorePolicyDefinition() { .body("policy.'odrl:permission'.'odrl:constraint'.'odrl:operator'.@id", is("odrl:eq")); } + @Test + void shouldStorePolicyDefinitionWithPrivateProperties() { + var requestBody = createObjectBuilder() + .add(CONTEXT, createObjectBuilder() + .add("edc", EDC_NAMESPACE) + .build()) + .add(TYPE, "PolicyDefinition") + .add("policy", sampleOdrlPolicy()) + .add("privateProperties", createObjectBuilder() + .add("newKey", "newValue") + .build()) + .build(); + + var id = baseRequest() + .body(requestBody) + .contentType(JSON) + .post("/v2/policydefinitions") + .then() + .contentType(JSON) + .extract().jsonPath().getString(ID); + + PolicyDefinition result = store().findById(id); + assertThat(result).isNotNull() + .extracting(PolicyDefinition::getPolicy).isNotNull() + .extracting(Policy::getPermissions).asList().hasSize(1); + + assertThat(result).isNotNull() + .extracting(PolicyDefinition::getPrivateProperties) + .asList().hasSize(1).contains("newValue"); + + baseRequest() + .get("/v2/policydefinitions/" + id) + .then() + .statusCode(200) + .contentType(JSON) + .body(ID, is(id)) + .body(CONTEXT, hasEntry(EDC_PREFIX, EDC_NAMESPACE)) + .body(CONTEXT, hasEntry(ODRL_PREFIX, ODRL_SCHEMA)) + .log().all() + .body("policy.'odrl:permission'.'odrl:constraint'.'odrl:operator'.@id", is("odrl:eq")); + } + @Test void shouldUpdate() { var requestBody = createObjectBuilder() From 1375c04277908247ea44b7e4480909f4c377021d Mon Sep 17 00:00:00 2001 From: I037636 Date: Thu, 23 Nov 2023 11:46:20 +0100 Subject: [PATCH 9/9] endtoend tests --- .../PolicyDefinitionApiEndToEndTest.java | 106 +++++++++++++++++- 1 file changed, 103 insertions(+), 3 deletions(-) diff --git a/system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/PolicyDefinitionApiEndToEndTest.java b/system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/PolicyDefinitionApiEndToEndTest.java index 055973dbe5c..66d55657b45 100644 --- a/system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/PolicyDefinitionApiEndToEndTest.java +++ b/system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/PolicyDefinitionApiEndToEndTest.java @@ -23,6 +23,9 @@ import org.eclipse.edc.spi.entity.Entity; import org.junit.jupiter.api.Test; +import java.util.HashMap; +import java.util.Map; + import static io.restassured.http.ContentType.JSON; import static jakarta.json.Json.createArrayBuilder; import static jakarta.json.Json.createObjectBuilder; @@ -100,10 +103,10 @@ void shouldStorePolicyDefinitionWithPrivateProperties() { assertThat(result).isNotNull() .extracting(PolicyDefinition::getPolicy).isNotNull() .extracting(Policy::getPermissions).asList().hasSize(1); - + Map privateProp = new HashMap<>(); + privateProp.put("https://w3id.org/edc/v0.0.1/ns/newKey", "newValue"); assertThat(result).isNotNull() - .extracting(PolicyDefinition::getPrivateProperties) - .asList().hasSize(1).contains("newValue"); + .extracting(PolicyDefinition::getPrivateProperties).isEqualTo(privateProp); baseRequest() .get("/v2/policydefinitions/" + id) @@ -156,6 +159,70 @@ void shouldUpdate() { .isNotEqualTo(createdAt); } + @Test + void shouldUpdateWithProperties() { + var requestBody = createObjectBuilder() + .add(CONTEXT, createObjectBuilder() + .add("edc", EDC_NAMESPACE) + .build()) + .add(TYPE, "PolicyDefinition") + .add("policy", sampleOdrlPolicy()) + .add("privateProperties", createObjectBuilder() + .add("newKey", "newValue") + .build()) + .build(); + + var updatedBody = createObjectBuilder() + .add(CONTEXT, createObjectBuilder() + .add("edc", EDC_NAMESPACE) + .build()) + .add(TYPE, "PolicyDefinition") + .add("policy", sampleOdrlPolicy()) + .add("privateProperties", createObjectBuilder() + .add("newKey", "updatedValue") + .build()) + .build(); + + var id = baseRequest() + .body(requestBody) + .contentType(JSON) + .post("/v2/policydefinitions") + .then() + .statusCode(200) + .extract().jsonPath().getString(ID); + + var createdAt = baseRequest() + .contentType(JSON) + .post("/v2/policydefinitions/request") + .then() + .statusCode(200) + .extract().as(JsonArray.class) + .get(0).asJsonObject() + .getJsonNumber("createdAt").longValue(); + + baseRequest() + .contentType(JSON) + .body(createObjectBuilder(updatedBody).add(ID, id).build()) + .put("/v2/policydefinitions/" + id) + .then() + .statusCode(204); + + var result = store().findById(id); + assertThat(result) + .extracting(Entity::getCreatedAt) + .isNotEqualTo(createdAt); + + + Map privateProp = new HashMap<>(); + privateProp.put("https://w3id.org/edc/v0.0.1/ns/newKey", "updatedValue"); + assertThat(result).isNotNull() + .extracting(PolicyDefinition::getPrivateProperties).isEqualTo(privateProp); + + assertThat(store().findById(id)) + .extracting(Entity::getCreatedAt) + .isNotEqualTo(createdAt); + } + @Test void shouldDelete() { var requestBody = createObjectBuilder() @@ -185,6 +252,39 @@ void shouldDelete() { .statusCode(404); } + + @Test + void shouldDeleteWithProperties() { + var requestBody = createObjectBuilder() + .add(CONTEXT, createObjectBuilder() + .add("edc", EDC_NAMESPACE) + .build()) + .add(TYPE, "PolicyDefinition") + .add("policy", sampleOdrlPolicy()) + .add("privateProperties", createObjectBuilder() + .add("newKey", "newValue") + .build()) + .build(); + + var id = baseRequest() + .body(requestBody) + .contentType(JSON) + .post("/v2/policydefinitions") + .then() + .statusCode(200) + .extract().jsonPath().getString(ID); + + baseRequest() + .delete("/v2/policydefinitions/" + id) + .then() + .statusCode(204); + + baseRequest() + .get("/v2/policydefinitions/" + id) + .then() + .statusCode(404); + } + private JsonObject sampleOdrlPolicy() { return createObjectBuilder() .add(CONTEXT, "http://www.w3.org/ns/odrl.jsonld")