897f84a16aa0810345f881dfc4dd735da0782418
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalsdk.core.onboarding.crossapi;
39
40 import java.io.BufferedReader;
41 import java.io.InputStreamReader;
42 import java.io.PrintWriter;
43 import java.lang.reflect.Field;
44 import java.lang.reflect.Method;
45 import java.lang.reflect.Modifier;
46 import java.util.HashMap;
47 import java.util.Map;
48
49 import javax.servlet.ServletException;
50 import javax.servlet.ServletInputStream;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53
54 import org.junit.Before;
55 import org.junit.Test;
56 import org.junit.runner.RunWith;
57 import org.mockito.Mock;
58 import org.mockito.Mockito;
59 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
60 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
61 import org.onap.portalsdk.core.restful.domain.EcompUser;
62 import org.powermock.api.mockito.PowerMockito;
63 import org.powermock.core.classloader.annotations.PrepareForTest;
64 import org.powermock.modules.junit4.PowerMockRunner;
65
66 import com.fasterxml.jackson.databind.ObjectMapper;
67
68 @RunWith(PowerMockRunner.class)
69 @PrepareForTest({PortalRestAPIProxy.class})
70 public class PortalRestAPIProxyTest {
71         @Mock
72         HttpServletRequest request;
73         @Mock
74         HttpServletResponse response;
75         @Mock
76         PrintWriter writer;
77         @Mock
78         PortalRestAPICentralServiceImpl portalRestAPICentralServiceImpl;
79         @Mock
80         private Method doPost;
81         private Method doGet;
82         private PortalRestAPIProxy portalRestAPIProxyObj;
83         private Class portalRestAPIProxyClass;
84         private Field portalRestApiServiceImplField;
85         private Field isAccessCentralizedField;
86         private Field mapperField;
87         @Mock
88         ObjectMapper mapper;
89         
90         @Before
91         public void setUp() throws Exception {
92                 portalRestAPIProxyClass = PortalRestAPIProxy.class;
93                 portalRestAPIProxyObj = (PortalRestAPIProxy) portalRestAPIProxyClass.newInstance();
94                 Mockito.when(response.getWriter()).thenReturn(writer);
95                 PowerMockito.doNothing().when(writer).print(Mockito.anyString());
96         ServletInputStream in = Mockito.mock(ServletInputStream.class);
97         Mockito.when(request.getInputStream()).thenReturn(in);
98         InputStreamReader ir = Mockito.mock(InputStreamReader.class);
99         PowerMockito.whenNew(InputStreamReader.class).withArguments(in).thenReturn(ir);
100         BufferedReader br = Mockito.mock(BufferedReader.class);
101         PowerMockito.whenNew(BufferedReader.class).withArguments(ir).thenReturn(br);
102         char[] charBuffer = new char[1024];
103         Mockito.when(br.read(charBuffer)).thenReturn(0);
104         PowerMockito.doNothing().when(writer).print(Mockito.anyString());
105         portalRestApiServiceImplField = portalRestAPIProxyClass.getDeclaredField("portalRestApiServiceImpl");
106         portalRestApiServiceImplField.setAccessible(true);
107         portalRestApiServiceImplField.set(portalRestAPIProxyClass, portalRestAPICentralServiceImpl);
108         isAccessCentralizedField = portalRestAPIProxyClass.getDeclaredField("isAccessCentralized");
109         isAccessCentralizedField.setAccessible(true);
110         mapperField = portalRestAPIProxyClass.getDeclaredField("mapper");
111         mapperField.setAccessible(true);
112         Field modifiersField = Field.class.getDeclaredField("modifiers");
113         modifiersField.setAccessible(true);
114         modifiersField.setInt(isAccessCentralizedField, isAccessCentralizedField.getModifiers() & ~Modifier.FINAL);
115         isAccessCentralizedField.set(null, "remote");
116         modifiersField.setInt(mapperField, mapperField.getModifiers() & ~Modifier.FINAL);
117         ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
118         mapperField.set(portalRestAPIProxyObj, mapper);
119         doPost = portalRestAPIProxyClass.getDeclaredMethod("doPost", new Class[]{HttpServletRequest.class, HttpServletResponse.class});
120         doPost.setAccessible(true);
121         doGet = portalRestAPIProxyClass.getDeclaredMethod("doGet", new Class[]{HttpServletRequest.class, HttpServletResponse.class});
122         doGet.setAccessible(true);
123         Map<String,String> appCredentials = new HashMap<>();
124         Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request,appCredentials)).thenReturn(true);
125         }
126         
127         @Test(expected=ServletException.class)
128         public void testInit() throws ServletException {
129                 portalRestAPIProxyObj.init();
130         }
131         
132         @Test
133         public void testDoPost_WhenProxyObjectIsNull() throws Exception {
134                 portalRestApiServiceImplField.set(portalRestAPIProxyClass, null);
135             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
136         }
137         
138         @Test
139         public void testDoPost_WhenRequestForStoreAnalytics() throws Exception {
140             Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenReturn("test");
141             Mockito.when(portalRestAPICentralServiceImpl.getCredentials()).thenReturn(new HashMap(){{put("username","test");put("password","test");}});
142         Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/storeAnalytics");
143             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
144         }
145         
146         @Test
147         public void testDoPost_WhenRequestForStoreAnalyticsAndUserIdIsNull() throws Exception {
148             Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenReturn(null);
149         Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/storeAnalytics");
150             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
151         }
152         
153         @Test(expected=ServletException.class)
154         public void testDoPost1_WhenExceptionOccurInGetUserID() throws Exception {
155             Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenThrow(new PortalAPIException("test"));
156         Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/storeAnalytics");
157             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
158         }
159         
160         @Test
161         public void testDoPost_WhenRequestForUpdateSessionTimeOuts() throws Exception {
162         Mockito.when(request.getRequestURI()).thenReturn("/updateSessionTimeOuts");
163             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
164         }
165         
166         @Test
167         public void testDoPost_WhenRequestForTimeoutSession() throws Exception {
168         Mockito.when(request.getRequestURI()).thenReturn("/timeoutSession");
169             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
170         }
171         
172         @Test
173         public void testDoPost_WhenRequestForUserAndEcompUserIsNull() throws Exception {
174         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user");
175             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
176         }
177         
178         @Test
179         public void testDoPost_WhenRequestForUser() throws Exception {
180                 EcompUser ecompUser = Mockito.mock(EcompUser.class);
181         Mockito.when(mapper.readValue(Mockito.anyString(), Mockito.eq(EcompUser.class))).thenReturn(ecompUser);
182         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user");
183             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
184         }
185         
186         @Test
187         public void testDoPost_WhenRequestForUserButNotRoles() throws Exception {
188         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/role");
189             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
190         }
191         
192         @Test
193         public void testDoPost_WhenRequestForUserRoles() throws Exception {
194         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/1/roles");
195             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
196         }
197         
198         @Test
199         public void testDoPost_WhenRequestForUserRolesWithRemote() throws Exception {
200                 isAccessCentralizedField.set(portalRestAPIProxyClass, "remote1");
201         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/1/roles");
202             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
203         }
204         
205
206         @Test
207         public void testDoPost_WhenIsAppAuthenticatedIsFalse() throws Exception {
208         Map<String,String> appCredentials = new HashMap<>();
209                 Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request,appCredentials)).thenReturn(false);
210                 Mockito.when(request.getRequestURI()).thenReturn("");
211             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
212         }
213         
214         @Test
215         public void testDoPost_WhenIsAppAuthenticatedThrowException() throws Exception {
216                   Map<String,String> appCredentials = new HashMap<>();
217                 Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request,appCredentials)).thenThrow(new PortalAPIException());
218                 Mockito.when(request.getRequestURI()).thenReturn("");
219             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
220         }
221         
222         @Test
223         public void testDoGet_WhenProxyObjectIsNull() throws Exception {
224                 portalRestApiServiceImplField.set(portalRestAPIProxyClass, null);
225             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
226         }
227         
228         @Test
229         public void testDoGet_WhenRequestForAnalytics() throws Exception {
230             Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenReturn("test");
231             Mockito.when(portalRestAPICentralServiceImpl.getCredentials()).thenReturn(new HashMap(){{put("username","test");put("password","test");}});
232         Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/analytics");
233             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
234         }
235         
236         @Test
237         public void testDoGet_WhenRequestForAnalyticsAndUserIdIsNull() throws Exception {
238             Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenReturn(null);
239         Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/analytics");
240             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
241         }
242         
243         @Test(expected=ServletException.class)
244         public void testDoGet1_WhenExceptionOccurInGetUserID() throws Exception {
245             Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenThrow(new PortalAPIException("test"));
246         Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/analytics");
247             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
248         }
249         
250         @Test
251         public void testDoGet_WhenRequestForSessionTimeOuts() throws Exception {
252         Mockito.when(request.getRequestURI()).thenReturn("/sessionTimeOuts");
253             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
254         }
255         
256         @Test
257         public void testDoGet_WhenRequestForUsers() throws Exception {
258         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/users");
259             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
260         }
261         
262         @Test
263         public void testDoGet_WhenRequestForRoles() throws Exception {
264                 EcompUser ecompUser = Mockito.mock(EcompUser.class);
265         Mockito.when(mapper.readValue(Mockito.anyString(), Mockito.eq(EcompUser.class))).thenReturn(ecompUser);
266         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/roles");
267             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
268         }
269         
270         @Test
271         public void testDoGet_WhenRequestForUserButNotRoles() throws Exception {
272         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/role");
273             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
274         }
275         
276         @Test
277         public void testDoGet_WhenRequestForUserRoles() throws Exception {
278         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/1/roles");
279             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
280         }
281         
282         @Test
283         public void testDoGet_WhenRequestForUserRolesWithRemote() throws Exception {
284                 isAccessCentralizedField.set(portalRestAPIProxyClass, "remote1");
285         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/1/roles");
286             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
287         }
288         
289
290         @Test
291         public void testDoGet_WhenIsAppAuthenticatedIsFalse() throws Exception {
292                 Map<String,String> appCredentials = new HashMap<>();
293                 Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request,appCredentials)).thenReturn(false);
294                 Mockito.when(request.getRequestURI()).thenReturn("");
295             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
296         }
297         
298         @Test
299         public void testDoGet_WhenIsAppAuthenticatedThrowException() throws Exception {
300                 Map<String,String> appCredentials = new HashMap<>();
301                 Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request,appCredentials)).thenThrow(new PortalAPIException());
302                 Mockito.when(request.getRequestURI()).thenReturn("");
303             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
304         }
305 }