diff --git a/src/main/java/org/tkit/onecx/permission/bff/rs/controllers/WorkspaceRestController.java b/src/main/java/org/tkit/onecx/permission/bff/rs/controllers/WorkspaceRestController.java index 8502d8d..3f6c783 100644 --- a/src/main/java/org/tkit/onecx/permission/bff/rs/controllers/WorkspaceRestController.java +++ b/src/main/java/org/tkit/onecx/permission/bff/rs/controllers/WorkspaceRestController.java @@ -6,9 +6,12 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.transaction.Transactional; +import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.Response; import org.eclipse.microprofile.rest.client.inject.RestClient; +import org.jboss.resteasy.reactive.server.ServerExceptionMapper; +import org.tkit.onecx.permission.bff.rs.mappers.ExceptionMapper; import org.tkit.onecx.permission.bff.rs.mappers.WorkspaceMapper; import org.tkit.quarkus.log.cdi.LogService; @@ -40,6 +43,9 @@ public class WorkspaceRestController implements WorkspaceApiService { @Inject WorkspaceMapper mapper; + @Inject + ExceptionMapper exceptionMapper; + @Override public Response getAllProductsByWorkspaceName(String workspaceName) { try (Response response = workspaceClient.loadWorkspaceByName(workspaceName)) { @@ -61,7 +67,7 @@ public Response getDetailsByWorkspaceName(String workspaceName) { WorkspaceDetailsDTO workspaceDetails; List productNames; List workspaceRoles = new ArrayList<>(); - ProductsLoadResult productsLoadResult; + ProductsLoadResult productsLoadResult = new ProductsLoadResult(); var workspaceResponse = response.readEntity(Workspace.class); if (workspaceResponse.getWorkspaceRoles() != null) { workspaceRoles = workspaceResponse.getWorkspaceRoles().stream().toList(); @@ -72,16 +78,22 @@ public Response getDetailsByWorkspaceName(String workspaceName) { //list of product names registered in workspace productNames = wsProductsResponse.readEntity(WorkspaceLoad.class).getProducts().stream() .map(Product::getProductName).toList(); - - //get mfe and ms for each product by name from product-store - ProductItemLoadSearchCriteria mfeAndMsCriteria = new ProductItemLoadSearchCriteria(); - mfeAndMsCriteria.setProductNames(productNames); - try (Response productStoreResponse = productStoreClient.loadProductsByCriteria(mfeAndMsCriteria)) { - productsLoadResult = productStoreResponse.readEntity(ProductsLoadResult.class); + if (!productNames.isEmpty()) { + //get mfe and ms for each product by name from product-store + ProductItemLoadSearchCriteria mfeAndMsCriteria = new ProductItemLoadSearchCriteria(); + mfeAndMsCriteria.setProductNames(productNames); + try (Response productStoreResponse = productStoreClient.loadProductsByCriteria(mfeAndMsCriteria)) { + productsLoadResult = productStoreResponse.readEntity(ProductsLoadResult.class); + } } workspaceDetails = mapper.map(workspaceRoles, productsLoadResult); } return Response.status(Response.Status.OK).entity(workspaceDetails).build(); } } + + @ServerExceptionMapper + public Response restException(WebApplicationException ex) { + return Response.status(ex.getResponse().getStatus()).build(); + } } diff --git a/src/main/java/org/tkit/onecx/permission/bff/rs/mappers/WorkspaceMapper.java b/src/main/java/org/tkit/onecx/permission/bff/rs/mappers/WorkspaceMapper.java index f803d33..70ffb84 100644 --- a/src/main/java/org/tkit/onecx/permission/bff/rs/mappers/WorkspaceMapper.java +++ b/src/main/java/org/tkit/onecx/permission/bff/rs/mappers/WorkspaceMapper.java @@ -1,5 +1,6 @@ package org.tkit.onecx.permission.bff.rs.mappers; +import java.util.ArrayList; import java.util.List; import org.mapstruct.Mapper; @@ -39,7 +40,11 @@ default List map(WorkspaceLoad load) { default WorkspaceDetailsDTO map(List workspaceRoles, ProductsLoadResult productsLoadResult) { WorkspaceDetailsDTO workspaceDetailsDTO = new WorkspaceDetailsDTO(); workspaceDetailsDTO.setWorkspaceRoles(workspaceRoles); - workspaceDetailsDTO.setProducts(map(productsLoadResult.getStream())); + if (productsLoadResult.getStream() != null) { + workspaceDetailsDTO.setProducts(map(productsLoadResult.getStream())); + } else { + workspaceDetailsDTO.setProducts(new ArrayList<>()); + } return workspaceDetailsDTO; } diff --git a/src/main/openapi/openapi-bff.yaml b/src/main/openapi/openapi-bff.yaml index b732a2c..16160f6 100644 --- a/src/main/openapi/openapi-bff.yaml +++ b/src/main/openapi/openapi-bff.yaml @@ -825,6 +825,8 @@ components: properties: productName: type: string + displayName: + type: string mfe: type: array items: diff --git a/src/test/java/org/tkit/onecx/permission/rs/WorkspaceRestControllerTest.java b/src/test/java/org/tkit/onecx/permission/rs/WorkspaceRestControllerTest.java index faef559..484707f 100644 --- a/src/test/java/org/tkit/onecx/permission/rs/WorkspaceRestControllerTest.java +++ b/src/test/java/org/tkit/onecx/permission/rs/WorkspaceRestControllerTest.java @@ -20,10 +20,7 @@ import org.mockserver.model.MediaType; import org.tkit.onecx.permission.bff.rs.controllers.WorkspaceRestController; -import gen.org.tkit.onecx.permission.bff.rs.internal.model.ProductDTO; -import gen.org.tkit.onecx.permission.bff.rs.internal.model.WorkspaceDetailsDTO; -import gen.org.tkit.onecx.permission.bff.rs.internal.model.WorkspacePageResultDTO; -import gen.org.tkit.onecx.permission.bff.rs.internal.model.WorkspaceSearchCriteriaDTO; +import gen.org.tkit.onecx.permission.bff.rs.internal.model.*; import gen.org.tkit.onecx.permission.client.model.*; import gen.org.tkit.onecx.permission.client.model.Product; import gen.org.tkit.onecx.product.store.client.model.*; @@ -220,7 +217,7 @@ void getDetailsByWorkspaceNameTest() { } @Test - void getDetailsByWorkspaceNameMissingRolesTest() { + void getDetailsByWorkspaceNameMissingRolesAndMissingProductNamesTest() { String workspaceName = "test-workspace"; Workspace workspace = new Workspace(); @@ -234,12 +231,6 @@ void getDetailsByWorkspaceNameMissingRolesTest() { .withBody(JsonBody.json(workspace))); List productsOfWorkspace = new ArrayList<>(); - Product product1 = new Product(); - product1.productName("product1"); - Product product2 = new Product(); - product2.productName("product2"); - productsOfWorkspace.add(product1); - productsOfWorkspace.add(product2); WorkspaceLoad loadResponse = new WorkspaceLoad(); loadResponse.setName(workspaceName); @@ -251,44 +242,6 @@ void getDetailsByWorkspaceNameMissingRolesTest() { .respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode()) .withBody(JsonBody.json(loadResponse))); - List productNames = List.of("product1", "product2"); - ProductItemLoadSearchCriteria criteria = new ProductItemLoadSearchCriteria(); - criteria.setProductNames(productNames); - - ProductsLoadResult result = new ProductsLoadResult(); - - ProductsAbstract productsAbstract1 = new ProductsAbstract(); - productsAbstract1.setName("product1"); - MicrofrontendAbstract mfe1 = new MicrofrontendAbstract(); - mfe1.appId("mfe1").appName("mfe1"); - MicroserviceAbstract ms1 = new MicroserviceAbstract(); - ms1.appId("ms1").appName("ms1"); - productsAbstract1.setMicrofrontends(List.of(mfe1)); - productsAbstract1.setMicroservices(List.of(ms1)); - - ProductsAbstract productsAbstract2 = new ProductsAbstract(); - productsAbstract2.setName("product2"); - MicrofrontendAbstract mfe2 = new MicrofrontendAbstract(); - mfe2.appId("mfe2").appName("mfe2"); - MicroserviceAbstract ms2 = new MicroserviceAbstract(); - ms2.appId("ms2").appName("ms2"); - productsAbstract2.setMicrofrontends(List.of(mfe2)); - productsAbstract2.setMicroservices(List.of(ms2)); - - result.setStream(List.of(productsAbstract1, productsAbstract2)); - result.setTotalElements(2L); - result.setNumber(0); - result.setSize(2); - result.setTotalPages(1L); - - // create mock rest endpoint - mockServerClient - .when(request().withPath("/v1/products/load").withMethod(HttpMethod.POST) - .withBody(JsonBody.json(criteria))) - .withId("MOCKID3") - .respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode()) - .withBody(JsonBody.json(result))); - var output = given() .when() .auth().oauth2(keycloakClient.getAccessToken(ADMIN)) @@ -300,18 +253,33 @@ void getDetailsByWorkspaceNameMissingRolesTest() { .extract().as(WorkspaceDetailsDTO.class); Assertions.assertNotNull(output); - Assertions.assertEquals(2, output.getProducts().size()); + Assertions.assertTrue(output.getProducts().isEmpty()); Assertions.assertTrue(output.getWorkspaceRoles().isEmpty()); - Assertions.assertEquals(1, output.getProducts().get(0).getMfe().size()); - Assertions.assertEquals(1, output.getProducts().get(0).getMs().size()); - Assertions.assertNotNull(output.getProducts().get(0).getMfe().get(0).getAppId()); - - Assertions.assertEquals(1, output.getProducts().get(1).getMfe().size()); - Assertions.assertEquals(1, output.getProducts().get(1).getMs().size()); - Assertions.assertNotNull(output.getProducts().get(1).getMs().get(0).getAppId()); mockServerClient.clear(MOCKID); mockServerClient.clear("MOCKID2"); - mockServerClient.clear("MOCKID3"); + } + + @Test + void getDetailsByInvalidWorkspaceNameTest() { + + String workspaceName = "test-workspace"; + // create mock rest endpoint + mockServerClient + .when(request().withPath("/v1/workspaces/" + workspaceName).withMethod(HttpMethod.GET)) + .withId(MOCKID) + .respond(httpRequest -> response().withStatusCode(Response.Status.NOT_FOUND.getStatusCode())); + + given() + .when() + .auth().oauth2(keycloakClient.getAccessToken(ADMIN)) + .header(APM_HEADER_PARAM, ADMIN) + .pathParam("workspaceName", workspaceName) + .get("/{workspaceName}/details") + .then() + .statusCode(Response.Status.NOT_FOUND.getStatusCode()); + + mockServerClient.clear(MOCKID); + } }