/* * ============LICENSE_START========================================== * ONAP Portal SDK * =================================================================== * Copyright © 2017 AT&T Intellectual Property. All rights reserved. * =================================================================== * * Unless otherwise specified, all software contained herein is licensed * under the Apache License, Version 2.0 (the "License"); * you may not use this software except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Unless otherwise specified, all documentation contained herein is licensed * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); * you may not use this documentation except in compliance with the License. * You may obtain a copy of the License at * * https://creativecommons.org/licenses/by/4.0/ * * Unless required by applicable law or agreed to in writing, documentation * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ============LICENSE_END============================================ * * */ package org.onap.portalsdk.core.onboarding.crossapi; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.onap.portalsdk.core.onboarding.exception.PortalAPIException; import org.onap.portalsdk.core.onboarding.util.PortalApiConstants; import org.onap.portalsdk.core.restful.domain.EcompUser; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import com.fasterxml.jackson.databind.ObjectMapper; @RunWith(PowerMockRunner.class) @PrepareForTest({PortalRestAPIProxy.class}) public class PortalRestAPIProxyTest { @Mock HttpServletRequest request; @Mock HttpServletResponse response; @Mock PrintWriter writer; @Mock PortalRestAPICentralServiceImpl portalRestAPICentralServiceImpl; @Mock private Method doPost; private Method doGet; private PortalRestAPIProxy portalRestAPIProxyObj; private Class portalRestAPIProxyClass; private Field portalRestApiServiceImplField; private Field isAccessCentralizedField; private Field mapperField; @Mock ObjectMapper mapper; @Before public void setUp() throws Exception { portalRestAPIProxyClass = PortalRestAPIProxy.class; portalRestAPIProxyObj = (PortalRestAPIProxy) portalRestAPIProxyClass.newInstance(); Mockito.when(response.getWriter()).thenReturn(writer); PowerMockito.doNothing().when(writer).print(Mockito.anyString()); ServletInputStream in = Mockito.mock(ServletInputStream.class); Mockito.when(request.getInputStream()).thenReturn(in); InputStreamReader ir = Mockito.mock(InputStreamReader.class); PowerMockito.whenNew(InputStreamReader.class).withArguments(in).thenReturn(ir); BufferedReader br = Mockito.mock(BufferedReader.class); PowerMockito.whenNew(BufferedReader.class).withArguments(ir).thenReturn(br); char[] charBuffer = new char[1024]; Mockito.when(br.read(charBuffer)).thenReturn(0); PowerMockito.doNothing().when(writer).print(Mockito.anyString()); portalRestApiServiceImplField = portalRestAPIProxyClass.getDeclaredField("portalRestApiServiceImpl"); portalRestApiServiceImplField.setAccessible(true); portalRestApiServiceImplField.set(portalRestAPIProxyClass, portalRestAPICentralServiceImpl); isAccessCentralizedField = portalRestAPIProxyClass.getDeclaredField("isAccessCentralized"); isAccessCentralizedField.setAccessible(true); mapperField = portalRestAPIProxyClass.getDeclaredField("mapper"); mapperField.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(isAccessCentralizedField, isAccessCentralizedField.getModifiers() & ~Modifier.FINAL); isAccessCentralizedField.set(null, "remote"); modifiersField.setInt(mapperField, mapperField.getModifiers() & ~Modifier.FINAL); ObjectMapper mapper = Mockito.mock(ObjectMapper.class); mapperField.set(portalRestAPIProxyObj, mapper); doPost = portalRestAPIProxyClass.getDeclaredMethod("doPost", new Class[]{HttpServletRequest.class, HttpServletResponse.class}); doPost.setAccessible(true); doGet = portalRestAPIProxyClass.getDeclaredMethod("doGet", new Class[]{HttpServletRequest.class, HttpServletResponse.class}); doGet.setAccessible(true); Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request)).thenReturn(true); } @Test(expected=ServletException.class) public void testInit() throws ServletException { portalRestAPIProxyObj.init(); } @Test public void testDoPost_WhenProxyObjectIsNull() throws Exception { portalRestApiServiceImplField.set(portalRestAPIProxyClass, null); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoPost_WhenRequestForStoreAnalytics() throws Exception { Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenReturn("test"); Mockito.when(portalRestAPICentralServiceImpl.getCredentials()).thenReturn(new HashMap(){{put("username","test");put("password","test");}}); Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/storeAnalytics"); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoPost_WhenRequestForStoreAnalyticsAndUserIdIsNull() throws Exception { Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenReturn(null); Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/storeAnalytics"); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test(expected=ServletException.class) public void testDoPost1_WhenExceptionOccurInGetUserID() throws Exception { Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenThrow(new PortalAPIException("test")); Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/storeAnalytics"); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoPost_WhenRequestForUpdateSessionTimeOuts() throws Exception { Mockito.when(request.getRequestURI()).thenReturn("/updateSessionTimeOuts"); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoPost_WhenRequestForTimeoutSession() throws Exception { Mockito.when(request.getRequestURI()).thenReturn("/timeoutSession"); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoPost_WhenRequestForUserAndEcompUserIsNull() throws Exception { Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user"); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoPost_WhenRequestForUser() throws Exception { EcompUser ecompUser = Mockito.mock(EcompUser.class); Mockito.when(mapper.readValue(Mockito.anyString(), Mockito.eq(EcompUser.class))).thenReturn(ecompUser); Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user"); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoPost_WhenRequestForUserButNotRoles() throws Exception { Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/role"); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoPost_WhenRequestForUserRoles() throws Exception { Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/1/roles"); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoPost_WhenRequestForUserRolesWithRemote() throws Exception { isAccessCentralizedField.set(portalRestAPIProxyClass, "remote1"); Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/1/roles"); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoPost_WhenIsAppAuthenticatedIsFalse() throws Exception { Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request)).thenReturn(false); Mockito.when(request.getRequestURI()).thenReturn(""); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoPost_WhenIsAppAuthenticatedThrowException() throws Exception { Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request)).thenThrow(new PortalAPIException()); Mockito.when(request.getRequestURI()).thenReturn(""); doPost.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoGet_WhenProxyObjectIsNull() throws Exception { portalRestApiServiceImplField.set(portalRestAPIProxyClass, null); doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoGet_WhenRequestForAnalytics() throws Exception { Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenReturn("test"); Mockito.when(portalRestAPICentralServiceImpl.getCredentials()).thenReturn(new HashMap(){{put("username","test");put("password","test");}}); Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/analytics"); doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoGet_WhenRequestForAnalyticsAndUserIdIsNull() throws Exception { Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenReturn(null); Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/analytics"); doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test(expected=ServletException.class) public void testDoGet1_WhenExceptionOccurInGetUserID() throws Exception { Mockito.when(portalRestAPICentralServiceImpl.getUserId(request)).thenThrow(new PortalAPIException("test")); Mockito.when(request.getRequestURI()).thenReturn(PortalApiConstants.API_PREFIX+"/analytics"); doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoGet_WhenRequestForSessionTimeOuts() throws Exception { Mockito.when(request.getRequestURI()).thenReturn("/sessionTimeOuts"); doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoGet_WhenRequestForUsers() throws Exception { Mockito.when(request.getRequestURI()).thenReturn("/api/v3/users"); doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoGet_WhenRequestForRoles() throws Exception { EcompUser ecompUser = Mockito.mock(EcompUser.class); Mockito.when(mapper.readValue(Mockito.anyString(), Mockito.eq(EcompUser.class))).thenReturn(ecompUser); Mockito.when(request.getRequestURI()).thenReturn("/api/v3/roles"); doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoGet_WhenRequestForUserButNotRoles() throws Exception { Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/role"); doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoGet_WhenRequestForUserRoles() throws Exception { Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/1/roles"); doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoGet_WhenRequestForUserRolesWithRemote() throws Exception { isAccessCentralizedField.set(portalRestAPIProxyClass, "remote1"); Mockito.when(request.getRequestURI()).thenReturn("/api/v3/user/1/roles"); doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoGet_WhenIsAppAuthenticatedIsFalse() throws Exception { Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request)).thenReturn(false); Mockito.when(request.getRequestURI()).thenReturn(""); doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } @Test public void testDoGet_WhenIsAppAuthenticatedThrowException() throws Exception { Mockito.when(portalRestAPICentralServiceImpl.isAppAuthenticated(request)).thenThrow(new PortalAPIException()); Mockito.when(request.getRequestURI()).thenReturn(""); doGet.invoke(portalRestAPIProxyObj, new Object[] {request, response}); } }