2 * Copyright 2016 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.msb.apiroute.wrapper;
18 import java.lang.reflect.InvocationHandler;
19 import java.lang.reflect.Method;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.List;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.msb.apiroute.api.CustomRouteInfo;
32 import org.onap.msb.apiroute.api.RouteServer;
33 import org.onap.msb.apiroute.api.exception.ExtendedNotFoundException;
34 import org.onap.msb.apiroute.wrapper.CustomRouteServiceWrapper;
35 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
36 import org.onap.msb.apiroute.wrapper.util.ConfigUtil;
37 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
38 import org.onap.msb.apiroute.wrapper.util.RouteUtil;
39 import org.powermock.api.mockito.PowerMockito;
40 import org.powermock.core.classloader.annotations.PowerMockIgnore;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
44 import redis.clients.jedis.JedisPool;
45 import redis.clients.jedis.JedisPoolConfig;
47 import com.fiftyonred.mock_jedis.MockJedisPool;
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({JedisUtil.class,RouteUtil.class,RedisAccessWrapper.class})
51 @PowerMockIgnore( {"javax.management.*"})
52 public class CustomRouteServiceWrapperTest {
54 private static CustomRouteServiceWrapper customRouteServiceWrapper;
55 private static Comparator<CustomRouteInfo> customRouteComparator = null;
58 public static void setUpBeforeClass() throws Exception {
59 customRouteServiceWrapper=CustomRouteServiceWrapper.getInstance();
60 customRouteComparator = new Comparator<CustomRouteInfo>() {
62 public int compare(CustomRouteInfo o1, CustomRouteInfo o2) {
63 if (!o1.getServiceName().equals(o2.getServiceName()))
64 return (o1.getServiceName()).compareTo(o2.getServiceName());
69 PowerMockito.mockStatic(System.class);
70 PowerMockito.when(System.getenv("ROUTE_WAY")).thenReturn(null);
71 PowerMockito.when(System.getenv("ROUTE_WAY")).thenReturn("ip|domain");
72 ConfigUtil.getInstance().initRouteWay();
76 public void setUpBeforeTest() throws Exception {
77 final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
78 PowerMockito.mockStatic(JedisUtil.class);
79 JedisUtil jedisUtil=PowerMockito.mock(JedisUtil.class);
80 PowerMockito.when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());
82 PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
84 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
85 return mockJedisPool.getResource().keys((String) args[0]);
91 public void test_getCustomRouteInstance_not_exist(){
93 customRouteServiceWrapper.getCustomRouteInstance("/testForJunit","","","ip");
94 Assert.fail("should not process to here.");
97 Assert.assertTrue(e instanceof ExtendedNotFoundException);
104 public void test_getCustomRouteInstance(){
106 CustomRouteInfo customrouteInfo = buildCustomRouteInfo();
108 customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo, "ip");
109 CustomRouteInfo dbCustomRouteInfo=customRouteServiceWrapper.getCustomRouteInstance("/testcustom", "", "", "ip");
110 Assert.assertEquals(customrouteInfo,dbCustomRouteInfo );
111 } catch (Exception e) {
112 Assert.fail("throw exception means error occured!" + e.getMessage());
118 public void test_getAllCustomRouteInstances(){
119 CustomRouteInfo customrouteInfo = buildCustomRouteInfo();
120 CustomRouteInfo customrouteInfo2 = buildCustomRouteInfo2();
121 List<CustomRouteInfo> expected = new ArrayList<>();
122 expected.add(customrouteInfo);
123 expected.add(customrouteInfo2);
124 Collections.sort(expected, customRouteComparator);
127 customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo, "ip");
128 customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo2, "ip");
130 PowerMockito.mockStatic(RouteUtil.class);
131 PowerMockito.when(RouteUtil.getMutiRedisKey(RouteUtil.CUSTOMROUTE, "ip")).thenReturn("msb:routing:custom:*");
132 List<CustomRouteInfo> customRouterList=customRouteServiceWrapper.getAllCustomRouteInstances("ip");
133 Collections.sort(customRouterList, customRouteComparator);
135 Assert.assertEquals(expected,customRouterList);
137 } catch (Exception e) {
138 Assert.fail("throw exception means error occured!" + e.getMessage());
144 public void test_updateCustomRouteStatus(){
145 CustomRouteInfo customrouteInfo = buildCustomRouteInfo();
147 customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo, "ip");
148 CustomRouteInfo dbCustomrouteInfo=customRouteServiceWrapper.getCustomRouteInstance("/testcustom", "", "", "ip");
149 Assert.assertEquals("1",dbCustomrouteInfo.getStatus() );
150 customRouteServiceWrapper.updateCustomRouteStatus("/testcustom","","","0", "ip");
151 dbCustomrouteInfo=customRouteServiceWrapper.getCustomRouteInstance("/testcustom", "", "", "ip");
152 Assert.assertEquals("0",dbCustomrouteInfo.getStatus() );
153 } catch (Exception e) {
154 Assert.fail("throw exception means error occured!" + e.getMessage());
162 public void test_deleteCustomRoute(){
163 CustomRouteInfo customrouteInfo2 = buildCustomRouteInfo2();
165 customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo2, "ip");
166 CustomRouteInfo dbCustomrouteInfo=customRouteServiceWrapper.getCustomRouteInstance("/testcustom2","","","ip");
167 Assert.assertNotNull(dbCustomrouteInfo);
169 } catch (Exception e) {
170 Assert.fail("throw exception means error occured!" + e.getMessage());
173 customRouteServiceWrapper.deleteCustomRoute("/testcustom2","","","ip");
174 customRouteServiceWrapper.getCustomRouteInstance("/testcustom2","","","ip");
177 Assert.assertTrue(e instanceof ExtendedNotFoundException);
182 private CustomRouteInfo buildCustomRouteInfo(){
183 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
184 customrouteInfo.setServiceName("/testcustom");
185 customrouteInfo.setStatus("1");
186 customrouteInfo.setUrl("/custom/testcustom");
187 customrouteInfo.setUseOwnUpstream("0");
188 customrouteInfo.setVisualRange("0");
189 customrouteInfo.setEnable_ssl(false);
190 RouteServer[] servers = new RouteServer[]{new RouteServer("10.74.148.88","8080")};
191 customrouteInfo.setServers(servers);
192 return customrouteInfo;
195 private CustomRouteInfo buildCustomRouteInfo2(){
196 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
197 customrouteInfo.setServiceName("/testcustom2");
198 customrouteInfo.setStatus("1");
199 customrouteInfo.setUrl("/custom/testcustom");
200 customrouteInfo.setUseOwnUpstream("0");
201 customrouteInfo.setVisualRange("1");
202 customrouteInfo.setEnable_ssl(true);
203 RouteServer[] servers = new RouteServer[]{new RouteServer("10.74.148.89","8080")};
204 customrouteInfo.setServers(servers);
205 return customrouteInfo;