Skip to content

Commit

Permalink
feat: add IdResponse on addEntry API
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Sep 13, 2023
1 parent f467cff commit 356f52d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.web.spi.WebService;

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

import static jakarta.json.Json.createBuilderFactory;
Expand All @@ -57,6 +58,9 @@ public class DataPlaneSelectorApiExtension implements ServiceExtension {
@Inject
private JsonObjectValidatorRegistry validatorRegistry;

@Inject
private Clock clock;

@Override
public void initialize(ServiceExtensionContext context) {
typeManager.registerTypes(DataPlaneInstance.class);
Expand All @@ -65,7 +69,7 @@ public void initialize(ServiceExtensionContext context) {
transformerRegistry.register(new JsonObjectToSelectionRequestTransformer());
transformerRegistry.register(new JsonObjectToDataPlaneInstanceTransformer());
transformerRegistry.register(new JsonObjectFromDataPlaneInstanceTransformer(createBuilderFactory(Map.of()), typeManager.getMapper(JSON_LD)));
var controller = new DataplaneSelectorApiController(selectionService, transformerRegistry, validatorRegistry);
var controller = new DataplaneSelectorApiController(selectionService, transformerRegistry, validatorRegistry, clock);

webservice.registerResource(managementApiConfiguration.getContextAlias(), controller);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ public interface DataplaneSelectorApi {
description = "Adds one datatplane instance to the internal database of the selector",
requestBody = @RequestBody(content = @Content(schema = @Schema(implementation = DataPlaneInstanceSchema.class))),
responses = {
@ApiResponse(responseCode = "200", description = "Entry was added successfully to the database"),
@ApiResponse(responseCode = "200", description = "Entry was added successfully to the database", content = @Content(schema = @Schema(implementation = ApiCoreSchema.IdResponseSchema.class))),
@ApiResponse(responseCode = "400", description = "Request body was malformed", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiCoreSchema.ApiErrorDetailSchema.class))))
}
)
@POST
void addEntry(JsonObject instance);
JsonObject addEntry(JsonObject instance);

@Operation(method = "GET",
description = "Returns a list of all currently registered data plane instances",
responses = {
@ApiResponse(responseCode = "204", description = "A (potentially empty) list of currently registered data plane instances"),
@ApiResponse(responseCode = "200", description = "A (potentially empty) list of currently registered data plane instances",
content = @Content(array = @ArraySchema(schema = @Schema(implementation = DataPlaneInstanceSchema.class)))),
@ApiResponse(responseCode = "400", description = "Request body was malformed", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiCoreSchema.ApiErrorDetailSchema.class))))
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.edc.api.model.IdResponse;
import org.eclipse.edc.connector.dataplane.selector.api.v2.model.SelectionRequest;
import org.eclipse.edc.connector.dataplane.selector.spi.DataPlaneSelectorService;
import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance;
Expand All @@ -32,6 +33,7 @@
import org.eclipse.edc.web.spi.exception.InvalidRequestException;
import org.eclipse.edc.web.spi.exception.ValidationFailureException;

import java.time.Clock;
import java.util.function.Supplier;

import static jakarta.json.stream.JsonCollectors.toJsonArray;
Expand All @@ -49,11 +51,13 @@ public class DataplaneSelectorApiController implements DataplaneSelectorApi {

private final JsonObjectValidatorRegistry validatorRegistry;

private final Clock clock;

public DataplaneSelectorApiController(DataPlaneSelectorService selectionService, TypeTransformerRegistry transformerRegistry, JsonObjectValidatorRegistry validatorRegistry) {
public DataplaneSelectorApiController(DataPlaneSelectorService selectionService, TypeTransformerRegistry transformerRegistry, JsonObjectValidatorRegistry validatorRegistry, Clock clock) {
this.selectionService = selectionService;
this.transformerRegistry = transformerRegistry;
this.validatorRegistry = validatorRegistry;
this.clock = clock;
}

@Override
Expand All @@ -76,14 +80,22 @@ public JsonObject find(JsonObject requestObject) {

@Override
@POST
public void addEntry(JsonObject jsonObject) {
public JsonObject addEntry(JsonObject jsonObject) {
validatorRegistry.validate(DATAPLANE_INSTANCE_TYPE, jsonObject).orElseThrow(ValidationFailureException::new);

var instance = transformerRegistry.transform(jsonObject, DataPlaneInstance.class)
.orElseThrow(InvalidRequestException::new);

selectionService.addInstance(instance)
.orElseThrow(exceptionMapper(DataPlaneInstance.class, instance.getId()));

var idResponse = IdResponse.Builder.newInstance()
.id(instance.getId())
.createdAt(clock.millis())
.build();

return transformerRegistry.transform(idResponse, JsonObject.class)
.orElseThrow(f -> new EdcException(f.getFailureDetail()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ void getAll_noneExist() {
void addEntry(DataPlaneInstanceStore store) {
var dpi = createInstanceJson("test-id");

baseRequest()
var result = baseRequest()
.body(dpi)
.contentType(JSON)
.post()
.then()
.statusCode(204);
.statusCode(200)
.extract().body().as(JsonObject.class);

assertThat(result.getString(ID)).isEqualTo("test-id");
assertThat(store.getAll()).hasSize(1)
.allMatch(d -> d.getId().equals("test-id"));
}
Expand All @@ -99,7 +101,7 @@ void addEntry_fails_whenMissingUrl(DataPlaneInstanceStore store) {
.post()
.then()
.statusCode(400);

}

@Test
Expand All @@ -119,7 +121,7 @@ void addEntry_exists_shouldOverwrite(DataPlaneInstanceStore store) {
.contentType(JSON)
.post()
.then()
.statusCode(204);
.statusCode(200);


assertThat(store.getAll()).hasSize(3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

Expand Down Expand Up @@ -107,7 +108,7 @@ void find_withStrategy() {

@Override
protected Object controller() {
return new DataplaneSelectorApiController(SELECTOR_SERVICE_MOCK, typeTransformerRegistry, validator);
return new DataplaneSelectorApiController(SELECTOR_SERVICE_MOCK, typeTransformerRegistry, validator, Clock.systemUTC());
}

@Override
Expand Down

0 comments on commit 356f52d

Please sign in to comment.