064f6fd6f13ab21b49af895443e9da2b1cf570a3
[msb/discovery.git] / sdclient / discovery-service / src / test / java / org / onap / msb / sdclient / wrapper / ConsulServiceWrapperTest.java
1 /**
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
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
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
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
12  * the License.
13  */
14
15 package org.onap.msb.sdclient.wrapper;
16
17 import java.math.BigInteger;
18 import java.util.ArrayList;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Set;
22
23 import org.junit.Assert;
24 import org.junit.Ignore;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.onap.msb.sdclient.DiscoverAppConfig;
28 import org.onap.msb.sdclient.core.ConsulResponse;
29 import org.onap.msb.sdclient.core.KeyVaulePair;
30 import org.onap.msb.sdclient.core.MicroServiceFullInfo;
31 import org.onap.msb.sdclient.core.MicroServiceInfo;
32 import org.onap.msb.sdclient.core.Node;
33 import org.onap.msb.sdclient.core.NodeAddress;
34 import org.onap.msb.sdclient.core.NodeInfo;
35 import org.onap.msb.sdclient.core.exception.ExtendedNotFoundException;
36 import org.onap.msb.sdclient.core.exception.UnprocessableEntityException;
37 import org.onap.msb.sdclient.wrapper.util.ConfigUtil;
38 import org.onap.msb.sdclient.wrapper.util.HttpClientUtil;
39 import org.powermock.api.mockito.PowerMockito;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
42
43 @RunWith(PowerMockRunner.class)
44 @PrepareForTest({HttpClientUtil.class, ConfigUtil.class, DiscoverAppConfig.class})
45 public class ConsulServiceWrapperTest {
46
47     private static final String restJson =
48                     "[{\"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\"}]}]";
49     private static final String catalogJson =
50                     "[{\"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}]";
51     private static final String catalog4ttlJson =
52                     "[{\"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}]";
53     private static final String mockRestUrl = "http://127.0.0.1:8500/v1/health/service/test-v1";
54     private static final String mockPostUrl = "http://127.0.0.1:8500/v1/catalog/register";
55     private static final String mockdel_gettUrl = "http://127.0.0.1:8500/v1/catalog/service/test-v1";
56     private static final String mockdeltUrl = "http://127.0.0.1:8500/v1/catalog/deregister";
57     private static final String mockdeltUrl4agent = "http://127.0.0.1:8500/v1/agent/deregister";
58
59     private static final String mockgetListUrl = "http://127.0.0.1:8500/v1/catalog/services";
60     private static final String restListJson =
61                     "{\"consul\":[],\"test-tt\":[\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0\\\"}\",\"\\\"base\\\":{\\\"version\\\":\\\"v1\\\",\\\"protocol\\\":\\\"REST\\\",\\\"url\\\":\\\"/api/microservices/v1\\\",\\\"visualRange\\\":\\\"0\\\"}\",\"\\\"ns\\\":{\\\"namespace\\\":\\\"tt\\\"}\"]}";
62
63
64     private static ConsulServiceWrapper consulServiceWrapper = ConsulServiceWrapper.getInstance();
65
66
67     @Test
68     public void test_getAllMicroServiceInstances() {
69         mockGetList();
70         List<MicroServiceFullInfo> serviceList = consulServiceWrapper.getAllMicroServiceInstances();
71         Assert.assertEquals(1, serviceList.size());
72         MicroServiceFullInfo service = serviceList.get(0);
73         Assert.assertEquals("test-tt", service.getServiceName());
74         Assert.assertEquals("", service.getNamespace());
75     }
76
77
78     @Test
79     public void test_getMicroServiceInstance() {
80         mockGetRest();
81         MicroServiceFullInfo service = consulServiceWrapper.getMicroServiceInstance("test", "v1", "");
82         Assert.assertEquals(service.getServiceName(), "test");
83         Assert.assertEquals(service.getProtocol(), "REST");
84     }
85
86     @Test
87     public void test_getMicroServiceForNodes() {
88         mockGetRest();
89         List<MicroServiceFullInfo> services = consulServiceWrapper.getMicroServiceForNodes("test", "v1", false, "", "");
90         Assert.assertEquals(1, services.size());
91         Assert.assertEquals(services.get(0).getServiceName(), "test");
92         Assert.assertEquals(services.get(0).getProtocol(), "REST");
93     }
94
95     @Test
96     public void test_saveMicroServiceInstance() {
97         MicroServiceInfo serviceInfo = new MicroServiceInfo();
98         serviceInfo.setServiceName("test");
99         serviceInfo.setVersion("v1");
100         serviceInfo.setNamespace("ns");
101         serviceInfo.setPublish_port("8086");
102         serviceInfo.setUrl("/api/test/v1");
103         serviceInfo.setProtocol("REST");
104         serviceInfo.setVisualRange("1");
105         serviceInfo.setLb_policy("ip_hash");
106         serviceInfo.setHost("host");
107         serviceInfo.setPath("/test");
108         serviceInfo.setNetwork_plane_type("net");
109
110         List<KeyVaulePair> metadata = new ArrayList<KeyVaulePair>();
111         metadata.add(new KeyVaulePair("key1", "val1"));
112         metadata.add(new KeyVaulePair("key2", "val2"));
113         serviceInfo.setMetadata(metadata);
114
115
116         List<String> labels = new ArrayList<String>();
117         labels.add("111:111");
118         labels.add("222:222");
119         serviceInfo.setLabels(labels);
120
121         Set<Node> nodes = new HashSet<Node>();
122         Node node = new Node();
123         node.setIp("10.74.44.1");
124         node.setPort("10080");
125         node.setLb_server_params("weight=1,max_fails=5,fail_timeout=8");
126         node.setHa_role("active");
127         node.setCheckType("HTTP");
128         node.setCheckInterval("10");
129         node.setCheckTimeOut("10");
130         node.setCheckUrl("http://check");
131         nodes.add(node);
132         serviceInfo.setNodes(nodes);
133
134         mockGetRest4null();
135         // mockGetPost();
136         try {
137             consulServiceWrapper.saveMicroServiceInstance(serviceInfo, true, "127.0.0.1", true);
138         } catch (Exception e) {
139             Assert.assertEquals("HTTP 500 Internal Server Error", e.getMessage());
140         }
141     }
142
143
144     @Test
145     public void test_saveMicroServiceInstance2() {
146         MicroServiceInfo serviceInfo = new MicroServiceInfo();
147         serviceInfo.setServiceName("test");
148         serviceInfo.setVersion("v1");
149         serviceInfo.setNamespace("ns");
150         serviceInfo.setPublish_port("28005");
151         serviceInfo.setUrl("/api/test/v1");
152         serviceInfo.setProtocol("TCP");
153         serviceInfo.setVisualRange("1");
154         serviceInfo.setLb_policy("ip_hash");
155         serviceInfo.setHost("host");
156         serviceInfo.setPath("/test");
157         serviceInfo.setNetwork_plane_type("net");
158
159         List<KeyVaulePair> metadata = new ArrayList<KeyVaulePair>();
160         metadata.add(new KeyVaulePair("key1", "val1"));
161         metadata.add(new KeyVaulePair("key2", "val2"));
162         serviceInfo.setMetadata(metadata);
163
164
165         List<String> labels = new ArrayList<String>();
166         labels.add("111:111");
167         labels.add("222:222");
168         serviceInfo.setLabels(labels);
169
170         Set<Node> nodes = new HashSet<Node>();
171         Node node = new Node();
172         node.setIp("10.74.44.1");
173         node.setPort("28005");
174         node.setLb_server_params("weight=1,max_fails=5,fail_timeout=8");
175         node.setHa_role("active");
176         node.setCheckType("TCP");
177         node.setCheckInterval("10");
178         node.setCheckTimeOut("10");
179         node.setCheckUrl("tcp://check");
180         nodes.add(node);
181         serviceInfo.setNodes(nodes);
182
183         mockGetRest4null();
184         // mockGetPost();
185         try {
186             consulServiceWrapper.saveMicroServiceInstance(serviceInfo, true, "127.0.0.1", true);
187         } catch (Exception e) {
188             Assert.assertEquals("HTTP 500 Internal Server Error", e.getMessage());
189         }
190     }
191
192     @Test
193     public void test_saveMicroServiceInstance4agent() {
194         MicroServiceInfo serviceInfo = new MicroServiceInfo();
195         serviceInfo.setServiceName("test");
196         serviceInfo.setVersion("v1");
197         serviceInfo.setUrl("/api/test/v1");
198         serviceInfo.setProtocol("REST");
199         serviceInfo.setVisualRange("1");
200         Set<Node> nodes = new HashSet<Node>();
201         Node node = new Node();
202         node.setIp("10.74.44.1");
203         node.setPort("10080");
204         nodes.add(node);
205         serviceInfo.setNodes(nodes);
206
207         PowerMockito.mockStatic(System.class);
208         PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("agent");
209         DiscoverAppConfig discoverConfig = PowerMockito.mock(DiscoverAppConfig.class);
210         ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig);
211
212
213         mockGetRest4null();
214         // mockGetPost();
215         try {
216             consulServiceWrapper.saveMicroServiceInstance(serviceInfo, true, "127.0.0.1", true);
217         } catch (Exception e) {
218             Assert.assertEquals("HTTP 500 Internal Server Error", e.getMessage());
219         }
220
221         PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("catalog");
222         ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig);
223     }
224
225     @Test
226     public void test_deleteMicroService() {
227         mockGet4Delete();
228         mockDelete();
229         consulServiceWrapper.deleteMicroService("test", "v1", "");
230     }
231
232     @Test
233     public void test_deleteMicroServiceInstance() {
234         mockGet4Delete();
235         mockDelete();
236         consulServiceWrapper.deleteMicroServiceInstance("test", "v1", "", "10.74.56.36", "5656");
237     }
238
239     @Test
240     public void test_deleteMicroService4agent() {
241         mockGet4Delete();
242         mockDelete4agent();
243
244         DiscoverAppConfig discoverConfig = PowerMockito.mock(DiscoverAppConfig.class);
245         PowerMockito.mockStatic(System.class);
246         PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("agent");
247         ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig);
248
249         consulServiceWrapper.deleteMicroService("test", "v1", "");
250
251         PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("catalog");
252         ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig);
253     }
254
255
256
257     @Test
258     public void test_healthCheckbyTTL() {
259         NodeAddress nodeAddress = new NodeAddress("10.74.56.36", "5656");
260         mockGet4healthCheck();
261         consulServiceWrapper.healthCheckbyTTL("test", "v1", "", nodeAddress);
262     }
263
264
265
266     private void mockGetRest() {
267         PowerMockito.mockStatic(HttpClientUtil.class);
268         ConsulResponse<Object> consulResponse = new ConsulResponse(restJson, new BigInteger("1000"));
269         PowerMockito.when(HttpClientUtil.httpWaitGet(mockRestUrl)).thenReturn(consulResponse);
270     }
271
272
273     private void mockGet4Delete() {
274         PowerMockito.mockStatic(HttpClientUtil.class);
275         PowerMockito.when(HttpClientUtil.httpGet(mockdel_gettUrl)).thenReturn(catalogJson);
276
277     }
278
279     private void mockGetList() {
280         PowerMockito.mockStatic(HttpClientUtil.class);
281         PowerMockito.when(HttpClientUtil.httpGet(mockgetListUrl)).thenReturn(restListJson);
282
283         ConsulResponse<Object> consulResponse = new ConsulResponse(restJson, new BigInteger("1000"));
284         PowerMockito.when(HttpClientUtil.httpWaitGet("http://127.0.0.1:8500/v1/health/service/test-tt"))
285                         .thenReturn(consulResponse);
286
287     }
288
289     private void mockGet4healthCheck() {
290         PowerMockito.mockStatic(HttpClientUtil.class);
291         PowerMockito.when(HttpClientUtil.httpGet(mockdel_gettUrl)).thenReturn(catalog4ttlJson);
292
293
294         String checkUrl = "http://127.0.0.1:8500/v1/agent/check/pass/service:_test_10.74.56.36_5656";
295         PowerMockito.when(HttpClientUtil.httpGet(checkUrl)).thenReturn("ok");
296
297     }
298
299     private void mockDelete() {
300         String serviceJson = "{\"Node\": \"externalService\",\"ServiceID\": \"_test_10.74.56.36_5656\"}";
301         PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockdeltUrl, serviceJson)).thenReturn(200);
302
303     }
304
305     private void mockDelete4agent() {
306         PowerMockito.when(HttpClientUtil.httpPostWithJSON(
307                         "http://127.0.0.1:8500/v1/agent/service/deregister/_test_10.74.56.36_5656", ""))
308                         .thenReturn(200);
309
310     }
311
312
313
314     private void mockGetRest4null() {
315         PowerMockito.mockStatic(HttpClientUtil.class);
316         PowerMockito.when(HttpClientUtil.httpWaitGet(mockRestUrl)).thenReturn(null);
317     }
318
319     private void mockGetPost() {
320         PowerMockito.mockStatic(HttpClientUtil.class);
321         String serviceJson =
322                         "{\"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}}";
323         PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockPostUrl, serviceJson)).thenReturn(200);
324
325
326     }
327
328 }