Skip to content

Commit

Permalink
feat: removed endpoint and added exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
JordenReuter committed Apr 23, 2024
1 parent 3a18d72 commit f0493d0
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,7 +84,7 @@ public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationExce
}

@ServerExceptionMapper
public Response restException(WebApplicationException ex) {
return Response.status(ex.getResponse().getStatus()).build();
public Response restException(ClientWebApplicationException ex) {
return exceptionMapper.clientException(ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,7 +105,7 @@ public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationExce
}

@ServerExceptionMapper
public Response restException(WebApplicationException ex) {
return Response.status(ex.getResponse().getStatus()).build();
public Response restException(ClientWebApplicationException ex) {
return exceptionMapper.clientException(ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))) {
Expand All @@ -102,7 +95,7 @@ public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationExce
}

@ServerExceptionMapper
public Response restException(WebApplicationException ex) {
return Response.status(ex.getResponse().getStatus()).build();
public Response restException(ClientWebApplicationException ex) {
return exceptionMapper.clientException(ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -133,7 +134,7 @@ public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationExce
}

@ServerExceptionMapper
public Response restException(WebApplicationException ex) {
return Response.status(ex.getResponse().getStatus()).build();
public Response restException(ClientWebApplicationException ex) {
return exceptionMapper.clientException(ex);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -27,6 +31,24 @@ default RestResponse<ProblemDetailResponseDTO> 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)
Expand Down
25 changes: 0 additions & 25 deletions src/main/openapi/onecx-product-store-bff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand Down

0 comments on commit f0493d0

Please sign in to comment.