Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduces dymanic feature binding in WebService #4559

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: applies versioned JSON-LD scope to JsonLdRemoteMessageSerialize…
…rImpl
wolf4ood committed Oct 18, 2024

Verified

This commit was signed with the committer’s verified signature.
wolf4ood Enrico Risa
commit a4522563743e41d6668cfe91164d3771ba77854c
Original file line number Diff line number Diff line change
@@ -148,7 +148,7 @@ public DspRequestHandler dspRequestHandler() {

@Provider
public JsonLdRemoteMessageSerializer jsonLdRemoteMessageSerializer() {
return new JsonLdRemoteMessageSerializerImpl(dspTransformerRegistry(), typeManager.getMapper(JSON_LD), jsonLdService, DSP_SCOPE);
return new JsonLdRemoteMessageSerializerImpl(dspTransformerRegistry(), typeManager.getMapper(JSON_LD), jsonLdService, dspProtocolParser(), DSP_SCOPE);
}

@Provider
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.JsonObject;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.protocol.dsp.http.spi.DspProtocolParser;
import org.eclipse.edc.protocol.dsp.http.spi.serialization.JsonLdRemoteMessageSerializer;
import org.eclipse.edc.protocol.dsp.spi.transform.DspProtocolTypeTransformerRegistry;
import org.eclipse.edc.spi.EdcException;
@@ -26,6 +27,7 @@

import static java.lang.String.format;
import static java.lang.String.join;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_CONTEXT_SEPARATOR;

/**
* Serializes {@link RemoteMessage}s to JSON-LD.
@@ -34,15 +36,17 @@ public class JsonLdRemoteMessageSerializerImpl implements JsonLdRemoteMessageSer

private final ObjectMapper mapper;
private final JsonLd jsonLdService;
private final String scope;
private final String scopePrefix;
private final DspProtocolTypeTransformerRegistry dspTransformerRegistry;
private final DspProtocolParser protocolParser;

public JsonLdRemoteMessageSerializerImpl(DspProtocolTypeTransformerRegistry dspTransformerRegistry,
ObjectMapper mapper, JsonLd jsonLdService, String scope) {
ObjectMapper mapper, JsonLd jsonLdService, DspProtocolParser protocolParser, String scopePrefix) {
this.dspTransformerRegistry = dspTransformerRegistry;
this.mapper = mapper;
this.jsonLdService = jsonLdService;
this.scope = scope;
this.scopePrefix = scopePrefix;
this.protocolParser = protocolParser;
}

/**
@@ -66,7 +70,8 @@ public String serialize(RemoteMessage message) {
var transformResult = transformerRegistry.transform(message, JsonObject.class);

if (transformResult.succeeded()) {
var compacted = jsonLdService.compact(transformResult.getContent(), scope);
var compacted = protocolParser.parse(message.getProtocol())
.compose(protocol -> jsonLdService.compact(transformResult.getContent(), scopePrefix + DSP_CONTEXT_SEPARATOR + protocol.version()));
if (compacted.succeeded()) {
return mapper.writeValueAsString(compacted.getContent());
}
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import jakarta.json.Json;
import jakarta.json.JsonObject;
import org.eclipse.edc.jsonld.TitaniumJsonLd;
import org.eclipse.edc.protocol.dsp.http.spi.DspProtocolParser;
import org.eclipse.edc.protocol.dsp.spi.transform.DspProtocolTypeTransformerRegistry;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.monitor.Monitor;
@@ -32,6 +33,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.eclipse.edc.protocol.dsp.http.spi.types.HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_TRANSFORMER_CONTEXT_V_08;
import static org.eclipse.edc.protocol.dsp.spi.version.DspVersions.V_08;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
@@ -43,6 +45,7 @@ class JsonLdRemoteMessageSerializerImplTest {

private final TypeTransformerRegistry registry = mock(TypeTransformerRegistry.class);
private final DspProtocolTypeTransformerRegistry dspTransformerRegistry = mock();
private final DspProtocolParser protocolParser = mock();

private final ObjectMapper mapper = mock(ObjectMapper.class);
private final RemoteMessage message = mock(RemoteMessage.class);
@@ -53,7 +56,7 @@ void setUp() {
var jsonLdService = new TitaniumJsonLd(mock(Monitor.class));
jsonLdService.registerNamespace("schema", "http://schema/"); //needed for compaction
when(registry.forContext(DSP_TRANSFORMER_CONTEXT_V_08)).thenReturn(registry);
serializer = new JsonLdRemoteMessageSerializerImpl(dspTransformerRegistry, mapper, jsonLdService, "scope");
serializer = new JsonLdRemoteMessageSerializerImpl(dspTransformerRegistry, mapper, jsonLdService, protocolParser, "scope");
when(message.getProtocol()).thenReturn(DATASPACE_PROTOCOL_HTTP);
}

@@ -65,6 +68,7 @@ void serialize_shouldReturnString_whenValidMessage() throws JsonProcessingExcept
when(dspTransformerRegistry.forProtocol(DATASPACE_PROTOCOL_HTTP)).thenReturn(Result.success(registry));
when(registry.transform(message, JsonObject.class))
.thenReturn(Result.success(json));
when(protocolParser.parse(DATASPACE_PROTOCOL_HTTP)).thenReturn(Result.success(V_08));
when(mapper.writeValueAsString(any(JsonObject.class))).thenReturn(serialized);

var result = serializer.serialize(message);
@@ -91,6 +95,7 @@ void serialize_shouldThrowException_whenSerializationFails() throws JsonProcessi
var json = messageJson();

when(dspTransformerRegistry.forProtocol(DATASPACE_PROTOCOL_HTTP)).thenReturn(Result.success(registry));
when(protocolParser.parse(DATASPACE_PROTOCOL_HTTP)).thenReturn(Result.success(V_08));
when(registry.transform(message, JsonObject.class))
.thenReturn(Result.success(json));
when(mapper.writeValueAsString(any(JsonObject.class))).thenThrow(JsonProcessingException.class);