Skip to content

Commit

Permalink
feat: applies versioned JSON-LD scope to JsonLdRemoteMessageSerialize…
Browse files Browse the repository at this point in the history
…rImpl
  • Loading branch information
wolf4ood committed Oct 18, 2024
1 parent 4b8ae23 commit 0533195
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -36,13 +38,15 @@ public class JsonLdRemoteMessageSerializerImpl implements JsonLdRemoteMessageSer
private final JsonLd jsonLdService;
private final String scope;
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 prefix) {
this.dspTransformerRegistry = dspTransformerRegistry;
this.mapper = mapper;
this.jsonLdService = jsonLdService;
this.scope = scope;
this.scope = prefix;
this.protocolParser = protocolParser;
}

/**
Expand All @@ -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(), scope + DSP_CONTEXT_SEPARATOR + protocol.version()));
if (compacted.succeeded()) {
return mapper.writeValueAsString(compacted.getContent());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 0533195

Please sign in to comment.