diff --git a/src/main/java/org/tkit/onecx/permission/bff/rs/controllers/AssignmentRestController.java b/src/main/java/org/tkit/onecx/permission/bff/rs/controllers/AssignmentRestController.java index 52ec553..a9bfd33 100644 --- a/src/main/java/org/tkit/onecx/permission/bff/rs/controllers/AssignmentRestController.java +++ b/src/main/java/org/tkit/onecx/permission/bff/rs/controllers/AssignmentRestController.java @@ -6,10 +6,10 @@ import jakarta.inject.Inject; import jakarta.transaction.Transactional; import jakarta.validation.ConstraintViolationException; -import jakarta.ws.rs.WebApplicationException; 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.permission.bff.rs.mappers.AssignmentMapper; @@ -179,7 +179,7 @@ public RestResponse constraint(ConstraintViolationExce } @ServerExceptionMapper - public Response restException(WebApplicationException ex) { - return Response.status(ex.getResponse().getStatus()).build(); + public Response clientRestException(ClientWebApplicationException ex) { + return exceptionMapper.clientException(ex); } } diff --git a/src/main/java/org/tkit/onecx/permission/bff/rs/mappers/ExceptionMapper.java b/src/main/java/org/tkit/onecx/permission/bff/rs/mappers/ExceptionMapper.java index 9d27639..86bbd23 100644 --- a/src/main/java/org/tkit/onecx/permission/bff/rs/mappers/ExceptionMapper.java +++ b/src/main/java/org/tkit/onecx/permission/bff/rs/mappers/ExceptionMapper.java @@ -20,6 +20,7 @@ import gen.org.tkit.onecx.permission.bff.rs.internal.model.ProblemDetailInvalidParamDTO; import gen.org.tkit.onecx.permission.bff.rs.internal.model.ProblemDetailParamDTO; import gen.org.tkit.onecx.permission.bff.rs.internal.model.ProblemDetailResponseDTO; +import gen.org.tkit.onecx.permission.exim.client.model.EximProblemDetailResponse; import gen.org.tkit.onecx.permission.model.ProblemDetailResponse; @Mapper(uses = { OffsetDateTimeMapper.class }) @@ -65,6 +66,9 @@ default String mapPath(Path path) { default Response clientException(ClientWebApplicationException ex) { if (ex.getResponse().getStatus() == 500) { return Response.status(400).build(); + } else if (ex.getResponse().getStatus() == 409) { + return Response.status(ex.getResponse().getStatus()) + .entity(mapImportException(ex.getResponse().readEntity(EximProblemDetailResponse.class))).build(); } else { if (ex.getResponse().getMediaType() != null && ex.getResponse().getMediaType().toString().contains(APPLICATION_JSON)) { @@ -76,6 +80,10 @@ default Response clientException(ClientWebApplicationException ex) { } } + @Mapping(target = "removeParamsItem", ignore = true) + @Mapping(target = "removeInvalidParamsItem", ignore = true) + ProblemDetailResponseDTO mapImportException(EximProblemDetailResponse eximProblemDetailResponse); + @Mapping(target = "removeParamsItem", ignore = true) @Mapping(target = "removeInvalidParamsItem", ignore = true) ProblemDetailResponseDTO map(ProblemDetailResponse problemDetailResponse); diff --git a/src/test/java/org/tkit/onecx/permission/rs/AssignmentRestControllerTest.java b/src/test/java/org/tkit/onecx/permission/rs/AssignmentRestControllerTest.java index 1909141..8582116 100644 --- a/src/test/java/org/tkit/onecx/permission/rs/AssignmentRestControllerTest.java +++ b/src/test/java/org/tkit/onecx/permission/rs/AssignmentRestControllerTest.java @@ -24,6 +24,8 @@ import gen.org.tkit.onecx.permission.bff.rs.internal.model.*; import gen.org.tkit.onecx.permission.client.model.*; import gen.org.tkit.onecx.permission.exim.client.model.AssignmentSnapshot; +import gen.org.tkit.onecx.permission.exim.client.model.EximProblemDetailInvalidParam; +import gen.org.tkit.onecx.permission.exim.client.model.EximProblemDetailResponse; import gen.org.tkit.onecx.permission.exim.client.model.ExportAssignmentsRequest; import io.quarkiverse.mockserver.test.InjectMockServerClient; import io.quarkus.test.common.http.TestHTTPEndpoint; @@ -462,6 +464,51 @@ void importAssignmentsTest() { } + @Test + void importAssignments_exception_mapping_Test() { + AssignmentSnapshot assignmentSnapshot = new AssignmentSnapshot(); + assignmentSnapshot.setAssignments(Map.of("product1", + Map.of("app1", Map.of("role1", Map.of("resource1", List.of("read", "write")))))); + + EximProblemDetailResponse eximProblemDetailResponse = new EximProblemDetailResponse(); + eximProblemDetailResponse.setDetail("some Detail"); + EximProblemDetailInvalidParam invalidParam = new EximProblemDetailInvalidParam(); + invalidParam.setName("param1"); + invalidParam.setMessage("duplicated key"); + eximProblemDetailResponse.setInvalidParams(List.of(invalidParam)); + // create mock rest endpoint + mockServerClient.when(request().withPath("/exim/v1/assignments/import").withMethod(HttpMethod.POST) + .withBody(JsonBody.json(assignmentSnapshot))) + .withId("MOCK_ID") + .respond(httpRequest -> response() + .withStatusCode(Response.Status.CONFLICT.getStatusCode()) + .withBody(JsonBody.json(eximProblemDetailResponse))); + + AssignmentSnapshot assignmentSnapshotDTO = new AssignmentSnapshot(); + assignmentSnapshotDTO.setAssignments(Map.of("product1", + Map.of("app1", Map.of("role1", Map.of("resource1", List.of("read", "write")))))); + + var output = given() + .when() + .auth().oauth2(keycloakClient.getAccessToken(ADMIN)) + .header(APM_HEADER_PARAM, ADMIN) + .contentType(APPLICATION_JSON) + .body(assignmentSnapshotDTO) + .post("/import") + .then() + .contentType(APPLICATION_JSON) + .statusCode(Response.Status.CONFLICT.getStatusCode()) + .extract().as(ProblemDetailResponseDTO.class); + + Assertions.assertNotNull(output); + Assertions.assertEquals(output.getDetail(), eximProblemDetailResponse.getDetail()); + Assertions.assertEquals(output.getInvalidParams().get(0).getName(), + eximProblemDetailResponse.getInvalidParams().get(0).getName()); + + mockServerClient.clear("MOCK_ID"); + + } + @Test void searchUsersAssignmentsByCriteriaTest() {