use of com.alibaba.nacos.common.http.HttpRestResult 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());
}
use of com.alibaba.nacos.common.http.HttpRestResult 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());
}
use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.
the class NamingHttpClientProxyTest method testCreateService.
@Test
public void testCreateService() 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);
// when
clientProxy.createService(new Service(), new NoneSelector());
// then
verify(nacosRestTemplate, times(1)).exchangeForm(endsWith(UtilAndComs.nacosUrlService), any(), any(), any(), eq(HttpMethod.POST), any());
}
use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.
the class NamingHttpClientProxyTest method testUnsubscribe.
@Test
public void testUnsubscribe() throws Exception {
// TODO thrown.expect(UnsupportedOperationException.class);
// 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
clientProxy.unsubscribe(serviceName, groupName, clusters);
}
use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.
the class ServerListManagerTest method testConstructWithEndpoint.
@Ignore
@Test
public void testConstructWithEndpoint() throws Exception {
Properties properties = new Properties();
properties.put(PropertyKeyConst.ENDPOINT, "127.0.0.1");
final ServerListManager serverListManager = new ServerListManager(properties);
NacosRestTemplate mock = Mockito.mock(NacosRestTemplate.class);
HttpRestResult<Object> a = new HttpRestResult<Object>();
a.setData("127.0.0.1:8848");
a.setCode(200);
Mockito.when(mock.get(any(), any(), any(), any())).thenReturn(a);
final Field nacosRestTemplate = ServerListManager.class.getDeclaredField("nacosRestTemplate");
nacosRestTemplate.setAccessible(true);
nacosRestTemplate.set(serverListManager, mock);
TimeUnit.SECONDS.sleep(31);
final List<String> serverList = serverListManager.getServerList();
Assert.assertEquals(1, serverList.size());
Assert.assertEquals("127.0.0.1:8848", serverList.get(0));
}
Aggregations