2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalsdk.core.onboarding.crossapi;
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;
48 import javax.servlet.ServletException;
49 import javax.servlet.ServletInputStream;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
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;
65 import com.fasterxml.jackson.databind.ObjectMapper;
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({PortalRestAPIProxy.class})
69 public class PortalRestAPIProxyTest {
71 HttpServletRequest request;
73 HttpServletResponse response;
77 PortalRestAPICentralServiceImpl portalRestAPICentralServiceImpl;
79 private Method doPost;
81 private PortalRestAPIProxy portalRestAPIProxyObj;
82 private Class portalRestAPIProxyClass;
83 private Field portalRestApiServiceImplField;
84 private Field isAccessCentralizedField;
85 private Field mapperField;
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);
125 @Test(expected=ServletException.class)
126 public void testInit() throws ServletException {
127 portalRestAPIProxyObj.init();
131 public void testDoPost_WhenProxyObjectIsNull() throws Exception {
132 portalRestApiServiceImplField.set(portalRestAPIProxyClass, null);
133 doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
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});
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});
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});
159 public void testDoPost_WhenRequestForUpdateSessionTimeOuts() throws Exception {
160 Mockito.when(request.getRequestURI()).thenReturn("/updateSessionTimeOuts");
161 doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
165 public void testDoPost_WhenRequestForTimeoutSession() throws Exception {
166 Mockito.when(request.getRequestURI()).thenReturn("/timeoutSession");
167 doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
171 public void testDoPost_WhenRequestForUserAndEcompUserIsNull() throws Exception {
172 Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user");
173 doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
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});
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});
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});
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});
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});
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});
219 public void testDoGet_WhenProxyObjectIsNull() throws Exception {
220 portalRestApiServiceImplField.set(portalRestAPIProxyClass, null);
221 doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
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});
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});
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});
247 public void testDoGet_WhenRequestForSessionTimeOuts() throws Exception {
248 Mockito.when(request.getRequestURI()).thenReturn("/sessionTimeOuts");
249 doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
253 public void testDoGet_WhenRequestForUsers() throws Exception {
254 Mockito.when(request.getRequestURI()).thenReturn("/api/v3/users");
255 doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
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});
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});
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});
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});
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});
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});