DefaultDiscoveryService implements DiscoveryService { @Override public List<ServiceDTO> getServiceInstances(String serviceId) { Application application = eurekaClient.getApplication(serviceId); if (application == null || CollectionUtils.isEmpty(application.getInstances())) { Tracer.logEvent("Apollo.Discovery.NotFound", serviceId); return Collections.emptyList(); } return application.getInstances().stream().map(instanceInfoToServiceDTOFunc) .collect(Collectors.toList()); } DefaultDiscoveryService(final EurekaClient eurekaClient); @Override List<ServiceDTO> getServiceInstances(String serviceId);  }
@Test public void testGetServiceInstancesWithNullInstances() { when(eurekaClient.getApplication(someServiceId)).thenReturn(null); assertTrue(defaultDiscoveryService.getServiceInstances(someServiceId).isEmpty()); }
@Test public void testGetServiceInstancesWithEmptyInstances() { when(eurekaClient.getApplication(someServiceId)).thenReturn(someApplication); when(someApplication.getInstances()).thenReturn(new ArrayList<>()); assertTrue(defaultDiscoveryService.getServiceInstances(someServiceId).isEmpty()); }
@Test public void testGetServiceInstances() throws URISyntaxException { String someUri = "http: String someInstanceId = "someInstanceId"; InstanceInfo someServiceInstance = mockServiceInstance(someServiceId, someInstanceId, someUri); String anotherUri = "http: String anotherInstanceId = "anotherInstanceId"; InstanceInfo anotherServiceInstance = mockServiceInstance(someServiceId, anotherInstanceId, anotherUri); when(eurekaClient.getApplication(someServiceId)).thenReturn(someApplication); when(someApplication.getInstances()) .thenReturn(Lists.newArrayList(someServiceInstance, anotherServiceInstance)); List<ServiceDTO> serviceDTOList = defaultDiscoveryService.getServiceInstances(someServiceId); assertEquals(2, serviceDTOList.size()); check(someServiceInstance, serviceDTOList.get(0)); check(anotherServiceInstance, serviceDTOList.get(1)); }