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;
49 import javax.servlet.ServletException;
50 import javax.servlet.ServletInputStream;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
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;
66 import com.fasterxml.jackson.databind.ObjectMapper;
68 @RunWith(PowerMockRunner.class)
69 @PrepareForTest({PortalRestAPIProxy.class})
70 public class PortalRestAPIProxyTest {
72 HttpServletRequest request;
74 HttpServletResponse response;
78 PortalRestAPICentralServiceImpl portalRestAPICentralServiceImpl;
80 private Method doPost;
82 private PortalRestAPIProxy portalRestAPIProxyObj;
83 private Class portalRestAPIProxyClass;
84 private Field portalRestApiServiceImplField;
85 private Field isAccessCentralizedField;
86 private Field mapperField;
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);
127 @Test(expected=ServletException.class)
128 public void testInit() throws ServletException {
129 portalRestAPIProxyObj.init();
133 public void testDoPost_WhenProxyObjectIsNull() throws Exception {
134 portalRestApiServiceImplField.set(portalRestAPIProxyClass, null);
135 doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
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});
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});
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});
161 public void testDoPost_WhenRequestForUpdateSessionTimeOuts() throws Exception {
162 Mockito.when(request.getRequestURI()).thenReturn("/updateSessionTimeOuts");
163 doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
167 public void testDoPost_WhenRequestForTimeoutSession() throws Exception {
168 Mockito.when(request.getRequestURI()).thenReturn("/timeoutSession");
169 doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
173 public void testDoPost_WhenRequestForUserAndEcompUserIsNull() throws Exception {
174 Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user");
175 doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response});
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});
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});
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});
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});
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});
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});
223 public void testDoGet_WhenProxyObjectIsNull() throws Exception {
224 portalRestApiServiceImplField.set(portalRestAPIProxyClass, null);
225 doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
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});
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});
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});
251 public void testDoGet_WhenRequestForSessionTimeOuts() throws Exception {
252 Mockito.when(request.getRequestURI()).thenReturn("/sessionTimeOuts");
253 doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
257 public void testDoGet_WhenRequestForUsers() throws Exception {
258 Mockito.when(request.getRequestURI()).thenReturn("/api/v3/users");
259 doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response});
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});
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});
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});
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});
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});
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});