-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): expose dataplanes api v4alpha
- Loading branch information
Showing
21 changed files
with
693 additions
and
67 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
85 changes: 85 additions & 0 deletions
85
...ipse/edc/transform/transformer/edc/from/JsonObjectFromDataPlaneInstanceV3Transformer.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,85 @@ | ||
/* | ||
* Copyright (c) 2025 Cofinity-X | ||
* | ||
* 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: | ||
* Cofinity-X - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.transform.transformer.edc.from; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.json.JsonBuilderFactory; | ||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance; | ||
import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstanceStates; | ||
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 java.util.Optional; | ||
|
||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_DEST_TYPES; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_SOURCE_TYPES; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_TRANSFER_TYPES; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.DATAPLANE_INSTANCE_STATE; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.DATAPLANE_INSTANCE_STATE_TIMESTAMP; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.LAST_ACTIVE; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.PROPERTIES; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.TURN_COUNT; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.URL; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; | ||
|
||
public class JsonObjectFromDataPlaneInstanceV3Transformer extends AbstractJsonLdTransformer<DataPlaneInstance, JsonObject> { | ||
private final JsonBuilderFactory jsonFactory; | ||
private final ObjectMapper mapper; | ||
|
||
public JsonObjectFromDataPlaneInstanceV3Transformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper) { | ||
super(DataPlaneInstance.class, JsonObject.class); | ||
this.jsonFactory = jsonFactory; | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
public @Nullable JsonObject transform(@NotNull DataPlaneInstance dataPlaneInstance, @NotNull TransformerContext context) { | ||
var builder = jsonFactory.createObjectBuilder() | ||
.add(ID, dataPlaneInstance.getId()) | ||
.add(TYPE, DataPlaneInstance.DATAPLANE_INSTANCE_TYPE) | ||
.add(URL, dataPlaneInstance.getUrl().toString()) | ||
.add(LAST_ACTIVE, dataPlaneInstance.getLastActive()) | ||
.add(TURN_COUNT, dataPlaneInstance.getTurnCount()); | ||
Check notice Code scanning / CodeQL Deprecated method or constructor invocation Note
Invoking
DataPlaneInstance.getTurnCount Error loading related location Loading |
||
|
||
if (dataPlaneInstance.getProperties() != null && !dataPlaneInstance.getProperties().isEmpty()) { | ||
var propBuilder = jsonFactory.createObjectBuilder(); | ||
transformProperties(dataPlaneInstance.getProperties(), propBuilder, mapper, context); | ||
builder.add(PROPERTIES, propBuilder); | ||
} | ||
|
||
var srcBldr = jsonFactory.createArrayBuilder(dataPlaneInstance.getAllowedSourceTypes()); | ||
builder.add(ALLOWED_SOURCE_TYPES, srcBldr); | ||
|
||
var dstBldr = jsonFactory.createArrayBuilder(dataPlaneInstance.getAllowedDestTypes()); | ||
Check notice Code scanning / CodeQL Deprecated method or constructor invocation Note
Invoking
DataPlaneInstance.getAllowedDestTypes Error loading related location Loading |
||
builder.add(ALLOWED_DEST_TYPES, dstBldr); | ||
|
||
var transferTypesBldr = jsonFactory.createArrayBuilder(dataPlaneInstance.getAllowedTransferTypes()); | ||
builder.add(ALLOWED_TRANSFER_TYPES, transferTypesBldr); | ||
|
||
var state = Optional.ofNullable(DataPlaneInstanceStates.from(dataPlaneInstance.getState())) | ||
.map(Enum::name) | ||
.orElse(null); | ||
|
||
addIfNotNull(state, DATAPLANE_INSTANCE_STATE, builder); | ||
|
||
builder.add(DATAPLANE_INSTANCE_STATE_TIMESTAMP, dataPlaneInstance.getStateTimestamp()); | ||
|
||
return builder.build(); | ||
} | ||
} |
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
107 changes: 107 additions & 0 deletions
107
.../edc/transform/transformer/edc/from/JsonObjectFromDataPlaneInstanceV3TransformerTest.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,107 @@ | ||
/* | ||
* Copyright (c) 2025 Cofinity-X | ||
* | ||
* 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: | ||
* Cofinity-X - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.transform.transformer.edc.from; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonString; | ||
import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance; | ||
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.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_DEST_TYPES; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_SOURCE_TYPES; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_TRANSFER_TYPES; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.DATAPLANE_INSTANCE_STATE; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.DATAPLANE_INSTANCE_STATE_TIMESTAMP; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.LAST_ACTIVE; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.PROPERTIES; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.TURN_COUNT; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.URL; | ||
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstanceStates.AVAILABLE; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID; | ||
import static org.eclipse.edc.jsonld.util.JacksonJsonLd.createObjectMapper; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class JsonObjectFromDataPlaneInstanceV3TransformerTest { | ||
|
||
private final TransformerContext context = mock(); | ||
private JsonObjectFromDataPlaneInstanceV3Transformer transformer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
transformer = new JsonObjectFromDataPlaneInstanceV3Transformer(Json.createBuilderFactory(Map.of()), createObjectMapper()); | ||
} | ||
|
||
@Test | ||
void transform() { | ||
var dpi = DataPlaneInstance.Builder.newInstance() | ||
.id("test-id") | ||
.url("http://foo.bar") | ||
.allowedSourceType("test-source-type") | ||
.allowedDestType("test-dest-type") | ||
.allowedTransferType("test-transfer-type") | ||
.lastActive(15) | ||
.turnCount(42) | ||
.property("foo", "bar") | ||
.build(); | ||
|
||
var jsonObject = transformer.transform(dpi, context); | ||
|
||
assertThat(jsonObject).isNotNull(); | ||
assertThat(jsonObject.getString(ID)).isEqualTo("test-id"); | ||
assertThat(jsonObject.getString(URL)).isEqualTo("http://foo.bar"); | ||
assertThat(jsonObject.getJsonArray(ALLOWED_SOURCE_TYPES)).hasSize(1).allMatch(v -> ((JsonString) v).getString().equals("test-source-type")); | ||
assertThat(jsonObject.getJsonArray(ALLOWED_DEST_TYPES)).hasSize(1).allMatch(v -> ((JsonString) v).getString().equals("test-dest-type")); | ||
assertThat(jsonObject.getJsonArray(ALLOWED_TRANSFER_TYPES)).hasSize(1).allMatch(v -> ((JsonString) v).getString().equals("test-transfer-type")); | ||
assertThat(jsonObject.getJsonNumber(LAST_ACTIVE).intValue()).isEqualTo(15); | ||
assertThat(jsonObject.getJsonNumber(TURN_COUNT).intValue()).isEqualTo(42); | ||
assertThat(jsonObject.getJsonObject(PROPERTIES).getJsonString("foo").getString()).isEqualTo("bar"); | ||
|
||
} | ||
|
||
@Test | ||
void transform_withState() { | ||
var dpi = DataPlaneInstance.Builder.newInstance() | ||
.id("test-id") | ||
.url("http://foo.bar") | ||
.allowedSourceType("test-source-type") | ||
.allowedDestType("test-dest-type") | ||
.allowedTransferType("test-transfer-type") | ||
.lastActive(15) | ||
.turnCount(42) | ||
.state(AVAILABLE.code()) | ||
.property("foo", "bar") | ||
.build(); | ||
|
||
var jsonObject = transformer.transform(dpi, context); | ||
|
||
assertThat(jsonObject).isNotNull(); | ||
assertThat(jsonObject.getString(ID)).isEqualTo("test-id"); | ||
assertThat(jsonObject.getString(URL)).isEqualTo("http://foo.bar"); | ||
assertThat(jsonObject.getJsonArray(ALLOWED_SOURCE_TYPES)).hasSize(1).allMatch(v -> ((JsonString) v).getString().equals("test-source-type")); | ||
assertThat(jsonObject.getJsonArray(ALLOWED_DEST_TYPES)).hasSize(1).allMatch(v -> ((JsonString) v).getString().equals("test-dest-type")); | ||
assertThat(jsonObject.getJsonArray(ALLOWED_TRANSFER_TYPES)).hasSize(1).allMatch(v -> ((JsonString) v).getString().equals("test-transfer-type")); | ||
assertThat(jsonObject.getJsonNumber(LAST_ACTIVE).intValue()).isEqualTo(15); | ||
assertThat(jsonObject.getJsonNumber(TURN_COUNT).intValue()).isEqualTo(42); | ||
assertThat(jsonObject.getString(DATAPLANE_INSTANCE_STATE)).isEqualTo(AVAILABLE.name()); | ||
assertThat(jsonObject.getJsonNumber(DATAPLANE_INSTANCE_STATE_TIMESTAMP).longValue()).isEqualTo(dpi.getStateTimestamp()); | ||
assertThat(jsonObject.getJsonObject(PROPERTIES).getJsonString("foo").getString()).isEqualTo("bar"); | ||
|
||
} | ||
} |
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
Oops, something went wrong.