Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / service / CustomRouteServiceTest.java
1 package org.onap.msb.apiroute.wrapper.service;
2
3 import com.fiftyonred.mock_jedis.MockJedisPool;
4 import org.junit.Before;
5 import org.junit.BeforeClass;
6 import org.junit.Test;
7 import org.junit.runner.RunWith;
8 import org.onap.msb.apiroute.api.CustomRouteInfo;
9 import org.onap.msb.apiroute.api.RouteServer;
10 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
11 import org.onap.msb.apiroute.wrapper.service.CustomRouteService;
12 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
13 import org.powermock.api.mockito.PowerMockito;
14 import org.powermock.core.classloader.annotations.PowerMockIgnore;
15 import org.powermock.core.classloader.annotations.PrepareForTest;
16 import org.powermock.modules.junit4.PowerMockRunner;
17 import redis.clients.jedis.JedisPool;
18 import redis.clients.jedis.JedisPoolConfig;
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 static org.junit.Assert.*;
28
29 @RunWith(PowerMockRunner.class)
30 @PrepareForTest({JedisUtil.class,RedisAccessWrapper.class})
31 @PowerMockIgnore( {"javax.management.*"})
32 public class CustomRouteServiceTest {
33     private static CustomRouteService customRouteService = null;
34     private static Comparator<CustomRouteInfo> customRouteComparator = null;
35     @BeforeClass
36     public static void setUp() throws Exception{
37         customRouteService = CustomRouteService.getInstance();
38         customRouteComparator = new Comparator<CustomRouteInfo>() {
39             @Override
40             public int compare(CustomRouteInfo o1, CustomRouteInfo o2) {
41                 if (!o1.getServiceName().equals(o2.getServiceName()))
42                     return (o1.getServiceName()).compareTo(o2.getServiceName());
43                 return 0;
44             }
45         };
46     }
47     @Before
48     public void setUpBeforeTest() throws Exception {
49         final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
50         PowerMockito.mockStatic(JedisUtil.class);
51         JedisUtil jedisUtil=PowerMockito.mock(JedisUtil.class);
52         PowerMockito.when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());
53
54         PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
55             @Override
56             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
57                 return mockJedisPool.getResource().keys((String) args[0]);
58             }
59         });
60     }
61     @Test
62     public void testGetCustomRouteInstance_key_not_exist(){
63         try {
64             assertNull(customRouteService.getCustomRouteInstance("msb:routing:custom:notexistservice:v1"));
65         } catch (Exception e) {
66             assert false:"throw exception means error occured!"+e.getMessage();
67         }
68     }
69
70     @Test
71     public void testGetCustomRouteInstance_key_exist(){
72         CustomRouteInfo customrouteInfo = new CustomRouteInfo();
73         customrouteInfo.setServiceName("testcustom");
74         customrouteInfo.setStatus("1");
75         customrouteInfo.setUrl("/custom/testcustom");
76         customrouteInfo.setUseOwnUpstream("0");
77         customrouteInfo.setVisualRange("0");
78         customrouteInfo.setEnable_ssl(false);
79         RouteServer[] servers = new RouteServer[]{new RouteServer("10.74.148.88","8080")};
80         customrouteInfo.setServers(servers);
81         try {
82             customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
83             assertEquals(customrouteInfo, customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
84         } catch (Exception e) {
85             assert false:"throw exception means error occured!"+e.getMessage();
86         }
87     }
88
89     @Test
90     public void testSaveCustomRouteService2Redis(){
91         CustomRouteInfo customrouteInfo = new CustomRouteInfo();
92         customrouteInfo.setServiceName("testcustom");
93         customrouteInfo.setStatus("1");
94         customrouteInfo.setUrl("/custom/testcustom/v1");
95         customrouteInfo.setUseOwnUpstream("0");
96         customrouteInfo.setVisualRange("0");
97         customrouteInfo.setEnable_ssl(true);
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_urlIsSlash(){
110         CustomRouteInfo customrouteInfo = new CustomRouteInfo();
111         customrouteInfo.setServiceName("testcustom");
112         customrouteInfo.setStatus("1");
113         customrouteInfo.setUrl("/");
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             customrouteInfo.setUrl("");
122             assertEquals(customrouteInfo, customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
123         } catch (Exception e) {
124             assert false:"throw exception means error occured!"+e.getMessage();
125         }
126     }
127
128     @Test
129     public void testDeleteCustomRouteService2Redis(){
130         CustomRouteInfo customrouteInfo = new CustomRouteInfo();
131         customrouteInfo.setServiceName("testcustom");
132         customrouteInfo.setStatus("1");
133         customrouteInfo.setUrl("/custom/testcustom/v1");
134         customrouteInfo.setUseOwnUpstream("0");
135         customrouteInfo.setVisualRange("0");
136         customrouteInfo.setEnable_ssl(false);
137         RouteServer[] servers = new RouteServer[]{new RouteServer("10.74.148.88","8080")};
138         customrouteInfo.setServers(servers);
139         try {
140             customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
141             assertNotNull(customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
142             customRouteService.deleteCustomRouteService2Redis("msb:routing:custom:testcustom");
143             assertNull(customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom"));
144         } catch (Exception e) {
145             assert false:"throw exception means error occured!"+e.getMessage();
146         }
147     }
148
149     @Test
150     public void testUpdateCustomRouteStatus2Redis(){
151         CustomRouteInfo customrouteInfo = new CustomRouteInfo();
152         customrouteInfo.setServiceName("testcustom");
153         customrouteInfo.setStatus("1");
154         customrouteInfo.setUrl("/custom/testcustom/v1");
155         customrouteInfo.setUseOwnUpstream("0");
156         customrouteInfo.setVisualRange("0");
157         customrouteInfo.setEnable_ssl(true);
158         RouteServer[] servers = new RouteServer[]{new RouteServer("10.74.148.88","8080")};
159         customrouteInfo.setServers(servers);
160         try {
161             customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
162             assertEquals("1", customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom").getStatus());
163             customRouteService.updateCustomRouteStatus2Redis("msb:routing:custom:testcustom", "0");
164             assertEquals("0", customRouteService.getCustomRouteInstance("msb:routing:custom:testcustom").getStatus());
165         } catch (Exception e) {
166             assert false:"throw exception means error occured!"+e.getMessage();
167         }
168     }
169
170     @Test
171     public void testGetMultiCustomRouteInstances() throws Exception {
172         CustomRouteInfo customrouteInfo = new CustomRouteInfo();
173         customrouteInfo.setServiceName("testcustom");
174         customrouteInfo.setStatus("1");
175         customrouteInfo.setUrl("/custom/testcustom");
176         customrouteInfo.setUseOwnUpstream("0");
177         customrouteInfo.setVisualRange("0");
178         customrouteInfo.setEnable_ssl(false);
179         customrouteInfo.setServers(new RouteServer[]{new RouteServer("10.74.148.88","8080")});
180
181         CustomRouteInfo customrouteInfo2 = new CustomRouteInfo();
182         customrouteInfo2.setServiceName("testcustom2");
183         customrouteInfo2.setStatus("0");
184         customrouteInfo2.setUrl("/custom/testcustom2");
185         customrouteInfo2.setUseOwnUpstream("0");
186         customrouteInfo2.setVisualRange("0");;
187         customrouteInfo.setEnable_ssl(true);
188         customrouteInfo2.setServers(new RouteServer[]{new RouteServer("10.74.148.88","8088")});
189
190         customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
191         customRouteService.saveCustomRouteService2Redis(customrouteInfo2, "msb:routing:custom:testcustom2");
192
193         List<CustomRouteInfo> expected = new ArrayList<>();
194         expected.add(customrouteInfo);
195         expected.add(customrouteInfo2);
196         Collections.sort(expected, customRouteComparator);
197
198         List<CustomRouteInfo> result = customRouteService.getMultiCustomRouteInstances("msb:routing:custom:*");
199
200         Collections.sort(result, customRouteComparator);
201         assertEquals(expected, result);
202     }
203
204     @Test
205     public void testDeleteMultiCustomRouteInstances() throws Exception {
206         CustomRouteInfo customrouteInfo = new CustomRouteInfo();
207         customrouteInfo.setServiceName("testcustom");
208         customrouteInfo.setStatus("1");
209         customrouteInfo.setUrl("/custom/testcustom");
210         customrouteInfo.setUseOwnUpstream("0");
211         customrouteInfo.setVisualRange("0");
212         customrouteInfo.setEnable_ssl(false);
213         customrouteInfo.setServers(new RouteServer[]{new RouteServer("10.74.148.88","8080")});
214
215         CustomRouteInfo customrouteInfo2 = new CustomRouteInfo();
216         customrouteInfo2.setServiceName("testcustom2");
217         customrouteInfo2.setStatus("0");
218         customrouteInfo2.setUrl("/custom/testcustom2");
219         customrouteInfo2.setUseOwnUpstream("0");
220         customrouteInfo2.setVisualRange("0");;
221         customrouteInfo.setEnable_ssl(true);
222         customrouteInfo2.setServers(new RouteServer[]{new RouteServer("10.74.148.88","8088")});
223         customRouteService.saveCustomRouteService2Redis(customrouteInfo, "msb:routing:custom:testcustom");
224         customRouteService.saveCustomRouteService2Redis(customrouteInfo2, "msb:routing:custom:testcustom2");
225
226         assertEquals(2,customRouteService.getMultiCustomRouteInstances("msb:routing:custom:*").size());
227         assertEquals(2,customRouteService.deleteMultiCustomRouteService2Redis("msb:routing:custom:*"));
228         assertEquals(0, customRouteService.getMultiCustomRouteInstances("msb:routing:custom:*").size());
229     }
230
231     @Test(expected = Exception.class)
232     public void testUpdateCustomRouteStatus2Redis_keyNotExist() throws Exception {
233         customRouteService.updateCustomRouteStatus2Redis("msb:routing:custom:notexistservice", "0");
234     }
235
236     @Test(expected = Exception.class)
237     public void testSaveCustomRouteService2Redis_null() throws Exception {
238         customRouteService.saveCustomRouteService2Redis(null, "msb:routing:custom:null");
239     }
240
241 }