Skip to content

Commit

Permalink
fix: add missing transformers to data-plane-selector
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Aug 2, 2024
1 parent 8d259e9 commit 61e68b9
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* Base utility class that permits to test Rest controllers deploying a bare bone instance of Jetty
* with Jersey. The controller returned by the {@link #controller()} method gets registered on a test api context.
*/
public abstract class RestControllerTestBase { // TODO: can it be started once for class?
public abstract class RestControllerTestBase {

protected final int port = getFreePort();
protected final Monitor monitor = mock(Monitor.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ dependencies {
implementation(project(":extensions:common:json-ld"))

testImplementation(project(":core:common:junit"))
testImplementation(project(":extensions:common:api:api-core"))
testImplementation(project(":extensions:common:http"))
testImplementation(project(":extensions:common:http:lib:jersey-providers-lib"))
testImplementation(project(":extensions:data-plane-selector:data-plane-selector-api"))
testImplementation(project(":extensions:data-plane-selector:data-plane-selector-control-api"))

testImplementation(testFixtures(project(":core:common:lib:http-lib")))
testImplementation(testFixtures(project(":extensions:common:http:jersey-core")))
}


Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jakarta.json.Json;
import org.eclipse.edc.connector.dataplane.selector.spi.DataPlaneSelectorService;
import org.eclipse.edc.http.spi.ControlApiHttpClient;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
Expand All @@ -25,7 +26,11 @@
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.eclipse.edc.transform.transformer.edc.from.JsonObjectFromDataAddressTransformer;
import org.eclipse.edc.transform.transformer.edc.from.JsonObjectFromDataPlaneInstanceTransformer;
import org.eclipse.edc.transform.transformer.edc.to.JsonObjectToDataAddressTransformer;
import org.eclipse.edc.transform.transformer.edc.to.JsonObjectToDataPlaneInstanceTransformer;
import org.eclipse.edc.transform.transformer.edc.to.JsonValueToGenericTypeTransformer;

import static java.util.Collections.emptyMap;
import static org.eclipse.edc.spi.constants.CoreConstants.JSON_LD;
Expand All @@ -50,6 +55,9 @@ public class DataPlaneSelectorClientExtension implements ServiceExtension {
@Inject
private TypeTransformerRegistry typeTransformerRegistry;

@Inject
private JsonLd jsonLd;

@Override
public String name() {
return NAME;
Expand All @@ -60,14 +68,18 @@ public void initialize(ServiceExtensionContext context) {
var builderFactory = Json.createBuilderFactory(emptyMap());
var objectMapper = typeManager.getMapper(JSON_LD);
typeTransformerRegistry.register(new JsonObjectFromDataPlaneInstanceTransformer(builderFactory, objectMapper));
typeTransformerRegistry.register(new JsonObjectFromDataAddressTransformer(builderFactory));
typeTransformerRegistry.register(new JsonObjectToDataPlaneInstanceTransformer());
typeTransformerRegistry.register(new JsonObjectToDataAddressTransformer());
typeTransformerRegistry.register(new JsonValueToGenericTypeTransformer(objectMapper));
}

@Provider
public DataPlaneSelectorService dataPlaneSelectorService(ServiceExtensionContext context) {
var config = context.getConfig();
var url = config.getString(DPF_SELECTOR_URL_SETTING);
var selectionStrategy = config.getString(DPF_SELECTOR_STRATEGY, DataPlaneSelectorService.DEFAULT_STRATEGY);
return new RemoteDataPlaneSelectorService(httpClient, url, typeManager.getMapper(), typeTransformerRegistry,
selectionStrategy);
return new RemoteDataPlaneSelectorService(httpClient, url, typeManager.getMapper(JSON_LD), typeTransformerRegistry,
selectionStrategy, jsonLd);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.eclipse.edc.connector.dataplane.selector.spi.DataPlaneSelectorService;
import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance;
import org.eclipse.edc.http.spi.ControlApiHttpClient;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.result.Result;
import org.eclipse.edc.spi.result.ServiceResult;
Expand Down Expand Up @@ -52,14 +54,17 @@ public class RemoteDataPlaneSelectorService implements DataPlaneSelectorService
private final ObjectMapper mapper;
private final TypeTransformerRegistry typeTransformerRegistry;
private final String selectionStrategy;
private final JsonLd jsonLd;

public RemoteDataPlaneSelectorService(ControlApiHttpClient controlClient, String url, ObjectMapper mapper,
TypeTransformerRegistry typeTransformerRegistry, String selectionStrategy) {
TypeTransformerRegistry typeTransformerRegistry, String selectionStrategy,
JsonLd jsonLd) {
this.httpClient = controlClient;
this.url = url;
this.mapper = mapper;
this.typeTransformerRegistry = typeTransformerRegistry;
this.selectionStrategy = selectionStrategy;
this.jsonLd = jsonLd;
}

@Override
Expand All @@ -69,7 +74,8 @@ public ServiceResult<List<DataPlaneInstance>> getAll() {
return httpClient.request(requestBuilder)
.compose(this::toJsonArray)
.map(it -> it.stream()
.map(j -> typeTransformerRegistry.transform(j, DataPlaneInstance.class))
.map(JsonValue::asJsonObject)
.map(jo -> jsonLd.expand(jo).compose(joo -> typeTransformerRegistry.transform(joo, DataPlaneInstance.class)))
.filter(Result::succeeded)
.map(Result::getContent)
.toList()
Expand All @@ -92,6 +98,7 @@ public ServiceResult<DataPlaneInstance> select(DataAddress source, String transf
var requestBuilder = new Request.Builder().post(body).url(url + SELECT_PATH);

return httpClient.request(requestBuilder).compose(this::toJsonObject)
.compose(it -> jsonLd.expand(it).flatMap(ServiceResult::from))
.map(it -> typeTransformerRegistry.transform(it, DataPlaneInstance.class))
.compose(ServiceResult::from);
}
Expand Down Expand Up @@ -132,6 +139,7 @@ public ServiceResult<DataPlaneInstance> findById(String id) {
var requestBuilder = new Request.Builder().get().url(url + "/" + id);

return httpClient.request(requestBuilder).compose(this::toJsonObject)
.compose(it -> jsonLd.expand(it).flatMap(ServiceResult::from))
.map(it -> typeTransformerRegistry.transform(it, DataPlaneInstance.class).getContent());
}

Expand All @@ -151,5 +159,4 @@ private ServiceResult<JsonArray> toJsonArray(String it) {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,79 +14,75 @@

package org.eclipse.edc.connector.dataplane.selector;

import jakarta.json.Json;
import org.eclipse.edc.api.transformer.JsonObjectFromIdResponseTransformer;
import org.eclipse.edc.connector.dataplane.selector.control.api.DataplaneSelectorControlApiController;
import org.eclipse.edc.connector.dataplane.selector.spi.DataPlaneSelectorService;
import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance;
import org.eclipse.edc.connector.dataplane.selector.transformer.JsonObjectToSelectionRequestTransformer;
import org.eclipse.edc.http.client.ControlApiHttpClientImpl;
import org.eclipse.edc.jsonld.util.JacksonJsonLd;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.junit.extensions.EmbeddedRuntime;
import org.eclipse.edc.junit.extensions.RuntimeExtension;
import org.eclipse.edc.junit.extensions.RuntimePerMethodExtension;
import org.eclipse.edc.spi.result.ServiceFailure;
import org.eclipse.edc.spi.result.ServiceResult;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.transform.TypeTransformerRegistryImpl;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.eclipse.edc.transform.transformer.edc.from.JsonObjectFromDataAddressTransformer;
import org.eclipse.edc.transform.transformer.edc.from.JsonObjectFromDataPlaneInstanceTransformer;
import org.eclipse.edc.transform.transformer.edc.to.JsonObjectToDataAddressTransformer;
import org.eclipse.edc.transform.transformer.edc.to.JsonObjectToDataPlaneInstanceTransformer;
import org.eclipse.edc.transform.transformer.edc.to.JsonValueToGenericTypeTransformer;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.validator.spi.ValidationResult;
import org.eclipse.edc.web.jersey.testfixtures.RestControllerTestBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.time.Clock;
import java.util.Map;
import java.util.UUID;

import static org.eclipse.edc.http.client.testfixtures.HttpTestUtils.testHttpClient;
import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat;
import static org.eclipse.edc.spi.result.ServiceFailure.Reason.CONFLICT;
import static org.eclipse.edc.spi.result.ServiceFailure.Reason.NOT_FOUND;
import static org.eclipse.edc.util.io.Ports.getFreePort;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ComponentTest
class RemoteDataPlaneSelectorServiceTest extends RestControllerTestBase {
class RemoteDataPlaneSelectorServiceTest {

private final int port = getFreePort();
private static final String[] FIELDS_TO_BE_IGNORED = {"createdAt", "stateTimestamp", "updatedAt"};
private final String url = "http://localhost:%d/v1/dataplanes".formatted(port);
private final DataPlaneSelectorService serverService = mock();
private final TypeTransformerRegistry typeTransformerRegistry = new TypeTransformerRegistryImpl();
private final JsonObjectValidatorRegistry validator = mock();
private final ControlApiHttpClientImpl controlClient = new ControlApiHttpClientImpl(testHttpClient(), mock());
private final RemoteDataPlaneSelectorService service = new RemoteDataPlaneSelectorService(controlClient, url,
JacksonJsonLd.createObjectMapper(), typeTransformerRegistry, "selectionStrategy");

@BeforeEach
void setUp() {
var factory = Json.createBuilderFactory(Map.of());
var objectMapper = JacksonJsonLd.createObjectMapper();
typeTransformerRegistry.register(new JsonObjectFromDataAddressTransformer(factory));
typeTransformerRegistry.register(new JsonObjectToDataAddressTransformer());
typeTransformerRegistry.register(new JsonObjectToSelectionRequestTransformer());
typeTransformerRegistry.register(new JsonObjectFromDataPlaneInstanceTransformer(factory, JacksonJsonLd.createObjectMapper()));
typeTransformerRegistry.register(new JsonObjectToDataPlaneInstanceTransformer());
typeTransformerRegistry.register(new JsonObjectFromIdResponseTransformer(factory));
typeTransformerRegistry.register(new org.eclipse.edc.connector.dataplane.selector.control.api.transformer.JsonObjectToSelectionRequestTransformer());
typeTransformerRegistry.register(new JsonValueToGenericTypeTransformer(objectMapper));
}

@RegisterExtension
public final RuntimeExtension client = new RuntimePerMethodExtension(new EmbeddedRuntime(
"client",
Map.of(
"edc.dpf.selector.url", "http://localhost:%d/control/v1/dataplanes".formatted(port),
"edc.core.retry.retries.max", "0"
),
":core:common:connector-core",
":extensions:common:http"
));

@RegisterExtension
public final RuntimeExtension server = new RuntimePerMethodExtension(new EmbeddedRuntime(
"server",
Map.of(
"edc.dpf.selector.url", "http://not-used-but-mandatory",
"web.http.control.port", port + "",
"web.http.control.path", "/control"
),
":extensions:data-plane-selector:data-plane-selector-control-api",
":extensions:common:api:control-api-configuration",
":core:common:connector-core",
":extensions:common:http"))
.registerServiceMock(DataPlaneSelectorService.class, serverService)
.registerServiceMock(JsonObjectValidatorRegistry.class, validator);

@Test
void addInstance() {
when(validator.validate(any(), any())).thenReturn(ValidationResult.success());
when(serverService.addInstance(any())).thenReturn(ServiceResult.success());
var instance = createInstance("dataPlaneId");

var result = service.addInstance(instance);
var result = service().addInstance(instance);

assertThat(result).isSucceeded();
verify(serverService).addInstance(any());
Expand All @@ -97,20 +93,21 @@ void select() {
var expected = createInstance("some-instance");
when(serverService.select(any(), eq("transferType"), eq("random"))).thenReturn(ServiceResult.success(expected));

var result = service.select(DataAddress.Builder.newInstance().type("test1").build(), "transferType", "random");
var result = service().select(DataAddress.Builder.newInstance().type("test1").build(), "transferType", "random");

assertThat(result).isSucceeded().usingRecursiveComparison()
.ignoringFields(FIELDS_TO_BE_IGNORED).isEqualTo(expected);
}

@Nested
class Unregister {

@Test
void shouldUnregister() {
var instanceId = UUID.randomUUID().toString();
when(serverService.unregister(any())).thenReturn(ServiceResult.success());

var result = service.unregister(instanceId);
var result = service().unregister(instanceId);

assertThat(result).isSucceeded();
verify(serverService).unregister(instanceId);
Expand All @@ -121,10 +118,11 @@ void shouldFail_whenServiceFails() {
var instanceId = UUID.randomUUID().toString();
when(serverService.unregister(any())).thenReturn(ServiceResult.conflict("conflict"));

var result = service.unregister(instanceId);
var result = service().unregister(instanceId);

assertThat(result).isFailed().extracting(ServiceFailure::getReason).isEqualTo(CONFLICT);
}

}

@Nested
Expand All @@ -135,7 +133,7 @@ void shouldDelete() {
var instanceId = UUID.randomUUID().toString();
when(serverService.delete(any())).thenReturn(ServiceResult.success());

var result = service.delete(instanceId);
var result = service().delete(instanceId);

assertThat(result).isSucceeded();
verify(serverService).delete(instanceId);
Expand All @@ -146,21 +144,23 @@ void shouldFail_whenNotFound() {
var instanceId = UUID.randomUUID().toString();
when(serverService.delete(any())).thenReturn(ServiceResult.notFound("not found"));

var result = service.delete(instanceId);
var result = service().delete(instanceId);

assertThat(result).isFailed().extracting(ServiceFailure::getReason).isEqualTo(NOT_FOUND);
}

}

@Nested
class FindById {

@Test
void shouldReturnInstanceById() {
var instanceId = UUID.randomUUID().toString();
var instance = DataPlaneInstance.Builder.newInstance().url("http://any").build();
when(serverService.findById(any())).thenReturn(ServiceResult.success(instance));

var result = service.findById(instanceId);
var result = service().findById(instanceId);

assertThat(result).isSucceeded().usingRecursiveComparison()
.ignoringFields(FIELDS_TO_BE_IGNORED)
Expand All @@ -172,15 +172,15 @@ void shouldReturnNotFound_whenInstanceDoesNotExist() {
var instanceId = UUID.randomUUID().toString();
when(serverService.findById(any())).thenReturn(ServiceResult.notFound("not found"));

var result = service.findById(instanceId);
var result = service().findById(instanceId);

assertThat(result).isFailed().extracting(ServiceFailure::getReason).isEqualTo(NOT_FOUND);
}

}

@Override
protected Object controller() {
return new DataplaneSelectorControlApiController(validator, typeTransformerRegistry, serverService, Clock.systemUTC());
private DataPlaneSelectorService service() {
return client.getService(DataPlaneSelectorService.class);
}

private DataPlaneInstance createInstance(String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package org.eclipse.edc.connector.dataplane.selector.control.api;

import org.eclipse.edc.connector.dataplane.selector.control.api.transformer.JsonObjectToSelectionRequestTransformer;
import org.eclipse.edc.connector.dataplane.selector.spi.DataPlaneSelectorService;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
Expand All @@ -23,7 +24,9 @@
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.eclipse.edc.transform.transformer.edc.from.JsonObjectFromDataPlaneInstanceTransformer;
import org.eclipse.edc.transform.transformer.edc.to.JsonObjectToDataAddressTransformer;
import org.eclipse.edc.transform.transformer.edc.to.JsonObjectToDataPlaneInstanceTransformer;
import org.eclipse.edc.transform.transformer.edc.to.JsonValueToGenericTypeTransformer;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.web.spi.WebService;
import org.eclipse.edc.web.spi.configuration.ApiContext;
Expand Down Expand Up @@ -67,9 +70,13 @@ public String name() {
@Override
public void initialize(ServiceExtensionContext context) {
validatorRegistry.register(DATAPLANE_INSTANCE_TYPE, DataPlaneInstanceValidator.instance());
var objectMapper = typeManager.getMapper(JSON_LD);

typeTransformerRegistry.register(new JsonObjectToDataPlaneInstanceTransformer());
typeTransformerRegistry.register(new JsonObjectFromDataPlaneInstanceTransformer(createBuilderFactory(Map.of()), typeManager.getMapper(JSON_LD)));
typeTransformerRegistry.register(new JsonObjectToSelectionRequestTransformer());
typeTransformerRegistry.register(new JsonObjectToDataAddressTransformer());
typeTransformerRegistry.register(new JsonValueToGenericTypeTransformer(objectMapper));
typeTransformerRegistry.register(new JsonObjectFromDataPlaneInstanceTransformer(createBuilderFactory(Map.of()), objectMapper));

var controller = new DataplaneSelectorControlApiController(validatorRegistry, typeTransformerRegistry, dataPlaneSelectorService, clock);
webService.registerResource(ApiContext.CONTROL, controller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class SelectionRequest {
public static final String TRANSFER_TYPE = EDC_NAMESPACE + "transferType";
public static final String STRATEGY = EDC_NAMESPACE + "strategy";
private DataAddress source;
@Deprecated(since = "0.8.1")
private DataAddress destination;
private String strategy = "random";
private String transferType;
Expand All @@ -40,6 +41,7 @@ public DataAddress getSource() {
return source;
}

@Deprecated(since = "0.8.1")
public DataAddress getDestination() {
return destination;
}
Expand Down
Loading

0 comments on commit 61e68b9

Please sign in to comment.