Search in sources :

Example 6 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class NacosClientAuthServiceImplTest method testTestLoginFailCode.

@Test
public void testTestLoginFailCode() throws Exception {
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> result = new HttpRestResult<>();
    result.setCode(400);
    when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
    properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
    List<String> serverList = new ArrayList<>();
    serverList.add("localhost");
    NacosClientAuthServiceImpl nacosClientAuthService = new NacosClientAuthServiceImpl();
    nacosClientAuthService.setServerList(serverList);
    nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
    boolean ret = nacosClientAuthService.login(properties);
    Assert.assertFalse(ret);
}
Also used : HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) ArrayList(java.util.ArrayList) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 7 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class NacosClientAuthServiceImplTest method testGetAccessToken.

@Test
public void testGetAccessToken() throws Exception {
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> result = new HttpRestResult<>();
    result.setData("{\"accessToken\":\"abc\",\"tokenTtl\":1000}");
    result.setCode(200);
    when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
    properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
    List<String> serverList = new ArrayList<>();
    serverList.add("localhost");
    NacosClientAuthServiceImpl nacosClientAuthService = new NacosClientAuthServiceImpl();
    nacosClientAuthService.setServerList(serverList);
    nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
    // when
    Assert.assertTrue(nacosClientAuthService.login(properties));
    // then
    Assert.assertEquals("abc", nacosClientAuthService.getLoginIdentityContext(null).getParameter(NacosAuthLoginConstant.ACCESSTOKEN));
}
Also used : HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) ArrayList(java.util.ArrayList) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 8 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class NacosClientAuthServiceImplTest method testTestLoginServerListSuccess.

@Test
public void testTestLoginServerListSuccess() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> result = new HttpRestResult<>();
    result.setData("{\"accessToken\":\"ttttttttttttttttt\",\"tokenTtl\":1000}");
    result.setCode(200);
    when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
    properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
    List<String> serverList = new ArrayList<>();
    serverList.add("localhost");
    serverList.add("localhost");
    NacosClientAuthServiceImpl nacosClientAuthService = new NacosClientAuthServiceImpl();
    nacosClientAuthService.setServerList(serverList);
    nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
    boolean ret = nacosClientAuthService.login(properties);
    Assert.assertTrue(ret);
}
Also used : HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) ArrayList(java.util.ArrayList) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 9 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class NamingHttpClientProxyTest method testServerHealthy.

@Test
public void testServerHealthy() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> a = new HttpRestResult<Object>();
    a.setData("{\"status\":\"UP\"}");
    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
    boolean serverHealthy = clientProxy.serverHealthy();
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(endsWith("/operator/metrics"), any(), any(), any(), eq(HttpMethod.GET), any());
    Assert.assertTrue(serverHealthy);
}
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) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 10 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class NamingHttpClientProxyTest method testRegisterService.

@Test
public void testRegisterService() 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.registerService(serviceName, groupName, instance);
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(any(), any(), any(), any(), any(), 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)

Aggregations

HttpRestResult (com.alibaba.nacos.common.http.HttpRestResult)33 NacosRestTemplate (com.alibaba.nacos.common.http.client.NacosRestTemplate)29 Properties (java.util.Properties)29 Test (org.junit.Test)29 Field (java.lang.reflect.Field)19 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)18 ServerListManager (com.alibaba.nacos.client.naming.core.ServerListManager)18 SecurityProxy (com.alibaba.nacos.client.security.SecurityProxy)18 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Instance (com.alibaba.nacos.api.naming.pojo.Instance)3 Service (com.alibaba.nacos.api.naming.pojo.Service)3 NoneSelector (com.alibaba.nacos.api.selector.NoneSelector)3 Header (com.alibaba.nacos.common.http.param.Header)3 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 BeatInfo (com.alibaba.nacos.client.naming.beat.BeatInfo)1 DefaultClientHttpResponse (com.alibaba.nacos.common.http.client.response.DefaultClientHttpResponse)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1