2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
15 package org.onap.msb.sdclient.wrapper;
17 import java.math.BigInteger;
18 import java.util.HashSet;
19 import java.util.List;
22 import org.junit.Assert;
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.onap.msb.sdclient.core.ConsulResponse;
27 import org.onap.msb.sdclient.core.MicroServiceFullInfo;
28 import org.onap.msb.sdclient.core.MicroServiceInfo;
29 import org.onap.msb.sdclient.core.Node;
30 import org.onap.msb.sdclient.core.NodeAddress;
31 import org.onap.msb.sdclient.core.NodeInfo;
32 import org.onap.msb.sdclient.core.exception.ExtendedNotFoundException;
33 import org.onap.msb.sdclient.core.exception.UnprocessableEntityException;
34 import org.onap.msb.sdclient.wrapper.util.HttpClientUtil;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
39 @RunWith(PowerMockRunner.class)
40 @PrepareForTest({HttpClientUtil.class})
41 public class ConsulServiceWrapperTest {
43 private static final String restJson =
44 "[{\"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\"}]}]";
45 private static final String catalogJson =
46 "[{\"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}]";
47 private static final String catalog4ttlJson =
48 "[{\"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}]";
49 private static final String mockRestUrl = "http://127.0.0.1:8500/v1/health/service/test";
50 private static final String mockPostUrl = "http://127.0.0.1:8500/v1/catalog/register";
51 private static final String mockdel_gettUrl = "http://127.0.0.1:8500/v1/catalog/service/test";
52 private static final String mockdeltUrl = "http://127.0.0.1:8500/v1/catalog/deregister";
56 private static ConsulServiceWrapper consulServiceWrapper = ConsulServiceWrapper.getInstance();
59 public void test_getMicroServiceInstance() {
61 MicroServiceFullInfo service = consulServiceWrapper.getMicroServiceInstance("test", "v1", "");
62 Assert.assertEquals(service.getServiceName(), "test");
63 Assert.assertEquals(service.getProtocol(), "REST");
67 public void test_getMicroServiceForNodes() {
69 List<MicroServiceFullInfo> services = consulServiceWrapper.getMicroServiceForNodes("test", "v1", false, "", "");
70 Assert.assertEquals(1, services.size());
71 Assert.assertEquals(services.get(0).getServiceName(), "test");
72 Assert.assertEquals(services.get(0).getProtocol(), "REST");
76 public void test_saveMicroServiceInstance() {
77 MicroServiceInfo serviceInfo = new MicroServiceInfo();
78 serviceInfo.setServiceName("test");
79 serviceInfo.setVersion("v1");
80 serviceInfo.setUrl("/api/test/v1");
81 serviceInfo.setProtocol("REST");
82 serviceInfo.setVisualRange("1");
83 Set<Node> nodes = new HashSet<Node>();
84 Node node = new Node();
85 node.setIp("10.74.44.1");
86 node.setPort("10080");
88 serviceInfo.setNodes(nodes);
93 consulServiceWrapper.saveMicroServiceInstance(serviceInfo, true, "127.0.0.1", true);
94 } catch (Exception e) {
95 Assert.assertEquals("HTTP 500 Internal Server Error", e.getMessage());
100 public void test_deleteMicroService() {
103 consulServiceWrapper.deleteMicroService("test", "v1", "");
107 public void test_deleteMicroServiceInstance() {
110 consulServiceWrapper.deleteMicroServiceInstance("test", "v1", "", "10.74.56.36", "5656");
114 public void test_healthCheckbyTTL() {
115 NodeAddress nodeAddress = new NodeAddress("10.74.56.36", "5656");
116 mockGet4healthCheck();
117 consulServiceWrapper.healthCheckbyTTL("test", "v1", "", nodeAddress);
122 private void mockGetRest() {
123 PowerMockito.mockStatic(HttpClientUtil.class);
124 ConsulResponse<Object> consulResponse = new ConsulResponse(restJson, new BigInteger("1000"));
125 PowerMockito.when(HttpClientUtil.httpWaitGet(mockRestUrl)).thenReturn(consulResponse);
129 private void mockGet4Delete() {
130 PowerMockito.mockStatic(HttpClientUtil.class);
131 PowerMockito.when(HttpClientUtil.httpGet(mockdel_gettUrl)).thenReturn(catalogJson);
135 private void mockGet4healthCheck() {
136 PowerMockito.mockStatic(HttpClientUtil.class);
137 PowerMockito.when(HttpClientUtil.httpGet(mockdel_gettUrl)).thenReturn(catalog4ttlJson);
140 String checkUrl = "http://127.0.0.1:8500/v1/agent/check/pass/service:_test_10.74.56.36_5656";
141 PowerMockito.when(HttpClientUtil.httpGet(checkUrl)).thenReturn("ok");
145 private void mockDelete() {
146 String serviceJson = "{\"Node\": \"externalService\",\"ServiceID\": \"_test_10.74.56.36_5656\"}";
147 PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockdeltUrl, serviceJson)).thenReturn(200);
153 private void mockGetRest4null() {
154 PowerMockito.mockStatic(HttpClientUtil.class);
155 PowerMockito.when(HttpClientUtil.httpWaitGet(mockRestUrl)).thenReturn(null);
158 private void mockGetPost() {
159 PowerMockito.mockStatic(HttpClientUtil.class);
161 "{\"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}}";
162 PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockPostUrl, serviceJson)).thenReturn(200);