Skip to content

Commit

Permalink
feat: removes JSON-LD requirements in dsp version endpoint (#4655)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood authored Dec 3, 2024
1 parent 22dafd7 commit 0dc4cdb
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,21 +30,33 @@
/**
* Transform {@link ProtocolVersions} into {@link JsonObject}
*/
public class JsonObjectFromProtocolVersionsTransformer extends AbstractJsonLdTransformer<ProtocolVersions, JsonObject> {
public class JsonObjectFromProtocolVersionsTransformer implements TypeTransformer<ProtocolVersions, JsonObject> {

public JsonObjectFromProtocolVersionsTransformer() {
super(ProtocolVersions.class, JsonObject.class);
private final JsonBuilderFactory jsonFactory;

public JsonObjectFromProtocolVersionsTransformer(JsonBuilderFactory jsonFactory) {
this.jsonFactory = jsonFactory;
}

@Override
public Class<ProtocolVersions> getInputType() {
return ProtocolVersions.class;
}

@Override
public Class<JsonObject> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<VersionsError, JsonObject> {
public class JsonObjectFromVersionsError implements TypeTransformer<VersionsError, JsonObject> {

private final JsonBuilderFactory jsonFactory;

public JsonObjectFromVersionsError(JsonBuilderFactory jsonFactory) {
super(VersionsError.class, JsonObject.class);
this.jsonFactory = jsonFactory;
}

@Override
public Class<VersionsError> getInputType() {
return VersionsError.class;
}

@Override
public Class<JsonObject> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

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;
import org.eclipse.edc.transform.spi.TransformerContext;
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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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\"}")
Expand All @@ -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
Expand All @@ -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);
}
}

0 comments on commit 0dc4cdb

Please sign in to comment.