### Question:
InstanceRetrievalService { public Applications getAllInstancesFromDiscovery(boolean delta) { Pair<String, Pair<String, String>> requestInfo = constructServiceInfoQueryRequest(null, delta); ResponseEntity<String> response = queryDiscoveryForInstances(requestInfo); return extractApplications(requestInfo, response); } @Autowired  InstanceRetrievalService(DiscoveryConfigProperties discoveryConfigProperties,
                                    RestTemplate restTemplate); InstanceInfo getInstanceInfo(@NotBlank(message = "Service Id must be supplied") String serviceId); Applications getAllInstancesFromDiscovery(boolean delta);  }### Answer:
@Test public void testGetAllInstancesFromDiscovery_whenResponseCodeIsNotSuccess() { when( restTemplate.exchange( discoveryServiceAllAppsUrl, HttpMethod.GET, getHttpEntity(), String.class )).thenReturn(new ResponseEntity<>(null, HttpStatus.FORBIDDEN)); Applications actualApplications = instanceRetrievalService.getAllInstancesFromDiscovery(false); assertNull(actualApplications); }
@Test public void testGetAllInstancesFromDiscovery_whenResponseCodeIsSuccessWithUnParsedJsonText() { mockRetrieveApplicationService( discoveryServiceAllAppsUrl, "UNPARSED_JSON" ); Applications actualApplications = instanceRetrievalService.getAllInstancesFromDiscovery(false); assertNull(actualApplications); }
@Test public void testGetAllInstancesFromDiscovery_whenNeedApplicationsWithoutFilter() throws JsonProcessingException { Map<String, InstanceInfo> instanceInfoMap = createInstances(); Applications expectedApplications = new Applications(); instanceInfoMap.forEach((key, value) -> expectedApplications.addApplication(new Application(value.getAppName(), Collections.singletonList(value)))); ObjectMapper mapper = new ObjectMapper(); String bodyAll = mapper.writeValueAsString(new ApplicationsWrapper(expectedApplications)); mockRetrieveApplicationService(discoveryServiceAllAppsUrl, bodyAll); Applications actualApplications = instanceRetrievalService.getAllInstancesFromDiscovery(false); assertEquals(expectedApplications.size(), actualApplications.size()); List<Application> actualApplicationList = new ArrayList<>(actualApplications.getRegisteredApplications()); expectedApplications .getRegisteredApplications() .forEach(expectedApplication -> assertThat(actualApplicationList, hasItem(hasProperty("name", equalTo(expectedApplication.getName())))) ); }
@Test public void testGetAllInstancesFromDiscovery_whenNeedApplicationsWithDeltaFilter() throws JsonProcessingException { String discoveryServiceAppsUrl = discoveryConfigProperties.getLocations() + APPS_ENDPOINT + DELTA_ENDPOINT; Map<String, InstanceInfo> instanceInfoMap = createInstances(); Applications expectedApplications = new Applications(); instanceInfoMap.forEach((key, value) -> expectedApplications.addApplication(new Application(value.getAppName(), Collections.singletonList(value)))); ObjectMapper mapper = new ObjectMapper(); String bodyAll = mapper.writeValueAsString(new ApplicationsWrapper(expectedApplications)); mockRetrieveApplicationService(discoveryServiceAppsUrl, bodyAll); Applications actualApplications = instanceRetrievalService.getAllInstancesFromDiscovery(true); assertEquals(expectedApplications.size(), actualApplications.size()); List<Application> actualApplicationList = new ArrayList<>(actualApplications.getRegisteredApplications()); expectedApplications .getRegisteredApplications() .forEach(expectedApplication -> assertThat(actualApplicationList, hasItem(hasProperty("name", equalTo(expectedApplication.getName())))) ); }