Skip to content

Commit

Permalink
refactor: introduced message classes for errors (#4540)
Browse files Browse the repository at this point in the history
* refactor: introduced message classes for errors

* pr suggestions
  • Loading branch information
wolf4ood authored Oct 14, 2024
1 parent d2ebfcc commit 8f43ec8
Show file tree
Hide file tree
Showing 38 changed files with 1,336 additions and 411 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import org.eclipse.edc.connector.controlplane.catalog.spi.Catalog;
import org.eclipse.edc.connector.controlplane.catalog.spi.CatalogError;
import org.eclipse.edc.connector.controlplane.catalog.spi.CatalogRequestMessage;
import org.eclipse.edc.connector.controlplane.catalog.spi.Dataset;
import org.eclipse.edc.connector.controlplane.services.spi.catalog.CatalogProtocolService;
Expand All @@ -42,7 +43,6 @@
import static org.eclipse.edc.protocol.dsp.catalog.http.api.CatalogApiPaths.CATALOG_REQUEST;
import static org.eclipse.edc.protocol.dsp.catalog.http.api.CatalogApiPaths.DATASET_REQUEST;
import static org.eclipse.edc.protocol.dsp.http.spi.types.HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_ERROR;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_REQUEST_MESSAGE;

/**
Expand Down Expand Up @@ -81,12 +81,12 @@ public Response requestCatalog(JsonObject jsonObject, @HeaderParam(AUTHORIZATION
.orElseThrow(f -> new BadRequestException(f.getFailureDetail()));
}

var request = PostDspRequest.Builder.newInstance(CatalogRequestMessage.class, Catalog.class)
var request = PostDspRequest.Builder.newInstance(CatalogRequestMessage.class, Catalog.class, CatalogError.class)
.token(token)
.expectedMessageType(DSPACE_TYPE_CATALOG_REQUEST_MESSAGE)
.message(messageJson)
.serviceCall(service::getCatalog)
.errorType(DSPACE_TYPE_CATALOG_ERROR)
.errorProvider(CatalogError.Builder::newInstance)
.protocol(protocol)
.build();

Expand All @@ -97,11 +97,11 @@ public Response requestCatalog(JsonObject jsonObject, @HeaderParam(AUTHORIZATION
@GET
@Path(DATASET_REQUEST + "/{id}")
public Response getDataset(@PathParam("id") String id, @HeaderParam(AUTHORIZATION) String token) {
var request = GetDspRequest.Builder.newInstance(Dataset.class)
var request = GetDspRequest.Builder.newInstance(Dataset.class, CatalogError.class)
.token(token)
.id(id)
.serviceCall(service::getDataset)
.errorType(DSPACE_TYPE_CATALOG_ERROR)
.errorProvider(CatalogError.Builder::newInstance)
.protocol(protocol)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,37 @@ class DspCatalogApiControllerTest extends RestControllerTestBase {
private final DspRequestHandler dspRequestHandler = mock();
private final ContinuationTokenManager continuationTokenManager = mock();

@Test
void getDataset_shouldGetResource() {
when(dspRequestHandler.getResource(any())).thenReturn(Response.ok().type(APPLICATION_JSON).build());

baseRequest()
.get(DATASET_REQUEST + "/datasetId")
.then()
.statusCode(200)
.contentType(JSON);

var captor = ArgumentCaptor.forClass(GetDspRequest.class);
verify(dspRequestHandler).getResource(captor.capture());
var request = captor.getValue();
assertThat(request.getToken()).isEqualTo("auth");
assertThat(request.getResultClass()).isEqualTo(Dataset.class);
assertThat(request.getId()).isEqualTo("datasetId");
}

@Override
protected Object controller() {
return new DspCatalogApiController(service, dspRequestHandler, continuationTokenManager);
}

private RequestSpecification baseRequest() {
return given()
.baseUri("http://localhost:" + port)
.basePath(BASE_PATH)
.header(HttpHeaders.AUTHORIZATION, "auth")
.when();
}

@Nested
class RequestCatalog {

Expand Down Expand Up @@ -136,36 +167,4 @@ void shouldReturnBadRequest_whenContinuationTokenIsNotValid() {
}
}

@Test
void getDataset_shouldGetResource() {
when(dspRequestHandler.getResource(any())).thenReturn(Response.ok().type(APPLICATION_JSON).build());

baseRequest()
.get(DATASET_REQUEST + "/datasetId")
.then()
.statusCode(200)
.contentType(JSON);

var captor = ArgumentCaptor.forClass(GetDspRequest.class);
verify(dspRequestHandler).getResource(captor.capture());
var request = captor.getValue();
assertThat(request.getToken()).isEqualTo("auth");
assertThat(request.getResultClass()).isEqualTo(Dataset.class);
assertThat(request.getId()).isEqualTo("datasetId");
assertThat(request.getErrorType()).isNotNull();
}

@Override
protected Object controller() {
return new DspCatalogApiController(service, dspRequestHandler, continuationTokenManager);
}

private RequestSpecification baseRequest() {
return given()
.baseUri("http://localhost:" + port)
.basePath(BASE_PATH)
.header(HttpHeaders.AUTHORIZATION, "auth")
.when();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.Json;
import org.eclipse.edc.protocol.dsp.catalog.transform.from.JsonObjectFromCatalogErrorTransformer;
import org.eclipse.edc.protocol.dsp.catalog.transform.from.JsonObjectFromCatalogRequestMessageTransformer;
import org.eclipse.edc.protocol.dsp.catalog.transform.from.JsonObjectFromCatalogTransformer;
import org.eclipse.edc.protocol.dsp.catalog.transform.from.JsonObjectFromDataServiceTransformer;
Expand Down Expand Up @@ -77,5 +78,6 @@ private void registerTransformers(String version, ObjectMapper mapper) {
dspApiTransformerRegistry.register(new JsonObjectFromDatasetTransformer(jsonFactory, mapper));
dspApiTransformerRegistry.register(new JsonObjectFromDistributionTransformer(jsonFactory));
dspApiTransformerRegistry.register(new JsonObjectFromDataServiceTransformer(jsonFactory));
dspApiTransformerRegistry.register(new JsonObjectFromCatalogErrorTransformer(jsonFactory));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.catalog.transform.from;

import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.controlplane.catalog.spi.CatalogError;
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.TYPE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_ERROR;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON;

/**
* Transforms a {@link CatalogError} to a {@link JsonObject} in JSON-LD expanded form.
*/
public class JsonObjectFromCatalogErrorTransformer extends AbstractJsonLdTransformer<CatalogError, JsonObject> {

private final JsonBuilderFactory jsonFactory;

public JsonObjectFromCatalogErrorTransformer(JsonBuilderFactory jsonFactory) {
super(CatalogError.class, JsonObject.class);
this.jsonFactory = jsonFactory;
}

@Override
public @Nullable JsonObject transform(@NotNull CatalogError error, @NotNull TransformerContext context) {
return jsonFactory.createObjectBuilder()
.add(TYPE, DSPACE_TYPE_CATALOG_ERROR)
.add(DSPACE_PROPERTY_CODE, error.getCode())
.add(DSPACE_PROPERTY_REASON, jsonFactory.createArrayBuilder(error.getMessages()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.catalog.transform.from;

import jakarta.json.Json;
import jakarta.json.JsonBuilderFactory;
import org.eclipse.edc.connector.controlplane.catalog.spi.CatalogError;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;
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.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_ERROR;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON;
import static org.mockito.Mockito.mock;

class JsonObjectFromCatalogErrorTransformerTest {

private final JsonBuilderFactory jsonFactory = Json.createBuilderFactory(Map.of());
private final TransformerContext context = mock(TransformerContext.class);

private JsonObjectFromCatalogErrorTransformer transformer;

@BeforeEach
void setUp() {
transformer = new JsonObjectFromCatalogErrorTransformer(jsonFactory);
}

@Test
void transform_returnJsonObject() {

var error = CatalogError.Builder.newInstance()
.code("code")
.messages(List.of("message"))
.build();

var result = transformer.transform(error, context);

assertThat(result).isNotNull();
assertThat(result.getJsonString(TYPE).getString()).isEqualTo(DSPACE_TYPE_CATALOG_ERROR);
assertThat(result.getString(DSPACE_PROPERTY_CODE)).isEqualTo("code");
assertThat(result.getJsonArray(DSPACE_PROPERTY_REASON)).contains(Json.createValue("message"));
}
}
Loading

0 comments on commit 8f43ec8

Please sign in to comment.