From 316fa3a8916b50bc7d33476ca946b724a81ff259 Mon Sep 17 00:00:00 2001 From: ndr_brt Date: Fri, 5 Apr 2024 10:29:54 +0200 Subject: [PATCH] fix: accessService serialization (#4088) --- DEPENDENCIES | 6 ++-- ...JsonObjectFromDistributionTransformer.java | 15 ++++---- ...ObjectFromDistributionTransformerTest.java | 36 +++++++++---------- .../build.gradle.kts | 1 + .../managementapi/CatalogApiEndToEndTest.java | 23 ++++++++---- 5 files changed, 45 insertions(+), 36 deletions(-) diff --git a/DEPENDENCIES b/DEPENDENCIES index faba1599696..45be55a58d7 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -103,9 +103,9 @@ maven/mavencentral/commons-logging/commons-logging/1.2, Apache-2.0, approved, CQ maven/mavencentral/dev.failsafe/failsafe-okhttp/3.3.2, Apache-2.0, approved, #9178 maven/mavencentral/dev.failsafe/failsafe/3.3.2, Apache-2.0, approved, #9268 maven/mavencentral/info.picocli/picocli/4.7.5, Apache-2.0, approved, #4365 -maven/mavencentral/io.cloudevents/cloudevents-api/3.0.0, , restricted, clearlydefined -maven/mavencentral/io.cloudevents/cloudevents-core/3.0.0, , restricted, clearlydefined -maven/mavencentral/io.cloudevents/cloudevents-http-basic/3.0.0, , restricted, clearlydefined +maven/mavencentral/io.cloudevents/cloudevents-api/3.0.0, Apache-2.0, approved, #14228 +maven/mavencentral/io.cloudevents/cloudevents-core/3.0.0, Apache-2.0, approved, #14227 +maven/mavencentral/io.cloudevents/cloudevents-http-basic/3.0.0, Apache-2.0, approved, #14229 maven/mavencentral/io.github.classgraph/classgraph/4.8.154, MIT, approved, CQ22530 maven/mavencentral/io.micrometer/micrometer-commons/1.12.4, Apache-2.0 AND (Apache-2.0 AND MIT), approved, #11679 maven/mavencentral/io.micrometer/micrometer-core/1.12.4, Apache-2.0 AND (Apache-2.0 AND MIT), approved, #11678 diff --git a/data-protocols/dsp/dsp-catalog/dsp-catalog-transform/src/main/java/org/eclipse/edc/protocol/dsp/catalog/transform/from/JsonObjectFromDistributionTransformer.java b/data-protocols/dsp/dsp-catalog/dsp-catalog-transform/src/main/java/org/eclipse/edc/protocol/dsp/catalog/transform/from/JsonObjectFromDistributionTransformer.java index 3b7dd19f027..7d6c00d6b8c 100644 --- a/data-protocols/dsp/dsp-catalog/dsp-catalog-transform/src/main/java/org/eclipse/edc/protocol/dsp/catalog/transform/from/JsonObjectFromDistributionTransformer.java +++ b/data-protocols/dsp/dsp-catalog/dsp-catalog-transform/src/main/java/org/eclipse/edc/protocol/dsp/catalog/transform/from/JsonObjectFromDistributionTransformer.java @@ -42,14 +42,13 @@ public JsonObjectFromDistributionTransformer(JsonBuilderFactory jsonFactory) { @Override public @Nullable JsonObject transform(@NotNull Distribution distribution, @NotNull TransformerContext context) { - var objectBuilder = jsonFactory.createObjectBuilder(); - objectBuilder.add(TYPE, DCAT_DISTRIBUTION_TYPE); - objectBuilder.add(DCT_FORMAT_ATTRIBUTE, jsonFactory.createObjectBuilder() - .add(ID, distribution.getFormat()) - .build()); - objectBuilder.add(DCAT_ACCESS_SERVICE_ATTRIBUTE, distribution.getDataService().getId()); - - return objectBuilder.build(); + return jsonFactory.createObjectBuilder() + .add(TYPE, DCAT_DISTRIBUTION_TYPE) + .add(DCT_FORMAT_ATTRIBUTE, jsonFactory.createObjectBuilder() + .add(ID, distribution.getFormat()) + .build()) + .add(DCAT_ACCESS_SERVICE_ATTRIBUTE, context.transform(distribution.getDataService(), JsonObject.class)) + .build(); } } diff --git a/data-protocols/dsp/dsp-catalog/dsp-catalog-transform/src/test/java/org/eclipse/edc/protocol/dsp/catalog/transform/from/JsonObjectFromDistributionTransformerTest.java b/data-protocols/dsp/dsp-catalog/dsp-catalog-transform/src/test/java/org/eclipse/edc/protocol/dsp/catalog/transform/from/JsonObjectFromDistributionTransformerTest.java index 537ca9e9378..b31792b27ed 100644 --- a/data-protocols/dsp/dsp-catalog/dsp-catalog-transform/src/test/java/org/eclipse/edc/protocol/dsp/catalog/transform/from/JsonObjectFromDistributionTransformerTest.java +++ b/data-protocols/dsp/dsp-catalog/dsp-catalog-transform/src/test/java/org/eclipse/edc/protocol/dsp/catalog/transform/from/JsonObjectFromDistributionTransformerTest.java @@ -16,10 +16,10 @@ import jakarta.json.Json; import jakarta.json.JsonBuilderFactory; +import jakarta.json.JsonObject; import org.eclipse.edc.connector.controlplane.catalog.spi.DataService; import org.eclipse.edc.connector.controlplane.catalog.spi.Distribution; import org.eclipse.edc.transform.spi.TransformerContext; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.Map; @@ -30,37 +30,37 @@ import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_ACCESS_SERVICE_ATTRIBUTE; import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DISTRIBUTION_TYPE; import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCT_FORMAT_ATTRIBUTE; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; class JsonObjectFromDistributionTransformerTest { - private JsonBuilderFactory jsonFactory = Json.createBuilderFactory(Map.of()); - private TransformerContext context = mock(TransformerContext.class); + private final JsonBuilderFactory jsonFactory = Json.createBuilderFactory(Map.of()); + private final TransformerContext context = mock(); - private JsonObjectFromDistributionTransformer transformer; - - @BeforeEach - void setUp() { - transformer = new JsonObjectFromDistributionTransformer(jsonFactory); - } + private final JsonObjectFromDistributionTransformer transformer = new JsonObjectFromDistributionTransformer(jsonFactory); @Test void transform_returnJsonObject() { - var distribution = getDistribution(); + var accessServiceJson = jsonFactory.createObjectBuilder().add(ID, "dataServiceId").build(); + when(context.transform(any(), eq(JsonObject.class))).thenReturn(accessServiceJson); + var distribution = Distribution.Builder.newInstance() + .format("format") + .dataService(DataService.Builder.newInstance().id("dataServiceId").build()) + .build(); + var result = transformer.transform(distribution, context); assertThat(result).isNotNull(); assertThat(result.getJsonString(TYPE).getString()).isEqualTo(DCAT_DISTRIBUTION_TYPE); assertThat(result.getJsonObject(DCT_FORMAT_ATTRIBUTE).getJsonString(ID).getString()) .isEqualTo(distribution.getFormat()); - assertThat(result.getJsonString(DCAT_ACCESS_SERVICE_ATTRIBUTE).getString()) - .isEqualTo(distribution.getDataService().getId()); + assertThat(result.getJsonObject(DCAT_ACCESS_SERVICE_ATTRIBUTE).getString(ID)).isEqualTo("dataServiceId"); + verify(context).transform(isA(DataService.class), eq(JsonObject.class)); } - private Distribution getDistribution() { - return Distribution.Builder.newInstance() - .format("format") - .dataService(DataService.Builder.newInstance().id("dataServiceId").build()) - .build(); - } } diff --git a/system-tests/management-api/management-api-test-runner/build.gradle.kts b/system-tests/management-api/management-api-test-runner/build.gradle.kts index 6c9e2b281e7..f00269c265c 100644 --- a/system-tests/management-api/management-api-test-runner/build.gradle.kts +++ b/system-tests/management-api/management-api-test-runner/build.gradle.kts @@ -22,6 +22,7 @@ dependencies { testImplementation(project(":spi:common:json-ld-spi")) testImplementation(project(":spi:control-plane:asset-spi")) testImplementation(project(":spi:control-plane:contract-spi")) + testImplementation(project(":spi:data-plane-selector:data-plane-selector-spi")) testImplementation(project(":core:common:connector-core")) //useful for generic DTOs etc. diff --git a/system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/CatalogApiEndToEndTest.java b/system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/CatalogApiEndToEndTest.java index b895c70cb87..c848cbe92b2 100644 --- a/system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/CatalogApiEndToEndTest.java +++ b/system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/CatalogApiEndToEndTest.java @@ -20,6 +20,8 @@ import org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractDefinition; import org.eclipse.edc.connector.controlplane.policy.spi.PolicyDefinition; import org.eclipse.edc.connector.controlplane.policy.spi.store.PolicyDefinitionStore; +import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance; +import org.eclipse.edc.connector.dataplane.selector.spi.store.DataPlaneInstanceStore; import org.eclipse.edc.junit.annotations.EndToEndTest; import org.eclipse.edc.junit.annotations.PostgresqlIntegrationTest; import org.eclipse.edc.junit.extensions.EdcRuntimeExtension; @@ -43,6 +45,7 @@ import static org.eclipse.edc.sql.testfixtures.PostgresqlEndToEndInstance.createDatabase; import static org.eclipse.edc.test.e2e.managementapi.Runtimes.inMemoryRuntime; import static org.eclipse.edc.test.e2e.managementapi.Runtimes.postgresRuntime; +import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.Matchers.is; public class CatalogApiEndToEndTest { @@ -97,7 +100,7 @@ void requestCatalog_shouldReturnCatalog_withoutQuerySpec() { .body(requestBody) .post("/v2/catalog/request") .then() - .log().ifError() + .log().ifValidationFails() .statusCode(200) .contentType(JSON) .body(TYPE, is("dcat:Catalog")); @@ -123,8 +126,8 @@ void requestCatalog_shouldReturnCatalog_withQuerySpec() { policyDefinitionStore.create(PolicyDefinition.Builder.newInstance().id(policyId).policy(policy).build()); contractDefinitionStore.save(cd); - assetIndex.create(createAsset("id-1").build()); - assetIndex.create(createAsset("id-2").build()); + assetIndex.create(createAsset("id-1", "test-type").build()); + assetIndex.create(createAsset("id-2", "test-type").build()); var criteria = createArrayBuilder() .add(createObjectBuilder() @@ -162,8 +165,12 @@ void requestCatalog_shouldReturnCatalog_withQuerySpec() { @Test void getDataset_shouldReturnDataset() { + var dataPlaneInstance = DataPlaneInstance.Builder.newInstance().url("http://localhost/any") + .allowedDestType("any").allowedSourceType("test-type").allowedTransferType("any").build(); + runtime.getContext().getService(DataPlaneInstanceStore.class).create(dataPlaneInstance); + var assetIndex = runtime.getContext().getService(AssetIndex.class); - assetIndex.create(createAsset("asset-id").build()); + assetIndex.create(createAsset("asset-id", "test-type").build()); var requestBody = createObjectBuilder() .add(CONTEXT, createObjectBuilder().add(EDC_PREFIX, EDC_NAMESPACE)) .add(TYPE, "DatasetRequest") @@ -177,15 +184,17 @@ void getDataset_shouldReturnDataset() { .body(requestBody) .post("/v2/catalog/dataset/request") .then() + .log().ifValidationFails() .statusCode(200) .contentType(JSON) .body(ID, is("asset-id")) - .body(TYPE, is("dcat:Dataset")); + .body(TYPE, is("dcat:Dataset")) + .body("'dcat:distribution'.'dcat:accessService'.@id", notNullValue()); } - private Asset.Builder createAsset(String id) { + private Asset.Builder createAsset(String id, String sourceType) { return Asset.Builder.newInstance() - .dataAddress(DataAddress.Builder.newInstance().type("test-type").build()) + .dataAddress(DataAddress.Builder.newInstance().type(sourceType).build()) .id(id); }