Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: extract dsp catalog transform lib #4551

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data-protocols/dsp/dsp-catalog/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ dependencies {
api(project(":data-protocols:dsp:dsp-catalog:dsp-catalog-http-dispatcher"))
api(project(":data-protocols:dsp:dsp-catalog:dsp-catalog-transform"))
api(project(":data-protocols:dsp:dsp-catalog:lib:dsp-catalog-validation-lib"))
api(project(":data-protocols:dsp:dsp-catalog:lib:dsp-catalog-transform-lib"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ plugins {
}

dependencies {
api(project(":spi:common:json-ld-spi"))
api(project(":spi:control-plane:catalog-spi"))
api(project(":extensions:common:json-ld"))
api(project(":data-protocols:dsp:dsp-spi"))
api(project(":data-protocols:dsp:dsp-http-spi"))

api(project(":spi:common:core-spi"))

implementation(project(":core:common:lib:transform-lib"))
implementation(project(":data-protocols:dsp:dsp-catalog:lib:dsp-catalog-transform-lib"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

plugins {
`java-library`
}

dependencies {
api(project(":spi:common:json-ld-spi"))
api(project(":data-protocols:dsp:dsp-spi"))
implementation(project(":core:common:lib:transform-lib"))

testImplementation(project(":core:common:junit"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,39 @@
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.controlplane.catalog.spi.CatalogError;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.jsonld.spi.transformer.AbstractNamespaceAwareJsonLdTransformer;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_ERROR;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_ERROR_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON_TERM;

/**
* Transforms a {@link CatalogError} to a {@link JsonObject} in JSON-LD expanded form.
*/
public class JsonObjectFromCatalogErrorTransformer extends AbstractJsonLdTransformer<CatalogError, JsonObject> {
public class JsonObjectFromCatalogErrorTransformer extends AbstractNamespaceAwareJsonLdTransformer<CatalogError, JsonObject> {

private final JsonBuilderFactory jsonFactory;

public JsonObjectFromCatalogErrorTransformer(JsonBuilderFactory jsonFactory) {
super(CatalogError.class, JsonObject.class);
this(jsonFactory, DSPACE_SCHEMA);
}

public JsonObjectFromCatalogErrorTransformer(JsonBuilderFactory jsonFactory, String schema) {
super(CatalogError.class, JsonObject.class, schema);
this.jsonFactory = jsonFactory;
}

@Override
public @Nullable JsonObject transform(@NotNull CatalogError error, @NotNull TransformerContext context) {
return jsonFactory.createObjectBuilder()
.add(TYPE, DSPACE_TYPE_CATALOG_ERROR)
.add(DSPACE_PROPERTY_CODE, error.getCode())
.add(DSPACE_PROPERTY_REASON, jsonFactory.createArrayBuilder(error.getMessages()))
.add(TYPE, forNamespace(DSPACE_TYPE_CATALOG_ERROR_TERM))
.add(forNamespace(DSPACE_PROPERTY_CODE_TERM), error.getCode())
.add(forNamespace(DSPACE_PROPERTY_REASON_TERM), jsonFactory.createArrayBuilder(error.getMessages()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,39 @@
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.controlplane.catalog.spi.CatalogRequestMessage;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.jsonld.spi.transformer.AbstractNamespaceAwareJsonLdTransformer;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_PROPERTY_FILTER_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_REQUEST_MESSAGE_IRI;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_PROPERTY_FILTER_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_REQUEST_MESSAGE_TERM;

/**
* Transforms a {@link CatalogRequestMessage} to a {@link JsonObject} in JSON-LD expanded form.
*/
public class JsonObjectFromCatalogRequestMessageTransformer extends AbstractJsonLdTransformer<CatalogRequestMessage, JsonObject> {
public class JsonObjectFromCatalogRequestMessageTransformer extends AbstractNamespaceAwareJsonLdTransformer<CatalogRequestMessage, JsonObject> {

private final JsonBuilderFactory jsonFactory;

public JsonObjectFromCatalogRequestMessageTransformer(JsonBuilderFactory jsonFactory) {
super(CatalogRequestMessage.class, JsonObject.class);
this(jsonFactory, DSPACE_SCHEMA);
}

public JsonObjectFromCatalogRequestMessageTransformer(JsonBuilderFactory jsonFactory, String schema) {
super(CatalogRequestMessage.class, JsonObject.class, schema);
this.jsonFactory = jsonFactory;
}

@Override
public @Nullable JsonObject transform(@NotNull CatalogRequestMessage message, @NotNull TransformerContext context) {
var builder = jsonFactory.createObjectBuilder();
builder.add(TYPE, DSPACE_TYPE_CATALOG_REQUEST_MESSAGE_IRI);
builder.add(TYPE, forNamespace(DSPACE_TYPE_CATALOG_REQUEST_MESSAGE_TERM));

if (message.getQuerySpec() != null) {
builder.add(DSPACE_PROPERTY_FILTER_IRI, context.transform(message.getQuerySpec(), JsonObject.class));
builder.add(forNamespace(DSPACE_PROPERTY_FILTER_TERM), context.transform(message.getQuerySpec(), JsonObject.class));
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.controlplane.catalog.spi.Catalog;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.jsonld.spi.transformer.AbstractNamespaceAwareJsonLdTransformer;
import org.eclipse.edc.spi.agent.ParticipantIdMapper;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
Expand All @@ -28,22 +28,27 @@
import static java.util.Optional.ofNullable;
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.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_CATALOG_TYPE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DATASET_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DATA_SERVICE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DISTRIBUTION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DSPACE_PROPERTY_PARTICIPANT_ID;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DSPACE_PROPERTY_PARTICIPANT_ID_TERM;

/**
* Converts from a {@link Catalog} to a DCAT catalog as a {@link JsonObject} in JSON-LD expanded form.
*/
public class JsonObjectFromCatalogTransformer extends AbstractJsonLdTransformer<Catalog, JsonObject> {
public class JsonObjectFromCatalogTransformer extends AbstractNamespaceAwareJsonLdTransformer<Catalog, JsonObject> {
private final JsonBuilderFactory jsonFactory;
private final ObjectMapper mapper;
private final ParticipantIdMapper participantIdMapper;

public JsonObjectFromCatalogTransformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper, ParticipantIdMapper participantIdMapper) {
super(Catalog.class, JsonObject.class);
this(jsonFactory, mapper, participantIdMapper, DSPACE_SCHEMA);
}

public JsonObjectFromCatalogTransformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper, ParticipantIdMapper participantIdMapper, String namespace) {
super(Catalog.class, JsonObject.class, namespace);
this.jsonFactory = jsonFactory;
this.mapper = mapper;
this.participantIdMapper = participantIdMapper;
Expand All @@ -70,7 +75,7 @@ public JsonObjectFromCatalogTransformer(JsonBuilderFactory jsonFactory, ObjectMa
.add(DCAT_DISTRIBUTION_ATTRIBUTE, distributions)
.add(DCAT_DATA_SERVICE_ATTRIBUTE, dataServices);

ofNullable(catalog.getParticipantId()).ifPresent(pid -> objectBuilder.add(DSPACE_PROPERTY_PARTICIPANT_ID, participantIdMapper.toIri(pid)));
ofNullable(catalog.getParticipantId()).ifPresent(pid -> objectBuilder.add(forNamespace(DSPACE_PROPERTY_PARTICIPANT_ID_TERM), participantIdMapper.toIri(pid)));

transformProperties(catalog.getProperties(), objectBuilder, mapper, context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,36 @@

import jakarta.json.JsonObject;
import org.eclipse.edc.connector.controlplane.catalog.spi.CatalogRequestMessage;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.jsonld.spi.transformer.AbstractNamespaceAwareJsonLdTransformer;
import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_PROPERTY_FILTER_IRI;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_PROPERTY_FILTER_TERM;

/**
* Transforms a {@link JsonObject} in JSON-LD expanded form to a {@link CatalogRequestMessage}.
*/
public class JsonObjectToCatalogRequestMessageTransformer extends AbstractJsonLdTransformer<JsonObject, CatalogRequestMessage> {
public class JsonObjectToCatalogRequestMessageTransformer extends AbstractNamespaceAwareJsonLdTransformer<JsonObject, CatalogRequestMessage> {

public JsonObjectToCatalogRequestMessageTransformer() {
super(JsonObject.class, CatalogRequestMessage.class);
this(DSPACE_SCHEMA);
}

public JsonObjectToCatalogRequestMessageTransformer(String namespace) {
super(JsonObject.class, CatalogRequestMessage.class, namespace);
}

@Override
public @Nullable CatalogRequestMessage transform(@NotNull JsonObject object, @NotNull TransformerContext context) {
var builder = CatalogRequestMessage.Builder.newInstance();

Optional.of(object)
.map(it -> it.get(DSPACE_PROPERTY_FILTER_IRI))
.map(it -> it.get(forNamespace(DSPACE_PROPERTY_FILTER_TERM)))
.map(it -> transformObject(it, QuerySpec.class, context))
.ifPresent(builder::querySpec);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_ERROR;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_ERROR_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON_IRI;
import static org.mockito.Mockito.mock;

class JsonObjectFromCatalogErrorTransformerTest {
Expand All @@ -54,8 +54,8 @@ void transform_returnJsonObject() {
var result = transformer.transform(error, context);

assertThat(result).isNotNull();
assertThat(result.getJsonString(TYPE).getString()).isEqualTo(DSPACE_TYPE_CATALOG_ERROR);
assertThat(result.getString(DSPACE_PROPERTY_CODE)).isEqualTo("code");
assertThat(result.getJsonArray(DSPACE_PROPERTY_REASON)).contains(Json.createValue("message"));
assertThat(result.getJsonString(TYPE).getString()).isEqualTo(DSPACE_TYPE_CATALOG_ERROR_IRI);
assertThat(result.getString(DSPACE_PROPERTY_CODE_IRI)).isEqualTo("code");
assertThat(result.getJsonArray(DSPACE_PROPERTY_REASON_IRI)).contains(Json.createValue("message"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_CATALOG_TYPE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DATASET_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DATA_SERVICE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DSPACE_PROPERTY_PARTICIPANT_ID;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DSPACE_PROPERTY_PARTICIPANT_ID_IRI;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -97,7 +97,7 @@ void transform_returnJsonObject() {
.isInstanceOf(JsonArray.class)
.matches(v -> v.asJsonArray().size() == 1)
.matches(v -> v.asJsonArray().get(0).equals(dataServiceJson));
assertThat(result.getString(DSPACE_PROPERTY_PARTICIPANT_ID)).isEqualTo("urn:namespace:participantId");
assertThat(result.getString(DSPACE_PROPERTY_PARTICIPANT_ID_IRI)).isEqualTo("urn:namespace:participantId");
assertThat(result.get(CATALOG_PROPERTY)).isNotNull();

verify(context, times(1)).transform(catalog.getDatasets().get(0), JsonObject.class);
Expand Down
Loading
Loading