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: fixed case if productNames missing and added display name #12

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,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;

Expand Down Expand Up @@ -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)) {
Expand All @@ -61,7 +67,7 @@ public Response getDetailsByWorkspaceName(String workspaceName) {
WorkspaceDetailsDTO workspaceDetails;
List<String> productNames;
List<String> workspaceRoles = new ArrayList<>();
ProductsLoadResult productsLoadResult;
ProductsLoadResult productsLoadResult = new ProductsLoadResult();
var workspaceResponse = response.readEntity(Workspace.class);
if (workspaceResponse.getWorkspaceRoles() != null) {
workspaceRoles = workspaceResponse.getWorkspaceRoles().stream().toList();
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tkit.onecx.permission.bff.rs.mappers;

import java.util.ArrayList;
import java.util.List;

import org.mapstruct.Mapper;
Expand Down Expand Up @@ -39,7 +40,11 @@ default List<ProductDTO> map(WorkspaceLoad load) {
default WorkspaceDetailsDTO map(List<String> 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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/openapi/openapi-bff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,8 @@ components:
properties:
productName:
type: string
displayName:
type: string
mfe:
type: array
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -220,7 +217,7 @@ void getDetailsByWorkspaceNameTest() {
}

@Test
void getDetailsByWorkspaceNameMissingRolesTest() {
void getDetailsByWorkspaceNameMissingRolesAndMissingProductNamesTest() {

String workspaceName = "test-workspace";
Workspace workspace = new Workspace();
Expand All @@ -234,12 +231,6 @@ void getDetailsByWorkspaceNameMissingRolesTest() {
.withBody(JsonBody.json(workspace)));

List<Product> 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);
Expand All @@ -251,44 +242,6 @@ void getDetailsByWorkspaceNameMissingRolesTest() {
.respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode())
.withBody(JsonBody.json(loadResponse)));

List<String> 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))
Expand All @@ -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);

}
}
Loading