From 0dc4cdbb7b2d77cc219e3b7765622166e08e0a61 Mon Sep 17 00:00:00 2001 From: Enrico Risa Date: Tue, 3 Dec 2024 16:53:33 +0100 Subject: [PATCH] feat: removes JSON-LD requirements in dsp version endpoint (#4655) --- .../type/DspVersionPropertyAndTypeNames.java | 9 ++--- .../http/api/DspVersionApiExtension.java | 11 +++--- ...ObjectFromProtocolVersionsTransformer.java | 26 ++++++++++---- .../JsonObjectFromVersionsError.java | 26 ++++++++------ ...ctFromProtocolVersionsTransformerTest.java | 4 ++- ...bjectFromVersionsErrorTransformerTest.java | 11 +++--- .../protocol/DspVersionApiEndToEndTest.java | 36 ++++++++----------- 7 files changed, 63 insertions(+), 60 deletions(-) diff --git a/data-protocols/dsp/dsp-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/type/DspVersionPropertyAndTypeNames.java b/data-protocols/dsp/dsp-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/type/DspVersionPropertyAndTypeNames.java index c61aa9fc063..49567651c51 100644 --- a/data-protocols/dsp/dsp-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/type/DspVersionPropertyAndTypeNames.java +++ b/data-protocols/dsp/dsp-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/type/DspVersionPropertyAndTypeNames.java @@ -14,17 +14,14 @@ package org.eclipse.edc.protocol.dsp.spi.type; -import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA; - /** * Dataspace protocol types and attributes for version request. */ public interface DspVersionPropertyAndTypeNames { - String DSPACE_PROPERTY_PROTOCOL_VERSIONS = DSPACE_SCHEMA + "protocolVersions"; + String DSPACE_PROPERTY_PROTOCOL_VERSIONS = "protocolVersions"; - String DSPACE_TYPE_VERSIONS_ERROR = DSPACE_SCHEMA + "VersionsError"; - String DSPACE_PROPERTY_VERSION = DSPACE_SCHEMA + "version"; - String DSPACE_PROPERTY_PATH = DSPACE_SCHEMA + "path"; + String DSPACE_PROPERTY_VERSION = "version"; + String DSPACE_PROPERTY_PATH = "path"; } diff --git a/data-protocols/dsp/dsp-version/dsp-version-http-api/src/main/java/org/eclipse/edc/protocol/dsp/version/http/api/DspVersionApiExtension.java b/data-protocols/dsp/dsp-version/dsp-version-http-api/src/main/java/org/eclipse/edc/protocol/dsp/version/http/api/DspVersionApiExtension.java index 338dba314d8..14eb29433f2 100644 --- a/data-protocols/dsp/dsp-version/dsp-version-http-api/src/main/java/org/eclipse/edc/protocol/dsp/version/http/api/DspVersionApiExtension.java +++ b/data-protocols/dsp/dsp-version/dsp-version-http-api/src/main/java/org/eclipse/edc/protocol/dsp/version/http/api/DspVersionApiExtension.java @@ -26,15 +26,12 @@ import org.eclipse.edc.spi.system.ServiceExtensionContext; import org.eclipse.edc.spi.types.TypeManager; import org.eclipse.edc.transform.spi.TypeTransformerRegistry; -import org.eclipse.edc.web.jersey.providers.jsonld.JerseyJsonLdInterceptor; import org.eclipse.edc.web.spi.WebService; import org.eclipse.edc.web.spi.configuration.ApiContext; import java.util.Map; -import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_SCOPE_V_08; import static org.eclipse.edc.protocol.dsp.version.http.api.DspVersionApiExtension.NAME; -import static org.eclipse.edc.spi.constants.CoreConstants.JSON_LD; /** * Provide API for the protocol versions. @@ -69,13 +66,13 @@ public String name() { @Override public void initialize(ServiceExtensionContext context) { - var jsonLdMapper = typeManager.getMapper(JSON_LD); - transformerRegistry.register(new JsonObjectFromProtocolVersionsTransformer()); - transformerRegistry.register(new JsonObjectFromVersionsError(Json.createBuilderFactory(Map.of()))); + var jsonFactory = Json.createBuilderFactory(Map.of()); + + transformerRegistry.register(new JsonObjectFromProtocolVersionsTransformer(jsonFactory)); + transformerRegistry.register(new JsonObjectFromVersionsError(jsonFactory)); webService.registerResource(ApiContext.PROTOCOL, new DspVersionApiController(requestHandler, service)); - webService.registerDynamicResource(ApiContext.PROTOCOL, DspVersionApiController.class, new JerseyJsonLdInterceptor(jsonLd, jsonLdMapper, DSP_SCOPE_V_08)); } } diff --git a/data-protocols/dsp/dsp-version/dsp-version-http-api/src/main/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromProtocolVersionsTransformer.java b/data-protocols/dsp/dsp-version/dsp-version-http-api/src/main/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromProtocolVersionsTransformer.java index 38e9a7875d6..b4372378fe4 100644 --- a/data-protocols/dsp/dsp-version/dsp-version-http-api/src/main/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromProtocolVersionsTransformer.java +++ b/data-protocols/dsp/dsp-version/dsp-version-http-api/src/main/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromProtocolVersionsTransformer.java @@ -14,11 +14,11 @@ package org.eclipse.edc.protocol.dsp.version.http.api.transformer; -import jakarta.json.Json; +import jakarta.json.JsonBuilderFactory; import jakarta.json.JsonObject; import org.eclipse.edc.connector.controlplane.services.spi.protocol.ProtocolVersions; -import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; import org.eclipse.edc.transform.spi.TransformerContext; +import org.eclipse.edc.transform.spi.TypeTransformer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -30,21 +30,33 @@ /** * Transform {@link ProtocolVersions} into {@link JsonObject} */ -public class JsonObjectFromProtocolVersionsTransformer extends AbstractJsonLdTransformer { +public class JsonObjectFromProtocolVersionsTransformer implements TypeTransformer { - public JsonObjectFromProtocolVersionsTransformer() { - super(ProtocolVersions.class, JsonObject.class); + private final JsonBuilderFactory jsonFactory; + + public JsonObjectFromProtocolVersionsTransformer(JsonBuilderFactory jsonFactory) { + this.jsonFactory = jsonFactory; + } + + @Override + public Class getInputType() { + return ProtocolVersions.class; + } + + @Override + public Class getOutputType() { + return JsonObject.class; } @Override public @Nullable JsonObject transform(@NotNull ProtocolVersions protocolVersions, @NotNull TransformerContext context) { var versions = protocolVersions.protocolVersions().stream() - .map(version -> Json.createObjectBuilder() + .map(version -> jsonFactory.createObjectBuilder() .add(DSPACE_PROPERTY_VERSION, version.version()) .add(DSPACE_PROPERTY_PATH, version.path()) .build()) .collect(toJsonArray()); - return Json.createObjectBuilder().add(DSPACE_PROPERTY_PROTOCOL_VERSIONS, versions).build(); + return jsonFactory.createObjectBuilder().add(DSPACE_PROPERTY_PROTOCOL_VERSIONS, versions).build(); } } diff --git a/data-protocols/dsp/dsp-version/dsp-version-http-api/src/main/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromVersionsError.java b/data-protocols/dsp/dsp-version/dsp-version-http-api/src/main/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromVersionsError.java index f275aca3390..0cac55445d2 100644 --- a/data-protocols/dsp/dsp-version/dsp-version-http-api/src/main/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromVersionsError.java +++ b/data-protocols/dsp/dsp-version/dsp-version-http-api/src/main/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromVersionsError.java @@ -17,34 +17,40 @@ import jakarta.json.JsonBuilderFactory; import jakarta.json.JsonObject; import org.eclipse.edc.connector.controlplane.services.spi.protocol.VersionsError; -import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; import org.eclipse.edc.transform.spi.TransformerContext; +import org.eclipse.edc.transform.spi.TypeTransformer; 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.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE_IRI; -import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON_IRI; -import static org.eclipse.edc.protocol.dsp.spi.type.DspVersionPropertyAndTypeNames.DSPACE_TYPE_VERSIONS_ERROR; +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 VersionsError} to a {@link JsonObject} in JSON-LD expanded form. */ -public class JsonObjectFromVersionsError extends AbstractJsonLdTransformer { +public class JsonObjectFromVersionsError implements TypeTransformer { private final JsonBuilderFactory jsonFactory; public JsonObjectFromVersionsError(JsonBuilderFactory jsonFactory) { - super(VersionsError.class, JsonObject.class); this.jsonFactory = jsonFactory; } + @Override + public Class getInputType() { + return VersionsError.class; + } + + @Override + public Class getOutputType() { + return JsonObject.class; + } + @Override public @Nullable JsonObject transform(@NotNull VersionsError error, @NotNull TransformerContext context) { return jsonFactory.createObjectBuilder() - .add(TYPE, DSPACE_TYPE_VERSIONS_ERROR) - .add(DSPACE_PROPERTY_CODE_IRI, error.getCode()) - .add(DSPACE_PROPERTY_REASON_IRI, jsonFactory.createArrayBuilder(error.getMessages())) + .add(DSPACE_PROPERTY_CODE_TERM, error.getCode()) + .add(DSPACE_PROPERTY_REASON_TERM, jsonFactory.createArrayBuilder(error.getMessages())) .build(); } } diff --git a/data-protocols/dsp/dsp-version/dsp-version-http-api/src/test/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromProtocolVersionsTransformerTest.java b/data-protocols/dsp/dsp-version/dsp-version-http-api/src/test/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromProtocolVersionsTransformerTest.java index 8cf7ede0f89..e91b3eb038a 100644 --- a/data-protocols/dsp/dsp-version/dsp-version-http-api/src/test/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromProtocolVersionsTransformerTest.java +++ b/data-protocols/dsp/dsp-version/dsp-version-http-api/src/test/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromProtocolVersionsTransformerTest.java @@ -14,6 +14,7 @@ package org.eclipse.edc.protocol.dsp.version.http.api.transformer; +import jakarta.json.Json; import jakarta.json.JsonValue; import org.eclipse.edc.connector.controlplane.services.spi.protocol.ProtocolVersion; import org.eclipse.edc.connector.controlplane.services.spi.protocol.ProtocolVersions; @@ -21,6 +22,7 @@ import org.junit.jupiter.api.Test; import java.util.List; +import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.edc.protocol.dsp.spi.type.DspVersionPropertyAndTypeNames.DSPACE_PROPERTY_PATH; @@ -32,7 +34,7 @@ class JsonObjectFromProtocolVersionsTransformerTest { private final TransformerContext context = mock(); private final JsonObjectFromProtocolVersionsTransformer transformer = - new JsonObjectFromProtocolVersionsTransformer(); + new JsonObjectFromProtocolVersionsTransformer(Json.createBuilderFactory(Map.of())); @Test void shouldTransform() { diff --git a/data-protocols/dsp/dsp-version/dsp-version-http-api/src/test/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromVersionsErrorTransformerTest.java b/data-protocols/dsp/dsp-version/dsp-version-http-api/src/test/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromVersionsErrorTransformerTest.java index dbfecfa4e04..fd03e96e374 100644 --- a/data-protocols/dsp/dsp-version/dsp-version-http-api/src/test/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromVersionsErrorTransformerTest.java +++ b/data-protocols/dsp/dsp-version/dsp-version-http-api/src/test/java/org/eclipse/edc/protocol/dsp/version/http/api/transformer/JsonObjectFromVersionsErrorTransformerTest.java @@ -25,10 +25,8 @@ import java.util.Map; 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.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE_IRI; -import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON_IRI; -import static org.eclipse.edc.protocol.dsp.spi.type.DspVersionPropertyAndTypeNames.DSPACE_TYPE_VERSIONS_ERROR; +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; import static org.mockito.Mockito.mock; class JsonObjectFromVersionsErrorTransformerTest { @@ -54,8 +52,7 @@ void transform_returnJsonObject() { var result = transformer.transform(error, context); assertThat(result).isNotNull(); - assertThat(result.getJsonString(TYPE).getString()).isEqualTo(DSPACE_TYPE_VERSIONS_ERROR); - assertThat(result.getString(DSPACE_PROPERTY_CODE_IRI)).isEqualTo("code"); - assertThat(result.getJsonArray(DSPACE_PROPERTY_REASON_IRI)).contains(Json.createValue("message")); + assertThat(result.getString(DSPACE_PROPERTY_CODE_TERM)).isEqualTo("code"); + assertThat(result.getJsonArray(DSPACE_PROPERTY_REASON_TERM)).contains(Json.createValue("message")); } } diff --git a/system-tests/protocol-test/src/test/java/org/eclipse/edc/test/e2e/protocol/DspVersionApiEndToEndTest.java b/system-tests/protocol-test/src/test/java/org/eclipse/edc/test/e2e/protocol/DspVersionApiEndToEndTest.java index d186b444d92..93c9a3ba970 100644 --- a/system-tests/protocol-test/src/test/java/org/eclipse/edc/test/e2e/protocol/DspVersionApiEndToEndTest.java +++ b/system-tests/protocol-test/src/test/java/org/eclipse/edc/test/e2e/protocol/DspVersionApiEndToEndTest.java @@ -19,13 +19,10 @@ import jakarta.json.JsonValue; import org.eclipse.edc.connector.controlplane.services.spi.protocol.ProtocolVersion; import org.eclipse.edc.connector.controlplane.services.spi.protocol.ProtocolVersionRegistry; -import org.eclipse.edc.jsonld.TitaniumJsonLd; -import org.eclipse.edc.jsonld.spi.JsonLd; import org.eclipse.edc.junit.annotations.EndToEndTest; import org.eclipse.edc.junit.extensions.EmbeddedRuntime; import org.eclipse.edc.junit.extensions.RuntimeExtension; import org.eclipse.edc.junit.extensions.RuntimePerClassExtension; -import org.eclipse.edc.spi.monitor.ConsoleMonitor; import org.eclipse.edc.util.io.Ports; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -34,13 +31,11 @@ import static io.restassured.RestAssured.given; import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VALUE; -import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA; -import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; import static org.eclipse.edc.protocol.dsp.spi.type.DspVersionPropertyAndTypeNames.DSPACE_PROPERTY_PATH; import static org.eclipse.edc.protocol.dsp.spi.type.DspVersionPropertyAndTypeNames.DSPACE_PROPERTY_PROTOCOL_VERSIONS; import static org.eclipse.edc.protocol.dsp.spi.type.DspVersionPropertyAndTypeNames.DSPACE_PROPERTY_VERSION; import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.nullValue; @EndToEndTest public class DspVersionApiEndToEndTest { @@ -61,14 +56,13 @@ public class DspVersionApiEndToEndTest { ":core:control-plane:control-plane-core", ":extensions:common:http" )); - private final JsonLd jsonLd = new TitaniumJsonLd(new ConsoleMonitor()); @Test - void shouldReturnValidJsonLd() { + void shouldReturnValidJson() { runtime.getService(ProtocolVersionRegistry.class) .register(new ProtocolVersion("1.0", "/v1/path")); - var compacted = given() + var response = given() .port(PROTOCOL_PORT) .basePath("/protocol") .header("Authorization", "{\"region\": \"any\", \"audience\": \"any\", \"clientId\":\"any\"}") @@ -77,14 +71,15 @@ void shouldReturnValidJsonLd() { .log().ifError() .statusCode(200) .contentType(ContentType.JSON) + .body("'@context'", nullValue()) .extract().body().as(JsonObject.class); - var expansion = jsonLd.expand(compacted); - assertThat(expansion).isSucceeded().satisfies(expanded -> { - assertThat(expanded.getJsonArray(DSPACE_PROPERTY_PROTOCOL_VERSIONS)).hasSize(1).extracting(JsonValue::asJsonObject) - .first().satisfies(protocolVersion -> versionIs(protocolVersion, "1.0", "/v1/path")); - }); + assertThat(response.getJsonArray(DSPACE_PROPERTY_PROTOCOL_VERSIONS)) + .hasSize(1) + .extracting(JsonValue::asJsonObject) + .first().satisfies(protocolVersion -> versionIs(protocolVersion, "1.0", "/v1/path")); + } @Test @@ -103,16 +98,13 @@ void shouldReturnError_whenNotAuthorized() { .log().ifError() .statusCode(401) .contentType(ContentType.JSON) - .body("'@type'", equalTo("dspace:VersionsError")) - .body("'dspace:code'", equalTo("401")) - .body("'dspace:reason'", equalTo("Unauthorized")) - .body("'@context'.dspace", equalTo(DSPACE_SCHEMA)); + .body("code", equalTo("401")) + .body("reason[0]", equalTo("Unauthorized")) + .body("'@context'", nullValue()); } private void versionIs(JsonObject protocolVersion, String version, String path) { - assertThat(protocolVersion.getJsonArray(DSPACE_PROPERTY_VERSION)).hasSize(1).first() - .extracting(JsonValue::asJsonObject).extracting(it -> it.getString(VALUE)).isEqualTo(version); - assertThat(protocolVersion.getJsonArray(DSPACE_PROPERTY_PATH)).hasSize(1).first() - .extracting(JsonValue::asJsonObject).extracting(it -> it.getString(VALUE)).isEqualTo(path); + assertThat(protocolVersion.getString(DSPACE_PROPERTY_VERSION)).isEqualTo(version); + assertThat(protocolVersion.getString(DSPACE_PROPERTY_PATH)).isEqualTo(path); } }