1 /*******************************************************************************
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
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper;
16 import java.lang.reflect.InvocationHandler;
17 import java.lang.reflect.Method;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.List;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.onap.msb.apiroute.api.CustomRouteInfo;
29 import org.onap.msb.apiroute.api.RouteServer;
30 import org.onap.msb.apiroute.api.exception.ExtendedNotFoundException;
31 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
32 import org.onap.msb.apiroute.wrapper.util.ConfigUtil;
33 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
34 import org.onap.msb.apiroute.wrapper.util.RouteUtil;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.PowerMockIgnore;
37 import org.powermock.core.classloader.annotations.PrepareForTest;
38 import org.powermock.modules.junit4.PowerMockRunner;
40 import com.fiftyonred.mock_jedis.MockJedisPool;
42 import redis.clients.jedis.JedisPool;
43 import redis.clients.jedis.JedisPoolConfig;
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest({JedisUtil.class, RouteUtil.class, RedisAccessWrapper.class})
47 @PowerMockIgnore({"javax.management.*"})
48 public class CustomRouteServiceWrapperTest {
50 private static CustomRouteServiceWrapper customRouteServiceWrapper;
51 private static Comparator<CustomRouteInfo> customRouteComparator = null;
54 public static void setUpBeforeClass() throws Exception {
55 customRouteServiceWrapper = CustomRouteServiceWrapper.getInstance();
56 customRouteComparator = new Comparator<CustomRouteInfo>() {
58 public int compare(CustomRouteInfo o1, CustomRouteInfo o2) {
59 if (!o1.getServiceName().equals(o2.getServiceName()))
60 return (o1.getServiceName()).compareTo(o2.getServiceName());
65 PowerMockito.mockStatic(System.class);
66 PowerMockito.when(System.getenv("ROUTE_WAY")).thenReturn(null);
67 PowerMockito.when(System.getenv("ROUTE_WAY")).thenReturn("ip|domain");
68 ConfigUtil.getInstance().initRouteWay();
72 public void setUpBeforeTest() throws Exception {
73 final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
74 PowerMockito.mockStatic(JedisUtil.class);
75 JedisUtil jedisUtil = PowerMockito.mock(JedisUtil.class);
76 PowerMockito.when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());
78 PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
80 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
81 return mockJedisPool.getResource().keys((String) args[0]);
87 public void test_getCustomRouteInstance_not_exist() {
89 customRouteServiceWrapper.getCustomRouteInstance("/testForJunit", "", "", "ip");
90 Assert.fail("should not process to here.");
91 } catch (Exception e) {
92 Assert.assertTrue(e instanceof ExtendedNotFoundException);
99 public void test_getCustomRouteInstance() {
101 CustomRouteInfo customrouteInfo = buildCustomRouteInfo();
103 customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo, "ip");
104 CustomRouteInfo dbCustomRouteInfo =
105 customRouteServiceWrapper.getCustomRouteInstance("/testcustom", "", "", "ip");
106 Assert.assertEquals(customrouteInfo, dbCustomRouteInfo);
107 } catch (Exception e) {
108 Assert.fail("throw exception means error occured!" + e.getMessage());
114 public void test_getAllCustomRouteInstances() {
115 CustomRouteInfo customrouteInfo = buildCustomRouteInfo();
116 CustomRouteInfo customrouteInfo2 = buildCustomRouteInfo2();
117 List<CustomRouteInfo> expected = new ArrayList<>();
118 expected.add(customrouteInfo);
119 expected.add(customrouteInfo2);
120 Collections.sort(expected, customRouteComparator);
123 customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo, "ip");
124 customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo2, "ip");
126 PowerMockito.mockStatic(RouteUtil.class);
127 PowerMockito.when(RouteUtil.getMutiRedisKey(RouteUtil.CUSTOMROUTE, "ip"))
128 .thenReturn("msb:routing:custom:*");
129 List<CustomRouteInfo> customRouterList = customRouteServiceWrapper.getAllCustomRouteInstances("ip");
130 Collections.sort(customRouterList, customRouteComparator);
132 Assert.assertEquals(expected, customRouterList);
134 } catch (Exception e) {
135 Assert.fail("throw exception means error occured!" + e.getMessage());
141 public void test_updateCustomRouteStatus() {
142 CustomRouteInfo customrouteInfo = buildCustomRouteInfo();
144 customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo, "ip");
145 CustomRouteInfo dbCustomrouteInfo =
146 customRouteServiceWrapper.getCustomRouteInstance("/testcustom", "", "", "ip");
147 Assert.assertEquals("1", dbCustomrouteInfo.getStatus());
148 customRouteServiceWrapper.updateCustomRouteStatus("/testcustom", "", "", "0", "ip");
149 dbCustomrouteInfo = customRouteServiceWrapper.getCustomRouteInstance("/testcustom", "", "", "ip");
150 Assert.assertEquals("0", dbCustomrouteInfo.getStatus());
151 } catch (Exception e) {
152 Assert.fail("throw exception means error occured!" + e.getMessage());
160 public void test_deleteCustomRoute() {
161 CustomRouteInfo customrouteInfo2 = buildCustomRouteInfo2();
163 customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo2, "ip");
164 CustomRouteInfo dbCustomrouteInfo =
165 customRouteServiceWrapper.getCustomRouteInstance("/testcustom2", "", "", "ip");
166 Assert.assertNotNull(dbCustomrouteInfo);
168 } catch (Exception e) {
169 Assert.fail("throw exception means error occured!" + e.getMessage());
172 customRouteServiceWrapper.deleteCustomRoute("/testcustom2", "", "", "ip");
173 customRouteServiceWrapper.getCustomRouteInstance("/testcustom2", "", "", "ip");
174 } catch (Exception e) {
175 Assert.assertTrue(e instanceof ExtendedNotFoundException);
180 private CustomRouteInfo buildCustomRouteInfo() {
181 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
182 customrouteInfo.setServiceName("/testcustom");
183 customrouteInfo.setStatus("1");
184 customrouteInfo.setUrl("/custom/testcustom");
185 customrouteInfo.setUseOwnUpstream("0");
186 customrouteInfo.setVisualRange("0");
187 customrouteInfo.setEnable_ssl(false);
188 RouteServer[] servers = new RouteServer[] {new RouteServer("10.74.148.88", "8080")};
189 customrouteInfo.setServers(servers);
190 return customrouteInfo;
193 private CustomRouteInfo buildCustomRouteInfo2() {
194 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
195 customrouteInfo.setServiceName("/testcustom2");
196 customrouteInfo.setStatus("1");
197 customrouteInfo.setUrl("/custom/testcustom");
198 customrouteInfo.setUseOwnUpstream("0");
199 customrouteInfo.setVisualRange("1");
200 customrouteInfo.setEnable_ssl(true);
201 RouteServer[] servers = new RouteServer[] {new RouteServer("10.74.148.89", "8080")};
202 customrouteInfo.setServers(servers);
203 return customrouteInfo;