diff --git a/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/ImagesRestController.java b/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/ImagesRestController.java index f01ddbe..8fed93f 100644 --- a/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/ImagesRestController.java +++ b/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/ImagesRestController.java @@ -4,11 +4,11 @@ import jakarta.inject.Inject; import jakarta.transaction.Transactional; import jakarta.validation.ConstraintViolationException; -import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.Response; import org.eclipse.microprofile.rest.client.inject.RestClient; +import org.jboss.resteasy.reactive.ClientWebApplicationException; import org.jboss.resteasy.reactive.RestResponse; import org.jboss.resteasy.reactive.server.ServerExceptionMapper; import org.tkit.onecx.product.store.bff.rs.mappers.ExceptionMapper; @@ -84,7 +84,7 @@ public RestResponse constraint(ConstraintViolationExce } @ServerExceptionMapper - public Response restException(WebApplicationException ex) { - return Response.status(ex.getResponse().getStatus()).build(); + public Response restException(ClientWebApplicationException ex) { + return exceptionMapper.clientException(ex); } } diff --git a/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/MicrofrontendsRestController.java b/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/MicrofrontendsRestController.java index 5f3d9cf..7a187cb 100644 --- a/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/MicrofrontendsRestController.java +++ b/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/MicrofrontendsRestController.java @@ -8,6 +8,7 @@ import jakarta.ws.rs.core.Response; import org.eclipse.microprofile.rest.client.inject.RestClient; +import org.jboss.resteasy.reactive.ClientWebApplicationException; import org.jboss.resteasy.reactive.RestResponse; import org.jboss.resteasy.reactive.server.ServerExceptionMapper; import org.tkit.onecx.product.store.bff.rs.mappers.ExceptionMapper; @@ -104,7 +105,7 @@ public RestResponse constraint(ConstraintViolationExce } @ServerExceptionMapper - public Response restException(WebApplicationException ex) { - return Response.status(ex.getResponse().getStatus()).build(); + public Response restException(ClientWebApplicationException ex) { + return exceptionMapper.clientException(ex); } } diff --git a/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/MicroservicesRestController.java b/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/MicroservicesRestController.java index e9b047a..00a9860 100644 --- a/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/MicroservicesRestController.java +++ b/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/MicroservicesRestController.java @@ -8,6 +8,7 @@ import jakarta.ws.rs.core.Response; import org.eclipse.microprofile.rest.client.inject.RestClient; +import org.jboss.resteasy.reactive.ClientWebApplicationException; import org.jboss.resteasy.reactive.RestResponse; import org.jboss.resteasy.reactive.server.ServerExceptionMapper; import org.tkit.onecx.product.store.bff.rs.mappers.ExceptionMapper; @@ -69,14 +70,6 @@ public Response getMicroservice(String id) { } } - @Override - public Response getMicroserviceByAppId(String appId) { - try (Response response = client.getMicroserviceByAppId(appId)) { - MicroserviceDTO result = mapper.map(response.readEntity(Microservice.class)); - return Response.status(response.getStatus()).entity(result).build(); - } - } - @Override public Response searchMicroservice(MfeAndMsSearchCriteriaDTO microserviceSearchCriteriaDTO) { try (Response response = client.searchMicroservice(mapper.mapMsSearchCriteria(microserviceSearchCriteriaDTO))) { @@ -102,7 +95,7 @@ public RestResponse constraint(ConstraintViolationExce } @ServerExceptionMapper - public Response restException(WebApplicationException ex) { - return Response.status(ex.getResponse().getStatus()).build(); + public Response restException(ClientWebApplicationException ex) { + return exceptionMapper.clientException(ex); } } diff --git a/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/ProductsRestController.java b/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/ProductsRestController.java index de7aa54..950d4a0 100644 --- a/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/ProductsRestController.java +++ b/src/main/java/org/tkit/onecx/product/store/bff/rs/controllers/ProductsRestController.java @@ -10,6 +10,7 @@ import jakarta.ws.rs.core.Response; import org.eclipse.microprofile.rest.client.inject.RestClient; +import org.jboss.resteasy.reactive.ClientWebApplicationException; import org.jboss.resteasy.reactive.RestResponse; import org.jboss.resteasy.reactive.server.ServerExceptionMapper; import org.tkit.onecx.product.store.bff.rs.mappers.ExceptionMapper; @@ -133,7 +134,7 @@ public RestResponse constraint(ConstraintViolationExce } @ServerExceptionMapper - public Response restException(WebApplicationException ex) { - return Response.status(ex.getResponse().getStatus()).build(); + public Response restException(ClientWebApplicationException ex) { + return exceptionMapper.clientException(ex); } } diff --git a/src/main/java/org/tkit/onecx/product/store/bff/rs/mappers/ExceptionMapper.java b/src/main/java/org/tkit/onecx/product/store/bff/rs/mappers/ExceptionMapper.java index 932828c..c53d2de 100644 --- a/src/main/java/org/tkit/onecx/product/store/bff/rs/mappers/ExceptionMapper.java +++ b/src/main/java/org/tkit/onecx/product/store/bff/rs/mappers/ExceptionMapper.java @@ -1,5 +1,7 @@ package org.tkit.onecx.product.store.bff.rs.mappers; +import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; + import java.util.List; import java.util.Map; import java.util.Set; @@ -9,11 +11,13 @@ import jakarta.validation.Path; import jakarta.ws.rs.core.Response; +import org.jboss.resteasy.reactive.ClientWebApplicationException; import org.jboss.resteasy.reactive.RestResponse; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.tkit.quarkus.rs.mappers.OffsetDateTimeMapper; +import gen.org.tkit.onecx.permission.model.ProblemDetailResponse; import gen.org.tkit.onecx.product.store.bff.rs.internal.model.ProblemDetailInvalidParamDTO; import gen.org.tkit.onecx.product.store.bff.rs.internal.model.ProblemDetailParamDTO; import gen.org.tkit.onecx.product.store.bff.rs.internal.model.ProblemDetailResponseDTO; @@ -27,6 +31,24 @@ default RestResponse constraint(ConstraintViolationExc return RestResponse.status(Response.Status.BAD_REQUEST, dto); } + default Response clientException(ClientWebApplicationException ex) { + if (ex.getResponse().getStatus() == 500) { + return Response.status(400).build(); + } else { + if (ex.getResponse().getMediaType() != null + && ex.getResponse().getMediaType().toString().equals(APPLICATION_JSON)) { + return Response.status(ex.getResponse().getStatus()) + .entity(map(ex.getResponse().readEntity(ProblemDetailResponse.class))).build(); + } else { + return Response.status(ex.getResponse().getStatus()).build(); + } + } + } + + @Mapping(target = "removeParamsItem", ignore = true) + @Mapping(target = "removeInvalidParamsItem", ignore = true) + ProblemDetailResponseDTO map(ProblemDetailResponse problemDetailResponse); + @Mapping(target = "removeParamsItem", ignore = true) @Mapping(target = "params", ignore = true) @Mapping(target = "invalidParams", ignore = true) diff --git a/src/main/openapi/onecx-product-store-bff.yaml b/src/main/openapi/onecx-product-store-bff.yaml index f8e7b10..123fd93 100644 --- a/src/main/openapi/onecx-product-store-bff.yaml +++ b/src/main/openapi/onecx-product-store-bff.yaml @@ -498,31 +498,6 @@ paths: application/json: schema: $ref: '#/components/schemas/ProblemDetailResponse' - /microservices/appId/{appId}: - x-onecx: - permissions: - microservice: - - read - get: - tags: - - Microservices - description: Return microservice by its appId - operationId: getMicroserviceByAppId - parameters: - - name: appId - in: path - required: true - schema: - type: string - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/Microservice' - "404": - description: Not found /images/{refId}/{refType}: post: x-onecx: diff --git a/src/test/java/org/tkit/onecx/product/store/bff/rs/MicroservicesRestControllerTest.java b/src/test/java/org/tkit/onecx/product/store/bff/rs/MicroservicesRestControllerTest.java index 9deb3c3..737e7e5 100644 --- a/src/test/java/org/tkit/onecx/product/store/bff/rs/MicroservicesRestControllerTest.java +++ b/src/test/java/org/tkit/onecx/product/store/bff/rs/MicroservicesRestControllerTest.java @@ -97,72 +97,6 @@ void getMicroservice_shouldReturnMicroservice() { } - /** - * Scenario: Receives microservice (ms) by app-id successfully. - * Given - * When I query GET endpoint with an existing id - * Then I get a 'OK' response code back - * AND associated ms is returned - */ - @Test - void getMicroserviceByAppId_shouldReturnMicroservice() { - - OffsetDateTime offsetDateTime = OffsetDateTime.parse("2023-11-30T13:53:03.688710200+01:00"); - OffsetDateTimeMapper mapper = new OffsetDateTimeMapper(); - - Microservice data = createMicroservice("7a0ee705-8fd0-47b0-8205-b2a5f6540b9e", offsetDateTime, "csommer", - offsetDateTime, "csommer", 1, "App-ID", "1.0.0", - "AppName", "some description", "ProductName"); - - // create mock rest endpoint - mockServerClient - .when(request().withPath(PRODUCT_STORE_SVC_INTERNAL_API_BASE_PATH + "/appId/" + data.getAppId()) - .withMethod(HttpMethod.GET)) - .respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode()) - .withContentType(MediaType.APPLICATION_JSON) - .withBody(JsonBody.json(data))); - - var response = given() - .when() - .auth().oauth2(keycloakClient.getAccessToken(ADMIN)) - .header(APM_HEADER_PARAM, ADMIN) - .contentType(APPLICATION_JSON) - .get("appId/" + data.getAppId()) - .then() - .statusCode(Response.Status.OK.getStatusCode()) - .contentType(APPLICATION_JSON) - .extract().as(MicroserviceDTO.class); - - Assertions.assertEquals(data.getId(), response.getId()); - Assertions.assertEquals(data.getCreationUser(), response.getCreationUser()); - Assertions.assertEquals(mapper.map(data.getCreationDate()), mapper.map(response.getCreationDate())); - Assertions.assertEquals(mapper.map(data.getModificationDate()), mapper.map(response.getModificationDate())); - Assertions.assertEquals(data.getModificationUser(), response.getModificationUser()); - Assertions.assertEquals(data.getModificationCount(), response.getModificationCount()); - Assertions.assertEquals(data.getAppId(), response.getAppId()); - Assertions.assertEquals(data.getVersion(), response.getAppVersion()); - Assertions.assertEquals(data.getName(), response.getAppName()); - Assertions.assertEquals(data.getDescription(), response.getDescription()); - - Assertions.assertEquals(data.getProductName(), response.getProductName()); - - // create mock rest endpoint - mockServerClient - .when(request().withPath(PRODUCT_STORE_SVC_INTERNAL_API_BASE_PATH + "/appId/not-existing") - .withMethod(HttpMethod.GET)) - .respond(httpRequest -> response().withStatusCode(Response.Status.NOT_FOUND.getStatusCode())); - - //expect not found for non-existing appId - given() - .when() - .auth().oauth2(keycloakClient.getAccessToken(ADMIN)) - .header(APM_HEADER_PARAM, ADMIN) - .contentType(APPLICATION_JSON) - .get("/appId/not-existing") - .then() - .statusCode(Response.Status.NOT_FOUND.getStatusCode()); - } - /** * Scenario: Receive 404 Not Found when microservice (ms) is not existing in backend service. * Given diff --git a/src/test/java/org/tkit/onecx/product/store/bff/rs/ProductsRestControllerTest.java b/src/test/java/org/tkit/onecx/product/store/bff/rs/ProductsRestControllerTest.java index ef70e5d..23477fa 100644 --- a/src/test/java/org/tkit/onecx/product/store/bff/rs/ProductsRestControllerTest.java +++ b/src/test/java/org/tkit/onecx/product/store/bff/rs/ProductsRestControllerTest.java @@ -662,7 +662,7 @@ void searchProducts_shouldReturnInternalServerError_whenDownStreamServiceRunsInt .body(requestDTO) .post("/search") .then() - .statusCode(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + .statusCode(Response.Status.BAD_REQUEST.getStatusCode()); } /** @@ -820,7 +820,7 @@ void deleteProduct_shouldReturnInternalServerError_whenDownStreamServiceRunsInto .pathParam("id", id) .delete("/{id}") .then() - .statusCode(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + .statusCode(Response.Status.BAD_REQUEST.getStatusCode()); } /**