Skip to content

Commit

Permalink
feat: fixed null-pointer caused by changed ws api (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordenReuter authored Mar 1, 2024
1 parent 703fff5 commit c4ff98d
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tkit.onecx.permission.bff.rs.controllers;

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

import jakarta.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -59,10 +60,12 @@ public Response getDetailsByWorkspaceName(String workspaceName) {
try (Response response = workspaceClient.getWorkspaceByName(workspaceName)) {
WorkspaceDetailsDTO workspaceDetails;
List<String> productNames;
List<String> workspaceRoles;
List<String> workspaceRoles = new ArrayList<>();
ProductsLoadResult productsLoadResult;
var workspaceResponse = response.readEntity(Workspace.class);
workspaceRoles = workspaceResponse.getWorkspaceRoles().stream().toList();
if (workspaceResponse.getWorkspaceRoles() != null) {
workspaceRoles = workspaceResponse.getWorkspaceRoles().stream().toList();
}

//get products of workspace
try (Response wsProductsResponse = workspaceClient.loadWorkspaceByName(workspaceName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,99 @@ void getDetailsByWorkspaceNameTest() {
mockServerClient.clear("MOCKID3");
}

@Test
void getDetailsByWorkspaceNameMissingRolesTest() {

String workspaceName = "test-workspace";
Workspace workspace = new Workspace();
workspace.name("test-workspace");

// create mock rest endpoint
mockServerClient
.when(request().withPath("/v1/workspaces/" + workspaceName).withMethod(HttpMethod.GET))
.withId(MOCKID)
.respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode())
.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);
loadResponse.setProducts(productsOfWorkspace);
// create mock rest endpoint
mockServerClient
.when(request().withPath("/v1/workspaces/" + workspaceName + "/load").withMethod(HttpMethod.GET))
.withId("MOCKID2")
.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))
.header(APM_HEADER_PARAM, ADMIN)
.pathParam("workspaceName", workspaceName)
.get("/{workspaceName}/details")
.then()
.statusCode(Response.Status.OK.getStatusCode())
.extract().as(WorkspaceDetailsDTO.class);

Assertions.assertNotNull(output);
Assertions.assertEquals(2, output.getProducts().size());
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");
}
}

0 comments on commit c4ff98d

Please sign in to comment.