Skip to content

Commit

Permalink
refactor: add JsonLdNamespace usage in DataAddressDspaceTransformer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood authored Oct 21, 2024
1 parent e8b9713 commit 258b7c6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@
* to JSON-LD using the `dspace:` prefix format.
*/
public interface DataAddressDspaceSerialization {
String DSPACE_DATAADDRESS_TYPE = DSPACE_SCHEMA + "DataAddress";
String ENDPOINT_TYPE_PROPERTY = DSPACE_SCHEMA + "endpointType";
String ENDPOINT_PROPERTY = DSPACE_SCHEMA + "endpoint";
String ENDPOINT_PROPERTIES_PROPERTY = DSPACE_SCHEMA + "endpointProperties";
String ENDPOINT_PROPERTY_PROPERTY_TYPE = DSPACE_SCHEMA + "EndpointProperty";
String ENDPOINT_PROPERTY_NAME_PROPERTY = DSPACE_SCHEMA + "name";
String ENDPOINT_PROPERTY_VALUE_PROPERTY = DSPACE_SCHEMA + "value";
String DSPACE_DATAADDRESS_TYPE_TERM = "DataAddress";
String DSPACE_DATAADDRESS_TYPE_IRI = DSPACE_SCHEMA + DSPACE_DATAADDRESS_TYPE_TERM;
String ENDPOINT_TYPE_PROPERTY_TERM = "endpointType";
String ENDPOINT_TYPE_PROPERTY_IRI = DSPACE_SCHEMA + ENDPOINT_TYPE_PROPERTY_TERM;
String ENDPOINT_PROPERTY_TERM = "endpoint";
String ENDPOINT_PROPERTY_IRI = DSPACE_SCHEMA + ENDPOINT_PROPERTY_TERM;
String ENDPOINT_PROPERTIES_PROPERTY_TERM = "endpointProperties";
String ENDPOINT_PROPERTIES_PROPERTY_IRI = DSPACE_SCHEMA + ENDPOINT_PROPERTIES_PROPERTY_TERM;
String ENDPOINT_PROPERTY_PROPERTY_TYPE_TERM = "EndpointProperty";
String ENDPOINT_PROPERTY_PROPERTY_TYPE_IRI = DSPACE_SCHEMA + ENDPOINT_PROPERTY_PROPERTY_TYPE_TERM;
String ENDPOINT_PROPERTY_NAME_PROPERTY_TERM = "name";
String ENDPOINT_PROPERTY_NAME_PROPERTY_IRI = DSPACE_SCHEMA + ENDPOINT_PROPERTY_NAME_PROPERTY_TERM;
String ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM = "value";
String ENDPOINT_PROPERTY_VALUE_PROPERTY_IRI = DSPACE_SCHEMA + ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,37 @@
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import jakarta.json.stream.JsonCollectors;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.jsonld.spi.JsonLdNamespace;
import org.eclipse.edc.jsonld.spi.transformer.AbstractNamespaceAwareJsonLdTransformer;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Set;

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.spi.types.domain.DataAddress.EDC_DATA_ADDRESS_TYPE_PROPERTY;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.DSPACE_DATAADDRESS_TYPE_TERM;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTIES_PROPERTY_TERM;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_NAME_PROPERTY_TERM;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_PROPERTY_TYPE_TERM;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_TYPE_PROPERTY_TERM;

public class JsonObjectFromDataAddressDspaceTransformer extends AbstractJsonLdTransformer<DataAddress, JsonObject> {
public class JsonObjectFromDataAddressDspaceTransformer extends AbstractNamespaceAwareJsonLdTransformer<DataAddress, JsonObject> {

private static final Set<String> EXCLUDED_PROPERTIES = Set.of(EDC_DATA_ADDRESS_TYPE_PROPERTY);
private final JsonBuilderFactory jsonFactory;
private final ObjectMapper mapper;

public JsonObjectFromDataAddressDspaceTransformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper) {
super(DataAddress.class, JsonObject.class);
this(jsonFactory, mapper, new JsonLdNamespace(DSPACE_SCHEMA));
}

public JsonObjectFromDataAddressDspaceTransformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper, JsonLdNamespace namespace) {
super(DataAddress.class, JsonObject.class, namespace);
this.jsonFactory = jsonFactory;
this.mapper = mapper;
}
Expand All @@ -50,21 +61,21 @@ public JsonObjectFromDataAddressDspaceTransformer(JsonBuilderFactory jsonFactory
.collect(JsonCollectors.toJsonArray());

return jsonFactory.createObjectBuilder()
.add(TYPE, DataAddressDspaceSerialization.DSPACE_DATAADDRESS_TYPE)
.add(DataAddressDspaceSerialization.ENDPOINT_TYPE_PROPERTY, dataAddress.getType())
.add(DataAddressDspaceSerialization.ENDPOINT_PROPERTIES_PROPERTY, endpointProperties)
.add(TYPE, forNamespace(DSPACE_DATAADDRESS_TYPE_TERM))
.add(forNamespace(ENDPOINT_TYPE_PROPERTY_TERM), dataAddress.getType())
.add(forNamespace(ENDPOINT_PROPERTIES_PROPERTY_TERM), endpointProperties)
.build();
}

private JsonObject endpointProperty(String key, Object value) {
var builder = jsonFactory.createObjectBuilder()
.add(TYPE, DataAddressDspaceSerialization.ENDPOINT_PROPERTY_PROPERTY_TYPE)
.add(DataAddressDspaceSerialization.ENDPOINT_PROPERTY_NAME_PROPERTY, key);
.add(TYPE, forNamespace(ENDPOINT_PROPERTY_PROPERTY_TYPE_TERM))
.add(forNamespace(ENDPOINT_PROPERTY_NAME_PROPERTY_TERM), key);

if (value instanceof String stringVal) {
builder.add(DataAddressDspaceSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY, stringVal);
builder.add(forNamespace(ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM), stringVal);
} else {
builder.add(DataAddressDspaceSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY, mapper.convertValue(value, JsonObject.class));
builder.add(forNamespace(ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM), mapper.convertValue(value, JsonObject.class));
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,43 @@
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.jsonld.spi.JsonLdNamespace;
import org.eclipse.edc.jsonld.spi.transformer.AbstractNamespaceAwareJsonLdTransformer;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Consumer;
import java.util.function.Function;

import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTIES_PROPERTY_TERM;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_NAME_PROPERTY_TERM;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_TYPE_PROPERTY_TERM;

/**
* Transforms a {@link JsonObject} into a DataAddress using the DSPACE-serialization format.
*/
public class JsonObjectToDataAddressDspaceTransformer extends AbstractJsonLdTransformer<JsonObject, DataAddress> {
public class JsonObjectToDataAddressDspaceTransformer extends AbstractNamespaceAwareJsonLdTransformer<JsonObject, DataAddress> {

public JsonObjectToDataAddressDspaceTransformer() {
super(JsonObject.class, DataAddress.class);
this(new JsonLdNamespace(DSPACE_SCHEMA));
}

public JsonObjectToDataAddressDspaceTransformer(JsonLdNamespace namespace) {
super(JsonObject.class, DataAddress.class, namespace);
}

@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));
transformString(jsonObject.get(forNamespace(ENDPOINT_TYPE_PROPERTY_TERM)), builder::type, context);
transformEndpointProperties(jsonObject.get(forNamespace(ENDPOINT_PROPERTIES_PROPERTY_TERM)), ep -> builder.property(ep.name(), ep.value()), context);
return builder.build();
}

private void transformProperties(String key, JsonValue jsonValue, DataAddress.Builder builder, TransformerContext context) {
switch (key) {
case DataAddressDspaceSerialization.ENDPOINT_PROPERTY -> { }
case DataAddressDspaceSerialization.ENDPOINT_TYPE_PROPERTY -> builder.type(transformString(jsonValue, context));
case DataAddressDspaceSerialization.ENDPOINT_PROPERTIES_PROPERTY ->
transformEndpointProperties(jsonValue, ep -> builder.property(ep.name(), ep.value()), context);
default -> throw new IllegalArgumentException("Unexpected value: " + key);
}
}

/**
* This method transforms a {@code dspace:EndpointProperties} array, which consists of {@code dspace:EndpointProperty} entries
* and invokes a consumer for each of those entries.
Expand All @@ -62,8 +64,8 @@ private void transformProperties(String key, JsonValue jsonValue, DataAddress.Bu
*/
private void transformEndpointProperties(JsonValue jsonValue, Consumer<DspaceEndpointProperty> consumer, TransformerContext context) {
Function<JsonObject, DspaceEndpointProperty> converter = (jo) -> {
var name = transformString(jo.get(DataAddressDspaceSerialization.ENDPOINT_PROPERTY_NAME_PROPERTY), context);
var value = transformString(jo.get(DataAddressDspaceSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY), context);
var name = transformString(jo.get(forNamespace(ENDPOINT_PROPERTY_NAME_PROPERTY_TERM)), context);
var value = transformString(jo.get(forNamespace(ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM)), context);
return new DspaceEndpointProperty(name, value);
};
if (jsonValue instanceof JsonObject object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTIES_PROPERTY;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_NAME_PROPERTY;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_TYPE_PROPERTY;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTIES_PROPERTY_IRI;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_IRI;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_NAME_PROPERTY_IRI;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY_IRI;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_TYPE_PROPERTY_IRI;
import static org.mockito.Mockito.mock;

class JsonObjectFromDataAddressDspaceTransformerTest {
Expand All @@ -48,19 +48,19 @@ void transform() {
var jsonObject = transformer.transform(dataAddress, context);

assertThat(jsonObject).isNotNull();
assertThat(jsonObject.getString(ENDPOINT_TYPE_PROPERTY)).isEqualTo("https://w3id.org/idsa/v4.1/HTTP");
assertThat(jsonObject.get(ENDPOINT_PROPERTY)).isEqualTo(null);
assertThat(jsonObject.getJsonArray(ENDPOINT_PROPERTIES_PROPERTY)).hasSize(3)
assertThat(jsonObject.getString(ENDPOINT_TYPE_PROPERTY_IRI)).isEqualTo("https://w3id.org/idsa/v4.1/HTTP");
assertThat(jsonObject.get(ENDPOINT_PROPERTY_IRI)).isEqualTo(null);
assertThat(jsonObject.getJsonArray(ENDPOINT_PROPERTIES_PROPERTY_IRI)).hasSize(3)
.anySatisfy(jv -> {
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_NAME_PROPERTY)).isEqualTo("authorization");
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_VALUE_PROPERTY)).isEqualTo("secret-token");
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_NAME_PROPERTY_IRI)).isEqualTo("authorization");
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_VALUE_PROPERTY_IRI)).isEqualTo("secret-token");
}).anySatisfy(jv -> {
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_NAME_PROPERTY)).isEqualTo("foo");
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_VALUE_PROPERTY)).isEqualTo("bar");
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_NAME_PROPERTY_IRI)).isEqualTo("foo");
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_VALUE_PROPERTY_IRI)).isEqualTo("bar");
})
.anySatisfy(jv -> {
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_NAME_PROPERTY)).isEqualTo("endpoint");
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_VALUE_PROPERTY)).isEqualTo("https://example.com");
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_NAME_PROPERTY_IRI)).isEqualTo("endpoint");
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_VALUE_PROPERTY_IRI)).isEqualTo("https://example.com");
});
}

Expand All @@ -76,10 +76,10 @@ void transform_withComplexProperty() {
var jsonObject = transformer.transform(dataAddress, context);

assertThat(jsonObject).isNotNull();
assertThat(jsonObject.getJsonArray(ENDPOINT_PROPERTIES_PROPERTY))
assertThat(jsonObject.getJsonArray(ENDPOINT_PROPERTIES_PROPERTY_IRI))
.anySatisfy(jv -> {
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_NAME_PROPERTY)).isEqualTo("foo");
assertThat(jv.asJsonObject().getJsonObject(ENDPOINT_PROPERTY_VALUE_PROPERTY))
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_NAME_PROPERTY_IRI)).isEqualTo("foo");
assertThat(jv.asJsonObject().getJsonObject(ENDPOINT_PROPERTY_VALUE_PROPERTY_IRI))
.containsKey("complexObj");
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.CONTEXT;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VOCAB;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_PREFIX;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.transform.transformer.TestInput.getExpanded;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.DSPACE_DATAADDRESS_TYPE;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTIES_PROPERTY;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_PROPERTY_TYPE;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_TYPE_PROPERTY;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.DSPACE_DATAADDRESS_TYPE_IRI;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTIES_PROPERTY_IRI;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_IRI;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_PROPERTY_PROPERTY_TYPE_IRI;
import static org.eclipse.edc.transform.transformer.dspace.DataAddressDspaceSerialization.ENDPOINT_TYPE_PROPERTY_IRI;
import static org.mockito.Mockito.mock;

class JsonObjectToDataAddressDspaceTransformerTest {
Expand All @@ -47,10 +46,10 @@ class JsonObjectToDataAddressDspaceTransformerTest {
void transform() {
var jsonObj = jsonFactory.createObjectBuilder()
.add(CONTEXT, createContextBuilder().build())
.add(TYPE, DSPACE_DATAADDRESS_TYPE)
.add(ENDPOINT_TYPE_PROPERTY, "https://w3id.org/idsa/v4.1/HTTP")
.add(ENDPOINT_PROPERTY, "http://example.com")
.add(ENDPOINT_PROPERTIES_PROPERTY, jsonFactory.createArrayBuilder()
.add(TYPE, DSPACE_DATAADDRESS_TYPE_IRI)
.add(ENDPOINT_TYPE_PROPERTY_IRI, "https://w3id.org/idsa/v4.1/HTTP")
.add(ENDPOINT_PROPERTY_IRI, "http://example.com")
.add(ENDPOINT_PROPERTIES_PROPERTY_IRI, jsonFactory.createArrayBuilder()
.add(property("authorization", "some-token"))
.add(property("authType", "bearer"))
.add(property("foo", "bar"))
Expand All @@ -68,27 +67,9 @@ void transform() {
.containsEntry("fizz", "buzz");
}

@Test
void transform_withIllegalProperty() {
var jsonObj = jsonFactory.createObjectBuilder()
.add(CONTEXT, createContextBuilder().build())
.add(TYPE, DSPACE_DATAADDRESS_TYPE)
.add(ENDPOINT_TYPE_PROPERTY, "https://w3id.org/idsa/v4.1/HTTP")
.add(ENDPOINT_PROPERTY, "http://example.com")
.add(ENDPOINT_PROPERTIES_PROPERTY, jsonFactory.createArrayBuilder()
.add(property("fizz", "buzz"))
)
.add("rogueProperty", 42L)
.build();

assertThatThrownBy(() -> transformer.transform(getExpanded(jsonObj), context))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Unexpected value: %srogueProperty".formatted(DSPACE_SCHEMA));
}

private JsonObjectBuilder property(String key, String value) {
return jsonFactory.createObjectBuilder()
.add(TYPE, ENDPOINT_PROPERTY_PROPERTY_TYPE)
.add(TYPE, ENDPOINT_PROPERTY_PROPERTY_TYPE_IRI)
.add("name", key)
.add("value", value);
}
Expand Down

0 comments on commit 258b7c6

Please sign in to comment.