### Question:
InstanceRetrievalService { public InstanceInfo getInstanceInfo(@NotBlank(message = "Service Id must be supplied") String serviceId) { if (serviceId.equalsIgnoreCase(UNKNOWN)) { return null; } InstanceInfo instanceInfo = null; try { Pair<String, Pair<String, String>> requestInfo = constructServiceInfoQueryRequest(serviceId, false); ResponseEntity<String> response = queryDiscoveryForInstances(requestInfo); if (response.getStatusCode().is2xxSuccessful()) { instanceInfo = extractSingleInstanceFromApplication(serviceId, requestInfo.getLeft(), response); } } catch (Exception e) { String msg = "An error occurred when trying to get instance info for: " + serviceId; log.debug(msg, e.getMessage()); throw new InstanceInitializationException(msg); } return instanceInfo; } @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 testGetInstanceInfo_whenServiceIdIsUNKNOWN() { InstanceInfo instanceInfo = instanceRetrievalService.getInstanceInfo(UNKNOWN); assertNull(instanceInfo); }
@Test public void testGetInstanceInfo_whenResponseCodeIsNotSuccess() { when( restTemplate.exchange( discoveryServiceAllAppsUrl + CoreService.API_CATALOG.getServiceId(), HttpMethod.GET, getHttpEntity(), String.class )).thenReturn(new ResponseEntity<>(null, HttpStatus.FORBIDDEN)); InstanceInfo instanceInfo = instanceRetrievalService.getInstanceInfo(CoreService.API_CATALOG.getServiceId()); assertNull(instanceInfo); }
@Test public void testGetInstanceInfo_whenResponseHasNullBody() { when( restTemplate.exchange( discoveryServiceAllAppsUrl + CoreService.API_CATALOG.getServiceId(), HttpMethod.GET, getHttpEntity(), String.class )).thenReturn(new ResponseEntity<>(null, HttpStatus.OK)); InstanceInfo instanceInfo = instanceRetrievalService.getInstanceInfo(CoreService.API_CATALOG.getServiceId()); assertNull(instanceInfo); }
@Test public void testGetInstanceInfo_whenResponseCodeIsSuccessWithUnParsedJsonText() { mockRetrieveApplicationService( discoveryServiceAllAppsUrl + CoreService.API_CATALOG.getServiceId(), "UNPARSED_JSON" ); InstanceInfo instanceInfo = instanceRetrievalService.getInstanceInfo(CoreService.API_CATALOG.getServiceId()); assertNull(instanceInfo); }
@Test(expected = InstanceInitializationException.class) public void testGetInstanceInfo_whenUnexpectedErrorHappened() { when( restTemplate.exchange( discoveryServiceAllAppsUrl + CoreService.API_CATALOG.getServiceId(), HttpMethod.GET, null, String.class )).thenReturn(new ResponseEntity<>("{}", HttpStatus.OK)); instanceRetrievalService.getInstanceInfo(CoreService.API_CATALOG.getServiceId()); }
@Test public void testGetInstanceInfo() throws JsonProcessingException { InstanceInfo expectedInstanceInfo = getStandardInstance( CoreService.API_CATALOG.getServiceId(), InstanceInfo.InstanceStatus.UP ); ObjectMapper mapper = new ObjectMapper(); String bodyCatalog = mapper.writeValueAsString( new ApplicationWrapper(new Application( CoreService.API_CATALOG.getServiceId(), Collections.singletonList(expectedInstanceInfo) ))); mockRetrieveApplicationService( discoveryServiceAllAppsUrl + CoreService.API_CATALOG.getServiceId(), bodyCatalog ); InstanceInfo actualInstanceInfo = instanceRetrievalService.getInstanceInfo(CoreService.API_CATALOG.getServiceId()); assertNotNull(actualInstanceInfo); assertThat(actualInstanceInfo, hasProperty("instanceId", equalTo(expectedInstanceInfo.getInstanceId()))); assertThat(actualInstanceInfo, hasProperty("appName", equalTo(expectedInstanceInfo.getAppName()))); assertThat(actualInstanceInfo, hasProperty("status", equalTo(expectedInstanceInfo.getStatus()))); }