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.service;
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertNull;
20 import java.lang.reflect.InvocationHandler;
21 import java.lang.reflect.Method;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.List;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
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.wrapper.dao.RedisAccessWrapper;
34 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
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, RedisAccessWrapper.class})
47 @PowerMockIgnore({"javax.management.*"})
48 public class CustomRouteServiceTest {
49 private static CustomRouteService customRouteService = null;
50 private static Comparator<CustomRouteInfo> customRouteComparator = null;
53 public static void setUp() throws Exception {
54 customRouteService = CustomRouteService.getInstance();
55 customRouteComparator = new Comparator<CustomRouteInfo>() {
57 public int compare(CustomRouteInfo o1, CustomRouteInfo o2) {
58 if (!o1.getServiceName().equals(o2.getServiceName()))
59 return (o1.getServiceName()).compareTo(o2.getServiceName());
66 public void setUpBeforeTest() throws Exception {
67 final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
68 PowerMockito.mockStatic(JedisUtil.class);
69 JedisUtil jedisUtil = PowerMockito.mock(JedisUtil.class);
70 PowerMockito.when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());
72 PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
74 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
75 return mockJedisPool.getResource().keys((String) args[0]);
81 public void testGetCustomRouteInstance_key_not_exist() {
83 assertNull(customRouteService.getCustomRouteInstance("msb:routing:custom:notexistservice:v1"));
84 } catch (Exception e) {
85 assert false : "throw exception means error occured!" + e.getMessage();
90 public void testGetCustomRouteInstance_key_exist() {
91 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
92 customrouteInfo.setServiceName("testcustom");
93 customrouteInfo.setStatus("1");
94 customrouteInfo.setUrl("/custom/testcustom");
95 customrouteInfo.setUseOwnUpstream("0");
96 customrouteInfo.setVisualRange("0");
97 customrouteInfo.setEnable_ssl(false);
98 RouteServer[] servers = new RouteServer[] {new RouteServer("10.74.148.88", "8080")};
99 customrouteInfo.setServers(servers);
101 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
102 assertEquals(customrouteInfo, customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
103 } catch (Exception e) {
104 assert false : "throw exception means error occured!" + e.getMessage();
109 public void testSaveCustomRouteService2Redis() {
110 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
111 customrouteInfo.setServiceName("testcustom");
112 customrouteInfo.setStatus("1");
113 customrouteInfo.setUrl("/custom/testcustom/v1");
114 customrouteInfo.setUseOwnUpstream("0");
115 customrouteInfo.setVisualRange("0");
116 customrouteInfo.setEnable_ssl(true);
117 RouteServer[] servers = new RouteServer[] {new RouteServer("10.74.148.88", "8080")};
118 customrouteInfo.setServers(servers);
120 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
121 assertEquals(customrouteInfo, customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
122 } catch (Exception e) {
123 assert false : "throw exception means error occured!" + e.getMessage();
128 public void testSaveCustomRouteService2Redis_urlIsSlash() {
129 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
130 customrouteInfo.setServiceName("testcustom");
131 customrouteInfo.setStatus("1");
132 customrouteInfo.setUrl("/");
133 customrouteInfo.setUseOwnUpstream("0");
134 customrouteInfo.setVisualRange("0");
135 customrouteInfo.setEnable_ssl(true);
136 RouteServer[] servers = new RouteServer[] {new RouteServer("10.74.148.88", "8080")};
137 customrouteInfo.setServers(servers);
139 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
140 customrouteInfo.setUrl("");
141 assertEquals(customrouteInfo, customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
142 } catch (Exception e) {
143 assert false : "throw exception means error occured!" + e.getMessage();
148 public void testDeleteCustomRouteService2Redis() {
149 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
150 customrouteInfo.setServiceName("testcustom");
151 customrouteInfo.setStatus("1");
152 customrouteInfo.setUrl("/custom/testcustom/v1");
153 customrouteInfo.setUseOwnUpstream("0");
154 customrouteInfo.setVisualRange("0");
155 customrouteInfo.setEnable_ssl(false);
156 RouteServer[] servers = new RouteServer[] {new RouteServer("10.74.148.88", "8080")};
157 customrouteInfo.setServers(servers);
159 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
160 assertNotNull(customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
161 customRouteService.deleteCustomRouteService2Redis("msb:routing:custom:testcustom");
162 assertNull(customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
163 } catch (Exception e) {
164 assert false : "throw exception means error occured!" + e.getMessage();
169 public void testUpdateCustomRouteStatus2Redis() {
170 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
171 customrouteInfo.setServiceName("testcustom");
172 customrouteInfo.setStatus("1");
173 customrouteInfo.setUrl("/custom/testcustom/v1");
174 customrouteInfo.setUseOwnUpstream("0");
175 customrouteInfo.setVisualRange("0");
176 customrouteInfo.setEnable_ssl(true);
177 RouteServer[] servers = new RouteServer[] {new RouteServer("10.74.148.88", "8080")};
178 customrouteInfo.setServers(servers);
180 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
181 assertEquals("1", customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom").getStatus());
182 customRouteService.updateCustomRouteStatus2Redis("msb:routing:custom:testcustom", "0");
183 assertEquals("0", customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom").getStatus());
184 } catch (Exception e) {
185 assert false : "throw exception means error occured!" + e.getMessage();
190 public void testGetMultiCustomRouteInstances() throws Exception {
191 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
192 customrouteInfo.setServiceName("testcustom");
193 customrouteInfo.setStatus("1");
194 customrouteInfo.setUrl("/custom/testcustom");
195 customrouteInfo.setUseOwnUpstream("0");
196 customrouteInfo.setVisualRange("0");
197 customrouteInfo.setEnable_ssl(false);
198 customrouteInfo.setServers(new RouteServer[] {new RouteServer("10.74.148.88", "8080")});
200 CustomRouteInfo customrouteInfo2 = new CustomRouteInfo();
201 customrouteInfo2.setServiceName("testcustom2");
202 customrouteInfo2.setStatus("0");
203 customrouteInfo2.setUrl("/custom/testcustom2");
204 customrouteInfo2.setUseOwnUpstream("0");
205 customrouteInfo2.setVisualRange("0");;
206 customrouteInfo.setEnable_ssl(true);
207 customrouteInfo2.setServers(new RouteServer[] {new RouteServer("10.74.148.88", "8088")});
209 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
210 customRouteService.saveCustomRouteService2Redis(customrouteInfo2, "msb:routing:custom:testcustom2");
212 List<CustomRouteInfo> expected = new ArrayList<>();
213 expected.add(customrouteInfo);
214 expected.add(customrouteInfo2);
215 Collections.sort(expected, customRouteComparator);
217 List<CustomRouteInfo> result = customRouteService.getMultiCustomRouteInstances("msb:routing:custom:*");
219 Collections.sort(result, customRouteComparator);
220 assertEquals(expected, result);
224 public void testDeleteMultiCustomRouteInstances() throws Exception {
225 CustomRouteInfo customrouteInfo = new CustomRouteInfo();
226 customrouteInfo.setServiceName("testcustom");
227 customrouteInfo.setStatus("1");
228 customrouteInfo.setUrl("/custom/testcustom");
229 customrouteInfo.setUseOwnUpstream("0");
230 customrouteInfo.setVisualRange("0");
231 customrouteInfo.setEnable_ssl(false);
232 customrouteInfo.setServers(new RouteServer[] {new RouteServer("10.74.148.88", "8080")});
234 CustomRouteInfo customrouteInfo2 = new CustomRouteInfo();
235 customrouteInfo2.setServiceName("testcustom2");
236 customrouteInfo2.setStatus("0");
237 customrouteInfo2.setUrl("/custom/testcustom2");
238 customrouteInfo2.setUseOwnUpstream("0");
239 customrouteInfo2.setVisualRange("0");;
240 customrouteInfo.setEnable_ssl(true);
241 customrouteInfo2.setServers(new RouteServer[] {new RouteServer("10.74.148.88", "8088")});
242 customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
243 customRouteService.saveCustomRouteService2Redis(customrouteInfo2, "msb:routing:custom:testcustom2");
245 assertEquals(2, customRouteService.getMultiCustomRouteInstances("msb:routing:custom:*").size());
246 assertEquals(2, customRouteService.deleteMultiCustomRouteService2Redis("msb:routing:custom:*"));
247 assertEquals(0, customRouteService.getMultiCustomRouteInstances("msb:routing:custom:*").size());
250 @Test(expected = Exception.class)
251 public void testUpdateCustomRouteStatus2Redis_keyNotExist() throws Exception {
252 customRouteService.updateCustomRouteStatus2Redis("msb:routing:custom:notexistservice", "0");
255 @Test(expected = Exception.class)
256 public void testSaveCustomRouteService2Redis_null() throws Exception {
257 customRouteService.saveCustomRouteService2Redis(null, "msb:routing:custom:null");