-
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: add trasformers from/to for DataFlowResponseMessage (#3946)
- Loading branch information
Showing
5 changed files
with
266 additions
and
0 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
51 changes: 51 additions & 0 deletions
51
...nector/api/signaling/transform/from/JsonObjectFromDataFlowResponseMessageTransformer.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,51 @@ | ||
/* | ||
* 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.connector.api.signaling.transform.from; | ||
|
||
import jakarta.json.JsonBuilderFactory; | ||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; | ||
import org.eclipse.edc.spi.types.domain.transfer.DataFlowResponseMessage; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; | ||
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowResponseMessage.DATA_FLOW_RESPONSE_MESSAGE_DATA_ADDRESS; | ||
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowResponseMessage.DATA_FLOW_RESPONSE_MESSAGE_TYPE; | ||
|
||
/** | ||
* Converts from a {@link DataFlowResponseMessage} to a {@link JsonObject} in JSON-LD expanded form . | ||
*/ | ||
public class JsonObjectFromDataFlowResponseMessageTransformer extends AbstractJsonLdTransformer<DataFlowResponseMessage, JsonObject> { | ||
private final JsonBuilderFactory jsonFactory; | ||
|
||
public JsonObjectFromDataFlowResponseMessageTransformer(JsonBuilderFactory jsonFactory) { | ||
super(DataFlowResponseMessage.class, JsonObject.class); | ||
this.jsonFactory = jsonFactory; | ||
} | ||
|
||
@Override | ||
public @Nullable JsonObject transform(@NotNull DataFlowResponseMessage message, @NotNull TransformerContext context) { | ||
var builder = jsonFactory.createObjectBuilder() | ||
.add(TYPE, DATA_FLOW_RESPONSE_MESSAGE_TYPE); | ||
|
||
Optional.ofNullable(message.getDataAddress()) | ||
.ifPresent(dataAddress -> builder.add(DATA_FLOW_RESPONSE_MESSAGE_DATA_ADDRESS, context.transform(message.getDataAddress(), JsonObject.class))); | ||
return builder.build(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
.../connector/api/signaling/transform/to/JsonObjectToDataFlowResponseMessageTransformer.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,47 @@ | ||
/* | ||
* 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.connector.api.signaling.transform.to; | ||
|
||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; | ||
import org.eclipse.edc.spi.types.domain.DataAddress; | ||
import org.eclipse.edc.spi.types.domain.transfer.DataFlowResponseMessage; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowResponseMessage.DATA_FLOW_RESPONSE_MESSAGE_DATA_ADDRESS; | ||
|
||
/** | ||
* Converts from a {@link JsonObject} in JSON-LD expanded form to a {@link DataFlowResponseMessage}. | ||
*/ | ||
public class JsonObjectToDataFlowResponseMessageTransformer extends AbstractJsonLdTransformer<JsonObject, DataFlowResponseMessage> { | ||
|
||
public JsonObjectToDataFlowResponseMessageTransformer() { | ||
super(JsonObject.class, DataFlowResponseMessage.class); | ||
} | ||
|
||
@Override | ||
public @Nullable DataFlowResponseMessage transform(@NotNull JsonObject object, @NotNull TransformerContext context) { | ||
var builder = DataFlowResponseMessage.Builder.newInstance(); | ||
|
||
Optional.ofNullable(object.get(DATA_FLOW_RESPONSE_MESSAGE_DATA_ADDRESS)) | ||
.ifPresent(jsonValue -> builder.dataAddress(transformObject(jsonValue, DataAddress.class, context))); | ||
|
||
return builder.build(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...or/api/signaling/transform/from/JsonObjectFromDataFlowResponseMessageTransformerTest.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,76 @@ | ||
/* | ||
* 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.connector.api.signaling.transform.from; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.spi.types.domain.DataAddress; | ||
import org.eclipse.edc.spi.types.domain.transfer.DataFlowResponseMessage; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
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.TYPE; | ||
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowResponseMessage.DATA_FLOW_RESPONSE_MESSAGE_DATA_ADDRESS; | ||
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowResponseMessage.DATA_FLOW_RESPONSE_MESSAGE_TYPE; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class JsonObjectFromDataFlowResponseMessageTransformerTest { | ||
|
||
|
||
private final TransformerContext context = mock(TransformerContext.class); | ||
private JsonObjectFromDataFlowResponseMessageTransformer transformer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
transformer = new JsonObjectFromDataFlowResponseMessageTransformer(Json.createBuilderFactory(Map.of())); | ||
when(context.transform(any(DataAddress.class), eq(JsonObject.class))).thenReturn(Json.createObjectBuilder().build()); | ||
} | ||
|
||
@Test | ||
void transform() { | ||
|
||
var message = DataFlowResponseMessage.Builder.newInstance().dataAddress(DataAddress.Builder.newInstance().type("type").build()).build(); | ||
|
||
var jsonObject = transformer.transform(message, context); | ||
|
||
assertThat(jsonObject).isNotNull(); | ||
|
||
assertThat(jsonObject.getJsonString(TYPE).getString()).isEqualTo(DATA_FLOW_RESPONSE_MESSAGE_TYPE); | ||
assertThat(jsonObject.get(DATA_FLOW_RESPONSE_MESSAGE_DATA_ADDRESS)).isNotNull(); | ||
|
||
} | ||
|
||
@Test | ||
void transform_withoutDataAddress() { | ||
|
||
var message = DataFlowResponseMessage.Builder.newInstance().build(); | ||
|
||
var jsonObject = transformer.transform(message, context); | ||
|
||
assertThat(jsonObject).isNotNull(); | ||
|
||
assertThat(jsonObject.getJsonString(TYPE).getString()).isEqualTo(DATA_FLOW_RESPONSE_MESSAGE_TYPE); | ||
assertThat(jsonObject.containsKey(DATA_FLOW_RESPONSE_MESSAGE_DATA_ADDRESS)).isFalse(); | ||
|
||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
...nector/api/signaling/transform/to/JsonObjectToDataFlowResponseMessageTransformerTest.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,90 @@ | ||
/* | ||
* 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.connector.api.signaling.transform.to; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonBuilderFactory; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonObjectBuilder; | ||
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 java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.eclipse.edc.connector.api.signaling.transform.TestFunctions.getExpanded; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.CONTEXT; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VOCAB; | ||
import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE; | ||
import static org.eclipse.edc.spi.CoreConstants.EDC_PREFIX; | ||
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowResponseMessage.DATA_FLOW_RESPONSE_MESSAGE_SIMPLE_TYPE; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class JsonObjectToDataFlowResponseMessageTransformerTest { | ||
|
||
private final JsonBuilderFactory jsonFactory = Json.createBuilderFactory(Map.of()); | ||
private final TransformerContext context = mock(TransformerContext.class); | ||
private JsonObjectToDataFlowResponseMessageTransformer transformer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
transformer = new JsonObjectToDataFlowResponseMessageTransformer(); | ||
when(context.transform(any(JsonObject.class), eq(DataAddress.class))).thenReturn(DataAddress.Builder.newInstance().type("type").build()); | ||
} | ||
|
||
@Test | ||
void transform() { | ||
|
||
var jsonObj = jsonFactory.createObjectBuilder() | ||
.add(CONTEXT, createContextBuilder().build()) | ||
.add(TYPE, DATA_FLOW_RESPONSE_MESSAGE_SIMPLE_TYPE) | ||
.add("dataAddress", jsonFactory.createObjectBuilder().build()) | ||
.build(); | ||
|
||
var message = transformer.transform(getExpanded(jsonObj), context); | ||
|
||
assertThat(message).isNotNull(); | ||
|
||
assertThat(message.getDataAddress()).isNotNull(); | ||
} | ||
|
||
@Test | ||
void transform_withoutDataAddress() { | ||
|
||
var jsonObj = jsonFactory.createObjectBuilder() | ||
.add(CONTEXT, createContextBuilder().build()) | ||
.add(TYPE, DATA_FLOW_RESPONSE_MESSAGE_SIMPLE_TYPE) | ||
.build(); | ||
|
||
var message = transformer.transform(getExpanded(jsonObj), context); | ||
|
||
assertThat(message).isNotNull(); | ||
|
||
assertThat(message.getDataAddress()).isNull(); | ||
} | ||
|
||
private JsonObjectBuilder createContextBuilder() { | ||
return jsonFactory.createObjectBuilder() | ||
.add(VOCAB, EDC_NAMESPACE) | ||
.add(EDC_PREFIX, EDC_NAMESPACE); | ||
} | ||
|
||
} |