diff options
author | tanghua <tang.hua52@zte.com.cn> | 2018-01-23 16:02:56 +0800 |
---|---|---|
committer | tanghua <tang.hua52@zte.com.cn> | 2018-01-23 16:02:56 +0800 |
commit | 96ae3343a0623e29fc20983d4ae26dae0ce8d0f1 (patch) | |
tree | 6a56d369db46bcefe1b183c0c26097c7ba84acb4 /sdclient/discovery-service/src/test | |
parent | 4d6d7250c84da7e8896b0585a53ee6cbd3143925 (diff) |
50% Code Coverage-MSB Discovery
Issue-ID: MSB-114
Change-Id: Ibf5f0acbd85fc7f4abbaae41ebbc04d6edc13770
Signed-off-by: tanghua <tang.hua52@zte.com.cn>
Diffstat (limited to 'sdclient/discovery-service/src/test')
4 files changed, 343 insertions, 3 deletions
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 index 73b5560..401d335 100644 --- 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 @@ -15,6 +15,7 @@ package org.onap.msb.sdclient.wrapper; import java.math.BigInteger; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -23,7 +24,9 @@ import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; +import org.onap.msb.sdclient.DiscoverAppConfig; 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.MicroServiceInfo; import org.onap.msb.sdclient.core.Node; @@ -31,13 +34,14 @@ 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.ConfigUtil; 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}) +@PrepareForTest({HttpClientUtil.class,ConfigUtil.class,DiscoverAppConfig.class}) public class ConsulServiceWrapperTest { private static final String restJson = @@ -50,11 +54,27 @@ public class ConsulServiceWrapperTest { 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 final String mockdeltUrl4agent = "http://127.0.0.1:8500/v1/agent/deregister"; + + private static final String mockgetListUrl = "http://127.0.0.1:8500/v1/catalog/services"; + private static final String restListJson = + "{\"consul\":[],\"test-tt\":[\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0\\\"}\",\"\\\"base\\\":{\\\"version\\\":\\\"v1\\\",\\\"protocol\\\":\\\"REST\\\",\\\"url\\\":\\\"/api/microservices/v1\\\",\\\"visualRange\\\":\\\"0\\\"}\",\"\\\"ns\\\":{\\\"namespace\\\":\\\"tt\\\"}\"]}"; private static ConsulServiceWrapper consulServiceWrapper = ConsulServiceWrapper.getInstance(); + + @Test + public void test_getAllMicroServiceInstances(){ + mockGetList(); + List<MicroServiceFullInfo> serviceList=consulServiceWrapper.getAllMicroServiceInstances(); + Assert.assertEquals(1, serviceList.size()); + MicroServiceFullInfo service=serviceList.get(0); + Assert.assertEquals( "test-tt",service.getServiceName()); + Assert.assertEquals( "",service.getNamespace()); + } + + @Test public void test_getMicroServiceInstance() { mockGetRest(); @@ -77,6 +97,103 @@ public class ConsulServiceWrapperTest { MicroServiceInfo serviceInfo = new MicroServiceInfo(); serviceInfo.setServiceName("test"); serviceInfo.setVersion("v1"); + serviceInfo.setNamespace("ns"); + serviceInfo.setPublish_port("8086"); + serviceInfo.setUrl("/api/test/v1"); + serviceInfo.setProtocol("REST"); + serviceInfo.setVisualRange("1"); + serviceInfo.setLb_policy("ip_hash"); + serviceInfo.setHost("host"); + serviceInfo.setPath("/test"); + serviceInfo.setNetwork_plane_type("net"); + + List<KeyVaulePair> metadata=new ArrayList<KeyVaulePair>(); + metadata.add(new KeyVaulePair("key1","val1")); + metadata.add(new KeyVaulePair("key2","val2")); + serviceInfo.setMetadata(metadata); + + + List<String> labels=new ArrayList<String>(); + labels.add("111:111"); + labels.add("222:222"); + serviceInfo.setLabels(labels); + + Set<Node> nodes = new HashSet<Node>(); + Node node = new Node(); + node.setIp("10.74.44.1"); + node.setPort("10080"); + node.setLb_server_params("weight=1,max_fails=5,fail_timeout=8"); + node.setHa_role("active"); + node.setCheckType("HTTP"); + node.setCheckInterval("10"); + node.setCheckTimeOut("10"); + node.setCheckUrl("http://check"); + 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_saveMicroServiceInstance2() { + MicroServiceInfo serviceInfo = new MicroServiceInfo(); + serviceInfo.setServiceName("test"); + serviceInfo.setVersion("v1"); + serviceInfo.setNamespace("ns"); + serviceInfo.setPublish_port("28005"); + serviceInfo.setUrl("/api/test/v1"); + serviceInfo.setProtocol("TCP"); + serviceInfo.setVisualRange("1"); + serviceInfo.setLb_policy("ip_hash"); + serviceInfo.setHost("host"); + serviceInfo.setPath("/test"); + serviceInfo.setNetwork_plane_type("net"); + + List<KeyVaulePair> metadata=new ArrayList<KeyVaulePair>(); + metadata.add(new KeyVaulePair("key1","val1")); + metadata.add(new KeyVaulePair("key2","val2")); + serviceInfo.setMetadata(metadata); + + + List<String> labels=new ArrayList<String>(); + labels.add("111:111"); + labels.add("222:222"); + serviceInfo.setLabels(labels); + + Set<Node> nodes = new HashSet<Node>(); + Node node = new Node(); + node.setIp("10.74.44.1"); + node.setPort("28005"); + node.setLb_server_params("weight=1,max_fails=5,fail_timeout=8"); + node.setHa_role("active"); + node.setCheckType("TCP"); + node.setCheckInterval("10"); + node.setCheckTimeOut("10"); + node.setCheckUrl("tcp://check"); + 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_saveMicroServiceInstance4agent() { + MicroServiceInfo serviceInfo = new MicroServiceInfo(); + serviceInfo.setServiceName("test"); + serviceInfo.setVersion("v1"); serviceInfo.setUrl("/api/test/v1"); serviceInfo.setProtocol("REST"); serviceInfo.setVisualRange("1"); @@ -87,6 +204,12 @@ public class ConsulServiceWrapperTest { nodes.add(node); serviceInfo.setNodes(nodes); + PowerMockito.mockStatic(System.class); + PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("agent"); + DiscoverAppConfig discoverConfig=PowerMockito.mock(DiscoverAppConfig.class); + ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig); + + mockGetRest4null(); // mockGetPost(); try { @@ -94,6 +217,9 @@ public class ConsulServiceWrapperTest { } catch (Exception e) { Assert.assertEquals("HTTP 500 Internal Server Error", e.getMessage()); } + + PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("catalog"); + ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig); } @Test @@ -102,13 +228,31 @@ public class ConsulServiceWrapperTest { 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_deleteMicroService4agent() { + mockGet4Delete(); + mockDelete4agent(); + + DiscoverAppConfig discoverConfig=PowerMockito.mock(DiscoverAppConfig.class); + PowerMockito.mockStatic(System.class); + PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("agent"); + ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig); + + consulServiceWrapper.deleteMicroService("test", "v1", ""); + + PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("catalog"); + ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig); + } + + @Test public void test_healthCheckbyTTL() { @@ -131,6 +275,15 @@ public class ConsulServiceWrapperTest { PowerMockito.when(HttpClientUtil.httpGet(mockdel_gettUrl)).thenReturn(catalogJson); } + + private void mockGetList() { + PowerMockito.mockStatic(HttpClientUtil.class); + PowerMockito.when(HttpClientUtil.httpGet(mockgetListUrl)).thenReturn(restListJson); + + ConsulResponse<Object> consulResponse = new ConsulResponse(restJson, new BigInteger("1000")); + PowerMockito.when(HttpClientUtil.httpWaitGet("http://127.0.0.1:8500/v1/health/service/test-tt")).thenReturn(consulResponse); + + } private void mockGet4healthCheck() { PowerMockito.mockStatic(HttpClientUtil.class); @@ -147,6 +300,12 @@ public class ConsulServiceWrapperTest { PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockdeltUrl, serviceJson)).thenReturn(200); } + + private void mockDelete4agent() { + PowerMockito.when(HttpClientUtil.httpPostWithJSON("http://127.0.0.1:8500/v1/agent/service/deregister/_test_10.74.56.36_5656", "")).thenReturn(200); + + } + diff --git a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/consul/model/health/NodeTest.java b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/consul/model/health/NodeTest.java new file mode 100644 index 0000000..17ca410 --- /dev/null +++ b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/consul/model/health/NodeTest.java @@ -0,0 +1,50 @@ +/** + * 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.consul.model.health; + +import org.junit.Assert; +import org.junit.Test; + +public class NodeTest { + @Test + public void testImmutableNode() { + ImmutableNode node0 = ImmutableNode.builder().address("10.74.151.26") + .node("test").build(); + Assert.assertEquals("10.74.151.26",node0.getAddress()); + Assert.assertEquals("test",node0.getNode()); + + + ImmutableNode node1 = node0.withAddress("10.74.151.27").withNode("test2"); + + Assert.assertFalse(node0.equals(node1)); + + System.out.println(node1.hashCode()); + + ImmutableNode node2 = ImmutableNode.builder().from(node1) + .build(); + Assert.assertEquals("10.74.151.27", node2.getAddress()); + + ImmutableNode node3 = ImmutableNode.copyOf(node1); + Assert.assertTrue(node3.equals(node1)); + } + + @Test + public void testtoString() { + ImmutableNode node0 = ImmutableNode.builder().address("10.74.151.26") + .node("test").build(); + String nodeInfo="Node{node=test, address=10.74.151.26}"; + Assert.assertEquals(nodeInfo, node0.toString()); + } +} diff --git a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/consul/model/health/ServiceTest.java b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/consul/model/health/ServiceTest.java new file mode 100644 index 0000000..a54f376 --- /dev/null +++ b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/consul/model/health/ServiceTest.java @@ -0,0 +1,54 @@ +/** + * 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.consul.model.health; + +import org.junit.Assert; +import org.junit.Test; + +public class ServiceTest { + @Test + public void testImmutableService() { + ImmutableService service0 = ImmutableService.builder() + .id("huangleibo_id").port(0).address("").service("huangleibo") + .addTags("111", "222").addTags("333").build(); + Assert.assertEquals("huangleibo_id", service0.getId()); + + + ImmutableService service1 = service0.withId("huangleibo_id") + .withId("new_id").withService("huangleibo") + .withService("new_service").withTags("new_tags") + .withAddress("").withAddress("new_address").withPort(0) + .withPort(1); + + Assert.assertFalse(service0.equals(service1)); + + System.out.println(service1.hashCode()); + + ImmutableService service2 = ImmutableService.builder().from(service1) + .build(); + Assert.assertEquals("new_id", service2.getId()); + + ImmutableService service3 = ImmutableService.copyOf(service2); + Assert.assertTrue(service3.equals(service2)); + } + + @Test + public void testtoString() { + ImmutableService service0 = ImmutableService.builder() + .id("huangleibo_id").port(0).address("").service("huangleibo") + .addTags("111", "222").build(); + String nodeInfo="Service{id=huangleibo_id, service=huangleibo, tags=[111, 222], address=, port=0}"; + Assert.assertEquals(nodeInfo, service0.toString()); + } +} diff --git a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/util/ConfigUtilTest.java b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/util/ConfigUtilTest.java new file mode 100644 index 0000000..b8e45f1 --- /dev/null +++ b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/util/ConfigUtilTest.java @@ -0,0 +1,77 @@ +/** + * 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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.msb.sdclient.DiscoverAppConfig; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({DiscoverAppConfig.class,ConfigUtil.class}) +@PowerMockIgnore( {"javax.management.*"}) +public class ConfigUtilTest { + + private static ConfigUtil configUtil = ConfigUtil.getInstance(); + private static DiscoverAppConfig discoverConfig; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + discoverConfig=PowerMockito.mock(DiscoverAppConfig.class); + PowerMockito.when(discoverConfig.getConsulAdderss()).thenReturn("127.0.0.1:8500"); + PowerMockito.when(discoverConfig.getConsulRegisterMode()).thenReturn("catalog"); + + } + + @Test + public void testinitConsulClientInfo() { + configUtil.initConsulClientInfo(discoverConfig); + Assert.assertEquals("127.0.0.1:8500",configUtil.getConsulAddress()); + + PowerMockito.mockStatic(System.class); + PowerMockito.when(System.getenv("CONSUL_IP")).thenReturn("10.74.151.26"); + configUtil.initConsulClientInfo(discoverConfig); + Assert.assertEquals("10.74.151.26:8500",configUtil.getConsulAddress()); + + } + + @Test + public void testinitTCP_UDP_portRange() { + PowerMockito.mockStatic(System.class); + PowerMockito.when(System.getenv("TCP_UDP_PORT_RANGE_START")).thenReturn("8500"); + PowerMockito.when(System.getenv("TCP_UDP_PORT_RANGE_END")).thenReturn("8600"); + + configUtil.initTCP_UDP_portRange(); + + Assert.assertEquals("8500",configUtil.getTcpudpPortRangeStart()); + Assert.assertEquals("8600",configUtil.getTcpudpPortRangeEnd()); + } + + @Test + public void testinitConsulRegisterMode() { + + configUtil.initConsulRegisterMode(discoverConfig); + Assert.assertEquals("catalog",configUtil.getConsulRegisterMode()); + + PowerMockito.mockStatic(System.class); + PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("agent"); + configUtil.initConsulRegisterMode(discoverConfig); + Assert.assertEquals("agent",configUtil.getConsulRegisterMode()); + } +} |