From d1576f2bf26f3689db934a97333eeb3108edb428 Mon Sep 17 00:00:00 2001 From: shalk Date: Sat, 6 Mar 2021 16:17:30 +0800 Subject: [PATCH 1/3] add ut for package com.alibaba.nacos.client.config.http --- .../config/http/MetricsHttpAgentTest.java | 165 ++++++++++++++++++ .../config/http/ServerHttpAgentTest.java | 127 ++++++++++++++ 2 files changed, 292 insertions(+) create mode 100644 client/src/test/java/com/alibaba/nacos/client/config/http/MetricsHttpAgentTest.java create mode 100644 client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java diff --git a/client/src/test/java/com/alibaba/nacos/client/config/http/MetricsHttpAgentTest.java b/client/src/test/java/com/alibaba/nacos/client/config/http/MetricsHttpAgentTest.java new file mode 100644 index 00000000000..5fe15031717 --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/config/http/MetricsHttpAgentTest.java @@ -0,0 +1,165 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.client.config.http; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.common.http.HttpRestResult; +import com.alibaba.nacos.common.http.param.Header; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * MetricsHttpAgentTest. + * + * @author shalk + * @since 2021 + */ +public class MetricsHttpAgentTest { + + private static class MockHttpAgent implements HttpAgent { + + private String name; + + private String encode; + + private String tenant; + + private String namespace; + + private boolean start = false; + + private boolean shutdown = false; + + public MockHttpAgent(String name, String encode, String tenant, String namespace) { + this.name = name; + this.encode = encode; + this.tenant = tenant; + this.namespace = namespace; + } + + @Override + public void start() throws NacosException { + start = true; + } + + @Override + public HttpRestResult httpGet(String path, Map headers, Map paramValues, + String encoding, long readTimeoutMs) throws Exception { + return new HttpRestResult(Header.newInstance(), 200, "get", "get " + path); + } + + @Override + public HttpRestResult httpPost(String path, Map headers, + Map paramValues, String encoding, long readTimeoutMs) throws Exception { + return new HttpRestResult(Header.newInstance(), 200, "post", "post " + path); + } + + @Override + public HttpRestResult httpDelete(String path, Map headers, + Map paramValues, String encoding, long readTimeoutMs) throws Exception { + return new HttpRestResult(Header.newInstance(), 200, "delete", "delete " + path); + } + + @Override + public String getName() { + return name; + } + + @Override + public String getNamespace() { + return namespace; + } + + @Override + public String getTenant() { + return tenant; + } + + @Override + public String getEncode() { + return encode; + } + + @Override + public void shutdown() throws NacosException { + shutdown = true; + } + + public boolean isStart() { + return start; + } + + public boolean isShutdown() { + return shutdown; + } + } + + @Test + public void testGetter() { + String name = "name"; + String encode = "UTF-8"; + String tenant = "aaa"; + String namespace = "aaa"; + final HttpAgent mockHttpAgent = new MockHttpAgent(name, encode, tenant, namespace); + final MetricsHttpAgent metricsHttpAgent = new MetricsHttpAgent(mockHttpAgent); + + Assert.assertEquals(name, metricsHttpAgent.getName()); + Assert.assertEquals(encode, metricsHttpAgent.getEncode()); + Assert.assertEquals(tenant, metricsHttpAgent.getTenant()); + Assert.assertEquals(namespace, metricsHttpAgent.getNamespace()); + } + + @Test + public void testLifeCycle() throws NacosException { + String name = "name"; + String encode = "UTF-8"; + String tenant = "aaa"; + String namespace = "aaa"; + final MockHttpAgent mockHttpAgent = new MockHttpAgent(name, encode, tenant, namespace); + final MetricsHttpAgent metricsHttpAgent = new MetricsHttpAgent(mockHttpAgent); + + metricsHttpAgent.start(); + Assert.assertTrue(mockHttpAgent.isStart()); + + metricsHttpAgent.shutdown(); + Assert.assertTrue(mockHttpAgent.isShutdown()); + } + + @Test + public void testHttpMethod() throws Exception { + String name = "name"; + String encode = "UTF-8"; + String tenant = "aaa"; + String namespace = "aaa"; + final MockHttpAgent mockHttpAgent = new MockHttpAgent(name, encode, tenant, namespace); + final MetricsHttpAgent metricsHttpAgent = new MetricsHttpAgent(mockHttpAgent); + + final HttpRestResult result1 = metricsHttpAgent + .httpGet("/aa", new HashMap(), new HashMap(), "UTF-8", 1L); + Assert.assertEquals("get /aa", result1.getMessage()); + final HttpRestResult result2 = metricsHttpAgent + .httpPost("/aa", new HashMap(), new HashMap(), "UTF-8", 1L); + Assert.assertEquals("post /aa", result2.getMessage()); + + final HttpRestResult result3 = metricsHttpAgent + .httpDelete("/aa", new HashMap(), new HashMap(), "UTF-8", 1L); + Assert.assertEquals("delete /aa", result3.getMessage()); + } +} \ No newline at end of file diff --git a/client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java b/client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java new file mode 100644 index 00000000000..772e1474543 --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java @@ -0,0 +1,127 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.client.config.http; + +import com.alibaba.nacos.api.PropertyKeyConst; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.client.config.impl.ServerListManager; +import com.alibaba.nacos.common.http.HttpRestResult; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.HashMap; +import java.util.Properties; + +/** + * ServerHttpAgentTest. + * + * @author shalk + * @since 2021 + */ +public class ServerHttpAgentTest { + + @Test + public void testConstruct() throws NacosException { + ServerListManager server = new ServerListManager(); + final ServerHttpAgent serverHttpAgent1 = new ServerHttpAgent(server); + Assert.assertNotNull(serverHttpAgent1); + + final ServerHttpAgent serverHttpAgent2 = new ServerHttpAgent(server, new Properties()); + Assert.assertNotNull(serverHttpAgent2); + + final Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR, "1.1.1.1"); + final ServerHttpAgent serverHttpAgent3 = new ServerHttpAgent(properties); + Assert.assertNotNull(serverHttpAgent3); + + } + + @Test + public void testGetterAndSetter() throws NacosException { + ServerListManager server = new ServerListManager("aaa", "namespace1"); + final ServerHttpAgent serverHttpAgent = new ServerHttpAgent(server, new Properties()); + + final String appname = ServerHttpAgent.getAppname(); + //set by AppNameUtils, init in ParamUtils static block + Assert.assertEquals("unknown", appname); + + final String encode = serverHttpAgent.getEncode(); + final String namespace = serverHttpAgent.getNamespace(); + final String tenant = serverHttpAgent.getTenant(); + final String name = serverHttpAgent.getName(); + Assert.assertEquals(null, encode); + Assert.assertEquals("namespace1", namespace); + Assert.assertEquals("namespace1", tenant); + Assert.assertEquals("aaa-namespace1", name); + + } + + @Test + public void testHttpGet() throws Exception { + Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR, "https://httpbin.org"); + properties.put(PropertyKeyConst.CONTEXT_PATH, "/"); + final ServerHttpAgent serverHttpAgent = new ServerHttpAgent(properties); + final HttpRestResult result = serverHttpAgent + .httpGet("/get", new HashMap(), new HashMap(), "UTF-8", 5000); + + Assert.assertEquals(200, result.getCode()); + } + + @Test + public void testHttpPost() throws Exception { + Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR, "https://httpbin.org"); + properties.put(PropertyKeyConst.CONTEXT_PATH, "/"); + final ServerHttpAgent serverHttpAgent = new ServerHttpAgent(properties); + final HttpRestResult result = serverHttpAgent + .httpPost("/post", new HashMap(), new HashMap(), "UTF-8", 5000); + + Assert.assertEquals(200, result.getCode()); + } + + @Test + public void testHttpDelete() throws Exception { + Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR, "https://httpbin.org"); + properties.put(PropertyKeyConst.CONTEXT_PATH, "/"); + final ServerHttpAgent serverHttpAgent = new ServerHttpAgent(properties); + final HttpRestResult result = serverHttpAgent + .httpDelete("/delete", new HashMap(), new HashMap(), "UTF-8", 5000); + + Assert.assertEquals(200, result.getCode()); + } + + @Test + public void testLifCycle() throws NacosException, NoSuchFieldException, IllegalAccessException { + Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR, "aaa"); + ServerListManager server = Mockito.mock(ServerListManager.class); + final ServerHttpAgent serverHttpAgent = new ServerHttpAgent(server, properties); + + serverHttpAgent.start(); + Mockito.verify(server).start(); + + try { + serverHttpAgent.shutdown(); + } catch (NullPointerException e) { + Assert.fail(); + } + } + +} \ No newline at end of file From bac04aa951d93845720280ee2b21ac361a5e5548 Mon Sep 17 00:00:00 2001 From: shalk Date: Tue, 9 Mar 2021 18:36:46 +0800 Subject: [PATCH 2/3] remove ut doc --- .../nacos/client/config/http/MetricsHttpAgentTest.java | 6 ------ .../nacos/client/config/http/ServerHttpAgentTest.java | 6 ------ 2 files changed, 12 deletions(-) diff --git a/client/src/test/java/com/alibaba/nacos/client/config/http/MetricsHttpAgentTest.java b/client/src/test/java/com/alibaba/nacos/client/config/http/MetricsHttpAgentTest.java index 5fe15031717..ef56426aebf 100644 --- a/client/src/test/java/com/alibaba/nacos/client/config/http/MetricsHttpAgentTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/config/http/MetricsHttpAgentTest.java @@ -25,12 +25,6 @@ import java.util.HashMap; import java.util.Map; -/** - * MetricsHttpAgentTest. - * - * @author shalk - * @since 2021 - */ public class MetricsHttpAgentTest { private static class MockHttpAgent implements HttpAgent { diff --git a/client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java b/client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java index 772e1474543..1d89bd4e29b 100644 --- a/client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java @@ -27,12 +27,6 @@ import java.util.HashMap; import java.util.Properties; -/** - * ServerHttpAgentTest. - * - * @author shalk - * @since 2021 - */ public class ServerHttpAgentTest { @Test From a512e3ca712a63a30cabf4f77911303f6ff54714 Mon Sep 17 00:00:00 2001 From: shalk Date: Fri, 12 Mar 2021 10:35:00 +0800 Subject: [PATCH 3/3] remove http request case --- .../config/http/ServerHttpAgentTest.java | 40 +------------------ 1 file changed, 1 insertion(+), 39 deletions(-) diff --git a/client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java b/client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java index 1d89bd4e29b..754e2cb699d 100644 --- a/client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java @@ -19,12 +19,10 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.impl.ServerListManager; -import com.alibaba.nacos.common.http.HttpRestResult; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; -import java.util.HashMap; import java.util.Properties; public class ServerHttpAgentTest { @@ -66,43 +64,7 @@ public void testGetterAndSetter() throws NacosException { } @Test - public void testHttpGet() throws Exception { - Properties properties = new Properties(); - properties.put(PropertyKeyConst.SERVER_ADDR, "https://httpbin.org"); - properties.put(PropertyKeyConst.CONTEXT_PATH, "/"); - final ServerHttpAgent serverHttpAgent = new ServerHttpAgent(properties); - final HttpRestResult result = serverHttpAgent - .httpGet("/get", new HashMap(), new HashMap(), "UTF-8", 5000); - - Assert.assertEquals(200, result.getCode()); - } - - @Test - public void testHttpPost() throws Exception { - Properties properties = new Properties(); - properties.put(PropertyKeyConst.SERVER_ADDR, "https://httpbin.org"); - properties.put(PropertyKeyConst.CONTEXT_PATH, "/"); - final ServerHttpAgent serverHttpAgent = new ServerHttpAgent(properties); - final HttpRestResult result = serverHttpAgent - .httpPost("/post", new HashMap(), new HashMap(), "UTF-8", 5000); - - Assert.assertEquals(200, result.getCode()); - } - - @Test - public void testHttpDelete() throws Exception { - Properties properties = new Properties(); - properties.put(PropertyKeyConst.SERVER_ADDR, "https://httpbin.org"); - properties.put(PropertyKeyConst.CONTEXT_PATH, "/"); - final ServerHttpAgent serverHttpAgent = new ServerHttpAgent(properties); - final HttpRestResult result = serverHttpAgent - .httpDelete("/delete", new HashMap(), new HashMap(), "UTF-8", 5000); - - Assert.assertEquals(200, result.getCode()); - } - - @Test - public void testLifCycle() throws NacosException, NoSuchFieldException, IllegalAccessException { + public void testLifCycle() throws NacosException { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, "aaa"); ServerListManager server = Mockito.mock(ServerListManager.class);