/*- * ============LICENSE_START========================================== * ONAP Portal * =================================================================== * Copyright (C) 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.portalapp.portal.controller; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; 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.InjectMocks; import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.onap.portalapp.annotation.ApiVersion; import org.onap.portalapp.controller.sessionmgt.SessionCommunicationVersionController; import org.onap.portalapp.externalsystemapproval.model.ExternalSystemUser; import org.onap.portalapp.portal.domain.EPUser; import org.onap.portalapp.portal.ecomp.model.PortalRestResponse; import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum; import org.onap.portalapp.portal.framework.MockitoTestSuite; import org.onap.portalapp.portal.transport.Analytics; import org.onap.portalapp.portal.transport.EpNotificationItem; import org.onap.portalapp.portal.transport.OnboardingApp; import org.onap.portalsdk.core.domain.Role; import org.onap.portalsdk.core.onboarding.crossapi.PortalAPIResponse; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.springframework.aop.support.AopUtils; import org.springframework.context.ApplicationContext; @RunWith(PowerMockRunner.class) @PrepareForTest(AopUtils.class) public class AuxApiRequestMapperControllerTest { @InjectMocks AuxApiRequestMapperController auxApiRequestMapperController = new AuxApiRequestMapperController(); @Before public void setup() { MockitoAnnotations.initMocks(this); } RolesController rolesController = new RolesController(); SessionCommunicationVersionController sessionCommunicationController = new SessionCommunicationVersionController(); WebAnalyticsExtAppVersionController webAnalyticsExtAppController = new WebAnalyticsExtAppVersionController(); RolesApprovalSystemVersionController rolesApprovalSystemController = new RolesApprovalSystemVersionController(); TicketEventVersionController ticketEventVersionController = new TicketEventVersionController(); AppsControllerExternalVersionRequest appsControllerExternalVersionRequest = new AppsControllerExternalVersionRequest(); ExternalAppsRestfulVersionController externalAppsRestfulVersionController = new ExternalAppsRestfulVersionController(); @Mock ApplicationContext context; Method method; MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); NullPointerException nullPointerException = new NullPointerException(); @Test public void getUserTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/roles"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("GET"); assertNull(auxApiRequestMapperController.getUser(mockedRequest, mockedResponse, "test12")); } @Test public void getUserXSSTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/roles"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("GET"); String expected = "Provided data is not valid"; String actual = auxApiRequestMapperController.getUser(mockedRequest, mockedResponse, "“>"); assertEquals(expected, actual); } @Test public void getUserTestWithException() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/roles"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("GET"); assertNull(auxApiRequestMapperController.getUser(mockedRequest, mockedResponse, "test12")); } @Test public void getRolesTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/roles"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("GET"); assertNull(auxApiRequestMapperController.getRoles(mockedRequest, mockedResponse)); } @Test public void saveRoleTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/role"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("POST"); Role role = new Role(); assertNull(auxApiRequestMapperController.saveRole(mockedRequest, mockedResponse, role)); } @Test public void getEPRolesTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v4/roles"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("GET"); assertNull(auxApiRequestMapperController.getRoles(mockedRequest, mockedResponse)); } @Test public void getUser1Test() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/user/test12"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("GET"); assertNull(auxApiRequestMapperController.getUser(mockedRequest, mockedResponse, "test12")); } @Test public void getRoleTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/role/1"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("GET"); assertNull(auxApiRequestMapperController.getRoleInfo(mockedRequest, mockedResponse, (long) 1)); } @Test public void getUsersOfApplicationTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/users"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("GET"); assertNull(auxApiRequestMapperController.getUsersOfApplication(mockedRequest, mockedResponse)); } @Test public void getRoleFunctionsListTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/functions"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("GET"); assertNull(auxApiRequestMapperController.getRoleFunctionsList(mockedRequest, mockedResponse)); } @Test public void getRoleFunctionTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/function/test"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("GET"); assertNull(auxApiRequestMapperController.getRoleFunction(mockedRequest, mockedResponse, "test")); } @Test public void saveRoleFunctionTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/roleFunction"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("POST"); PortalRestResponse response = auxApiRequestMapperController.saveRoleFunction(mockedRequest, mockedResponse, "test"); assertNotNull(response); } @Test public void saveRoleFunctionXSSTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/roleFunction"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("POST"); PortalRestResponse actual = auxApiRequestMapperController.saveRoleFunction(mockedRequest, mockedResponse, ""); PortalRestResponse expected = new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "Provided data is not valid", "Failed"); assertEquals(expected, actual); } @Test public void deleteRoleFunctionTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/roleFunction/test"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("DELETE"); assertNull(auxApiRequestMapperController.deleteRoleFunction(mockedRequest, mockedResponse, "test")); } @Test public void deleteRoleFunctionXSSTest() throws Exception { Mockito.when(mockedRequest.getRequestURI()).thenReturn("/auxapi/v3/roleFunction/test"); Mockito.when(mockedRequest.getHeader("MinorVersion")).thenReturn("0"); Map beans = new HashMap<>(); beans.put("bean1", rolesController); Mockito.when(context.getBeansWithAnnotation(ApiVersion.class)).thenReturn(beans); PowerMockito.mockStatic(AopUtils.class); Mockito.when(AopUtils.isAopProxy(Matchers.anyObject())).thenReturn(false); Mockito.when(mockedRequest.getMethod()).thenReturn("DELETE"); PortalRestResponse actual = auxApiRequestMapperController.deleteRoleFunction(mockedRequest, mockedResponse, "''