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 | |
parent | 4d6d7250c84da7e8896b0585a53ee6cbd3143925 (diff) |
50% Code Coverage-MSB Discovery
Issue-ID: MSB-114
Change-Id: Ibf5f0acbd85fc7f4abbaae41ebbc04d6edc13770
Signed-off-by: tanghua <tang.hua52@zte.com.cn>
8 files changed, 349 insertions, 180 deletions
diff --git a/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapper.java b/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapper.java index 2d3900c..87d2b84 100644 --- a/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapper.java +++ b/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapper.java @@ -1874,23 +1874,7 @@ public class ConsulServiceWrapper { } - // 检查同名不同协议注册 - try { - List<MicroServiceFullInfo> serviceList = getMicroServiceForNodes(microServiceInfo.getServiceName(), - microServiceInfo.getVersion(), false, "", microServiceInfo.getNamespace()); - if (serviceList != null && serviceList.size() > 0) { - for (MicroServiceFullInfo service : serviceList) { - if (!service.getProtocol().equalsIgnoreCase(microServiceInfo.getProtocol())) { - throw new UnprocessableEntityException( - "register MicroServiceInfo FAIL:There is a same service ,but different protocol--" - + service.getProtocol()); - } - - } - } - } catch (ExtendedNotFoundException e) { - // LOGGER.info("register MicroServiceInfo CHECK ok for protocol:service is not fond"); - } + } diff --git a/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/consul/model/health/ImmutableNode.java b/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/consul/model/health/ImmutableNode.java index b7083b4..a3f3de3 100644 --- a/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/consul/model/health/ImmutableNode.java +++ b/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/consul/model/health/ImmutableNode.java @@ -129,57 +129,7 @@ public final class ImmutableNode extends Node { return MoreObjects.toStringHelper("Node").add("node", node).add("address", address).toString(); } - /** - * Utility type used to correctly read immutable object from JSON representation. - * - * @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding - * infrastructure - */ - @Deprecated - @JsonDeserialize - static final class Json extends Node { - String node; - String address; - - @JsonProperty(value = "Node") - public void setNode(String node) { - this.node = node; - } - - @JsonProperty(value = "Address") - public void setAddress(String address) { - this.address = address; - } - - @Override - public String getNode() { - throw new UnsupportedOperationException(); - } - - @Override - public String getAddress() { - throw new UnsupportedOperationException(); - } - } - - /** - * @param json A JSON-bindable data structure - * @return An immutable value type - * @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding - * infrastructure - */ - @Deprecated - @JsonCreator - static ImmutableNode fromJson(Json json) { - ImmutableNode.Builder builder = ImmutableNode.builder(); - if (json.node != null) { - builder.node(json.node); - } - if (json.address != null) { - builder.address(json.address); - } - return builder.build(); - } + /** * Creates an immutable copy of a {@link Node} value. Uses accessors to get values to initialize diff --git a/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/consul/model/health/ImmutableService.java b/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/consul/model/health/ImmutableService.java index b7167da..acf5a5b 100644 --- a/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/consul/model/health/ImmutableService.java +++ b/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/consul/model/health/ImmutableService.java @@ -228,101 +228,7 @@ public final class ImmutableService extends Service { .add("address", address).add("port", port).toString(); } - /** - * Utility type used to correctly read immutable object from JSON representation. - * - * @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding - * infrastructure - */ - @Deprecated - @JsonDeserialize - static final class Json extends Service { - String id; - String service; - List<String> tags = ImmutableList.of(); - String address; - Integer port; - - @JsonProperty(value = "ID") - public void setId(String id) { - this.id = id; - } - - @JsonProperty(value = "Service") - public void setService(String service) { - this.service = service; - } - - @JsonProperty(value = "Tags") - @JsonDeserialize(as = ImmutableList.class, contentAs = String.class) - public void setTags(List<String> tags) { - this.tags = tags; - } - - @JsonProperty(value = "Address") - public void setAddress(String address) { - this.address = address; - } - - @JsonProperty(value = "Port") - public void setPort(int port) { - this.port = port; - } - - @Override - public String getId() { - throw new UnsupportedOperationException(); - } - - @Override - public String getService() { - throw new UnsupportedOperationException(); - } - - @Override - public List<String> getTags() { - throw new UnsupportedOperationException(); - } - - @Override - public String getAddress() { - throw new UnsupportedOperationException(); - } - - @Override - public int getPort() { - throw new UnsupportedOperationException(); - } - } - - /** - * @param json A JSON-bindable data structure - * @return An immutable value type - * @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding - * infrastructure - */ - @Deprecated - @JsonCreator - static ImmutableService fromJson(Json json) { - ImmutableService.Builder builder = ImmutableService.builder(); - if (json.id != null) { - builder.id(json.id); - } - if (json.service != null) { - builder.service(json.service); - } - if (json.tags != null) { - builder.addAllTags(json.tags); - } - if (json.address != null) { - builder.address(json.address); - } - if (json.port != null) { - builder.port(json.port); - } - return builder.build(); - } - + /** * Creates an immutable copy of a {@link Service} value. Uses accessors to get values to * initialize the new immutable instance. If an instance is already immutable, it is returned as diff --git a/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/util/JacksonJsonUtil.java b/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/util/JacksonJsonUtil.java index 57c6c66..acbb17d 100644 --- a/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/util/JacksonJsonUtil.java +++ b/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/util/JacksonJsonUtil.java @@ -109,8 +109,7 @@ public class JacksonJsonUtil { vo = objectMapper.readValue(json, new TypeReference<List<CatalogService>>() {}); } catch (Exception e) { - String errorMsg = " JsonTobean faild:" + e.getMessage(); - logger.error(errorMsg); + logger.error(" JsonTobean faild:" + e.getMessage()); } return vo; } @@ -120,12 +119,10 @@ public class JacksonJsonUtil { ObjectMapper objectMapper = getMapperInstance(); - return objectMapper.readValue(json, valueTypeRef); } catch (Exception e) { - String errorMsg = " JsonTobean faild:" + e.getMessage(); - logger.error(errorMsg); + logger.error(" JsonTobean faild:" + e.getMessage()); } return null; } @@ -150,20 +147,12 @@ public class JacksonJsonUtil { vo = objectMapper.readValue(json, new TypeReference<Map<String, String[]>>() {}); } catch (Exception e) { - String errorMsg = " JsonTobean faild"; - logger.error(errorMsg); + logger.error("JsonTobean faild"); } return vo; } - public static void main(String[] args) { - 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 = jsonToListBean(json, new TypeReference<List<HealthService>>() {}); - System.out.println(list); - - } - } 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()); + } +} |