use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.
the class NamingHttpClientProxyTest method testCallServerFail.
@Test
public void testCallServerFail() throws Exception {
// then
thrown.expect(NacosException.class);
// 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.setMessage("fail");
res.setCode(400);
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;
String curServer = "127.0.0.1";
// when
clientProxy.callServer(api, params, body, curServer, method);
}
use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.
the class NamingHttpClientProxyTest method testCallServerFail304.
@Test
public void testCallServerFail304() 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.setMessage("redirect");
res.setCode(304);
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;
String curServer = "127.0.0.1";
// when
String s = clientProxy.callServer(api, params, body, curServer, method);
// then
Assert.assertEquals("", s);
}
use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.
the class NamingHttpClientProxyTest method testSetServerPort.
@Test
public void testSetServerPort() {
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);
// when
clientProxy.setServerPort(1234);
}
use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.
the class NamingHttpClientProxyTest method testDeregisterService.
@Test
public void testDeregisterService() 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.deregisterService(serviceName, groupName, instance);
// then
verify(nacosRestTemplate, times(1)).exchangeForm(any(), 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 testQueryService.
@Test
public void testQueryService() 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
Service service = clientProxy.queryService(serviceName, groupName);
// then
verify(nacosRestTemplate, times(1)).exchangeForm(endsWith(UtilAndComs.nacosUrlService), any(), any(), any(), eq(HttpMethod.GET), any());
Assert.assertEquals(serviceName, service.getName());
Assert.assertEquals(groupName, service.getGroupName());
}
Aggregations