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.ArrayList;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.List;
24 import org.junit.Assert;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
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;
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;
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest({HttpClientUtil.class})
47 public class PublishAddressWrapperTest {
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";
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";
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";
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";
67 private static PublishAddressWrapper publishAddressWrapper = PublishAddressWrapper.getInstance();
70 public static void setUpBeforeClass() throws Exception {
71 mockinitApigatewayInfo();
76 public void test_get_apigateway_publishaddress4Rest() {
79 Set<PublishFullAddress> publishFullAddressList =
80 publishAddressWrapper.getAllPublishaddress("test", "v1", "", "1");
81 int addressNum = publishFullAddressList.size();
82 Assert.assertEquals(2, addressNum);
88 public void test_get_router_publishaddress4Ui() {
91 Set<PublishFullAddress> publishFullAddressList =
92 publishAddressWrapper.getAllPublishaddress("IUI_test", "null", "", "0");
93 int addressNum = publishFullAddressList.size();
94 Assert.assertEquals(2, addressNum);
100 public void test_get_router_publishaddress4Http() {
103 Set<PublishFullAddress> publishFullAddressList =
104 publishAddressWrapper.getAllPublishaddress("test", "v1", "ns", "0");
105 int addressNum = publishFullAddressList.size();
106 Assert.assertEquals(1, addressNum);
112 public void test_get_router_publishaddress4Tcp() {
115 Set<PublishFullAddress> publishFullAddressList =
116 publishAddressWrapper.getAllPublishaddress("tcp_test", "v1", "", "0");
117 int addressNum = publishFullAddressList.size();
118 Assert.assertEquals(2, addressNum);
126 public void test_get_apigateway_publishaddress4nameError() {
128 publishAddressWrapper.getAllPublishaddress("*.test", "v1", "", "1");
129 } catch (Exception e) {
130 Assert.assertTrue(e instanceof UnprocessableEntityException);
135 public void test_get_apigateway_publishaddress4nameEmpty() {
137 publishAddressWrapper.getAllPublishaddress("", "v1", "", "1");
138 } catch (Exception e) {
139 Assert.assertTrue(e instanceof UnprocessableEntityException);
144 public void test_get_apigateway_publishaddress4versionError() {
146 publishAddressWrapper.getAllPublishaddress("test", "verison1", "", "1");
147 } catch (Exception e) {
148 Assert.assertTrue(e instanceof UnprocessableEntityException);
153 public void test_get_apigateway_publishaddress4visualRangeError() {
155 publishAddressWrapper.getAllPublishaddress("test", "v1", "", "12");
156 } catch (Exception e) {
157 Assert.assertTrue(e instanceof UnprocessableEntityException);
162 public void test_getPublishaddress4Rest() {
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());
170 public void test_getPublishaddress4UI() {
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());
178 public void test_getPublishaddress4HTTP() {
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());
186 public void test_getPublishaddress4time() {
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());
194 public void test_getApigatewayServiceInfo_in() {
195 Set<MicroServiceFullInfo> apigatewayList = publishAddressWrapper.getApigatewayServiceInfo("all", "1");
196 Iterator<MicroServiceFullInfo> apigateway = apigatewayList.iterator();
198 Set<NodeInfo> nodes = apigateway.next().getNodes();
200 NodeInfo testNode = new NodeInfo();
201 testNode.setIp("10.74.44.1");
202 testNode.setPort("10080");
204 NodeInfo node = new NodeInfo();
205 Iterator it = nodes.iterator();
206 while (it.hasNext()) {
207 node = (NodeInfo) it.next();
210 Assert.assertEquals(testNode, node);
215 * @Test public void test_getApigatewayServiceInfo_out() { List<MicroServiceFullInfo>
216 * apigatewayList = publishAddressWrapper.getApigatewayServiceInfo("all", "0"); Set<NodeInfo>
217 * nodes = apigatewayList.get(0).getNodes();
219 * NodeInfo testNode = new NodeInfo(); testNode.setIp("10.74.44.3"); testNode.setPort("80");
221 * NodeInfo node = new NodeInfo(); Iterator it = nodes.iterator(); while (it.hasNext()) { node =
222 * (NodeInfo) it.next(); }
224 * Assert.assertEquals(testNode, node); }
228 public void test_getApigatewayServiceInfo_422ERROR() {
230 Set<MicroServiceFullInfo> apigatewayList = publishAddressWrapper.getApigatewayServiceInfo("test", "2");
232 Assert.fail("should not process to here.");
233 } catch (Exception e) {
234 Assert.assertTrue(e instanceof UnprocessableEntityException);
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);
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);
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);
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);
266 private static void mockinitApigatewayInfo() {
267 mockApigatewayInfo4all();
268 mockApigatewayInfo4Default();
269 mockRouterInfo4all();
270 mockRouterInfo4Default();
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");
286 serviceInfo.setNodes(nodes);
288 List<MicroServiceFullInfo> apigatewayList = new ArrayList<MicroServiceFullInfo>();
289 apigatewayList.add(serviceInfo);
290 publishAddressWrapper.publishApigateWayList.put("apigateway-all", apigatewayList);
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");
310 serviceInfo.setNodes(nodes);
312 List<MicroServiceFullInfo> apigatewayList = new ArrayList<MicroServiceFullInfo>();
313 apigatewayList.add(serviceInfo);
314 publishAddressWrapper.publishApigateWayList.put("apigateway-default", apigatewayList);
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");
327 Set<NodeInfo> nodes = new HashSet<NodeInfo>();
328 NodeInfo node = new NodeInfo();
329 node.setIp("10.74.44.3");
332 serviceInfo.setNodes(nodes);
334 List<MicroServiceFullInfo> apigatewayList = new ArrayList<MicroServiceFullInfo>();
335 apigatewayList.add(serviceInfo);
336 publishAddressWrapper.publishApigateWayList.put("router-all", apigatewayList);
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);
353 Set<NodeInfo> nodes = new HashSet<NodeInfo>();
354 NodeInfo node = new NodeInfo();
355 node.setIp("10.74.44.4");
358 serviceInfo.setNodes(nodes);
360 List<MicroServiceFullInfo> apigatewayList = new ArrayList<MicroServiceFullInfo>();
361 apigatewayList.add(serviceInfo);
362 publishAddressWrapper.publishApigateWayList.put("router-default", apigatewayList);