Search in sources :

Example 6 with NacosDiscoveryProperties

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();
}
Also used : NamingService(com.alibaba.nacos.api.naming.NamingService) NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties) NacosMockTest(com.alibaba.cloud.nacos.test.NacosMockTest) Test(org.junit.Test)

Example 7 with NacosDiscoveryProperties

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");
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) HashMap(java.util.HashMap) NamingService(com.alibaba.nacos.api.naming.NamingService) ArrayList(java.util.ArrayList) IClientConfig(com.netflix.client.config.IClientConfig) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties) NacosMockTest(com.alibaba.cloud.nacos.test.NacosMockTest) Test(org.junit.Test)

Example 8 with NacosDiscoveryProperties

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);
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) NamingService(com.alibaba.nacos.api.naming.NamingService) ArrayList(java.util.ArrayList) IClientConfig(com.netflix.client.config.IClientConfig) NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties) NacosMockTest(com.alibaba.cloud.nacos.test.NacosMockTest) Test(org.junit.Test)

Example 9 with NacosDiscoveryProperties

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");
}
Also used : NacosServiceManager(com.alibaba.cloud.nacos.NacosServiceManager) NacosMockTest.serviceInstance(com.alibaba.cloud.nacos.test.NacosMockTest.serviceInstance) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) HashMap(java.util.HashMap) NamingService(com.alibaba.nacos.api.naming.NamingService) ArrayList(java.util.ArrayList) ServiceInstance(org.springframework.cloud.client.ServiceInstance) NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties) Test(org.junit.jupiter.api.Test)

Aggregations

NacosDiscoveryProperties (com.alibaba.cloud.nacos.NacosDiscoveryProperties)9 NamingService (com.alibaba.nacos.api.naming.NamingService)6 Instance (com.alibaba.nacos.api.naming.pojo.Instance)5 NacosMockTest (com.alibaba.cloud.nacos.test.NacosMockTest)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 IClientConfig (com.netflix.client.config.IClientConfig)3 NacosServiceManager (com.alibaba.cloud.nacos.NacosServiceManager)2 Test (org.junit.jupiter.api.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 ServiceInstance (org.springframework.cloud.client.ServiceInstance)2 Bean (org.springframework.context.annotation.Bean)2 NacosMockTest.serviceInstance (com.alibaba.cloud.nacos.test.NacosMockTest.serviceInstance)1 ListView (com.alibaba.nacos.api.naming.pojo.ListView)1 SpringApplicationContext (com.hummer.core.SpringApplicationContext)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1