Search in sources :

Example 36 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class NamingPushRequestHandlerTest method testRequestReply.

@Test
public void testRequestReply() {
    // given
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingPushRequestHandler handler = new NamingPushRequestHandler(holder);
    ServiceInfo info = new ServiceInfo("name", "cluster1");
    Request req = NotifySubscriberRequest.buildNotifySubscriberRequest(info);
    // when
    Response response = handler.requestReply(req);
    // then
    Assert.assertTrue(response instanceof NotifySubscriberResponse);
    verify(holder, times(1)).processServiceInfo(info);
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Response(com.alibaba.nacos.api.remote.response.Response) NotifySubscriberResponse(com.alibaba.nacos.api.naming.remote.response.NotifySubscriberResponse) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) NotifySubscriberResponse(com.alibaba.nacos.api.naming.remote.response.NotifySubscriberResponse) NotifySubscriberRequest(com.alibaba.nacos.api.naming.remote.request.NotifySubscriberRequest) Request(com.alibaba.nacos.api.remote.request.Request) Test(org.junit.Test)

Example 37 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class NamingHttpClientProxyTest method testQueryInstancesOfService.

@Test
public void testQueryInstancesOfService() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> a = new HttpRestResult<Object>();
    a.setData("");
    a.setCode(200);
    when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenReturn(a);
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    Properties props = new Properties();
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, props, holder);
    final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
    nacosRestTemplateField.setAccessible(true);
    nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
    String serviceName = "service1";
    String groupName = "group1";
    String clusters = "cluster1";
    // when
    ServiceInfo serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusters, 0, false);
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(any(), any(), any(), any(), eq(HttpMethod.GET), any());
    Assert.assertEquals(groupName + "@@" + serviceName, serviceInfo.getName());
    Assert.assertEquals(clusters, serviceInfo.getClusters());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 38 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class NamingHttpClientProxyTest method testSubscribe.

@Test
public void testSubscribe() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> a = new HttpRestResult<Object>();
    a.setData("");
    a.setCode(200);
    when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenReturn(a);
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    Properties props = new Properties();
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, props, holder);
    final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
    nacosRestTemplateField.setAccessible(true);
    nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
    String groupName = "group1";
    String serviceName = "serviceName";
    String clusters = "clusters";
    // when
    ServiceInfo serviceInfo = clientProxy.subscribe(serviceName, groupName, clusters);
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(endsWith("/instance/list"), any(), any(), any(), eq(HttpMethod.GET), any());
    Assert.assertEquals(groupName + "@@" + serviceName, serviceInfo.getName());
    Assert.assertEquals(clusters, serviceInfo.getClusters());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 39 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class NacosNamingServiceTest method testSelectOneHealthyInstance3.

@Test
public void testSelectOneHealthyInstance3() throws NacosException {
    // given
    Instance healthyInstance = new Instance();
    healthyInstance.setIp("1.1.1.1");
    healthyInstance.setPort(1000);
    List<Instance> hosts = new ArrayList<>();
    hosts.add(healthyInstance);
    ServiceInfo infoWithHealthyInstance = new ServiceInfo();
    infoWithHealthyInstance.setHosts(hosts);
    when(proxy.queryInstancesOfService(anyString(), anyString(), anyString(), anyInt(), anyBoolean())).thenReturn(infoWithHealthyInstance);
    String serviceName = "service1";
    // when
    client.selectOneHealthyInstance(serviceName, false);
    // then
    verify(proxy, times(1)).queryInstancesOfService(serviceName, Constants.DEFAULT_GROUP, "", 0, false);
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 40 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class NacosNamingServiceTest method testSelectOneHealthyInstance6.

@Test
public void testSelectOneHealthyInstance6() throws NacosException {
    // given
    Instance healthyInstance = new Instance();
    healthyInstance.setIp("1.1.1.1");
    healthyInstance.setPort(1000);
    List<Instance> hosts = new ArrayList<>();
    hosts.add(healthyInstance);
    ServiceInfo infoWithHealthyInstance = new ServiceInfo();
    infoWithHealthyInstance.setHosts(hosts);
    when(proxy.subscribe(anyString(), anyString(), anyString())).thenReturn(infoWithHealthyInstance);
    String serviceName = "service1";
    String groupName = "group1";
    List<String> clusterList = Arrays.asList("cluster1", "cluster2");
    // when
    client.selectOneHealthyInstance(serviceName, groupName, clusterList);
    // then
    verify(proxy, times(1)).subscribe(serviceName, groupName, "cluster1,cluster2");
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)74 Test (org.junit.Test)44 Instance (com.alibaba.nacos.api.naming.pojo.Instance)29 ArrayList (java.util.ArrayList)17 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)12 Properties (java.util.Properties)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 NacosException (com.alibaba.nacos.api.exception.NacosException)8 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)8 Before (org.junit.Before)8 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)7 Field (java.lang.reflect.Field)6 HashSet (java.util.HashSet)5 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)4 SubscribeServiceResponse (com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse)4 PushDataWrapper (com.alibaba.nacos.naming.push.v2.PushDataWrapper)4 HashMap (java.util.HashMap)4 QueryServiceResponse (com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse)3 Secured (com.alibaba.nacos.auth.annotation.Secured)3 InstancesChangeNotifier (com.alibaba.nacos.client.naming.event.InstancesChangeNotifier)3