Skip to content

Commit

Permalink
feat(dsp): transform dspace DataAddress (#3975)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt authored Mar 8, 2024
1 parent 2707784 commit 1fe0c95
Show file tree
Hide file tree
Showing 35 changed files with 246 additions and 169 deletions.
4 changes: 2 additions & 2 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ maven/mavencentral/com.lmax/disruptor/3.4.4, Apache-2.0, approved, clearlydefine
maven/mavencentral/com.networknt/json-schema-validator/1.0.76, Apache-2.0, approved, CQ22638
maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.28, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.37.3, Apache-2.0, approved, #11701
maven/mavencentral/com.puppycrawl.tools/checkstyle/10.14.0, , restricted, clearlydefined
maven/mavencentral/com.puppycrawl.tools/checkstyle/10.14.0, LGPL-2.1-or-later AND (Apache-2.0 AND LGPL-2.1-or-later) AND Apache-2.0, approved, #13562
maven/mavencentral/com.samskivert/jmustache/1.15, BSD-2-Clause, approved, clearlydefined
maven/mavencentral/com.squareup.okhttp3/okhttp-dnsoverhttps/4.12.0, Apache-2.0, approved, #11159
maven/mavencentral/com.squareup.okhttp3/okhttp/4.12.0, Apache-2.0, approved, #11156
Expand Down Expand Up @@ -214,7 +214,7 @@ maven/mavencentral/org.apache.httpcomponents/httpclient/4.5.13, Apache-2.0 AND L
maven/mavencentral/org.apache.httpcomponents/httpcore/4.4.13, Apache-2.0, approved, CQ23528
maven/mavencentral/org.apache.httpcomponents/httpcore/4.4.14, Apache-2.0, approved, CQ23528
maven/mavencentral/org.apache.httpcomponents/httpmime/4.5.13, Apache-2.0, approved, CQ11718
maven/mavencentral/org.apache.kafka/kafka-clients/3.7.0, , restricted, clearlydefined
maven/mavencentral/org.apache.kafka/kafka-clients/3.7.0, Apache-2.0 AND (Apache-2.0 AND MIT) AND (Apache-2.0 AND BSD-3-Clause), approved, #13623
maven/mavencentral/org.apache.maven.doxia/doxia-core/1.12.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.apache.maven.doxia/doxia-logging-api/1.12.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.apache.maven.doxia/doxia-module-xdoc/1.12.0, Apache-2.0, approved, clearlydefined
Expand Down
30 changes: 30 additions & 0 deletions core/common/transform-dspace/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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`
`maven-publish`
}

dependencies {
api(project(":spi:common:catalog-spi"))
api(project(":spi:common:core-spi"))
api(project(":spi:common:json-ld-spi"))
api(project(":spi:common:transform-spi"))

api(libs.jakartaJson)

testImplementation(project(":core:common:junit"))
testImplementation(project(":extensions:common:json-ld"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
*/

package org.eclipse.edc.connector.api.signaling.transform;
package org.eclipse.edc.core.transform.dspace;

import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
*
*/

package org.eclipse.edc.connector.api.signaling.transform.from;
package org.eclipse.edc.core.transform.dspace.from;

import com.fasterxml.jackson.databind.ObjectMapper;
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.spi.types.domain.DataAddress;
import org.eclipse.edc.transform.spi.TransformerContext;
Expand All @@ -25,18 +26,18 @@

import java.util.Set;

import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.DSPACE_DATAADDRESS_TYPE;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_PROPERTIES_PROPERTY;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_PROPERTY;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_NAME_PROPERTY;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_PROPERTY_TYPE;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_TYPE_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.DSPACE_DATAADDRESS_TYPE;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTIES_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_NAME_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_PROPERTY_TYPE;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_TYPE_PROPERTY;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.spi.types.domain.DataAddress.EDC_DATA_ADDRESS_TYPE_PROPERTY;

public class JsonObjectFromDataAddressTransformer extends AbstractJsonLdTransformer<DataAddress, JsonObject> {
private static final Set<String> EXCLUDED_PROPERTIES = Set.of(EDC_DATA_ADDRESS_TYPE_PROPERTY, "endpoint");

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

Expand All @@ -48,22 +49,16 @@ public JsonObjectFromDataAddressTransformer(JsonBuilderFactory jsonFactory, Obje

@Override
public @Nullable JsonObject transform(@NotNull DataAddress dataAddress, @NotNull TransformerContext context) {

var propsBuilder = jsonFactory.createArrayBuilder();

dataAddress.getProperties().entrySet().stream()
var endpointProperties = dataAddress.getProperties().entrySet().stream()
.filter(e -> !EXCLUDED_PROPERTIES.contains(e.getKey()))
.forEach(e -> propsBuilder.add(endpointProperty(e.getKey(), e.getValue())));
.map(it -> endpointProperty(it.getKey(), it.getValue()))
.collect(JsonCollectors.toJsonArray());

var objectBuilder = jsonFactory.createObjectBuilder()
return jsonFactory.createObjectBuilder()
.add(TYPE, DSPACE_DATAADDRESS_TYPE)
.add(ENDPOINT_TYPE_PROPERTY, dataAddress.getType())
.add(ENDPOINT_PROPERTY, dataAddress.getProperties().getOrDefault("endpoint", "https://example.com").toString());


objectBuilder.add(ENDPOINT_PROPERTIES_PROPERTY, propsBuilder.build());

return objectBuilder.build();
.add(ENDPOINT_PROPERTIES_PROPERTY, endpointProperties)
.build();
}

private JsonObject endpointProperty(String key, Object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
*
*/

package org.eclipse.edc.connector.api.signaling.transform.to;
package org.eclipse.edc.core.transform.dspace.to;

import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.transform.spi.TransformerContext;
Expand All @@ -27,8 +26,11 @@
import java.util.function.Consumer;
import java.util.function.Function;

import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_NAME_PROPERTY;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTIES_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_NAME_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_TYPE_PROPERTY;

/**
* Transforms a {@link JsonObject} into a DataAddress using the DSPACE-serialization format.
Expand All @@ -47,13 +49,9 @@ public JsonObjectToDataAddressTransformer() {

private void transformProperties(String key, JsonValue jsonValue, DataAddress.Builder builder, TransformerContext context) {
switch (key) {
case DspaceDataAddressSerialization.ENDPOINT_PROPERTY ->
transformString(jsonValue, endpoint -> builder.property("endpoint", endpoint), context);
case DspaceDataAddressSerialization.ENDPOINT_TYPE_PROPERTY -> {
var endpointType = transformString(jsonValue, context);
builder.type(endpointType);
}
case DspaceDataAddressSerialization.ENDPOINT_PROPERTIES_PROPERTY ->
case ENDPOINT_PROPERTY -> { }
case ENDPOINT_TYPE_PROPERTY -> builder.type(transformString(jsonValue, context));
case ENDPOINT_PROPERTIES_PROPERTY ->
transformEndpointProperties(jsonValue, ep -> builder.property(ep.name(), ep.value()), context);
default -> throw new IllegalArgumentException("Unexpected value: " + key);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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
*
*/

package org.eclipse.edc.core.transform.dspace;

import com.apicatalog.jsonld.document.JsonDocument;
import jakarta.json.JsonObject;

import static com.apicatalog.jsonld.JsonLd.expand;

public class TestFunctions {

/**
* Expands test input as Json-ld is required to be in this form
*/
public static JsonObject getExpanded(JsonObject message) {
try {
return expand(JsonDocument.of(message)).get().asJsonArray().getJsonObject(0);
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
*/

package org.eclipse.edc.connector.api.signaling.transform.from;
package org.eclipse.edc.core.transform.dspace.from;

import jakarta.json.Json;
import org.eclipse.edc.jsonld.util.JacksonJsonLd;
Expand All @@ -23,16 +23,17 @@
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_PROPERTIES_PROPERTY;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_PROPERTY;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_NAME_PROPERTY;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_TYPE_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTIES_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_NAME_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_VALUE_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_TYPE_PROPERTY;
import static org.mockito.Mockito.mock;

class JsonObjectFromDataAddressTransformerTest {

private final JsonObjectFromDataAddressTransformer transformer = new JsonObjectFromDataAddressTransformer(Json.createBuilderFactory(Map.of()), JacksonJsonLd.createObjectMapper());
private final JsonObjectFromDataAddressTransformer transformer = new JsonObjectFromDataAddressTransformer(
Json.createBuilderFactory(Map.of()), JacksonJsonLd.createObjectMapper());
private final TransformerContext context = mock();

@Test
Expand All @@ -48,14 +49,18 @@ void transform() {

assertThat(jsonObject).isNotNull();
assertThat(jsonObject.getString(ENDPOINT_TYPE_PROPERTY)).isEqualTo("https://w3id.org/idsa/v4.1/HTTP");
assertThat(jsonObject.getString(ENDPOINT_PROPERTY)).isEqualTo("https://example.com");
assertThat(jsonObject.getJsonArray(ENDPOINT_PROPERTIES_PROPERTY)).hasSize(2)
assertThat(jsonObject.get(ENDPOINT_PROPERTY)).isEqualTo(null);
assertThat(jsonObject.getJsonArray(ENDPOINT_PROPERTIES_PROPERTY)).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");
}).anySatisfy(jv -> {
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_NAME_PROPERTY)).isEqualTo("foo");
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_VALUE_PROPERTY)).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");
});
}

Expand All @@ -71,16 +76,11 @@ void transform_withComplexProperty() {
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.getString(ENDPOINT_PROPERTY)).isEqualTo("https://example.com");
assertThat(jsonObject.getJsonArray(ENDPOINT_PROPERTIES_PROPERTY)).hasSize(2)
assertThat(jsonObject.getJsonArray(ENDPOINT_PROPERTIES_PROPERTY))
.anySatisfy(jv -> {
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_NAME_PROPERTY)).isEqualTo("authorization");
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_VALUE_PROPERTY)).isEqualTo("secret-token");
}).anySatisfy(jv -> {
assertThat(jv.asJsonObject().getString(ENDPOINT_PROPERTY_NAME_PROPERTY)).isEqualTo("foo");
assertThat(jv.asJsonObject().getJsonObject(ENDPOINT_PROPERTY_VALUE_PROPERTY))
.containsKey("complexObj");
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@
*
*/

package org.eclipse.edc.connector.api.signaling.transform.to;
package org.eclipse.edc.core.transform.dspace.to;

import jakarta.json.Json;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObjectBuilder;
import org.eclipse.edc.core.transform.dspace.TestFunctions;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.Test;

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.connector.api.signaling.transform.DspaceDataAddressSerialization.DSPACE_DATAADDRESS_TYPE;
import static org.eclipse.edc.connector.api.signaling.transform.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_PROPERTY_TYPE;
import static org.eclipse.edc.connector.api.signaling.transform.TestFunctions.getExpanded;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.DSPACE_DATAADDRESS_TYPE;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTIES_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTY;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_PROPERTY_PROPERTY_TYPE;
import static org.eclipse.edc.core.transform.dspace.DspaceDataAddressSerialization.ENDPOINT_TYPE_PROPERTY;
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;
Expand All @@ -45,42 +48,41 @@ void transform() {
var jsonObj = jsonFactory.createObjectBuilder()
.add(CONTEXT, createContextBuilder().build())
.add(TYPE, DSPACE_DATAADDRESS_TYPE)
.add("endpointType", "https://w3id.org/idsa/v4.1/HTTP")
.add("endpoint", "http://example.com")
.add("endpointProperties", jsonFactory.createArrayBuilder()
.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("authorization", "some-token"))
.add(property("authType", "bearer"))
.add(property("foo", "bar"))
.add(property("fizz", "buzz"))
)
.build();

var expanded = getExpanded(jsonObj);
var expanded = TestFunctions.getExpanded(jsonObj);

var dataAddress = transformer.transform(expanded, context);
assertThat(dataAddress).isNotNull();
assertThat(dataAddress.getType()).isEqualTo("https://w3id.org/idsa/v4.1/HTTP");
assertThat(dataAddress.getProperties())
.containsEntry("authorization", "some-token")
.containsEntry("authType", "bearer")
.containsEntry("fizz", "buzz")
.containsEntry("endpoint", "http://example.com");
.containsEntry("fizz", "buzz");
}

@Test
void transform_withIllegalProperty() {
var jsonObj = jsonFactory.createObjectBuilder()
.add(CONTEXT, createContextBuilder().build())
.add(TYPE, DSPACE_DATAADDRESS_TYPE)
.add("endpointType", "https://w3id.org/idsa/v4.1/HTTP")
.add("endpoint", "http://example.com")
.add("endpointProperties", jsonFactory.createArrayBuilder()
.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();

var expanded = getExpanded(jsonObj);
var expanded = TestFunctions.getExpanded(jsonObj);

assertThatThrownBy(() -> transformer.transform(expanded, context))
.isInstanceOf(IllegalArgumentException.class)
Expand All @@ -100,4 +102,4 @@ private JsonArrayBuilder createContextBuilder() {
.add(jsonFactory.createObjectBuilder().add(DSPACE_PREFIX, DSPACE_SCHEMA));
}

}
}
1 change: 1 addition & 0 deletions data-protocols/dsp/dsp-api-configuration/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
api(project(":data-protocols:dsp:dsp-http-spi"))
implementation(project(":core:common:jersey-providers"))
implementation(project(":core:common:transform-core"))
implementation(project(":core:common:transform-dspace"))

testImplementation(project(":core:common:junit"))
}
Loading

0 comments on commit 1fe0c95

Please sign in to comment.