Unit test for discovery 49/16749/1
authorHuabingZhao <zhao.huabing@zte.com.cn>
Sun, 1 Oct 2017 02:27:51 +0000 (10:27 +0800)
committerHuabingZhao <zhao.huabing@zte.com.cn>
Sun, 1 Oct 2017 02:31:02 +0000 (10:31 +0800)
Issue-Id: MSB-84
Change-Id: Icdd9b2de6ca373f8a91e747f276e92f5d46e464f
Signed-off-by: HuabingZhao <zhao.huabing@zte.com.cn>
sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/ConsulClientAppTest.java [new file with mode: 0644]
sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapperTest.java [new file with mode: 0644]
sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/PublishAddressWrapperTest.java [new file with mode: 0644]
sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/util/HttpClientUtilTest.java [new file with mode: 0644]
sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/util/JacksonJsonUtilTest.java [new file with mode: 0644]
sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/util/RegExpTestUtilTest.java [new file with mode: 0644]

diff --git a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/ConsulClientAppTest.java b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/ConsulClientAppTest.java
new file mode 100644 (file)
index 0000000..0162aea
--- /dev/null
@@ -0,0 +1,31 @@
+/**
+ * Copyright 2016-2017 ZTE, Inc. and others.
+ * 
+ * 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 org.onap.msb.sdclient.wrapper;
+
+import org.junit.Test;
+
+
+public class ConsulClientAppTest {
+
+
+
+    @Test
+    public void testConsulClientApp() {
+
+        ConsulClientApp consulTest = new ConsulClientApp("127.0.0.1", 8500);
+
+        consulTest.startHealthNodeListen("apigateway");
+    }
+}
diff --git a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapperTest.java b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapperTest.java
new file mode 100644 (file)
index 0000000..73b5560
--- /dev/null
@@ -0,0 +1,167 @@
+/**
+ * Copyright 2016-2017 ZTE, Inc. and others.
+ * 
+ * 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 org.onap.msb.sdclient.wrapper;
+
+import java.math.BigInteger;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.onap.msb.sdclient.core.ConsulResponse;
+import org.onap.msb.sdclient.core.MicroServiceFullInfo;
+import org.onap.msb.sdclient.core.MicroServiceInfo;
+import org.onap.msb.sdclient.core.Node;
+import org.onap.msb.sdclient.core.NodeAddress;
+import org.onap.msb.sdclient.core.NodeInfo;
+import org.onap.msb.sdclient.core.exception.ExtendedNotFoundException;
+import org.onap.msb.sdclient.core.exception.UnprocessableEntityException;
+import org.onap.msb.sdclient.wrapper.util.HttpClientUtil;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({HttpClientUtil.class})
+public class ConsulServiceWrapperTest {
+
+    private static final String restJson =
+                    "[{\"Node\":{\"Node\":\"server\",\"Address\":\"127.0.0.1\"},\"Service\":{\"ID\":\"_test_10.74.56.36_5656\",\"Service\":\"test\",\"Tags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"REST\\\",\\\"ha_role\\\":\\\"active\\\",\\\"is_manual\\\":\\\"true\\\",\\\"version\\\":\\\"v1\\\",\\\"url\\\":\\\"/test\\\",\\\"status\\\":\\\"1\\\"}\",\"\\\"lb\\\":{\\\"lb_server_params\\\":\\\"weight=10 max_fails=10 fail_timeout=10s\\\",\\\"lb_policy\\\":\\\"ip_hash\\\"}\",\"\\\"checks\\\":{\\\"http\\\":\\\"http://10.74.56.36:5656\\\",\\\"interval\\\":\\\"10s\\\",\\\"timeout\\\":\\\"10s\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0\\\",\\\"key\\\":\\\"value\\\"}\",\"\\\"metadata\\\":{\\\"key\\\":\\\"value\\\"}\"],\"Address\":\"10.74.56.36\",\"Port\":5656},\"Checks\":[{\"Node\":\"server\",\"CheckID\":\"service:_test_10.74.56.36_5656\",\"Name\":\"Service 'test' check\",\"Status\":\"critical\"}]}]";
+    private static final String catalogJson =
+                    "[{\"Node\":\"server\",\"Address\":\"127.0.0.1\",\"TaggedAddresses\":{\"lan\":\"127.0.0.1\",\"wan\":\"127.0.0.1\"},\"ServiceID\":\"_test_10.74.56.36_5656\",\"ServiceName\":\"test\",\"ServiceTags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"REST\\\",\\\"version\\\":\\\"v1\\\",\\\"url\\\":\\\"/test\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0\\\"}\"],\"ServiceAddress\":\"10.74.56.36\",\"ServicePort\":5656,\"ServiceEnableTagOverride\":false,\"CreateIndex\":1819452,\"ModifyIndex\":1819454}]";
+    private static final String catalog4ttlJson =
+                    "[{\"Node\":\"server\",\"Address\":\"127.0.0.1\",\"TaggedAddresses\":{\"lan\":\"127.0.0.1\",\"wan\":\"127.0.0.1\"},\"ServiceID\":\"_test_10.74.56.36_5656\",\"ServiceName\":\"test\",\"ServiceTags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"REST\\\",\\\"version\\\":\\\"v1\\\",\\\"url\\\":\\\"/test\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0\\\"}\",\"\\\"checks\\\":{\\\"ttl\\\":\\\"10s\\\"}\"],\"ServiceAddress\":\"10.74.56.36\",\"ServicePort\":5656,\"ServiceEnableTagOverride\":false,\"CreateIndex\":1819452,\"ModifyIndex\":1819454}]";
+    private static final String mockRestUrl = "http://127.0.0.1:8500/v1/health/service/test";
+    private static final String mockPostUrl = "http://127.0.0.1:8500/v1/catalog/register";
+    private static final String mockdel_gettUrl = "http://127.0.0.1:8500/v1/catalog/service/test";
+    private static final String mockdeltUrl = "http://127.0.0.1:8500/v1/catalog/deregister";
+
+
+
+    private static ConsulServiceWrapper consulServiceWrapper = ConsulServiceWrapper.getInstance();
+
+    @Test
+    public void test_getMicroServiceInstance() {
+        mockGetRest();
+        MicroServiceFullInfo service = consulServiceWrapper.getMicroServiceInstance("test", "v1", "");
+        Assert.assertEquals(service.getServiceName(), "test");
+        Assert.assertEquals(service.getProtocol(), "REST");
+    }
+
+    @Test
+    public void test_getMicroServiceForNodes() {
+        mockGetRest();
+        List<MicroServiceFullInfo> services = consulServiceWrapper.getMicroServiceForNodes("test", "v1", false, "", "");
+        Assert.assertEquals(1, services.size());
+        Assert.assertEquals(services.get(0).getServiceName(), "test");
+        Assert.assertEquals(services.get(0).getProtocol(), "REST");
+    }
+
+    @Test
+    public void test_saveMicroServiceInstance() {
+        MicroServiceInfo serviceInfo = new MicroServiceInfo();
+        serviceInfo.setServiceName("test");
+        serviceInfo.setVersion("v1");
+        serviceInfo.setUrl("/api/test/v1");
+        serviceInfo.setProtocol("REST");
+        serviceInfo.setVisualRange("1");
+        Set<Node> nodes = new HashSet<Node>();
+        Node node = new Node();
+        node.setIp("10.74.44.1");
+        node.setPort("10080");
+        nodes.add(node);
+        serviceInfo.setNodes(nodes);
+
+        mockGetRest4null();
+        // mockGetPost();
+        try {
+            consulServiceWrapper.saveMicroServiceInstance(serviceInfo, true, "127.0.0.1", true);
+        } catch (Exception e) {
+            Assert.assertEquals("HTTP 500 Internal Server Error", e.getMessage());
+        }
+    }
+
+    @Test
+    public void test_deleteMicroService() {
+        mockGet4Delete();
+        mockDelete();
+        consulServiceWrapper.deleteMicroService("test", "v1", "");
+    }
+
+    @Test
+    public void test_deleteMicroServiceInstance() {
+        mockGet4Delete();
+        mockDelete();
+        consulServiceWrapper.deleteMicroServiceInstance("test", "v1", "", "10.74.56.36", "5656");
+    }
+
+    @Test
+    public void test_healthCheckbyTTL() {
+        NodeAddress nodeAddress = new NodeAddress("10.74.56.36", "5656");
+        mockGet4healthCheck();
+        consulServiceWrapper.healthCheckbyTTL("test", "v1", "", nodeAddress);
+    }
+
+
+
+    private void mockGetRest() {
+        PowerMockito.mockStatic(HttpClientUtil.class);
+        ConsulResponse<Object> consulResponse = new ConsulResponse(restJson, new BigInteger("1000"));
+        PowerMockito.when(HttpClientUtil.httpWaitGet(mockRestUrl)).thenReturn(consulResponse);
+    }
+
+
+    private void mockGet4Delete() {
+        PowerMockito.mockStatic(HttpClientUtil.class);
+        PowerMockito.when(HttpClientUtil.httpGet(mockdel_gettUrl)).thenReturn(catalogJson);
+
+    }
+
+    private void mockGet4healthCheck() {
+        PowerMockito.mockStatic(HttpClientUtil.class);
+        PowerMockito.when(HttpClientUtil.httpGet(mockdel_gettUrl)).thenReturn(catalog4ttlJson);
+
+
+        String checkUrl = "http://127.0.0.1:8500/v1/agent/check/pass/service:_test_10.74.56.36_5656";
+        PowerMockito.when(HttpClientUtil.httpGet(checkUrl)).thenReturn("ok");
+
+    }
+
+    private void mockDelete() {
+        String serviceJson = "{\"Node\": \"externalService\",\"ServiceID\": \"_test_10.74.56.36_5656\"}";
+        PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockdeltUrl, serviceJson)).thenReturn(200);
+
+    }
+
+
+
+    private void mockGetRest4null() {
+        PowerMockito.mockStatic(HttpClientUtil.class);
+        PowerMockito.when(HttpClientUtil.httpWaitGet(mockRestUrl)).thenReturn(null);
+    }
+
+    private void mockGetPost() {
+        PowerMockito.mockStatic(HttpClientUtil.class);
+        String serviceJson =
+                        "{\"Node\":\"externalService\",\"Address\":\"127.0.0.1\",\"Service\":{\"ID\":\"_test_10.74.44.1_10080\",\"Service\":\"test\",\"Tags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"REST\\\",\\\"status\\\":\\\"1\\\",\\\"enable_ssl\\\":\\\"false\\\",\\\"is_manual\\\":\\\"true\\\",\\\"url\\\":\\\"/api/test/v1\\\",\\\"version\\\":\\\"v1\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"1\\\"}\"],\"Address\":\"10.74.44.1\",\"Port\":10080}}";
+        PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockPostUrl, serviceJson)).thenReturn(200);
+
+
+    }
+
+}
diff --git a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/PublishAddressWrapperTest.java b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/PublishAddressWrapperTest.java
new file mode 100644 (file)
index 0000000..1e5227f
--- /dev/null
@@ -0,0 +1,365 @@
+/**
+ * Copyright 2016-2017 ZTE, Inc. and others.
+ * 
+ * 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 org.onap.msb.sdclient.wrapper;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.onap.msb.sdclient.core.ConsulResponse;
+import org.onap.msb.sdclient.core.KeyVaulePair;
+import org.onap.msb.sdclient.core.MicroServiceFullInfo;
+import org.onap.msb.sdclient.core.NodeInfo;
+import org.onap.msb.sdclient.core.PublishAddress;
+import org.onap.msb.sdclient.core.PublishFullAddress;
+import org.onap.msb.sdclient.core.exception.UnprocessableEntityException;
+
+
+import org.onap.msb.sdclient.wrapper.util.HttpClientUtil;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({HttpClientUtil.class})
+public class PublishAddressWrapperTest {
+
+    private static final String restJson =
+                    "[{\"Node\":{\"Node\":\"A23179111\",\"Address\":\"10.74.44.16\"},\"Service\":{\"ID\":\"_test_10.74.44.16_2356\",\"Service\":\"test\",\"Tags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"REST\\\",\\\"url\\\":\\\"/api/test/v1\\\",\\\"version\\\":\\\"v1\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0|1\\\",\\\"apigateway\\\":\\\"test\\\",\\\"network_plane_type\\\":\\\"net\\\"}\"],\"Address\":\"10.74.44.16\",\"Port\":8086},\"Checks\":[{\"CheckID\":\"serfHealth\",\"Name\":\"Serf Health Status\",\"Status\":\"passing\"}]}]";
+    private static final String mockRestUrl = "http://127.0.0.1:8500/v1/health/service/test";
+
+    private static final String uiJson =
+                    "[{\"Node\":{\"Node\":\"A23179111\",\"Address\":\"10.74.44.16\"},\"Service\":{\"ID\":\"_test_10.74.44.16_2356\",\"Service\":\"IUI_test\",\"Tags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"UI\\\",\\\"url\\\":\\\"/iui/test\\\",\\\"path\\\":\\\"/iui/test_path\\\",\\\"host\\\":\\\"testhost\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0|1\\\",\\\"apigateway\\\":\\\"test\\\",\\\"network_plane_type\\\":\\\"net\\\"}\"],\"Address\":\"10.74.44.16\",\"Port\":8086},\"Checks\":[{\"CheckID\":\"serfHealth\",\"Name\":\"Serf Health Status\",\"Status\":\"passing\"}]}]";
+    private static final String mockUIUrl = "http://127.0.0.1:8500/v1/health/service/IUI_test";
+
+    private static final String tcpJson =
+                    "[{\"Node\":{\"Node\":\"A23179111\",\"Address\":\"10.74.44.16\"},\"Service\":{\"ID\":\"_test_10.74.44.16_2356\",\"Service\":\"tcp_test\",\"Tags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"TCP\\\",\\\"url\\\":\\\"/\\\",\\\"version\\\":\\\"v1\\\",\\\"publish_port\\\":\\\"40001\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0|1\\\",\\\"apigateway\\\":\\\"test\\\",\\\"network_plane_type\\\":\\\"net\\\"}\"],\"Address\":\"10.74.44.16\",\"Port\":8086},\"Checks\":[{\"CheckID\":\"serfHealth\",\"Name\":\"Serf Health Status\",\"Status\":\"passing\"}]}]";
+    private static final String mockTCPUrl = "http://127.0.0.1:8500/v1/health/service/tcp_test";
+
+    private static final String httpJson =
+                    "[{\"Node\":{\"Node\":\"A23179111\",\"Address\":\"10.74.44.16\"},\"Service\":{\"ID\":\"_test_10.74.44.16_2356\",\"Service\":\"test\",\"Tags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"HTTP\\\",\\\"url\\\":\\\"/test\\\",\\\"version\\\":\\\"v1\\\",\\\"host\\\":\\\"testhost\\\"}\",\"\\\"ns\\\":{\\\"namespace\\\":\\\"ns\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0|1\\\",\\\"apigateway\\\":\\\"test\\\",\\\"network_plane_type\\\":\\\"net\\\"}\"],\"Address\":\"10.74.44.16\",\"Port\":8086},\"Checks\":[{\"CheckID\":\"serfHealth\",\"Name\":\"Serf Health Status\",\"Status\":\"passing\"}]}]";
+    private static final String mockHTTPUrl = "http://127.0.0.1:8500/v1/health/service/test-ns";
+
+
+
+    private static PublishAddressWrapper publishAddressWrapper = PublishAddressWrapper.getInstance();
+
+    @BeforeClass
+    public static void setUpBeforeClass() throws Exception {
+        mockinitApigatewayInfo();
+    }
+
+
+    @Test
+    public void test_get_apigateway_publishaddress4Rest() {
+        mockGetRest();
+
+        Set<PublishFullAddress> publishFullAddressList =
+                        publishAddressWrapper.getAllPublishaddress("test", "v1", "", "1");
+        int addressNum = publishFullAddressList.size();
+        Assert.assertEquals(2, addressNum);
+
+    }
+
+
+    @Test
+    public void test_get_router_publishaddress4Ui() {
+        mockgetUi();
+
+        Set<PublishFullAddress> publishFullAddressList =
+                        publishAddressWrapper.getAllPublishaddress("IUI_test", "null", "", "0");
+        int addressNum = publishFullAddressList.size();
+        Assert.assertEquals(2, addressNum);
+
+
+    }
+
+    @Test
+    public void test_get_router_publishaddress4Http() {
+        mockgetHttp();
+
+        Set<PublishFullAddress> publishFullAddressList =
+                        publishAddressWrapper.getAllPublishaddress("test", "v1", "ns", "0");
+        int addressNum = publishFullAddressList.size();
+        Assert.assertEquals(1, addressNum);
+
+
+    }
+
+    @Test
+    public void test_get_router_publishaddress4Tcp() {
+        mockgetTcp();
+
+        Set<PublishFullAddress> publishFullAddressList =
+                        publishAddressWrapper.getAllPublishaddress("tcp_test", "v1", "", "0");
+        int addressNum = publishFullAddressList.size();
+        Assert.assertEquals(2, addressNum);
+
+
+
+    }
+
+
+    @Test
+    public void test_get_apigateway_publishaddress4nameError() {
+        try {
+            publishAddressWrapper.getAllPublishaddress("*.test", "v1", "", "1");
+        } catch (Exception e) {
+            Assert.assertTrue(e instanceof UnprocessableEntityException);
+        }
+    }
+
+    @Test
+    public void test_get_apigateway_publishaddress4nameEmpty() {
+        try {
+            publishAddressWrapper.getAllPublishaddress("", "v1", "", "1");
+        } catch (Exception e) {
+            Assert.assertTrue(e instanceof UnprocessableEntityException);
+        }
+    }
+
+    @Test
+    public void test_get_apigateway_publishaddress4versionError() {
+        try {
+            publishAddressWrapper.getAllPublishaddress("test", "verison1", "", "1");
+        } catch (Exception e) {
+            Assert.assertTrue(e instanceof UnprocessableEntityException);
+        }
+    }
+
+    @Test
+    public void test_get_apigateway_publishaddress4visualRangeError() {
+        try {
+            publishAddressWrapper.getAllPublishaddress("test", "v1", "", "12");
+        } catch (Exception e) {
+            Assert.assertTrue(e instanceof UnprocessableEntityException);
+        }
+    }
+
+    @Test
+    public void test_getPublishaddress4Rest() {
+        mockGetRest();
+        PublishAddress publishAddress = publishAddressWrapper.getPublishaddress("test", "v1", "", 0);
+        String test_address = "10.74.44.2:10080/api/test/v1";
+        Assert.assertEquals(test_address, publishAddress.toString());
+    }
+
+    @Test
+    public void test_getPublishaddress4UI() {
+        mockgetUi();
+        PublishAddress publishAddress = publishAddressWrapper.getPublishaddress("IUI_test", "", "", 1);
+        String test_address = "10.74.44.2:10080/iui/test_path";
+        Assert.assertEquals(test_address, publishAddress.toString());
+    }
+
+    @Test
+    public void test_getPublishaddress4HTTP() {
+        mockgetHttp();
+        PublishAddress publishAddress = publishAddressWrapper.getPublishaddress("test", "v1", "ns", 1);
+        String test_address = "10.74.44.1:10080/test/v1";
+        Assert.assertEquals(test_address, publishAddress.toString());
+    }
+
+    @Test
+    public void test_getPublishaddress4time() {
+        mockgetTcp();
+        PublishAddress publishAddress = publishAddressWrapper.getPublishaddress("tcp_test", "v1", "", 5);
+        String test_address = "10.74.44.2:10080/";
+        Assert.assertEquals(test_address, publishAddress.toString());
+    }
+
+    @Test
+    public void test_getApigatewayServiceInfo_in() {
+        Set<MicroServiceFullInfo> apigatewayList = publishAddressWrapper.getApigatewayServiceInfo("all", "1");
+        Iterator<MicroServiceFullInfo> apigateway = apigatewayList.iterator();
+
+        Set<NodeInfo> nodes = apigateway.next().getNodes();
+
+        NodeInfo testNode = new NodeInfo();
+        testNode.setIp("10.74.44.1");
+        testNode.setPort("10080");
+
+        NodeInfo node = new NodeInfo();
+        Iterator it = nodes.iterator();
+        while (it.hasNext()) {
+            node = (NodeInfo) it.next();
+        }
+
+        Assert.assertEquals(testNode, node);
+    }
+
+
+    /*
+     * @Test public void test_getApigatewayServiceInfo_out() { List<MicroServiceFullInfo>
+     * apigatewayList = publishAddressWrapper.getApigatewayServiceInfo("all", "0"); Set<NodeInfo>
+     * nodes = apigatewayList.get(0).getNodes();
+     * 
+     * NodeInfo testNode = new NodeInfo(); testNode.setIp("10.74.44.3"); testNode.setPort("80");
+     * 
+     * NodeInfo node = new NodeInfo(); Iterator it = nodes.iterator(); while (it.hasNext()) { node =
+     * (NodeInfo) it.next(); }
+     * 
+     * Assert.assertEquals(testNode, node); }
+     */
+
+    @Test
+    public void test_getApigatewayServiceInfo_422ERROR() {
+        try {
+            Set<MicroServiceFullInfo> apigatewayList = publishAddressWrapper.getApigatewayServiceInfo("test", "2");
+
+            Assert.fail("should not process to here.");
+        } catch (Exception e) {
+            Assert.assertTrue(e instanceof UnprocessableEntityException);
+        }
+
+    }
+
+
+
+    private void mockGetRest() {
+        PowerMockito.mockStatic(HttpClientUtil.class);
+        ConsulResponse<Object> consulResponse = new ConsulResponse(restJson, new BigInteger("1000"));
+        PowerMockito.when(HttpClientUtil.httpWaitGet(mockRestUrl)).thenReturn(consulResponse);
+    }
+
+    private void mockgetUi() {
+        PowerMockito.mockStatic(HttpClientUtil.class);
+        ConsulResponse<Object> consulResponse = new ConsulResponse(uiJson, new BigInteger("1000"));
+        PowerMockito.when(HttpClientUtil.httpWaitGet(mockUIUrl)).thenReturn(consulResponse);
+    }
+
+    private void mockgetHttp() {
+        PowerMockito.mockStatic(HttpClientUtil.class);
+        ConsulResponse<Object> consulResponse = new ConsulResponse(httpJson, new BigInteger("1000"));
+        PowerMockito.when(HttpClientUtil.httpWaitGet(mockHTTPUrl)).thenReturn(consulResponse);
+    }
+
+    private void mockgetTcp() {
+        PowerMockito.mockStatic(HttpClientUtil.class);
+        ConsulResponse<Object> consulResponse = new ConsulResponse(tcpJson, new BigInteger("1000"));
+        PowerMockito.when(HttpClientUtil.httpWaitGet(mockTCPUrl)).thenReturn(consulResponse);
+    }
+
+
+    private static void mockinitApigatewayInfo() {
+        mockApigatewayInfo4all();
+        mockApigatewayInfo4Default();
+        mockRouterInfo4all();
+        mockRouterInfo4Default();
+    }
+
+    private static void mockApigatewayInfo4all() {
+        MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();
+        serviceInfo.setServiceName("apigateway");
+        serviceInfo.setVersion("v1");
+        serviceInfo.setUrl("/api/microservices/v1");
+        serviceInfo.setProtocol("REST");
+        serviceInfo.setVisualRange("1");
+        serviceInfo.setNamespace("all");
+        Set<NodeInfo> nodes = new HashSet<NodeInfo>();
+        NodeInfo node = new NodeInfo();
+        node.setIp("10.74.44.1");
+        node.setPort("10080");
+        nodes.add(node);
+        serviceInfo.setNodes(nodes);
+
+        List<MicroServiceFullInfo> apigatewayList = new ArrayList<MicroServiceFullInfo>();
+        apigatewayList.add(serviceInfo);
+        publishAddressWrapper.publishApigateWayList.put("apigateway-all", apigatewayList);
+
+    }
+
+    private static void mockApigatewayInfo4Default() {
+        MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();
+        serviceInfo.setServiceName("apigateway");
+        serviceInfo.setVersion("v1");
+        serviceInfo.setUrl("/api/microservices/v1");
+        serviceInfo.setProtocol("REST");
+        serviceInfo.setVisualRange("1");
+        serviceInfo.setNamespace("default");
+        List<String> labels = new ArrayList<String>();
+        labels.add("apigateway:test|test2");
+        serviceInfo.setLabels(labels);
+        Set<NodeInfo> nodes = new HashSet<NodeInfo>();
+        NodeInfo node = new NodeInfo();
+        node.setIp("10.74.44.2");
+        node.setPort("10080");
+        nodes.add(node);
+        serviceInfo.setNodes(nodes);
+
+        List<MicroServiceFullInfo> apigatewayList = new ArrayList<MicroServiceFullInfo>();
+        apigatewayList.add(serviceInfo);
+        publishAddressWrapper.publishApigateWayList.put("apigateway-default", apigatewayList);
+    }
+
+
+    private static void mockRouterInfo4all() {
+        MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();
+        serviceInfo.setServiceName("router");
+        serviceInfo.setVersion("v1");
+        serviceInfo.setUrl("/api/microservices/v1");
+        serviceInfo.setProtocol("REST");
+        serviceInfo.setVisualRange("0");
+        serviceInfo.setNamespace("all");
+
+        Set<NodeInfo> nodes = new HashSet<NodeInfo>();
+        NodeInfo node = new NodeInfo();
+        node.setIp("10.74.44.3");
+        node.setPort("80");
+        nodes.add(node);
+        serviceInfo.setNodes(nodes);
+
+        List<MicroServiceFullInfo> apigatewayList = new ArrayList<MicroServiceFullInfo>();
+        apigatewayList.add(serviceInfo);
+        publishAddressWrapper.publishApigateWayList.put("router-all", apigatewayList);
+    }
+
+    private static void mockRouterInfo4Default() {
+        MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();
+        serviceInfo.setServiceName("router");
+        serviceInfo.setVersion("v1");
+        serviceInfo.setUrl("/api/microservices/v1");
+        serviceInfo.setProtocol("REST");
+        serviceInfo.setVisualRange("0");
+        serviceInfo.setNamespace("default");
+        serviceInfo.setNetwork_plane_type("net|net2");
+        List<KeyVaulePair> metadatas = new ArrayList<KeyVaulePair>();
+        metadatas.add(new KeyVaulePair("routeWay", "domain"));
+        metadatas.add(new KeyVaulePair("routeSubdomain", "openpalette.zte.com.cn"));
+        serviceInfo.setMetadata(metadatas);
+
+        Set<NodeInfo> nodes = new HashSet<NodeInfo>();
+        NodeInfo node = new NodeInfo();
+        node.setIp("10.74.44.4");
+        node.setPort("80");
+        nodes.add(node);
+        serviceInfo.setNodes(nodes);
+
+        List<MicroServiceFullInfo> apigatewayList = new ArrayList<MicroServiceFullInfo>();
+        apigatewayList.add(serviceInfo);
+        publishAddressWrapper.publishApigateWayList.put("router-default", apigatewayList);
+    }
+
+}
diff --git a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/util/HttpClientUtilTest.java b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/util/HttpClientUtilTest.java
new file mode 100644 (file)
index 0000000..3ccfc4e
--- /dev/null
@@ -0,0 +1,95 @@
+/**
+ * Copyright 2016-2017 ZTE, Inc. and others.
+ * 
+ * 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 org.onap.msb.sdclient.wrapper.util;
+
+import java.io.IOException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+public class HttpClientUtilTest {
+
+    private String testIp = "http://127.0.0.1:8500";
+    private String errorIp = "http://127.0.0.3:8500";
+
+    @Test
+    public void test_httpGetStatus() {
+        try {
+            int result = HttpClientUtil.httpGetStatus(testIp);
+            if (result == 200) {
+                Assert.assertEquals("Consul Agent", HttpClientUtil.httpGet(testIp));
+            } else {
+                Assert.assertEquals(500, result);
+            }
+
+
+        } catch (Exception e) {
+            Assert.assertTrue(e instanceof IOException);
+        }
+    }
+
+    @Test
+    public void test_httpGet_fail() {
+        try {
+            Assert.assertNull(HttpClientUtil.httpGet(errorIp));
+
+
+        } catch (Exception e) {
+            Assert.assertTrue(e instanceof IOException);
+        }
+    }
+
+    @Test
+    public void test_httpWaitGet_fail() {
+        try {
+            Assert.assertNull(HttpClientUtil.httpWaitGet(errorIp));
+
+
+        } catch (Exception e) {
+            Assert.assertTrue(e instanceof IOException);
+        }
+    }
+
+
+    @Test
+    public void test_httpdelete() {
+        try {
+            HttpClientUtil.delete(testIp, "service");
+
+        } catch (Exception e) {
+            Assert.assertTrue(e instanceof IOException);
+        }
+    }
+
+    @Test
+    public void test_httpPostWithJSON() {
+        String url = testIp + "/v1/catalog/service";
+        String json =
+                        "[{\"Node\":\"server\",\"Address\":\"127.0.0.1\",\"TaggedAddresses\":{\"lan\":\"127.0.0.1\",\"wan\":\"127.0.0.1\"},\"ServiceID\":\"_CJ-SNMP_10.74.216.65_12005\",\"ServiceName\":\"CJ-SNMP\",\"ServiceAddress\":\"10.74.216.65\",\"ServicePort\":12005,\"ServiceEnableTagOverride\":false,\"CreateIndex\":1813280,\"ModifyIndex\":1815062}]";
+
+        try {
+            int result = HttpClientUtil.httpPostWithJSON(url, json);
+            if (result == 201) {
+                Assert.assertEquals("Consul Agent", HttpClientUtil.httpGet(testIp));
+            } else {
+                Assert.assertEquals(0, result);
+            }
+
+        } catch (Exception e) {
+            Assert.assertTrue(e instanceof IOException);
+        }
+    }
+}
diff --git a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/util/JacksonJsonUtilTest.java b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/util/JacksonJsonUtilTest.java
new file mode 100644 (file)
index 0000000..e80419f
--- /dev/null
@@ -0,0 +1,101 @@
+/**
+ * Copyright 2016-2017 ZTE, Inc. and others.
+ * 
+ * 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 org.onap.msb.sdclient.wrapper.util;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.onap.msb.sdclient.core.CatalogService;
+import org.onap.msb.sdclient.core.HealthService;
+import org.onap.msb.sdclient.core.NodeAddress;
+
+
+public class JacksonJsonUtilTest {
+    @Test
+    public void testBeanToJson() {
+        try {
+            NodeAddress address = new NodeAddress("127.0.0.1", "80");
+            String json = JacksonJsonUtil.beanToJson(address);
+            Assert.assertEquals("{\"ip\":\"127.0.0.1\",\"port\":\"80\"}", json);
+        } catch (Exception e) {
+            Assert.fail("Exception" + e.getMessage());
+        }
+    }
+
+    @Test
+    public void testJsonToBean() {
+        try {
+            String json = "{\"ip\":\"127.0.0.1\",\"port\":\"80\"}";
+            NodeAddress address = (NodeAddress) JacksonJsonUtil.jsonToBean(json, NodeAddress.class);
+            Assert.assertEquals("127.0.0.1", address.getIp());
+            Assert.assertEquals("80", address.getPort());
+        } catch (Exception e) {
+            Assert.fail("Exception" + e.getMessage());
+        }
+    }
+
+
+
+    @Test
+    public void testJsonToListBean() {
+        try {
+            String json =
+                            "[{\"Node\":{\"Node\":\"A23179111\",\"Address\":\"10.74.44.27\",\"CreateIndex\":3,\"ModifyIndex\":318},\"Service\":{\"ID\":\"oo_10.74.56.36_5656\",\"Service\":\"oo\",\"Tags\":[\"url:/root\",\"protocol:REST\",\"version:\",\"visualRange:0|1\",\"ttl:-1\",\"status:1\",\"lb_policy:client_custom\",\"lb_server_params:weight=1 max_fails=1 fail_timeout=16s\",\"checkType:TCP\",\"checkInterval:10\",\"checkUrl:10.56.23.63:8989\"],\"Address\":\"10.74.56.36\",\"Port\":5656,\"EnableTagOverride\":false,\"CreateIndex\":314,\"ModifyIndex\":318},\"Checks\":[{\"Node\":\"A23179111\",\"CheckID\":\"serfHealth\",\"Name\":\"Serf Health Status\",\"Status\":\"passing\",\"Notes\":\"\",\"Output\":\"Agent alive and reachable\",\"ServiceID\":\"\",\"ServiceName\":\"\",\"CreateIndex\":3,\"ModifyIndex\":3},{\"Node\":\"A23179111\",\"CheckID\":\"service:oo_10.74.56.36_5656\",\"Name\":\"Service 'oo' check\",\"Status\":\"critical\",\"Notes\":\"\",\"Output\":\"\",\"ServiceID\":\"oo_10.74.56.36_5656\",\"ServiceName\":\"oo\",\"CreateIndex\":314,\"ModifyIndex\":318}]},{\"Node\":{\"Node\":\"A23179111\",\"Address\":\"10.74.44.27\",\"CreateIndex\":3,\"ModifyIndex\":318},\"Service\":{\"ID\":\"oo_10.78.36.36_111\",\"Service\":\"oo\",\"Tags\":[\"url:/root\",\"protocol:REST\",\"version:\",\"visualRange:0|1\",\"ttl:-1\",\"status:1\",\"lb_policy:client_custom\"],\"Address\":\"10.78.36.36\",\"Port\":111,\"EnableTagOverride\":false,\"CreateIndex\":315,\"ModifyIndex\":315},\"Checks\":[{\"Node\":\"A23179111\",\"CheckID\":\"serfHealth\",\"Name\":\"Serf Health Status\",\"Status\":\"passing\",\"Notes\":\"\",\"Output\":\"Agent alive and reachable\",\"ServiceID\":\"\",\"ServiceName\":\"\",\"CreateIndex\":3,\"ModifyIndex\":3}]}]";
+
+            List<HealthService> list =
+                            JacksonJsonUtil.jsonToListBean(json, new TypeReference<List<HealthService>>() {});
+
+            Assert.assertEquals(2, list.size());
+            Assert.assertEquals("10.74.44.27", list.get(0).getNode().getAddress());
+        } catch (Exception e) {
+            Assert.fail("Exception" + e.getMessage());
+        }
+    }
+
+    @Test
+    public void testJsonToList4CatalogService() {
+        try {
+            String json =
+                            "[{\"Node\":\"server\",\"Address\":\"127.0.0.1\",\"TaggedAddresses\":{\"lan\":\"127.0.0.1\",\"wan\":\"127.0.0.1\"},\"ServiceID\":\"_CJ-SNMP_10.74.216.65_12005\",\"ServiceName\":\"CJ-SNMP\",\"ServiceAddress\":\"10.74.216.65\",\"ServicePort\":12005,\"ServiceEnableTagOverride\":false,\"CreateIndex\":1813280,\"ModifyIndex\":1815062}]";
+
+            List<CatalogService> list = JacksonJsonUtil.jsonToListBean(json);
+
+            Assert.assertEquals(1, list.size());
+            Assert.assertEquals("CJ-SNMP", list.get(0).getServiceName());
+        } catch (Exception e) {
+            Assert.fail("Exception" + e.getMessage());
+        }
+    }
+
+    @Test
+    public void testjsonToMapBean() {
+        try {
+            String json =
+                            "{\"1YM-PM-TASK\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"REST\\\",\\\"is_manual\\\":\\\"true\\\",\\\"version\\\":\\\"v1\\\",\\\"url\\\":\\\"/api/pm-task/v1\\\",\\\"status\\\":\\\"1\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"1|0\\\"}\"],\"CJ-FM-history\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"REST\\\",\\\"is_manual\\\":\\\"true\\\",\\\"version\\\":\\\"v1\\\",\\\"url\\\":\\\"/api/fm-history/v1\\\",\\\"status\\\":\\\"1\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"1|0\\\"}\"]}";
+
+            Map<String, String[]> map = JacksonJsonUtil.jsonToMapBean(json);
+
+            Assert.assertEquals(2, map.size());
+
+        } catch (Exception e) {
+            Assert.fail("Exception" + e.getMessage());
+        }
+    }
+
+
+}
diff --git a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/util/RegExpTestUtilTest.java b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/util/RegExpTestUtilTest.java
new file mode 100644 (file)
index 0000000..194e63e
--- /dev/null
@@ -0,0 +1,101 @@
+/**
+ * Copyright 2016-2017 ZTE, Inc. and others.
+ * 
+ * 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 org.onap.msb.sdclient.wrapper.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+
+public class RegExpTestUtilTest {
+
+    @Test
+    public void testhostRegExpTest() {
+        Assert.assertTrue(RegExpTestUtil.hostRegExpTest("127.0.0.1:8080"));
+
+        Assert.assertFalse(RegExpTestUtil.hostRegExpTest("0.0.0.1:89"));
+    }
+
+    @Test
+    public void testhttpUrlRegExpTest() {
+        Assert.assertTrue(RegExpTestUtil.httpUrlRegExpTest("http://10.74.151.26:8989"));
+
+        Assert.assertFalse(RegExpTestUtil.httpUrlRegExpTest("httpr://0.74.0.26:8989"));
+    }
+
+    @Test
+    public void testipRegExpTest() {
+
+        Assert.assertTrue(RegExpTestUtil.ipRegExpTest("10.74.151.26"));
+
+        Assert.assertFalse(RegExpTestUtil.ipRegExpTest("0.74.0.26"));
+    }
+
+    @Test
+    public void testportRegExpTest() {
+        Assert.assertTrue(RegExpTestUtil.portRegExpTest("8989"));
+
+        Assert.assertFalse(RegExpTestUtil.portRegExpTest("99999"));
+    }
+
+    @Test
+    public void testversionRegExpTest() {
+
+        Assert.assertTrue(RegExpTestUtil.versionRegExpTest("v1"));
+
+        Assert.assertFalse(RegExpTestUtil.versionRegExpTest("vv2"));
+    }
+
+    @Test
+    public void testurlRegExpTest() {
+
+        Assert.assertTrue(RegExpTestUtil.urlRegExpTest("/test/v1"));
+
+        Assert.assertTrue(RegExpTestUtil.urlRegExpTest("/"));
+
+        Assert.assertFalse(RegExpTestUtil.urlRegExpTest("test/#?qwe"));
+    }
+
+    @Test
+    public void testserviceNameRegExpTest() {
+
+        Assert.assertTrue(RegExpTestUtil.serviceNameRegExpTest("servive_1"));
+
+
+        Assert.assertFalse(RegExpTestUtil.serviceNameRegExpTest("servive%_1"));
+    }
+
+
+    @Test
+    public void testapiRouteUrlRegExpTest() {
+
+        Assert.assertTrue(RegExpTestUtil.apiRouteUrlRegExpTest("/api/service/v1"));
+
+
+        Assert.assertFalse(RegExpTestUtil.apiRouteUrlRegExpTest("/servive"));
+    }
+
+    @Test
+    public void testlabelRegExpTest() {
+
+        Assert.assertTrue(RegExpTestUtil.labelRegExpTest("key:value,key2:value2"));
+
+
+        Assert.assertFalse(RegExpTestUtil.labelRegExpTest("keyvalue,key2*value2"));
+    }
+
+
+
+}