Skip to content

Commit

Permalink
fix: accessService serialization (#4088)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt authored Apr 5, 2024
1 parent eff6a21 commit 316fa3a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
6 changes: 3 additions & 3 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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"));
Expand All @@ -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()
Expand Down Expand Up @@ -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")
Expand All @@ -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);
}

Expand Down

0 comments on commit 316fa3a

Please sign in to comment.