-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dsp): implement
TransferSuspensionMessage
transformers (#3987)
* refactor: move common transformers to transformer-core (#3982) * feat(dsp): implement transfer suspension message transformers
- Loading branch information
Showing
11 changed files
with
422 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...sferprocess/transformer/type/from/JsonObjectFromTransferSuspensionMessageTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.from; | ||
|
||
import jakarta.json.JsonBuilderFactory; | ||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.connector.transfer.spi.types.protocol.TransferSuspensionMessage; | ||
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
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.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE; | ||
import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CONSUMER_PID; | ||
import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_PROVIDER_PID; | ||
import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON; | ||
import static org.eclipse.edc.protocol.dsp.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE; | ||
|
||
public class JsonObjectFromTransferSuspensionMessageTransformer extends AbstractJsonLdTransformer<TransferSuspensionMessage, JsonObject> { | ||
|
||
private final JsonBuilderFactory jsonBuilderFactory; | ||
|
||
public JsonObjectFromTransferSuspensionMessageTransformer(JsonBuilderFactory jsonBuilderFactory) { | ||
super(TransferSuspensionMessage.class, JsonObject.class); | ||
this.jsonBuilderFactory = jsonBuilderFactory; | ||
} | ||
|
||
@Override | ||
public @Nullable JsonObject transform(@NotNull TransferSuspensionMessage message, @NotNull TransformerContext context) { | ||
var builder = jsonBuilderFactory.createObjectBuilder() | ||
.add(ID, message.getId()) | ||
.add(TYPE, DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE) | ||
.add(DSPACE_PROPERTY_CONSUMER_PID, message.getConsumerPid()) | ||
.add(DSPACE_PROPERTY_PROVIDER_PID, message.getProviderPid()) | ||
.add(DSPACE_PROPERTY_REASON, jsonBuilderFactory.createArrayBuilder(message.getReason())); | ||
|
||
addIfNotNull(message.getCode(), DSPACE_PROPERTY_CODE, builder); | ||
|
||
return builder.build(); | ||
|
||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
...transferprocess/transformer/type/to/JsonObjectToTransferSuspensionMessageTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.to; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.json.JsonArray; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonValue; | ||
import org.eclipse.edc.connector.transfer.spi.types.protocol.TransferSuspensionMessage; | ||
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; | ||
import org.eclipse.edc.spi.EdcException; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Map; | ||
|
||
import static jakarta.json.JsonValue.ValueType.ARRAY; | ||
import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE; | ||
import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CONSUMER_PID; | ||
import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_PROVIDER_PID; | ||
import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON; | ||
import static org.eclipse.edc.protocol.dsp.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE; | ||
import static org.eclipse.edc.protocol.dsp.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_TERMINATION_MESSAGE; | ||
|
||
public class JsonObjectToTransferSuspensionMessageTransformer extends AbstractJsonLdTransformer<JsonObject, TransferSuspensionMessage> { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public JsonObjectToTransferSuspensionMessageTransformer(ObjectMapper objectMapper) { | ||
super(JsonObject.class, TransferSuspensionMessage.class); | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public @Nullable TransferSuspensionMessage transform(@NotNull JsonObject messageObject, @NotNull TransformerContext context) { | ||
var builder = TransferSuspensionMessage.Builder.newInstance(); | ||
|
||
if (!transformMandatoryString(messageObject.get(DSPACE_PROPERTY_CONSUMER_PID), builder::consumerPid, context)) { | ||
context.problem() | ||
.missingProperty() | ||
.type(DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE) | ||
.property(DSPACE_PROPERTY_CONSUMER_PID) | ||
.report(); | ||
return null; | ||
} | ||
|
||
if (!transformMandatoryString(messageObject.get(DSPACE_PROPERTY_PROVIDER_PID), builder::providerPid, context)) { | ||
context.problem() | ||
.missingProperty() | ||
.type(DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE) | ||
.property(DSPACE_PROPERTY_PROVIDER_PID) | ||
.report(); | ||
return null; | ||
} | ||
|
||
if (!transformMandatoryString(messageObject.get(DSPACE_PROPERTY_CODE), builder::code, context)) { | ||
context.problem() | ||
.missingProperty() | ||
.type(DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE) | ||
.property(DSPACE_PROPERTY_CODE) | ||
.report(); | ||
return null; | ||
} | ||
|
||
var reasons = messageObject.get(DSPACE_PROPERTY_REASON); | ||
if (reasons != null) { | ||
if (reasons instanceof JsonArray array && !array.isEmpty()) { | ||
builder.reason(array.stream().map(this::deserialize).toList()); | ||
} else { | ||
context.problem() | ||
.unexpectedType() | ||
.type(DSPACE_TYPE_TRANSFER_TERMINATION_MESSAGE) | ||
.property(DSPACE_PROPERTY_REASON) | ||
.actual(reasons.getValueType()) | ||
.expected(ARRAY) | ||
.report(); | ||
} | ||
} | ||
|
||
return builder.build(); | ||
|
||
} | ||
|
||
private Object deserialize(JsonValue it) { | ||
try { | ||
return objectMapper.readValue(it.toString(), Map.class); | ||
} catch (JsonProcessingException e) { | ||
throw new EdcException("Error deserializing 'reason' field."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...nsferprocess/transformer/from/JsonObjectFromTransferSuspensionMessageTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.protocol.dsp.transferprocess.transformer.from; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonBuilderFactory; | ||
import jakarta.json.JsonString; | ||
import org.eclipse.edc.connector.transfer.spi.types.protocol.TransferSuspensionMessage; | ||
import org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.from.JsonObjectFromTransferSuspensionMessageTransformer; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
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.TYPE; | ||
import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE; | ||
import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CONSUMER_PID; | ||
import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_PROVIDER_PID; | ||
import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON; | ||
import static org.eclipse.edc.protocol.dsp.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
|
||
|
||
class JsonObjectFromTransferSuspensionMessageTransformerTest { | ||
|
||
private final JsonBuilderFactory jsonFactory = Json.createBuilderFactory(Map.of()); | ||
private final TransformerContext context = mock(); | ||
|
||
private final JsonObjectFromTransferSuspensionMessageTransformer transformer = | ||
new JsonObjectFromTransferSuspensionMessageTransformer(jsonFactory); | ||
|
||
@Test | ||
void transformTransferSuspensionMessage() { | ||
var message = TransferSuspensionMessage.Builder.newInstance() | ||
.consumerPid("consumerPid") | ||
.providerPid("providerPid") | ||
.protocol("dsp") | ||
.code("testCode") | ||
.reason("testReason") | ||
.build(); | ||
|
||
var result = transformer.transform(message, context); | ||
|
||
assertThat(result).isNotNull(); | ||
assertThat(result.getJsonString(TYPE).getString()).isEqualTo(DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE); | ||
assertThat(result.getJsonString(DSPACE_PROPERTY_CONSUMER_PID).getString()).isEqualTo("consumerPid"); | ||
assertThat(result.getJsonString(DSPACE_PROPERTY_PROVIDER_PID).getString()).isEqualTo("providerPid"); | ||
assertThat(result.getJsonString(DSPACE_PROPERTY_CODE).getString()).isEqualTo("testCode"); | ||
assertThat(result.getJsonArray(DSPACE_PROPERTY_REASON)).hasSize(1).first() | ||
.isInstanceOfSatisfying(JsonString.class, reason -> assertThat(reason.getString()).isEqualTo("testReason")); | ||
|
||
verify(context, never()).reportProblem(anyString()); | ||
} | ||
} |
Oops, something went wrong.