From a96a3e49cd472aa902c22143358b87562603d47c Mon Sep 17 00:00:00 2001 From: "Kishore Reddy, Gujja (kg811t)" Date: Mon, 9 Jul 2018 13:41:00 -0400 Subject: Adding User Auth and permission aaf services Issue-ID: PORTAL-334 Change-Id: I2826f2a06f7d818d918ae5f45b500a8da78cec42 Signed-off-by: Kishore Reddy, Gujja (kg811t) --- .../crossapi/PortalRestAPIProxyTest.java | 258 ++++++++++++++++++++- 1 file changed, 257 insertions(+), 1 deletion(-) (limited to 'ecomp-sdk/epsdk-fw/src/test/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxyTest.java') diff --git a/ecomp-sdk/epsdk-fw/src/test/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxyTest.java b/ecomp-sdk/epsdk-fw/src/test/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxyTest.java index 62234679..ce1035e7 100644 --- a/ecomp-sdk/epsdk-fw/src/test/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxyTest.java +++ b/ecomp-sdk/epsdk-fw/src/test/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxyTest.java @@ -37,7 +37,263 @@ */ package org.onap.portalsdk.core.onboarding.crossapi; -//@RunWith(PowerMockRunner.class) +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}); + } } -- cgit 1.2.3-korg