use of com.alibaba.cloud.nacos.NacosDiscoveryProperties in project spring-cloud-alibaba by alibaba.
the class NacosServerListTests method testEmptyInstancesReturnsEmptyList.
@Test
@SuppressWarnings("unchecked")
public void testEmptyInstancesReturnsEmptyList() throws Exception {
NacosDiscoveryProperties nacosDiscoveryProperties = mock(NacosDiscoveryProperties.class);
NamingService namingService = mock(NamingService.class);
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
when(namingService.selectInstances(anyString(), eq("DEFAULT"), eq(true))).thenReturn(null);
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
List<NacosServer> servers = serverList.getInitialListOfServers();
assertThat(servers).isEmpty();
}
use of com.alibaba.cloud.nacos.NacosDiscoveryProperties in project spring-cloud-alibaba by alibaba.
the class NacosServerListTests method testGetServersWithInstanceStatus.
@Test
@SuppressWarnings("unchecked")
public void testGetServersWithInstanceStatus() throws Exception {
ArrayList<Instance> instances = new ArrayList<>();
HashMap<String, String> map1 = new HashMap<>();
map1.put("instanceNum", "1");
HashMap<String, String> map2 = new HashMap<>();
map2.put("instanceNum", "2");
instances.add(NacosMockTest.serviceInstance("test-service", false, map1));
instances.add(NacosMockTest.serviceInstance("test-service", true, map2));
NacosDiscoveryProperties nacosDiscoveryProperties = mock(NacosDiscoveryProperties.class);
NamingService namingService = mock(NamingService.class);
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
when(namingService.selectInstances(eq("test-service"), eq("DEFAULT"), eq(true))).thenReturn(instances.stream().filter(Instance::isHealthy).collect(Collectors.toList()));
IClientConfig clientConfig = mock(IClientConfig.class);
when(clientConfig.getClientName()).thenReturn("test-service");
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
serverList.initWithNiwsConfig(clientConfig);
List<NacosServer> servers = serverList.getInitialListOfServers();
assertThat(servers).hasSize(1);
NacosServer nacosServer = servers.get(0);
assertThat(nacosServer.getMetaInfo().getInstanceId()).isEqualTo(instances.get(1).getInstanceId());
assertThat(nacosServer.getMetadata()).isEqualTo(map2);
assertThat(nacosServer.getInstance().isHealthy()).isEqualTo(true);
assertThat(nacosServer.getInstance().getServiceName()).isEqualTo("test-service");
assertThat(nacosServer.getInstance().getMetadata().get("instanceNum")).isEqualTo("2");
}
use of com.alibaba.cloud.nacos.NacosDiscoveryProperties in project spring-cloud-alibaba by alibaba.
the class NacosServerListTests method testGetServers.
@Test
@SuppressWarnings("unchecked")
public void testGetServers() throws Exception {
ArrayList<Instance> instances = new ArrayList<>();
instances.add(NacosMockTest.serviceInstance("test-service", false, Collections.emptyMap()));
NacosDiscoveryProperties nacosDiscoveryProperties = mock(NacosDiscoveryProperties.class);
NamingService namingService = mock(NamingService.class);
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
when(namingService.selectInstances(eq("test-service"), eq("DEFAULT"), eq(true))).thenReturn(instances);
IClientConfig clientConfig = mock(IClientConfig.class);
when(clientConfig.getClientName()).thenReturn("test-service");
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
serverList.initWithNiwsConfig(clientConfig);
List<NacosServer> servers = serverList.getInitialListOfServers();
assertThat(servers).hasSize(1);
servers = serverList.getUpdatedListOfServers();
assertThat(servers).hasSize(1);
}
use of com.alibaba.cloud.nacos.NacosDiscoveryProperties in project spring-cloud-alibaba by alibaba.
the class NacosServiceDiscoveryTest method testGetInstances.
@Test
public void testGetInstances() throws NacosException {
ArrayList<Instance> instances = new ArrayList<>();
HashMap<String, String> map = new HashMap<>();
map.put("test-key", "test-value");
map.put("secure", "true");
instances.add(serviceInstance(serviceName, true, host, port, map));
NacosDiscoveryProperties nacosDiscoveryProperties = mock(NacosDiscoveryProperties.class);
NacosServiceManager nacosServiceManager = mock(NacosServiceManager.class);
NamingService namingService = mock(NamingService.class);
when(nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties())).thenReturn(namingService);
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
when(namingService.selectInstances(eq(serviceName), eq("DEFAULT"), eq(true))).thenReturn(instances);
NacosServiceDiscovery serviceDiscovery = new NacosServiceDiscovery(nacosDiscoveryProperties, nacosServiceManager);
List<ServiceInstance> serviceInstances = serviceDiscovery.getInstances(serviceName);
assertThat(serviceInstances.size()).isEqualTo(1);
ServiceInstance serviceInstance = serviceInstances.get(0);
assertThat(serviceInstance.getServiceId()).isEqualTo(serviceName);
assertThat(serviceInstance.getHost()).isEqualTo(host);
assertThat(serviceInstance.getPort()).isEqualTo(port);
assertThat(serviceInstance.isSecure()).isEqualTo(true);
assertThat(serviceInstance.getUri().toString()).isEqualTo(getUri(serviceInstance));
assertThat(serviceInstance.getMetadata().get("test-key")).isEqualTo("test-value");
}
Aggregations