ce1035e75cad6a4ba6dc3ad3b5937f2bc71ff3db
[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
48 import javax.servlet.ServletException;
49 import javax.servlet.ServletInputStream;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52
53 import org.junit.Before;
54 import org.junit.Test;
55 import org.junit.runner.RunWith;
56 import org.mockito.Mock;
57 import org.mockito.Mockito;
58 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
59 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
60 import org.onap.portalsdk.core.restful.domain.EcompUser;
61 import org.powermock.api.mockito.PowerMockito;
62 import org.powermock.core.classloader.annotations.PrepareForTest;
63 import org.powermock.modules.junit4.PowerMockRunner;
64
65 import com.fasterxml.jackson.databind.ObjectMapper;
66
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({PortalRestAPIProxy.class})
69 public class PortalRestAPIProxyTest {
70         @Mock
71         HttpServletRequest request;
72         @Mock
73         HttpServletResponse response;
74         @Mock
75         PrintWriter writer;
76         @Mock
77         PortalRestAPICentralServiceImpl portalRestAPICentralServiceImpl;
78         @Mock
79         private Method doPost;
80         private Method doGet;
81         private PortalRestAPIProxy portalRestAPIProxyObj;
82         private Class portalRestAPIProxyClass;
83         private Field portalRestApiServiceImplField;
84         private Field isAccessCentralizedField;
85         private Field mapperField;
86         @Mock
87         ObjectMapper mapper;
88         
89         @Before
90         public void setUp() throws Exception {
91                 portalRestAPIProxyClass = PortalRestAPIProxy.class;
92                 portalRestAPIProxyObj = (PortalRestAPIProxy) portalRestAPIProxyClass.newInstance();
93                 Mockito.when(response.getWriter()).thenReturn(writer);
94                 PowerMockito.doNothing().when(writer).print(Mockito.anyString());
95         ServletInputStream in = Mockito.mock(ServletInputStream.class);
96         Mockito.when(request.getInputStream()).thenReturn(in);
97         InputStreamReader ir = Mockito.mock(InputStreamReader.class);
98         PowerMockito.whenNew(InputStreamReader.class).withArguments(in).thenReturn(ir);
99         BufferedReader br = Mockito.mock(BufferedReader.class);
100         PowerMockito.whenNew(BufferedReader.class).withArguments(ir).thenReturn(br);
101         char[] charBuffer = new char[1024];
102         Mockito.when(br.read(charBuffer)).thenReturn(0);
103         PowerMockito.doNothing().when(writer).print(Mockito.anyString());
104         portalRestApiServiceImplField = portalRestAPIProxyClass.getDeclaredField("portalRestApiServiceImpl");
105         portalRestApiServiceImplField.setAccessible(true);
106         portalRestApiServiceImplField.set(portalRestAPIProxyClass, portalRestAPICentralServiceImpl);
107         isAccessCentralizedField = portalRestAPIProxyClass.getDeclaredField("isAccessCentralized");
108         isAccessCentralizedField.setAccessible(true);
109         mapperField = portalRestAPIProxyClass.getDeclaredField("mapper");
110         mapperField.setAccessible(true);
111         Field modifiersField = Field.class.getDeclaredField("modifiers");
112         modifiersField.setAccessible(true);
113         modifiersField.setInt(isAccessCentralizedField, isAccessCentralizedField.getModifiers() & ~Modifier.FINAL);
114         isAccessCentralizedField.set(null, "remote");
115         modifiersField.setInt(mapperField, mapperField.getModifiers() & ~Modifier.FINAL);
116         ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
117         mapperField.set(portalRestAPIProxyObj, mapper);
118         doPost = portalRestAPIProxyClass.getDeclaredMethod("doPost", new Class[]{HttpServletRequest.class, HttpServletResponse.class});
119         doPost.setAccessible(true);
120         doGet = portalRestAPIProxyClass.getDeclaredMethod("doGet", new Class[]{HttpServletRequest.class, HttpServletResponse.class});
121         doGet.setAccessible(true);
122         Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request)).thenReturn(true);
123         }
124         
125         @Test(expected=ServletException.class)
126         public void testInit() throws ServletException {
127                 portalRestAPIProxyObj.init();
128         }
129         
130         @Test
131         public void testDoPost_WhenProxyObjectIsNull() throws Exception {
132                 portalRestApiServiceImplField.set(portalRestAPIProxyClass, null);
133             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
134         }
135         
136         @Test
137         public void testDoPost_WhenRequestForStoreAnalytics() throws Exception {
138             Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenReturn("test");
139             Mockito.when(portalRestAPICentralServiceImpl.getCredentials()).thenReturn(new HashMap(){{put("username","test");put("password","test");}});
140         Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/storeAnalytics");
141             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
142         }
143         
144         @Test
145         public void testDoPost_WhenRequestForStoreAnalyticsAndUserIdIsNull() throws Exception {
146             Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenReturn(null);
147         Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/storeAnalytics");
148             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
149         }
150         
151         @Test(expected=ServletException.class)
152         public void testDoPost1_WhenExceptionOccurInGetUserID() throws Exception {
153             Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenThrow(new PortalAPIException("test"));
154         Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/storeAnalytics");
155             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
156         }
157         
158         @Test
159         public void testDoPost_WhenRequestForUpdateSessionTimeOuts() throws Exception {
160         Mockito.when(request.getRequestURI()).thenReturn("/updateSessionTimeOuts");
161             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
162         }
163         
164         @Test
165         public void testDoPost_WhenRequestForTimeoutSession() throws Exception {
166         Mockito.when(request.getRequestURI()).thenReturn("/timeoutSession");
167             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
168         }
169         
170         @Test
171         public void testDoPost_WhenRequestForUserAndEcompUserIsNull() throws Exception {
172         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user");
173             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
174         }
175         
176         @Test
177         public void testDoPost_WhenRequestForUser() throws Exception {
178                 EcompUser ecompUser = Mockito.mock(EcompUser.class);
179         Mockito.when(mapper.readValue(Mockito.anyString(), Mockito.eq(EcompUser.class))).thenReturn(ecompUser);
180         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user");
181             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
182         }
183         
184         @Test
185         public void testDoPost_WhenRequestForUserButNotRoles() throws Exception {
186         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/role");
187             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
188         }
189         
190         @Test
191         public void testDoPost_WhenRequestForUserRoles() throws Exception {
192         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/1/roles");
193             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
194         }
195         
196         @Test
197         public void testDoPost_WhenRequestForUserRolesWithRemote() throws Exception {
198                 isAccessCentralizedField.set(portalRestAPIProxyClass, "remote1");
199         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/1/roles");
200             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
201         }
202         
203
204         @Test
205         public void testDoPost_WhenIsAppAuthenticatedIsFalse() throws Exception {
206                 Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request)).thenReturn(false);
207                 Mockito.when(request.getRequestURI()).thenReturn("");
208             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
209         }
210         
211         @Test
212         public void testDoPost_WhenIsAppAuthenticatedThrowException() throws Exception {
213                 Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request)).thenThrow(new PortalAPIException());
214                 Mockito.when(request.getRequestURI()).thenReturn("");
215             doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
216         }
217         
218         @Test
219         public void testDoGet_WhenProxyObjectIsNull() throws Exception {
220                 portalRestApiServiceImplField.set(portalRestAPIProxyClass, null);
221             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
222         }
223         
224         @Test
225         public void testDoGet_WhenRequestForAnalytics() throws Exception {
226             Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenReturn("test");
227             Mockito.when(portalRestAPICentralServiceImpl.getCredentials()).thenReturn(new HashMap(){{put("username","test");put("password","test");}});
228         Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/analytics");
229             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
230         }
231         
232         @Test
233         public void testDoGet_WhenRequestForAnalyticsAndUserIdIsNull() throws Exception {
234             Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenReturn(null);
235         Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/analytics");
236             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
237         }
238         
239         @Test(expected=ServletException.class)
240         public void testDoGet1_WhenExceptionOccurInGetUserID() throws Exception {
241             Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenThrow(new PortalAPIException("test"));
242         Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/analytics");
243             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
244         }
245         
246         @Test
247         public void testDoGet_WhenRequestForSessionTimeOuts() throws Exception {
248         Mockito.when(request.getRequestURI()).thenReturn("/sessionTimeOuts");
249             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
250         }
251         
252         @Test
253         public void testDoGet_WhenRequestForUsers() throws Exception {
254         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/users");
255             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
256         }
257         
258         @Test
259         public void testDoGet_WhenRequestForRoles() throws Exception {
260                 EcompUser ecompUser = Mockito.mock(EcompUser.class);
261         Mockito.when(mapper.readValue(Mockito.anyString(), Mockito.eq(EcompUser.class))).thenReturn(ecompUser);
262         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/roles");
263             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
264         }
265         
266         @Test
267         public void testDoGet_WhenRequestForUserButNotRoles() throws Exception {
268         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/role");
269             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
270         }
271         
272         @Test
273         public void testDoGet_WhenRequestForUserRoles() throws Exception {
274         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/1/roles");
275             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
276         }
277         
278         @Test
279         public void testDoGet_WhenRequestForUserRolesWithRemote() throws Exception {
280                 isAccessCentralizedField.set(portalRestAPIProxyClass, "remote1");
281         Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/1/roles");
282             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
283         }
284         
285
286         @Test
287         public void testDoGet_WhenIsAppAuthenticatedIsFalse() throws Exception {
288                 Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request)).thenReturn(false);
289                 Mockito.when(request.getRequestURI()).thenReturn("");
290             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
291         }
292         
293         @Test
294         public void testDoGet_WhenIsAppAuthenticatedThrowException() throws Exception {
295                 Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request)).thenThrow(new PortalAPIException());
296                 Mockito.when(request.getRequestURI()).thenReturn("");
297             doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
298         }
299 }