Search in sources :

Example 16 with ServiceInfoHolder

use of com.alibaba.nacos.client.naming.cache.ServiceInfoHolder in project nacos by alibaba.

the class NamingHttpClientProxyTest method testReqApi3.

@Test
public void testReqApi3() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenAnswer(invocationOnMock -> {
        // return url
        HttpRestResult<Object> res = new HttpRestResult<Object>();
        res.setData(invocationOnMock.getArgument(0));
        res.setCode(200);
        return res;
    });
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, new Properties(), holder);
    final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
    nacosRestTemplateField.setAccessible(true);
    nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
    String api = "/api";
    Map<String, String> params = new HashMap<>();
    Map<String, String> body = new HashMap<>();
    String method = HttpMethod.GET;
    List<String> servers = Arrays.asList("127.0.0.1");
    // when
    String res = clientProxy.reqApi(api, params, body, servers, method);
    // then
    Assert.assertEquals("http://127.0.0.1:8848/api", res);
}
Also used : ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) Properties(java.util.Properties) Field(java.lang.reflect.Field) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 17 with ServiceInfoHolder

use of com.alibaba.nacos.client.naming.cache.ServiceInfoHolder in project nacos by alibaba.

the class NamingHttpClientProxyTest method testGetServiceList.

@Test
public void testGetServiceList() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> a = new HttpRestResult<Object>();
    a.setData("{\"count\":2,\"doms\":[\"aaa\",\"bbb\"]}");
    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";
    // when
    ListView<String> serviceList = clientProxy.getServiceList(1, 10, groupName, new NoneSelector());
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(endsWith("/service/list"), any(), any(), any(), eq(HttpMethod.GET), any());
    Assert.assertEquals(2, serviceList.getCount());
    Assert.assertEquals("aaa", serviceList.getData().get(0));
    Assert.assertEquals("bbb", serviceList.getData().get(1));
}
Also used : ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) Properties(java.util.Properties) NoneSelector(com.alibaba.nacos.api.selector.NoneSelector) Field(java.lang.reflect.Field) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 18 with ServiceInfoHolder

use of com.alibaba.nacos.client.naming.cache.ServiceInfoHolder in project nacos by alibaba.

the class NamingHttpClientProxyTest method testReqApi2.

@Test
public void testReqApi2() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenAnswer(invocationOnMock -> {
        // return url
        HttpRestResult<Object> res = new HttpRestResult<Object>();
        res.setData(invocationOnMock.getArgument(0));
        res.setCode(200);
        return res;
    });
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, new Properties(), holder);
    final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
    nacosRestTemplateField.setAccessible(true);
    nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
    String api = "/api";
    Map<String, String> params = new HashMap<>();
    Map<String, String> body = new HashMap<>();
    String method = HttpMethod.GET;
    // when
    String res = clientProxy.reqApi(api, params, body, method);
    // then
    Assert.assertEquals("http://localhost:8848/api", res);
}
Also used : ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) Properties(java.util.Properties) Field(java.lang.reflect.Field) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 19 with ServiceInfoHolder

use of com.alibaba.nacos.client.naming.cache.ServiceInfoHolder in project nacos by alibaba.

the class NacosNamingService method init.

private void init(Properties properties) throws NacosException {
    ValidatorUtils.checkInitParam(properties);
    this.namespace = InitUtils.initNamespaceForNaming(properties);
    InitUtils.initSerialization();
    InitUtils.initWebRootContext(properties);
    initLogName(properties);
    this.changeNotifier = new InstancesChangeNotifier();
    NotifyCenter.registerToPublisher(InstancesChangeEvent.class, 16384);
    NotifyCenter.registerSubscriber(changeNotifier);
    this.serviceInfoHolder = new ServiceInfoHolder(namespace, properties);
    this.clientProxy = new NamingClientProxyDelegate(this.namespace, serviceInfoHolder, properties, changeNotifier);
}
Also used : ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) InstancesChangeNotifier(com.alibaba.nacos.client.naming.event.InstancesChangeNotifier) NamingClientProxyDelegate(com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate)

Example 20 with ServiceInfoHolder

use of com.alibaba.nacos.client.naming.cache.ServiceInfoHolder in project nacos by alibaba.

the class NamingClientProxyDelegateTest method testGetServiceList.

@Test
public void testGetServiceList() throws NacosException, NoSuchFieldException, IllegalAccessException {
    String ns = "ns1";
    ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class);
    Properties props = new Properties();
    props.setProperty("serverAddr", "localhost");
    InstancesChangeNotifier notifier = new InstancesChangeNotifier();
    NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier);
    NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class);
    Field grpcClientProxyField = NamingClientProxyDelegate.class.getDeclaredField("grpcClientProxy");
    grpcClientProxyField.setAccessible(true);
    grpcClientProxyField.set(delegate, mockGrpcClient);
    AbstractSelector selector = new ExpressionSelector();
    int pageNo = 1;
    int pageSize = 10;
    String groupName = "group2";
    delegate.getServiceList(pageNo, pageSize, groupName, selector);
    verify(mockGrpcClient, times(1)).getServiceList(pageNo, pageSize, groupName, selector);
}
Also used : Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) AbstractSelector(com.alibaba.nacos.api.selector.AbstractSelector) InstancesChangeNotifier(com.alibaba.nacos.client.naming.event.InstancesChangeNotifier) NamingGrpcClientProxy(com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy) ExpressionSelector(com.alibaba.nacos.api.selector.ExpressionSelector) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)52 Test (org.junit.Test)51 Properties (java.util.Properties)41 Field (java.lang.reflect.Field)34 ServerListManager (com.alibaba.nacos.client.naming.core.ServerListManager)23 SecurityProxy (com.alibaba.nacos.client.security.SecurityProxy)23 InstancesChangeNotifier (com.alibaba.nacos.client.naming.event.InstancesChangeNotifier)19 HttpRestResult (com.alibaba.nacos.common.http.HttpRestResult)18 NacosRestTemplate (com.alibaba.nacos.common.http.client.NacosRestTemplate)18 Instance (com.alibaba.nacos.api.naming.pojo.Instance)10 NamingGrpcClientProxy (com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy)8 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)7 Service (com.alibaba.nacos.api.naming.pojo.Service)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 HashMap (java.util.HashMap)5 NoneSelector (com.alibaba.nacos.api.selector.NoneSelector)4 NacosException (com.alibaba.nacos.api.exception.NacosException)3 BeatReactor (com.alibaba.nacos.client.naming.beat.BeatReactor)3 NamingHttpClientProxy (com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy)3 ExecutorService (java.util.concurrent.ExecutorService)3