|
@@ -6,7 +6,9 @@ import static org.mockito.Mockito.when;
|
|
|
|
|
|
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
|
|
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Lists;
|
|
-import java.net.URI;
|
|
|
|
|
|
+import com.netflix.appinfo.InstanceInfo;
|
|
|
|
+import com.netflix.discovery.EurekaClient;
|
|
|
|
+import com.netflix.discovery.shared.Application;
|
|
import java.net.URISyntaxException;
|
|
import java.net.URISyntaxException;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -15,14 +17,15 @@ import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.junit.runner.RunWith;
|
|
import org.mockito.Mock;
|
|
import org.mockito.Mock;
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
-import org.springframework.cloud.client.ServiceInstance;
|
|
|
|
-import org.springframework.cloud.client.discovery.DiscoveryClient;
|
|
|
|
|
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
public class DefaultDiscoveryServiceTest {
|
|
public class DefaultDiscoveryServiceTest {
|
|
|
|
|
|
@Mock
|
|
@Mock
|
|
- private DiscoveryClient discoveryClient;
|
|
|
|
|
|
+ private EurekaClient eurekaClient;
|
|
|
|
+
|
|
|
|
+ @Mock
|
|
|
|
+ private Application someApplication;
|
|
|
|
|
|
private DefaultDiscoveryService defaultDiscoveryService;
|
|
private DefaultDiscoveryService defaultDiscoveryService;
|
|
|
|
|
|
@@ -30,68 +33,60 @@ public class DefaultDiscoveryServiceTest {
|
|
|
|
|
|
@Before
|
|
@Before
|
|
public void setUp() throws Exception {
|
|
public void setUp() throws Exception {
|
|
- defaultDiscoveryService = new DefaultDiscoveryService(discoveryClient);
|
|
|
|
|
|
+ defaultDiscoveryService = new DefaultDiscoveryService(eurekaClient);
|
|
|
|
|
|
someServiceId = "someServiceId";
|
|
someServiceId = "someServiceId";
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void testGetServiceInstancesWithNullInstances() {
|
|
public void testGetServiceInstancesWithNullInstances() {
|
|
- when(discoveryClient.getInstances(someServiceId)).thenReturn(null);
|
|
|
|
|
|
+ when(eurekaClient.getApplication(someServiceId)).thenReturn(null);
|
|
|
|
|
|
assertTrue(defaultDiscoveryService.getServiceInstances(someServiceId).isEmpty());
|
|
assertTrue(defaultDiscoveryService.getServiceInstances(someServiceId).isEmpty());
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void testGetServiceInstancesWithEmptyInstances() {
|
|
public void testGetServiceInstancesWithEmptyInstances() {
|
|
- when(discoveryClient.getInstances(someServiceId)).thenReturn(new ArrayList<>());
|
|
|
|
|
|
+ when(eurekaClient.getApplication(someServiceId)).thenReturn(someApplication);
|
|
|
|
+ when(someApplication.getInstances()).thenReturn(new ArrayList<>());
|
|
|
|
|
|
assertTrue(defaultDiscoveryService.getServiceInstances(someServiceId).isEmpty());
|
|
assertTrue(defaultDiscoveryService.getServiceInstances(someServiceId).isEmpty());
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void testGetServiceInstances() throws URISyntaxException {
|
|
public void testGetServiceInstances() throws URISyntaxException {
|
|
- String someHost = "1.2.3.4";
|
|
|
|
- int somePort = 8080;
|
|
|
|
- String someUri = String.format("http://%s:%s/some-path/", someHost, somePort);
|
|
|
|
- ServiceInstance someServiceInstance = mockServiceInstance(someServiceId, someHost, somePort,
|
|
|
|
|
|
+ String someUri = "http://1.2.3.4:8080/some-path/";
|
|
|
|
+ String someInstanceId = "someInstanceId";
|
|
|
|
+ InstanceInfo someServiceInstance = mockServiceInstance(someServiceId, someInstanceId,
|
|
someUri);
|
|
someUri);
|
|
|
|
|
|
- String anotherHost = "2.3.4.5";
|
|
|
|
- int anotherPort = 9090;
|
|
|
|
- String anotherUri = String.format("http://%s:%s/some-path-with-no-slash", anotherHost, anotherPort);
|
|
|
|
- ServiceInstance anotherServiceInstance = mockServiceInstance(someServiceId, anotherHost, anotherPort,
|
|
|
|
|
|
+ String anotherUri = "http://2.3.4.5:9090/anotherPath";
|
|
|
|
+ String anotherInstanceId = "anotherInstanceId";
|
|
|
|
+ InstanceInfo anotherServiceInstance = mockServiceInstance(someServiceId, anotherInstanceId,
|
|
anotherUri);
|
|
anotherUri);
|
|
|
|
|
|
- when(discoveryClient.getInstances(someServiceId))
|
|
|
|
|
|
+ when(eurekaClient.getApplication(someServiceId)).thenReturn(someApplication);
|
|
|
|
+ when(someApplication.getInstances())
|
|
.thenReturn(Lists.newArrayList(someServiceInstance, anotherServiceInstance));
|
|
.thenReturn(Lists.newArrayList(someServiceInstance, anotherServiceInstance));
|
|
|
|
|
|
List<ServiceDTO> serviceDTOList = defaultDiscoveryService.getServiceInstances(someServiceId);
|
|
List<ServiceDTO> serviceDTOList = defaultDiscoveryService.getServiceInstances(someServiceId);
|
|
|
|
|
|
assertEquals(2, serviceDTOList.size());
|
|
assertEquals(2, serviceDTOList.size());
|
|
- check(someServiceInstance, serviceDTOList.get(0), false);
|
|
|
|
- check(anotherServiceInstance, serviceDTOList.get(1), true);
|
|
|
|
|
|
+ check(someServiceInstance, serviceDTOList.get(0));
|
|
|
|
+ check(anotherServiceInstance, serviceDTOList.get(1));
|
|
}
|
|
}
|
|
|
|
|
|
- private void check(ServiceInstance serviceInstance, ServiceDTO serviceDTO, boolean appendSlashToUri) {
|
|
|
|
- assertEquals(serviceInstance.getServiceId(), serviceDTO.getAppName());
|
|
|
|
- assertEquals(serviceDTO.getInstanceId(), String
|
|
|
|
- .format("%s:%s:%s", serviceInstance.getHost(), serviceInstance.getServiceId(),
|
|
|
|
- serviceInstance.getPort()));
|
|
|
|
- if (appendSlashToUri) {
|
|
|
|
- assertEquals(serviceInstance.getUri().toString() + "/", serviceDTO.getHomepageUrl());
|
|
|
|
- } else {
|
|
|
|
- assertEquals(serviceInstance.getUri().toString(), serviceDTO.getHomepageUrl());
|
|
|
|
- }
|
|
|
|
|
|
+ private void check(InstanceInfo serviceInstance, ServiceDTO serviceDTO) {
|
|
|
|
+ assertEquals(serviceInstance.getAppName(), serviceDTO.getAppName());
|
|
|
|
+ assertEquals(serviceInstance.getInstanceId(), serviceDTO.getInstanceId());
|
|
|
|
+ assertEquals(serviceInstance.getHomePageUrl(), serviceDTO.getHomepageUrl());
|
|
}
|
|
}
|
|
|
|
|
|
- private ServiceInstance mockServiceInstance(String serviceId, String host, int port, String uri)
|
|
|
|
- throws URISyntaxException {
|
|
|
|
- ServiceInstance serviceInstance = mock(ServiceInstance.class);
|
|
|
|
- when(serviceInstance.getServiceId()).thenReturn(serviceId);
|
|
|
|
- when(serviceInstance.getHost()).thenReturn(host);
|
|
|
|
- when(serviceInstance.getPort()).thenReturn(port);
|
|
|
|
- when(serviceInstance.getUri()).thenReturn(new URI(uri));
|
|
|
|
|
|
+ private InstanceInfo mockServiceInstance(String serviceId, String instanceId, String homePageUrl) {
|
|
|
|
+ InstanceInfo serviceInstance = mock(InstanceInfo.class);
|
|
|
|
+ when(serviceInstance.getAppName()).thenReturn(serviceId);
|
|
|
|
+ when(serviceInstance.getInstanceId()).thenReturn(instanceId);
|
|
|
|
+ when(serviceInstance.getHomePageUrl()).thenReturn(homePageUrl);
|
|
|
|
|
|
return serviceInstance;
|
|
return serviceInstance;
|
|
}
|
|
}
|