use of com.alibaba.nacos.client.naming.core.ServerListManager 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));
}
use of com.alibaba.nacos.client.naming.core.ServerListManager 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);
}
use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.
the class NamingHttpClientProxyTest method testDeleteService.
@Test
public void testDeleteService() throws Exception {
// given
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> a = new HttpRestResult<Object>();
a.setData("{\"name\":\"service1\",\"groupName\":\"group1\"}");
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.deleteService(serviceName, groupName);
// then
verify(nacosRestTemplate, times(1)).exchangeForm(endsWith(UtilAndComs.nacosUrlService), any(), any(), any(), eq(HttpMethod.DELETE), any());
}
use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.
the class NamingHttpClientProxyTest method testShutdown.
@Test
public void testShutdown() throws Exception {
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);
String namespaceId = "aaa";
NamingHttpClientProxy clientProxy = new NamingHttpClientProxy(namespaceId, proxy, mgr, props, holder);
BeatReactor mockBeatReactor = mock(BeatReactor.class);
final Field mockBeatReactorField = NamingHttpClientProxy.class.getDeclaredField("beatReactor");
mockBeatReactorField.setAccessible(true);
mockBeatReactorField.set(clientProxy, mockBeatReactor);
// when
clientProxy.shutdown();
// then
verify(mockBeatReactor, times(1)).shutdown();
}
use of com.alibaba.nacos.client.naming.core.ServerListManager 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());
}
Aggregations