Skip to content

Commit

Permalink
feat: add dataAddress, code and reason fields to Transfer-Process-Tra…
Browse files Browse the repository at this point in the history
…nsformer (#2812)

* chore(dsp-transfer-process-transform): add code and reason field to transformer

* chore(dsp-transfer-process-transform): add data Address to TransferTermination[to|from]Transformer

* chore(dsp-transfer-process-transform): fix unit Test Bug

* chore(dsp-transfer-process-transform): remove duplicated JsonObjectToDataAddressTransformer.java

* chore(dsp-transfer-process-transform): refactor if clauses

* chore(dsp-transfer-process-transform): refactor tests

* chore(dsp-transfer-process-transform): refactor tests
  • Loading branch information
janpmeyer authored May 8, 2023
1 parent af5b9d4 commit 7185d0c
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ public interface DspTransferProcessPropertyAndTypeNames {
String DSPACE_CORRELATIONID_TYPE = DSPACE_SCHEMA + "correlationId";

String DSPACE_STATE_TYPE = DSPACE_SCHEMA + "state";

String DSPACE_REASON_TYPE = DSPACE_SCHEMA + "reason";

String DSPACE_CODE_TYPE = DSPACE_SCHEMA + "code";
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_DATAADDRESS_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_PROCESSID_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_TRANSFER_START_TYPE;

Expand All @@ -45,8 +46,9 @@ public JsonObjectFromTransferStartMessageTransformer(JsonBuilderFactory jsonBuil
builder.add(ID, UUID.randomUUID().toString());
builder.add(TYPE, DSPACE_TRANSFER_START_TYPE);
builder.add(DSPACE_PROCESSID_TYPE, transferStartMessage.getProcessId());

//TODO ADD missing fields dataAddress from spec issue https://github.com/eclipse-edc/Connector/issues/2727
if (transferStartMessage.getDataAddress() != null) {
builder.add(DSPACE_DATAADDRESS_TYPE, context.transform(transferStartMessage, JsonObject.class));
}

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_CODE_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_PROCESSID_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_REASON_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_TRANSFER_TERMINATION_TYPE;

public class JsonObjectFromTransferTerminationMessageTransformer extends AbstractJsonLdTransformer<TransferTerminationMessage, JsonObject> {
Expand All @@ -45,8 +47,12 @@ public JsonObjectFromTransferTerminationMessageTransformer(JsonBuilderFactory js
builder.add(ID, String.valueOf(UUID.randomUUID()));
builder.add(TYPE, DSPACE_TRANSFER_TERMINATION_TYPE);
builder.add(DSPACE_PROCESSID_TYPE, transferTerminationMessage.getProcessId());

//TODO Add field when Message is evolved (code, reason) issue https://github.com/eclipse-edc/Connector/issues/2764
if (transferTerminationMessage.getCode() != null) {
builder.add(DSPACE_CODE_TYPE, transferTerminationMessage.getCode());
}
if (transferTerminationMessage.getReason() != null) {
builder.add(DSPACE_REASON_TYPE, transferTerminationMessage.getReason());
}

return builder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import org.eclipse.edc.connector.transfer.spi.types.protocol.TransferStartMessage;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.protocol.dsp.spi.types.HttpMessageProtocol;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_DATAADDRESS_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_PROCESSID_TYPE;

public class JsonObjectToTransferStartMessageTransformer extends AbstractJsonLdTransformer<JsonObject, TransferStartMessage> {
Expand All @@ -37,7 +39,10 @@ public JsonObjectToTransferStartMessageTransformer() {
transferStartMessageBuilder.protocol(HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP);

transformString(jsonObject.get(DSPACE_PROCESSID_TYPE), transferStartMessageBuilder::processId, context);
//TODO ADD missing fields dataAddress from spec issue https://github.com/eclipse-edc/Connector/issues/2727

if (jsonObject.containsKey(DSPACE_DATAADDRESS_TYPE) && !jsonObject.get(DSPACE_DATAADDRESS_TYPE).asJsonObject().isEmpty()) {
transferStartMessageBuilder.dataAddress(context.transform(jsonObject.get(DSPACE_DATAADDRESS_TYPE), DataAddress.class));
}

return transferStartMessageBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static java.lang.String.format;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_CODE_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_PROCESSID_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_REASON_TYPE;

public class JsonObjectToTransferTerminationMessageTransformer extends AbstractJsonLdTransformer<JsonObject, TransferTerminationMessage> {

Expand All @@ -37,7 +40,20 @@ public JsonObjectToTransferTerminationMessageTransformer() {
transferTerminationMessageBuilder.protocol(HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP);

transformString(jsonObject.get(DSPACE_PROCESSID_TYPE), transferTerminationMessageBuilder::processId, context);
//TODO ADD missing fields (code, reason) from spec issue https://github.com/eclipse-edc/Connector/issues/2764

if (jsonObject.containsKey(DSPACE_CODE_TYPE)) {
transformString(jsonObject.get(DSPACE_CODE_TYPE), transferTerminationMessageBuilder::code, context);
}

var reasons = jsonObject.get(DSPACE_REASON_TYPE);
if (reasons != null) {
var result = typeValueArray(reasons, context);
if (result == null) {
context.reportProblem(format("Cannot transform property %s in ContractNegotiationTerminationMessage", DSPACE_REASON_TYPE));
} else if (result.size() > 0) {
transferTerminationMessageBuilder.reason(String.valueOf(result.get(0)));
}
}

return transferTerminationMessageBuilder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_CODE_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_PROCESSID_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_REASON_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_TRANSFER_TERMINATION_TYPE;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
Expand All @@ -51,14 +53,17 @@ void transformTransferTerminationMessage() {
var message = TransferTerminationMessage.Builder.newInstance()
.processId("TestID")
.protocol("dsp")
.code("testCode")
.reason("testReason")
.build();

var result = transformer.transform(message, context);

assertThat(result).isNotNull();
assertThat(result.getJsonString(JsonLdKeywords.TYPE).getString()).isEqualTo(DSPACE_TRANSFER_TERMINATION_TYPE);
assertThat(result.getJsonString(DSPACE_PROCESSID_TYPE).getString()).isEqualTo("TestID");
//TODO Add missing fields (code, reason) from Spec issue https://github.com/eclipse-edc/Connector/issues/2764
assertThat(result.getJsonString(DSPACE_CODE_TYPE).getString()).isEqualTo("testCode");
assertThat(result.getJsonString(DSPACE_REASON_TYPE).getString()).isEqualTo("testReason");

verify(context, never()).reportProblem(anyString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,28 @@
package org.eclipse.edc.protocol.dsp.transferprocess.transformer.to;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import org.eclipse.edc.protocol.dsp.spi.types.HttpMessageProtocol;
import org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.to.JsonObjectToTransferStartMessageTransformer;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.CONTEXT;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_DATAADDRESS_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_PROCESSID_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_SCHEMA;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_TRANSFER_START_TYPE;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class JsonObjectToTransferStartMessageTransformerTest {

Expand All @@ -47,12 +53,57 @@ void setUp() {

@Test
void jsonObjectToTransferStartMessage() {
//TODO Add missing DataAddress from Spec Issue https://github.com/eclipse-edc/Connector/issues/2727
var json = Json.createObjectBuilder()
.add(CONTEXT, DSPACE_SCHEMA)
.add(TYPE, DSPACE_TRANSFER_START_TYPE)
.add(DSPACE_PROCESSID_TYPE, processId)
.build();

var result = transformer.transform(json, context);

assertThat(result).isNotNull();

assertThat(result.getProcessId()).isEqualTo(processId);
assertThat(result.getProtocol()).isEqualTo(HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP);

verify(context, never()).reportProblem(anyString());
}

@Test
void jsonObjectToTransferStartMessageWithDataAddress() {
var json = Json.createObjectBuilder()
.add(CONTEXT, DSPACE_SCHEMA)
.add(TYPE, DSPACE_TRANSFER_START_TYPE)
.add(DSPACE_PROCESSID_TYPE, processId)
.add(DSPACE_DATAADDRESS_TYPE, Json.createObjectBuilder()
.add("type", "AWS")
.build())
.build();

var dataAddress = DataAddress.Builder.newInstance()
.type("AWS")
.build();
when(context.transform(isA(JsonObject.class), eq(DataAddress.class))).thenReturn(dataAddress);

var result = transformer.transform(json, context);

assertThat(result).isNotNull();

assertThat(result.getProcessId()).isEqualTo(processId);
assertThat(result.getProtocol()).isEqualTo(HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP);
assertThat(result.getDataAddress()).isSameAs(dataAddress);

verify(context, never()).reportProblem(anyString());
}

@Test
void jsonObjectToTransferStartMessageWithEmptyDataAddress() {
var json = Json.createObjectBuilder()
.add(CONTEXT, DSPACE_SCHEMA)
.add(TYPE, DSPACE_TRANSFER_START_TYPE)
.add(DSPACE_PROCESSID_TYPE, processId)
.add(DSPACE_DATAADDRESS_TYPE, Json.createObjectBuilder()
.build())
.build();

var result = transformer.transform(json, context);
Expand All @@ -61,6 +112,7 @@ void jsonObjectToTransferStartMessage() {

assertThat(result.getProcessId()).isEqualTo(processId);
assertThat(result.getProtocol()).isEqualTo(HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP);
assertThat(result.getDataAddress()).isNull();

verify(context, never()).reportProblem(anyString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.CONTEXT;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_CODE_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_PROCESSID_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_REASON_TYPE;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_SCHEMA;
import static org.eclipse.edc.protocol.dsp.transferprocess.transformer.DspTransferProcessPropertyAndTypeNames.DSPACE_TRANSFER_TERMINATION_TYPE;
import static org.mockito.ArgumentMatchers.anyString;
Expand All @@ -36,6 +40,8 @@ class JsonObjectToTransferTerminationMessageTransformerTest {

private final String processId = "TestProcessId";

private final String code = "testCode";

private TransformerContext context = mock(TransformerContext.class);

private JsonObjectToTransferTerminationMessageTransformer transformer;
Expand All @@ -47,21 +53,24 @@ void setUp() {

@Test
void jsonObjectToTransferTerminationMessage() {

//TODO Add missing (code,reason) attributes from Spec issue https://github.com/eclipse-edc/Connector/issues/2764
var reason = Json.createBuilderFactory(Map.of()).createObjectBuilder().add("foo", "bar");

var json = Json.createObjectBuilder()
.add(CONTEXT, DSPACE_SCHEMA)
.add(TYPE, DSPACE_TRANSFER_TERMINATION_TYPE)
.add(DSPACE_PROCESSID_TYPE, processId)
.add(DSPACE_PROCESSID_TYPE, "TestProcessId")
.add(DSPACE_CODE_TYPE, "testCode")
.add(DSPACE_REASON_TYPE, Json.createBuilderFactory(Map.of()).createArrayBuilder().add(reason).build())
.build();

var result = transformer.transform(json, context);

assertThat(result).isNotNull();

assertThat(result.getProcessId()).isEqualTo(processId);
assertThat(result.getProcessId()).isEqualTo("TestProcessId");
assertThat(result.getProtocol()).isEqualTo(HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP);
assertThat(result.getReason()).isEqualTo("{\"foo\":\"bar\"}");
assertThat(result.getCode()).isEqualTo("testCode");

verify(context, never()).reportProblem(anyString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public Builder reason(String reason) {
public TransferTerminationMessage build() {
Objects.requireNonNull(message.protocol, "The protocol must be specified");
Objects.requireNonNull(message.processId, "The processId must be specified");
//TODO add Nullcheck for message.code Issue https://github.com/eclipse-edc/Connector/issues/2810
return message;
}
}
Expand Down

0 comments on commit 7185d0c

Please sign in to comment.