WorkloadDetailsService { public ResponseEntity<String> getWorkloadDetails(String token, UserDetails userDetails) { String api = workloadEndpoint; List<WorkloadAppDetails> workloadAppDetailsList = new ArrayList<>(); workloadAppDetailsList.add(new WorkloadAppDetails(WorkloadAppDetails.APP_NAME_OTHER, WorkloadAppDetails.APP_TAG_OTHER, WorkloadAppDetails.APP_ID_OTHER)); JsonObject response = getApiResponse(api); if (response != null) { JsonArray results = response.getAsJsonArray("items"); for(JsonElement jsonElement: results) { if (jsonElement.getAsJsonObject() != null) { JsonObject metadata = jsonElement.getAsJsonObject().getAsJsonObject("metadata"); JsonObject spec = jsonElement.getAsJsonObject().getAsJsonObject("spec"); WorkloadAppDetails workloadAppDetails = new WorkloadAppDetails(); if (spec != null) { workloadAppDetails.setAppName((spec.get("summary").isJsonNull()?"":spec.get("summary").getAsString())); workloadAppDetails.setAppID((spec.get("id").isJsonNull()?"":spec.get("id").getAsString())); workloadAppDetails.setAppTag((spec.get("id").isJsonNull()?"":spec.get("id").getAsString())); workloadAppDetailsList.add(workloadAppDetails); } } } } return ResponseEntity.status(HttpStatus.OK).body(JSONUtil.getJSON(workloadAppDetailsList)); }  ResponseEntity<String> getWorkloadDetails(String token, UserDetails userDetails); ResponseEntity<String> getWorkloadDetailsByAppName(String appName);  }
@Test public void test_getWorkloadDetails_success() throws Exception { String responseStr = "{\"items\": [{\"spec\": {\"id\": \"aac\",\"summary\": \"app1\"}}]}"; String workloadResponse = "[{\"appName\":\"Other\",\"appTag\":\"Other\",\"appID\":\"oth\"},{\"appName\":\"app1\",\"appTag\":\"aac\",\"appID\":\"aac\"}]"; ReflectionTestUtils.setField(workloadDetailsService, "workloadEndpoint", "http: when(ControllerUtil.getCwmToken()).thenReturn("dG9rZW4="); when(HttpClientBuilder.create()).thenReturn(httpClientBuilder); when(httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)).thenReturn(httpClientBuilder); when(httpClientBuilder.setSSLContext(any())).thenReturn(httpClientBuilder); when(httpClientBuilder.setRedirectStrategy(any())).thenReturn(httpClientBuilder); when(httpClientBuilder.build()).thenReturn(httpClient1); when(httpClient1.execute(any())).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(200); when(httpResponse.getEntity()).thenReturn(mockHttpEntity); InputStream inputStream = new ByteArrayInputStream(responseStr.getBytes()); when(mockHttpEntity.getContent()).thenReturn(inputStream); when(JSONUtil.getJSON(anyList())).thenReturn(workloadResponse); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("[{\"appName\":\"Other\",\"appTag\":\"Other\",\"appID\":\"oth\"},{\"appName\":\"app1\",\"appTag\":\"aac\",\"appID\":\"aac\"}]"); ResponseEntity<String> responseEntityActual = workloadDetailsService.getWorkloadDetails(token, userDetails); assertEquals(responseEntityExpected.getStatusCode(), responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected, responseEntityActual); }
@Test public void test_getWorkloadDetails_failed() throws Exception { String workloadResponse = "[{\"appName\":\"Other\",\"appTag\":\"Other\",\"appID\":\"oth\"}]"; ReflectionTestUtils.setField(workloadDetailsService, "workloadEndpoint", "http: when(ControllerUtil.getCwmToken()).thenReturn("dG9rZW4="); when(HttpClientBuilder.create()).thenReturn(httpClientBuilder); when(httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)).thenReturn(httpClientBuilder); when(httpClientBuilder.setSSLContext(any())).thenReturn(httpClientBuilder); when(httpClientBuilder.setRedirectStrategy(any())).thenReturn(httpClientBuilder); when(httpClientBuilder.build()).thenReturn(httpClient1); when(httpClient1.execute(any())).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(400); when(JSONUtil.getJSON(anyList())).thenReturn(workloadResponse); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("[{\"appName\":\"Other\",\"appTag\":\"Other\",\"appID\":\"oth\"}]"); ResponseEntity<String> responseEntityActual = workloadDetailsService.getWorkloadDetails(token, userDetails); assertEquals(responseEntityExpected.getStatusCode(), responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected, responseEntityActual); }
@Test public void test_getWorkloadDetails_failed_token() throws Exception { String workloadResponse = "[{\"appName\":\"Other\",\"appTag\":\"Other\",\"appID\":\"oth\"}]"; ReflectionTestUtils.setField(workloadDetailsService, "workloadEndpoint", "http: when(ControllerUtil.getCwmToken()).thenReturn(""); when(HttpClientBuilder.create()).thenReturn(httpClientBuilder); when(httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)).thenReturn(httpClientBuilder); when(httpClientBuilder.setSSLContext(any())).thenReturn(httpClientBuilder); when(httpClientBuilder.setRedirectStrategy(any())).thenReturn(httpClientBuilder); when(httpClientBuilder.build()).thenReturn(httpClient1); when(httpClient1.execute(any())).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(400); when(JSONUtil.getJSON(anyList())).thenReturn(workloadResponse); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("[{\"appName\":\"Other\",\"appTag\":\"Other\",\"appID\":\"oth\"}]"); ResponseEntity<String> responseEntityActual = workloadDetailsService.getWorkloadDetails(token, userDetails); assertEquals(responseEntityExpected.getStatusCode(), responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected, responseEntityActual); }