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