Search in sources :

Example 6 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NamingHttpClientProxyTest method testSendBeat.

@Test
public void testSendBeat() 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);
    BeatInfo beat = new BeatInfo();
    // when
    clientProxy.sendBeat(beat, true);
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(endsWith("/instance/beat"), any(), any(), any(), eq(HttpMethod.PUT), any());
}
Also used : 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) BeatInfo(com.alibaba.nacos.client.naming.beat.BeatInfo) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 7 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NamingHttpClientProxyTest method testUpdateInstance.

@Test
public void testUpdateInstance() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> a = new HttpRestResult<Object>();
    a.setData("127.0.0.1:8848");
    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";
    Instance instance = new Instance();
    // when
    clientProxy.updateInstance(serviceName, groupName, instance);
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(any(), any(), any(), any(), eq(HttpMethod.PUT), any());
}
Also used : Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) Instance(com.alibaba.nacos.api.naming.pojo.Instance) 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 8 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NamingHttpClientProxyTest method testUpdateService.

@Test
public void testUpdateService() 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";
    // when
    clientProxy.updateService(new Service(), new NoneSelector());
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(endsWith(UtilAndComs.nacosUrlService), any(), any(), any(), eq(HttpMethod.PUT), any());
}
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) Service(com.alibaba.nacos.api.naming.pojo.Service) 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 9 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NamingHttpClientProxyTest method testReqApi.

@Test
public void testReqApi() 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<>();
    String method = HttpMethod.GET;
    // when
    String res = clientProxy.reqApi(api, params, 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 10 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager 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)

Aggregations

ServerListManager (com.alibaba.nacos.client.naming.core.ServerListManager)25 SecurityProxy (com.alibaba.nacos.client.security.SecurityProxy)25 Properties (java.util.Properties)24 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)23 Test (org.junit.Test)23 Field (java.lang.reflect.Field)22 HttpRestResult (com.alibaba.nacos.common.http.HttpRestResult)18 NacosRestTemplate (com.alibaba.nacos.common.http.client.NacosRestTemplate)18 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 HashMap (java.util.HashMap)5 Instance (com.alibaba.nacos.api.naming.pojo.Instance)4 Service (com.alibaba.nacos.api.naming.pojo.Service)3 NoneSelector (com.alibaba.nacos.api.selector.NoneSelector)3 BeatReactor (com.alibaba.nacos.client.naming.beat.BeatReactor)3 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 BeatInfo (com.alibaba.nacos.client.naming.beat.BeatInfo)2 NamingHttpClientProxy (com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy)2 HashSet (java.util.HashSet)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Before (org.junit.Before)1