Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added exception mapping for import #88

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -179,7 +179,7 @@ public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationExce
}

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

Expand Down
Loading