fix powermock issue
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / service / CustomRouteServiceTest.java
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
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
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
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
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.service;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertNull;
19
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;
26
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;
39
40 import com.fiftyonred.mock_jedis.MockJedisPool;
41
42 import redis.clients.jedis.JedisPool;
43 import redis.clients.jedis.JedisPoolConfig;
44
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest({JedisUtil.class, RedisAccessWrapper.class})
47 @PowerMockIgnore({"javax.management.*", "jdk.internal.reflect.*"})
48 public class CustomRouteServiceTest {
49     private static CustomRouteService customRouteService = null;
50     private static Comparator<CustomRouteInfo> customRouteComparator = null;
51
52     @BeforeClass
53     public static void setUp() throws Exception {
54         customRouteService = CustomRouteService.getInstance();
55         customRouteComparator = new Comparator<CustomRouteInfo>() {
56             @Override
57             public int compare(CustomRouteInfo o1, CustomRouteInfo o2) {
58                 if (!o1.getServiceName().equals(o2.getServiceName()))
59                     return (o1.getServiceName()).compareTo(o2.getServiceName());
60                 return 0;
61             }
62         };
63     }
64
65     @Before
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());
71
72         PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
73             @Override
74             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
75                 return mockJedisPool.getResource().keys((String) args[0]);
76             }
77         });
78     }
79
80     @Test
81     public void testGetCustomRouteInstance_key_not_exist() {
82         try {
83             assertNull(customRouteService.getCustomRouteInstance("msb:routing:custom:notexistservice:v1"));
84         } catch (Exception e) {
85             assert false : "throw exception means error occured!" + e.getMessage();
86         }
87     }
88
89     @Test
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);
100         try {
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();
105         }
106     }
107
108     @Test
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);
119         try {
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();
124         }
125     }
126
127     @Test
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);
138         try {
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();
144         }
145     }
146
147     @Test
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);
158         try {
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();
165         }
166     }
167
168     @Test
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);
179         try {
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();
186         }
187     }
188
189     @Test
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")});
199
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")});
208
209         customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
210         customRouteService.saveCustomRouteService2Redis(customrouteInfo2, "msb:routing:custom:testcustom2");
211
212         List<CustomRouteInfo> expected = new ArrayList<>();
213         expected.add(customrouteInfo);
214         expected.add(customrouteInfo2);
215         Collections.sort(expected, customRouteComparator);
216
217         List<CustomRouteInfo> result = customRouteService.getMultiCustomRouteInstances("msb:routing:custom:*");
218
219         Collections.sort(result, customRouteComparator);
220         assertEquals(expected, result);
221     }
222
223     @Test
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")});
233
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");
244
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());
248     }
249
250     @Test(expected = Exception.class)
251     public void testUpdateCustomRouteStatus2Redis_keyNotExist() throws Exception {
252         customRouteService.updateCustomRouteStatus2Redis("msb:routing:custom:notexistservice", "0");
253     }
254
255     @Test(expected = Exception.class)
256     public void testSaveCustomRouteService2Redis_null() throws Exception {
257         customRouteService.saveCustomRouteService2Redis(null, "msb:routing:custom:null");
258     }
259
260 }