1 /*******************************************************************************
2 * Copyright 2016-2017 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.
15 ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.service;
18 import com.fiftyonred.mock_jedis.MockJedisPool;
19 import org.junit.Before;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.onap.msb.apiroute.api.CustomRouteInfo;
24 import org.onap.msb.apiroute.api.RouteServer;
25 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
26 import org.onap.msb.apiroute.wrapper.service.CustomRouteService;
27 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
28 import org.powermock.api.mockito.PowerMockito;
29 import org.powermock.core.classloader.annotations.PowerMockIgnore;
30 import org.powermock.core.classloader.annotations.PrepareForTest;
31 import org.powermock.modules.junit4.PowerMockRunner;
32 import redis.clients.jedis.JedisPool;
33 import redis.clients.jedis.JedisPoolConfig;
35 import java.lang.reflect.InvocationHandler;
36 import java.lang.reflect.Method;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.Comparator;
40 import java.util.List;
42 import static org.junit.Assert.*;
44 @RunWith(PowerMockRunner.class)
45 @PrepareForTest({JedisUtil.class,RedisAccessWrapper.class})
46 @PowerMockIgnore( {"javax.management.*"})
47 public class CustomRouteServiceTest {
48 private static CustomRouteService customRouteService = null;
49 private static Comparator<CustomRouteInfo> customRouteComparator = null;
51 public static void setUp() throws Exception{
52 customRouteService = CustomRouteService.getInstance();
53 customRouteComparator = new Comparator<CustomRouteInfo>() {
55 public int compare(CustomRouteInfo o1, CustomRouteInfo o2) {
56 if (!o1.getServiceName().equals(o2.getServiceName()))
57 return (o1.getServiceName()).compareTo(o2.getServiceName());
63 public void setUpBeforeTest() throws Exception {
64 final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
65 PowerMockito.mockStatic(JedisUtil.class);
66 JedisUtil jedisUtil=PowerMockito.mock(JedisUtil.class);
67 PowerMockito.when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());
69 PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
71 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
72 return mockJedisPool.getResource().keys((String) args[0]);
77 public void testGetCustomRouteInstance_key_not_exist(){
79 assertNull(customRouteService.getCustomRouteInstance("msb:routing:custom:notexistservice:v1"));
80 } catch (Exception e) {
81 assert false:"throw exception means error occured!"+e.getMessage();
86 public void testGetCustomRouteInstance_key_exist(){
87 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
88 customrouteInfo.setServiceName("testcustom");
89 customrouteInfo.setStatus("1");
90 customrouteInfo.setUrl("/custom/testcustom");
91 customrouteInfo.setUseOwnUpstream("0");
92 customrouteInfo.setVisualRange("0");
93 customrouteInfo.setEnable_ssl(false);
94 RouteServer[] servers = new RouteServer[]{new RouteServer("10.74.148.88","8080")};
95 customrouteInfo.setServers(servers);
97 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
98 assertEquals(customrouteInfo, customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
99 } catch (Exception e) {
100 assert false:"throw exception means error occured!"+e.getMessage();
105 public void testSaveCustomRouteService2Redis(){
106 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
107 customrouteInfo.setServiceName("testcustom");
108 customrouteInfo.setStatus("1");
109 customrouteInfo.setUrl("/custom/testcustom/v1");
110 customrouteInfo.setUseOwnUpstream("0");
111 customrouteInfo.setVisualRange("0");
112 customrouteInfo.setEnable_ssl(true);
113 RouteServer[] servers = new RouteServer[]{new RouteServer("10.74.148.88","8080")};
114 customrouteInfo.setServers(servers);
116 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
117 assertEquals(customrouteInfo, customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
118 } catch (Exception e) {
119 assert false:"throw exception means error occured!"+e.getMessage();
124 public void testSaveCustomRouteService2Redis_urlIsSlash(){
125 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
126 customrouteInfo.setServiceName("testcustom");
127 customrouteInfo.setStatus("1");
128 customrouteInfo.setUrl("/");
129 customrouteInfo.setUseOwnUpstream("0");
130 customrouteInfo.setVisualRange("0");
131 customrouteInfo.setEnable_ssl(true);
132 RouteServer[] servers = new RouteServer[]{new RouteServer("10.74.148.88","8080")};
133 customrouteInfo.setServers(servers);
135 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
136 customrouteInfo.setUrl("");
137 assertEquals(customrouteInfo, customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
138 } catch (Exception e) {
139 assert false:"throw exception means error occured!"+e.getMessage();
144 public void testDeleteCustomRouteService2Redis(){
145 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
146 customrouteInfo.setServiceName("testcustom");
147 customrouteInfo.setStatus("1");
148 customrouteInfo.setUrl("/custom/testcustom/v1");
149 customrouteInfo.setUseOwnUpstream("0");
150 customrouteInfo.setVisualRange("0");
151 customrouteInfo.setEnable_ssl(false);
152 RouteServer[] servers = new RouteServer[]{new RouteServer("10.74.148.88","8080")};
153 customrouteInfo.setServers(servers);
155 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
156 assertNotNull(customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
157 customRouteService.deleteCustomRouteService2Redis("msb:routing:custom:testcustom");
158 assertNull(customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
159 } catch (Exception e) {
160 assert false:"throw exception means error occured!"+e.getMessage();
165 public void testUpdateCustomRouteStatus2Redis(){
166 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
167 customrouteInfo.setServiceName("testcustom");
168 customrouteInfo.setStatus("1");
169 customrouteInfo.setUrl("/custom/testcustom/v1");
170 customrouteInfo.setUseOwnUpstream("0");
171 customrouteInfo.setVisualRange("0");
172 customrouteInfo.setEnable_ssl(true);
173 RouteServer[] servers = new RouteServer[]{new RouteServer("10.74.148.88","8080")};
174 customrouteInfo.setServers(servers);
176 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
177 assertEquals("1", customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom").getStatus());
178 customRouteService.updateCustomRouteStatus2Redis("msb:routing:custom:testcustom", "0");
179 assertEquals("0", customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom").getStatus());
180 } catch (Exception e) {
181 assert false:"throw exception means error occured!"+e.getMessage();
186 public void testGetMultiCustomRouteInstances() throws Exception {
187 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
188 customrouteInfo.setServiceName("testcustom");
189 customrouteInfo.setStatus("1");
190 customrouteInfo.setUrl("/custom/testcustom");
191 customrouteInfo.setUseOwnUpstream("0");
192 customrouteInfo.setVisualRange("0");
193 customrouteInfo.setEnable_ssl(false);
194 customrouteInfo.setServers(new RouteServer[]{new RouteServer("10.74.148.88","8080")});
196 CustomRouteInfo customrouteInfo2 = new CustomRouteInfo();
197 customrouteInfo2.setServiceName("testcustom2");
198 customrouteInfo2.setStatus("0");
199 customrouteInfo2.setUrl("/custom/testcustom2");
200 customrouteInfo2.setUseOwnUpstream("0");
201 customrouteInfo2.setVisualRange("0");;
202 customrouteInfo.setEnable_ssl(true);
203 customrouteInfo2.setServers(new RouteServer[]{new RouteServer("10.74.148.88","8088")});
205 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
206 customRouteService.saveCustomRouteService2Redis(customrouteInfo2, "msb:routing:custom:testcustom2");
208 List<CustomRouteInfo> expected = new ArrayList<>();
209 expected.add(customrouteInfo);
210 expected.add(customrouteInfo2);
211 Collections.sort(expected, customRouteComparator);
213 List<CustomRouteInfo> result = customRouteService.getMultiCustomRouteInstances("msb:routing:custom:*");
215 Collections.sort(result, customRouteComparator);
216 assertEquals(expected, result);
220 public void testDeleteMultiCustomRouteInstances() throws Exception {
221 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
222 customrouteInfo.setServiceName("testcustom");
223 customrouteInfo.setStatus("1");
224 customrouteInfo.setUrl("/custom/testcustom");
225 customrouteInfo.setUseOwnUpstream("0");
226 customrouteInfo.setVisualRange("0");
227 customrouteInfo.setEnable_ssl(false);
228 customrouteInfo.setServers(new RouteServer[]{new RouteServer("10.74.148.88","8080")});
230 CustomRouteInfo customrouteInfo2 = new CustomRouteInfo();
231 customrouteInfo2.setServiceName("testcustom2");
232 customrouteInfo2.setStatus("0");
233 customrouteInfo2.setUrl("/custom/testcustom2");
234 customrouteInfo2.setUseOwnUpstream("0");
235 customrouteInfo2.setVisualRange("0");;
236 customrouteInfo.setEnable_ssl(true);
237 customrouteInfo2.setServers(new RouteServer[]{new RouteServer("10.74.148.88","8088")});
238 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
239 customRouteService.saveCustomRouteService2Redis(customrouteInfo2, "msb:routing:custom:testcustom2");
241 assertEquals(2,customRouteService.getMultiCustomRouteInstances("msb:routing:custom:*").size());
242 assertEquals(2,customRouteService.deleteMultiCustomRouteService2Redis("msb:routing:custom:*"));
243 assertEquals(0, customRouteService.getMultiCustomRouteInstances("msb:routing:custom:*").size());
246 @Test(expected = Exception.class)
247 public void testUpdateCustomRouteStatus2Redis_keyNotExist() throws Exception {
248 customRouteService.updateCustomRouteStatus2Redis("msb:routing:custom:notexistservice", "0");
251 @Test(expected = Exception.class)
252 public void testSaveCustomRouteService2Redis_null() throws Exception {
253 customRouteService.saveCustomRouteService2Redis(null, "msb:routing:custom:null");