d72c57c98c201b77b6e0ab8ad76fb63a35a53255
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / CustomRouteServiceWrapperTest.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;
15
16 import java.lang.reflect.InvocationHandler;
17 import java.lang.reflect.Method;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.List;
22
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.onap.msb.apiroute.api.CustomRouteInfo;
29 import org.onap.msb.apiroute.api.RouteServer;
30 import org.onap.msb.apiroute.api.exception.ExtendedNotFoundException;
31 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
32 import org.onap.msb.apiroute.wrapper.util.ConfigUtil;
33 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
34 import org.onap.msb.apiroute.wrapper.util.RouteUtil;
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, RouteUtil.class, RedisAccessWrapper.class})
47 @PowerMockIgnore({"javax.management.*"})
48 public class CustomRouteServiceWrapperTest {
49
50     private static CustomRouteServiceWrapper customRouteServiceWrapper;
51     private static Comparator<CustomRouteInfo> customRouteComparator = null;
52
53     @BeforeClass
54     public static void setUpBeforeClass() throws Exception {
55         customRouteServiceWrapper = CustomRouteServiceWrapper.getInstance();
56         customRouteComparator = new Comparator<CustomRouteInfo>() {
57             @Override
58             public int compare(CustomRouteInfo o1, CustomRouteInfo o2) {
59                 if (!o1.getServiceName().equals(o2.getServiceName()))
60                     return (o1.getServiceName()).compareTo(o2.getServiceName());
61                 return 0;
62             }
63         };
64
65         PowerMockito.mockStatic(System.class);
66         PowerMockito.when(System.getenv("ROUTE_WAY")).thenReturn(null);
67         PowerMockito.when(System.getenv("ROUTE_WAY")).thenReturn("ip|domain");
68         ConfigUtil.getInstance().initRouteWay();
69     }
70
71     @Before
72     public void setUpBeforeTest() throws Exception {
73         final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
74         PowerMockito.mockStatic(JedisUtil.class);
75         JedisUtil jedisUtil = PowerMockito.mock(JedisUtil.class);
76         PowerMockito.when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());
77
78         PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
79             @Override
80             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
81                 return mockJedisPool.getResource().keys((String) args[0]);
82             }
83         });
84     }
85
86     @Test
87     public void test_getCustomRouteInstance_not_exist() {
88         try {
89             customRouteServiceWrapper.getCustomRouteInstance("/testForJunit", "", "", "ip");
90             Assert.fail("should not process to here.");
91         } catch (Exception e) {
92             Assert.assertTrue(e instanceof ExtendedNotFoundException);
93
94         }
95
96     }
97
98     @Test
99     public void test_getCustomRouteInstance() {
100
101         CustomRouteInfo customrouteInfo = buildCustomRouteInfo();
102         try {
103             customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo, "ip");
104             CustomRouteInfo dbCustomRouteInfo =
105                             customRouteServiceWrapper.getCustomRouteInstance("/testcustom", "", "", "ip");
106             Assert.assertEquals(customrouteInfo, dbCustomRouteInfo);
107         } catch (Exception e) {
108             Assert.fail("throw exception means error occured!" + e.getMessage());
109         }
110
111     }
112
113     @Test
114     public void test_getAllCustomRouteInstances() {
115         CustomRouteInfo customrouteInfo = buildCustomRouteInfo();
116         CustomRouteInfo customrouteInfo2 = buildCustomRouteInfo2();
117         List<CustomRouteInfo> expected = new ArrayList<>();
118         expected.add(customrouteInfo);
119         expected.add(customrouteInfo2);
120         Collections.sort(expected, customRouteComparator);
121
122         try {
123             customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo, "ip");
124             customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo2, "ip");
125
126             PowerMockito.mockStatic(RouteUtil.class);
127             PowerMockito.when(RouteUtil.getMutiRedisKey(RouteUtil.CUSTOMROUTE, "ip"))
128                             .thenReturn("msb:routing:custom:*");
129             List<CustomRouteInfo> customRouterList = customRouteServiceWrapper.getAllCustomRouteInstances("ip");
130             Collections.sort(customRouterList, customRouteComparator);
131
132             Assert.assertEquals(expected, customRouterList);
133
134         } catch (Exception e) {
135             Assert.fail("throw exception means error occured!" + e.getMessage());
136         }
137
138     }
139
140     @Test
141     public void test_updateCustomRouteStatus() {
142         CustomRouteInfo customrouteInfo = buildCustomRouteInfo();
143         try {
144             customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo, "ip");
145             CustomRouteInfo dbCustomrouteInfo =
146                             customRouteServiceWrapper.getCustomRouteInstance("/testcustom", "", "", "ip");
147             Assert.assertEquals("1", dbCustomrouteInfo.getStatus());
148             customRouteServiceWrapper.updateCustomRouteStatus("/testcustom", "", "", "0", "ip");
149             dbCustomrouteInfo = customRouteServiceWrapper.getCustomRouteInstance("/testcustom", "", "", "ip");
150             Assert.assertEquals("0", dbCustomrouteInfo.getStatus());
151         } catch (Exception e) {
152             Assert.fail("throw exception means error occured!" + e.getMessage());
153         }
154
155     }
156
157
158
159     @Test
160     public void test_deleteCustomRoute() {
161         CustomRouteInfo customrouteInfo2 = buildCustomRouteInfo2();
162         try {
163             customRouteServiceWrapper.saveCustomRouteInstance4Rest(customrouteInfo2, "ip");
164             CustomRouteInfo dbCustomrouteInfo =
165                             customRouteServiceWrapper.getCustomRouteInstance("/testcustom2", "", "", "ip");
166             Assert.assertNotNull(dbCustomrouteInfo);
167
168         } catch (Exception e) {
169             Assert.fail("throw exception means error occured!" + e.getMessage());
170         }
171         try {
172             customRouteServiceWrapper.deleteCustomRoute("/testcustom2", "", "", "ip");
173             customRouteServiceWrapper.getCustomRouteInstance("/testcustom2", "", "", "ip");
174         } catch (Exception e) {
175             Assert.assertTrue(e instanceof ExtendedNotFoundException);
176         }
177     }
178
179
180     private CustomRouteInfo buildCustomRouteInfo() {
181         CustomRouteInfo customrouteInfo = new CustomRouteInfo();
182         customrouteInfo.setServiceName("/testcustom");
183         customrouteInfo.setStatus("1");
184         customrouteInfo.setUrl("/custom/testcustom");
185         customrouteInfo.setUseOwnUpstream("0");
186         customrouteInfo.setVisualRange("0");
187         customrouteInfo.setEnable_ssl(false);
188         RouteServer[] servers = new RouteServer[] {new RouteServer("10.74.148.88", "8080")};
189         customrouteInfo.setServers(servers);
190         return customrouteInfo;
191     }
192
193     private CustomRouteInfo buildCustomRouteInfo2() {
194         CustomRouteInfo customrouteInfo = new CustomRouteInfo();
195         customrouteInfo.setServiceName("/testcustom2");
196         customrouteInfo.setStatus("1");
197         customrouteInfo.setUrl("/custom/testcustom");
198         customrouteInfo.setUseOwnUpstream("0");
199         customrouteInfo.setVisualRange("1");
200         customrouteInfo.setEnable_ssl(true);
201         RouteServer[] servers = new RouteServer[] {new RouteServer("10.74.148.89", "8080")};
202         customrouteInfo.setServers(servers);
203         return customrouteInfo;
204     }
205
206 }