1e5227fd763102d7104f635c77370f787b9d6513
[msb/discovery.git] /
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.Iterator;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.junit.Assert;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28
29 import org.onap.msb.sdclient.core.ConsulResponse;
30 import org.onap.msb.sdclient.core.KeyVaulePair;
31 import org.onap.msb.sdclient.core.MicroServiceFullInfo;
32 import org.onap.msb.sdclient.core.NodeInfo;
33 import org.onap.msb.sdclient.core.PublishAddress;
34 import org.onap.msb.sdclient.core.PublishFullAddress;
35 import org.onap.msb.sdclient.core.exception.UnprocessableEntityException;
36
37
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
44
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest({HttpClientUtil.class})
47 public class PublishAddressWrapperTest {
48
49     private static final String restJson =
50                     "[{\"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\"}]}]";
51     private static final String mockRestUrl = "http://127.0.0.1:8500/v1/health/service/test";
52
53     private static final String uiJson =
54                     "[{\"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\"}]}]";
55     private static final String mockUIUrl = "http://127.0.0.1:8500/v1/health/service/IUI_test";
56
57     private static final String tcpJson =
58                     "[{\"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\"}]}]";
59     private static final String mockTCPUrl = "http://127.0.0.1:8500/v1/health/service/tcp_test";
60
61     private static final String httpJson =
62                     "[{\"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\"}]}]";
63     private static final String mockHTTPUrl = "http://127.0.0.1:8500/v1/health/service/test-ns";
64
65
66
67     private static PublishAddressWrapper publishAddressWrapper = PublishAddressWrapper.getInstance();
68
69     @BeforeClass
70     public static void setUpBeforeClass() throws Exception {
71         mockinitApigatewayInfo();
72     }
73
74
75     @Test
76     public void test_get_apigateway_publishaddress4Rest() {
77         mockGetRest();
78
79         Set<PublishFullAddress> publishFullAddressList =
80                         publishAddressWrapper.getAllPublishaddress("test", "v1", "", "1");
81         int addressNum = publishFullAddressList.size();
82         Assert.assertEquals(2, addressNum);
83
84     }
85
86
87     @Test
88     public void test_get_router_publishaddress4Ui() {
89         mockgetUi();
90
91         Set<PublishFullAddress> publishFullAddressList =
92                         publishAddressWrapper.getAllPublishaddress("IUI_test", "null", "", "0");
93         int addressNum = publishFullAddressList.size();
94         Assert.assertEquals(2, addressNum);
95
96
97     }
98
99     @Test
100     public void test_get_router_publishaddress4Http() {
101         mockgetHttp();
102
103         Set<PublishFullAddress> publishFullAddressList =
104                         publishAddressWrapper.getAllPublishaddress("test", "v1", "ns", "0");
105         int addressNum = publishFullAddressList.size();
106         Assert.assertEquals(1, addressNum);
107
108
109     }
110
111     @Test
112     public void test_get_router_publishaddress4Tcp() {
113         mockgetTcp();
114
115         Set<PublishFullAddress> publishFullAddressList =
116                         publishAddressWrapper.getAllPublishaddress("tcp_test", "v1", "", "0");
117         int addressNum = publishFullAddressList.size();
118         Assert.assertEquals(2, addressNum);
119
120
121
122     }
123
124
125     @Test
126     public void test_get_apigateway_publishaddress4nameError() {
127         try {
128             publishAddressWrapper.getAllPublishaddress("*.test", "v1", "", "1");
129         } catch (Exception e) {
130             Assert.assertTrue(e instanceof UnprocessableEntityException);
131         }
132     }
133
134     @Test
135     public void test_get_apigateway_publishaddress4nameEmpty() {
136         try {
137             publishAddressWrapper.getAllPublishaddress("", "v1", "", "1");
138         } catch (Exception e) {
139             Assert.assertTrue(e instanceof UnprocessableEntityException);
140         }
141     }
142
143     @Test
144     public void test_get_apigateway_publishaddress4versionError() {
145         try {
146             publishAddressWrapper.getAllPublishaddress("test", "verison1", "", "1");
147         } catch (Exception e) {
148             Assert.assertTrue(e instanceof UnprocessableEntityException);
149         }
150     }
151
152     @Test
153     public void test_get_apigateway_publishaddress4visualRangeError() {
154         try {
155             publishAddressWrapper.getAllPublishaddress("test", "v1", "", "12");
156         } catch (Exception e) {
157             Assert.assertTrue(e instanceof UnprocessableEntityException);
158         }
159     }
160
161     @Test
162     public void test_getPublishaddress4Rest() {
163         mockGetRest();
164         PublishAddress publishAddress = publishAddressWrapper.getPublishaddress("test", "v1", "", 0);
165         String test_address = "10.74.44.2:10080/api/test/v1";
166         Assert.assertEquals(test_address, publishAddress.toString());
167     }
168
169     @Test
170     public void test_getPublishaddress4UI() {
171         mockgetUi();
172         PublishAddress publishAddress = publishAddressWrapper.getPublishaddress("IUI_test", "", "", 1);
173         String test_address = "10.74.44.2:10080/iui/test_path";
174         Assert.assertEquals(test_address, publishAddress.toString());
175     }
176
177     @Test
178     public void test_getPublishaddress4HTTP() {
179         mockgetHttp();
180         PublishAddress publishAddress = publishAddressWrapper.getPublishaddress("test", "v1", "ns", 1);
181         String test_address = "10.74.44.1:10080/test/v1";
182         Assert.assertEquals(test_address, publishAddress.toString());
183     }
184
185     @Test
186     public void test_getPublishaddress4time() {
187         mockgetTcp();
188         PublishAddress publishAddress = publishAddressWrapper.getPublishaddress("tcp_test", "v1", "", 5);
189         String test_address = "10.74.44.2:10080/";
190         Assert.assertEquals(test_address, publishAddress.toString());
191     }
192
193     @Test
194     public void test_getApigatewayServiceInfo_in() {
195         Set<MicroServiceFullInfo> apigatewayList = publishAddressWrapper.getApigatewayServiceInfo("all", "1");
196         Iterator<MicroServiceFullInfo> apigateway = apigatewayList.iterator();
197
198         Set<NodeInfo> nodes = apigateway.next().getNodes();
199
200         NodeInfo testNode = new NodeInfo();
201         testNode.setIp("10.74.44.1");
202         testNode.setPort("10080");
203
204         NodeInfo node = new NodeInfo();
205         Iterator it = nodes.iterator();
206         while (it.hasNext()) {
207             node = (NodeInfo) it.next();
208         }
209
210         Assert.assertEquals(testNode, node);
211     }
212
213
214     /*
215      * @Test public void test_getApigatewayServiceInfo_out() { List<MicroServiceFullInfo>
216      * apigatewayList = publishAddressWrapper.getApigatewayServiceInfo("all", "0"); Set<NodeInfo>
217      * nodes = apigatewayList.get(0).getNodes();
218      * 
219      * NodeInfo testNode = new NodeInfo(); testNode.setIp("10.74.44.3"); testNode.setPort("80");
220      * 
221      * NodeInfo node = new NodeInfo(); Iterator it = nodes.iterator(); while (it.hasNext()) { node =
222      * (NodeInfo) it.next(); }
223      * 
224      * Assert.assertEquals(testNode, node); }
225      */
226
227     @Test
228     public void test_getApigatewayServiceInfo_422ERROR() {
229         try {
230             Set<MicroServiceFullInfo> apigatewayList = publishAddressWrapper.getApigatewayServiceInfo("test", "2");
231
232             Assert.fail("should not process to here.");
233         } catch (Exception e) {
234             Assert.assertTrue(e instanceof UnprocessableEntityException);
235         }
236
237     }
238
239
240
241     private void mockGetRest() {
242         PowerMockito.mockStatic(HttpClientUtil.class);
243         ConsulResponse<Object> consulResponse = new ConsulResponse(restJson, new BigInteger("1000"));
244         PowerMockito.when(HttpClientUtil.httpWaitGet(mockRestUrl)).thenReturn(consulResponse);
245     }
246
247     private void mockgetUi() {
248         PowerMockito.mockStatic(HttpClientUtil.class);
249         ConsulResponse<Object> consulResponse = new ConsulResponse(uiJson, new BigInteger("1000"));
250         PowerMockito.when(HttpClientUtil.httpWaitGet(mockUIUrl)).thenReturn(consulResponse);
251     }
252
253     private void mockgetHttp() {
254         PowerMockito.mockStatic(HttpClientUtil.class);
255         ConsulResponse<Object> consulResponse = new ConsulResponse(httpJson, new BigInteger("1000"));
256         PowerMockito.when(HttpClientUtil.httpWaitGet(mockHTTPUrl)).thenReturn(consulResponse);
257     }
258
259     private void mockgetTcp() {
260         PowerMockito.mockStatic(HttpClientUtil.class);
261         ConsulResponse<Object> consulResponse = new ConsulResponse(tcpJson, new BigInteger("1000"));
262         PowerMockito.when(HttpClientUtil.httpWaitGet(mockTCPUrl)).thenReturn(consulResponse);
263     }
264
265
266     private static void mockinitApigatewayInfo() {
267         mockApigatewayInfo4all();
268         mockApigatewayInfo4Default();
269         mockRouterInfo4all();
270         mockRouterInfo4Default();
271     }
272
273     private static void mockApigatewayInfo4all() {
274         MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();
275         serviceInfo.setServiceName("apigateway");
276         serviceInfo.setVersion("v1");
277         serviceInfo.setUrl("/api/microservices/v1");
278         serviceInfo.setProtocol("REST");
279         serviceInfo.setVisualRange("1");
280         serviceInfo.setNamespace("all");
281         Set<NodeInfo> nodes = new HashSet<NodeInfo>();
282         NodeInfo node = new NodeInfo();
283         node.setIp("10.74.44.1");
284         node.setPort("10080");
285         nodes.add(node);
286         serviceInfo.setNodes(nodes);
287
288         List<MicroServiceFullInfo> apigatewayList = new ArrayList<MicroServiceFullInfo>();
289         apigatewayList.add(serviceInfo);
290         publishAddressWrapper.publishApigateWayList.put("apigateway-all", apigatewayList);
291
292     }
293
294     private static void mockApigatewayInfo4Default() {
295         MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();
296         serviceInfo.setServiceName("apigateway");
297         serviceInfo.setVersion("v1");
298         serviceInfo.setUrl("/api/microservices/v1");
299         serviceInfo.setProtocol("REST");
300         serviceInfo.setVisualRange("1");
301         serviceInfo.setNamespace("default");
302         List<String> labels = new ArrayList<String>();
303         labels.add("apigateway:test|test2");
304         serviceInfo.setLabels(labels);
305         Set<NodeInfo> nodes = new HashSet<NodeInfo>();
306         NodeInfo node = new NodeInfo();
307         node.setIp("10.74.44.2");
308         node.setPort("10080");
309         nodes.add(node);
310         serviceInfo.setNodes(nodes);
311
312         List<MicroServiceFullInfo> apigatewayList = new ArrayList<MicroServiceFullInfo>();
313         apigatewayList.add(serviceInfo);
314         publishAddressWrapper.publishApigateWayList.put("apigateway-default", apigatewayList);
315     }
316
317
318     private static void mockRouterInfo4all() {
319         MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();
320         serviceInfo.setServiceName("router");
321         serviceInfo.setVersion("v1");
322         serviceInfo.setUrl("/api/microservices/v1");
323         serviceInfo.setProtocol("REST");
324         serviceInfo.setVisualRange("0");
325         serviceInfo.setNamespace("all");
326
327         Set<NodeInfo> nodes = new HashSet<NodeInfo>();
328         NodeInfo node = new NodeInfo();
329         node.setIp("10.74.44.3");
330         node.setPort("80");
331         nodes.add(node);
332         serviceInfo.setNodes(nodes);
333
334         List<MicroServiceFullInfo> apigatewayList = new ArrayList<MicroServiceFullInfo>();
335         apigatewayList.add(serviceInfo);
336         publishAddressWrapper.publishApigateWayList.put("router-all", apigatewayList);
337     }
338
339     private static void mockRouterInfo4Default() {
340         MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();
341         serviceInfo.setServiceName("router");
342         serviceInfo.setVersion("v1");
343         serviceInfo.setUrl("/api/microservices/v1");
344         serviceInfo.setProtocol("REST");
345         serviceInfo.setVisualRange("0");
346         serviceInfo.setNamespace("default");
347         serviceInfo.setNetwork_plane_type("net|net2");
348         List<KeyVaulePair> metadatas = new ArrayList<KeyVaulePair>();
349         metadatas.add(new KeyVaulePair("routeWay", "domain"));
350         metadatas.add(new KeyVaulePair("routeSubdomain", "openpalette.zte.com.cn"));
351         serviceInfo.setMetadata(metadatas);
352
353         Set<NodeInfo> nodes = new HashSet<NodeInfo>();
354         NodeInfo node = new NodeInfo();
355         node.setIp("10.74.44.4");
356         node.setPort("80");
357         nodes.add(node);
358         serviceInfo.setNodes(nodes);
359
360         List<MicroServiceFullInfo> apigatewayList = new ArrayList<MicroServiceFullInfo>();
361         apigatewayList.add(serviceInfo);
362         publishAddressWrapper.publishApigateWayList.put("router-default", apigatewayList);
363     }
364
365 }