Skip to content

Commit

Permalink
feat(dsp): add transformer for JsonObjectToDataAddress (eclipse-edc#2811
Browse files Browse the repository at this point in the history
)

* feat(dsp): add transformer for JsonObjectToDataAddress

* pr remarks
  • Loading branch information
paullatzelsperger authored and majadlymhmd committed May 10, 2023
1 parent 61a7cd2 commit 1ec7eb5
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import jakarta.json.Json;
import org.eclipse.edc.jsonld.spi.transformer.JsonLdTransformerRegistry;
import org.eclipse.edc.jsonld.transformer.to.JsonObjectToDataAddressTransformer;
import org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.from.JsonObjectFromDataAddressTransformer;
import org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.from.JsonObjectFromTransferCompletionMessageTransformer;
import org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.from.JsonObjectFromTransferProcessTransformer;
Expand Down Expand Up @@ -66,5 +67,6 @@ public void initialize(ServiceExtensionContext context) {
registry.register(new JsonObjectToTransferCompletionMessageTransformer());
registry.register(new JsonObjectToTransferStartMessageTransformer());
registry.register(new JsonObjectToTransferTerminationMessageTransformer());
registry.register(new JsonObjectToDataAddressTransformer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public JsonObjectFromDataAddressTransformer(JsonBuilderFactory jsonBuilderFactor
}

@Override
public @Nullable JsonObject transform(@NotNull DataAddress transferCompletionMessage, @NotNull TransformerContext context) {
public @Nullable JsonObject transform(@NotNull DataAddress dataAddress, @NotNull TransformerContext context) {
var builder = jsonBuilderFactory.createObjectBuilder();

transferCompletionMessage.getProperties().forEach((k, v) -> builder.add(k, v));
dataAddress.getProperties().forEach(builder::add);

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@


class JsonObjectFromDataAddressTransformerTest {
private static final String TEST_KEY = "region";
private static final String TEST_VALUE = "europe";
private final String type = "testType";

private final String key = "testKey";

private final String propertyKey = "region";

private final String propertyValue = "europe";

private final JsonBuilderFactory jsonFactory = Json.createBuilderFactory(Map.of());
private final TransformerContext context = mock(TransformerContext.class);

Expand All @@ -51,20 +47,35 @@ void setUp() {
}

@Test
void transformTransferCompletionMessage() {
void transform() {
var message = DataAddress.Builder.newInstance()
.type(type)
.keyName(key)
.property(propertyKey, propertyValue)
.build();
.type(type)
.keyName(key)
.property(TEST_KEY, TEST_VALUE)
.build();

var result = transformer.transform(message, context);

assertThat(result).isNotNull();
assertThat(result.getJsonString(propertyKey).getString()).isEqualTo(propertyValue);
assertThat(result.getJsonString("type").getString()).isEqualTo(type);
assertThat(result.getJsonString("keyName").getString()).isEqualTo(key);
assertThat(result.getJsonString(TEST_KEY).getString()).isEqualTo(TEST_VALUE);
assertThat(result.getJsonString(DataAddress.TYPE).getString()).isEqualTo(type);
assertThat(result.getJsonString(DataAddress.KEY_NAME).getString()).isEqualTo(key);

verify(context, never()).reportProblem(anyString());
}

@Test
void transform_withNamespace() {
var schema = "https://some.custom.org/schema/";
var message = DataAddress.Builder.newInstance()
.type(type)
.keyName(key)
.property(schema + TEST_KEY, TEST_VALUE)
.build();

var result = transformer.transform(message, context);
assertThat(result).isNotNull();
assertThat(result.getJsonString(schema + TEST_KEY).getString()).isEqualTo(TEST_VALUE);
verify(context, never()).reportProblem(anyString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.jsonld.spi.Namespaces;
import org.eclipse.edc.jsonld.spi.PropertyAndTypeNames;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.spi.types.domain.asset.Asset;
Expand All @@ -30,6 +29,7 @@
import static java.util.stream.Collectors.toMap;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE;

public class JsonObjectFromAssetTransformer extends AbstractJsonLdTransformer<Asset, JsonObject> {
private final ObjectMapper mapper;
Expand All @@ -49,7 +49,7 @@ public JsonObjectFromAssetTransformer(JsonBuilderFactory jsonFactory, ObjectMapp

// replace the special asset properties, starting with "asset:prop:", with the EDC_SCHEMA
var props = asset.getProperties().entrySet().stream()
.collect(toMap(entry -> entry.getKey().replace("asset:prop:", Namespaces.EDC_SCHEMA), Map.Entry::getValue));
.collect(toMap(entry -> entry.getKey().replace("asset:prop:", EDC_NAMESPACE), Map.Entry::getValue));

transformProperties(props, builder, mapper, context);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2023 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
*
*/

package org.eclipse.edc.jsonld.transformer.to;

import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE;


/**
* Converts from an {@link DataAddress} as a {@link JsonObject} in JSON-LD expanded form to an {@link DataAddress}.
*/
public class JsonObjectToDataAddressTransformer extends AbstractJsonLdTransformer<JsonObject, DataAddress> {

//TODO: move into a module-level constants file
public static final String PROPERTIES_KEY = EDC_NAMESPACE + "properties";

public JsonObjectToDataAddressTransformer() {
super(JsonObject.class, DataAddress.class);
}

@Override
public @Nullable DataAddress transform(@NotNull JsonObject jsonObject, @NotNull TransformerContext context) {
var builder = DataAddress.Builder.newInstance();
visitProperties(jsonObject, (s, v) -> transformProperties(s, v, builder, context));
return builder.build();
}

private void transformProperties(String key, JsonValue jsonValue, DataAddress.Builder builder, TransformerContext context) {
if ((PROPERTIES_KEY).equals(key) && jsonValue instanceof JsonArray) {
var props = jsonValue.asJsonArray().getJsonObject(0);
visitProperties(props, (k, val) -> transformProperties(k, val, builder, context));
} else {
var object = transformGenericProperty(jsonValue, context);
if (object instanceof String) {
builder.property(key, object.toString());
} else {
context.reportProblem("DataAddress#properties can only contain Strings, but an attempt was made to put in a " + object.getClass());
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
package org.eclipse.edc.jsonld.transformer;

import jakarta.json.JsonObject;
import org.eclipse.edc.jsonld.spi.Namespaces;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE;


public class PayloadTransformer extends AbstractJsonLdTransformer<JsonObject, Payload> {
public PayloadTransformer() {
Expand All @@ -30,8 +31,8 @@ public PayloadTransformer() {
@Override
public @Nullable Payload transform(@NotNull JsonObject jsonObject, @NotNull TransformerContext context) {
var payload = new Payload();
var nameObj = jsonObject.get(Namespaces.EDC_SCHEMA + "name");
var ageObj = jsonObject.get(Namespaces.EDC_SCHEMA + "age");
var nameObj = jsonObject.get(EDC_NAMESPACE + "name");
var ageObj = jsonObject.get(EDC_NAMESPACE + "age");
transformString(nameObj, payload::setName, context);
var age = (Double) transformGenericProperty(ageObj, context);
payload.setAge(age.intValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import jakarta.json.Json;
import jakarta.json.JsonObject;
import org.eclipse.edc.jsonld.spi.Namespaces;
import org.eclipse.edc.jsonld.spi.PropertyAndTypeNames;
import org.eclipse.edc.jsonld.transformer.Payload;
import org.eclipse.edc.spi.types.domain.asset.Asset;
Expand All @@ -30,6 +29,7 @@
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.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE;
import static org.mockito.Mockito.mock;

class JsonObjectFromAssetTransformerTest {
Expand All @@ -56,11 +56,11 @@ void transform_noCustomProperties() {
assertThat(jsonObject).isNotNull().hasSize(7);
assertThat(jsonObject.getJsonString(ID).getString()).isEqualTo(TEST_ASSET_ID);
assertThat(jsonObject.getJsonString(TYPE).getString()).isEqualTo(PropertyAndTypeNames.EDC_ASSET_TYPE);
assertThat(jsonObject.getJsonString(Namespaces.EDC_SCHEMA + "id").getString()).isEqualTo(TEST_ASSET_ID);
assertThat(jsonObject.getJsonString(Namespaces.EDC_SCHEMA + "contenttype").getString()).isEqualTo(TEST_CONTENT_TYPE);
assertThat(jsonObject.getJsonString(Namespaces.EDC_SCHEMA + "description").getString()).isEqualTo(TEST_DESCRIPTION);
assertThat(jsonObject.getJsonString(Namespaces.EDC_SCHEMA + "name").getString()).isEqualTo(TEST_ASSET_NAME);
assertThat(jsonObject.getJsonString(Namespaces.EDC_SCHEMA + "version").getString()).isEqualTo(TEST_VERSION);
assertThat(jsonObject.getJsonString(EDC_NAMESPACE + "id").getString()).isEqualTo(TEST_ASSET_ID);
assertThat(jsonObject.getJsonString(EDC_NAMESPACE + "contenttype").getString()).isEqualTo(TEST_CONTENT_TYPE);
assertThat(jsonObject.getJsonString(EDC_NAMESPACE + "description").getString()).isEqualTo(TEST_DESCRIPTION);
assertThat(jsonObject.getJsonString(EDC_NAMESPACE + "name").getString()).isEqualTo(TEST_ASSET_NAME);
assertThat(jsonObject.getJsonString(EDC_NAMESPACE + "version").getString()).isEqualTo(TEST_VERSION);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.Json;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import org.eclipse.edc.jsonld.spi.Namespaces;
import org.eclipse.edc.jsonld.spi.PropertyAndTypeNames;
import org.eclipse.edc.jsonld.spi.transformer.JsonLdTransformerRegistryImpl;
import org.eclipse.edc.jsonld.transformer.Payload;
Expand All @@ -39,7 +37,7 @@
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VOCAB;
import static org.eclipse.edc.jsonld.util.JacksonJsonLd.createObjectMapper;
import static org.eclipse.edc.junit.testfixtures.TestUtils.getResourceFileContentAsString;
import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE;

class JsonObjectToAssetTransformerTest {

Expand All @@ -63,7 +61,7 @@ void setUp() throws JsonProcessingException {
typeTransformerRegistry.register(new JsonValueToGenericTypeTransformer(jsonPmapper));
typeTransformerRegistry.register(transformer);
typeTransformerRegistry.register(new PayloadTransformer());
typeTransformerRegistry.registerTypeAlias(Namespaces.EDC_SCHEMA + "customPayload", Payload.class);
typeTransformerRegistry.registerTypeAlias(EDC_NAMESPACE + "customPayload", Payload.class);
}

@Test
Expand Down Expand Up @@ -103,7 +101,7 @@ void transform_withCustomProperty() {
AbstractResultAssert.assertThat(asset).withFailMessage(asset::getFailureDetail).isSucceeded();
assertThat(asset.getContent().getProperties())
.hasSize(6)
.hasEntrySatisfying(Namespaces.EDC_SCHEMA + "payload", o -> assertThat(o).isInstanceOf(Payload.class)
.hasEntrySatisfying(EDC_NAMESPACE + "payload", o -> assertThat(o).isInstanceOf(Payload.class)
.hasFieldOrPropertyWithValue("age", CUSTOM_PAYLOAD_AGE)
.hasFieldOrPropertyWithValue("name", CUSTOM_PAYLOAD_NAME));
}
Expand Down Expand Up @@ -163,22 +161,8 @@ private JsonObjectBuilder createPropertiesBuilder() {

private JsonObjectBuilder createContextBuilder() {
return jsonFactory.createObjectBuilder()
.add(VOCAB, Namespaces.EDC_SCHEMA)
.add("edc", Namespaces.EDC_SCHEMA);
}

/**
* will read the resource with the given name, assuming it's JSON, and expand it to JSON-LD's expanded form.
*/
private JsonObject parseJson(String filename) {
try {
var jsonLd = getResourceFileContentAsString(filename);
JsonObject assetJsonObject = jsonPmapper.readValue(jsonLd, JsonObject.class);
var expandedJsonLd = JsonLdUtil.expand(assetJsonObject).getJsonObject(0);
return expandedJsonLd;
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
.add(VOCAB, EDC_NAMESPACE)
.add("edc", EDC_NAMESPACE);
}


Expand Down
Loading

0 comments on commit 1ec7eb5

Please sign in to comment.