diff options
author | 2018-01-30 17:29:36 -0500 | |
---|---|---|
committer | 2018-02-01 15:10:02 -0500 | |
commit | 21a8761f684745bb300e075c7e98ad897ace9eed (patch) | |
tree | 6d585c3fe39fbb42a314941dbc8646e6ccf188cf /ecomp-portal-BE-os/src/test/java/org/onap/portalapp | |
parent | 3af8af1310d5a27cb58be29505573f0bbdc1717c (diff) |
Security/ Package Name changes
Issue-ID: PORTAL-174, PORTAL-157, PORTAL-156, PORTAL-148, PORTAL-145,
PORTAL-140, PORTAL-133, PORTAL-121, PORTAL-111, PORTAL-88
Includes security fixes, Role Centralization, replace certain ECOMP
occurrences etc
Change-Id: I3c8b706709c6b92e646e3cbe50c2d660e8a46ef4
Signed-off-by: st782s <statta@research.att.com>
Diffstat (limited to 'ecomp-portal-BE-os/src/test/java/org/onap/portalapp')
18 files changed, 2689 insertions, 0 deletions
diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/authentication/OpenIdConnectLoginStrategyTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/authentication/OpenIdConnectLoginStrategyTest.java new file mode 100644 index 00000000..58e596ae --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/authentication/OpenIdConnectLoginStrategyTest.java @@ -0,0 +1,155 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.authentication; + +import static org.junit.Assert.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.onap.portalapp.authentication.OpenIdConnectLoginStrategy; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.utils.EPSystemProperties; +import org.onap.portalapp.util.EPUserUtils; +import org.onap.portalapp.util.SessionCookieUtil; +import org.onap.portalsdk.core.onboarding.exception.PortalAPIException; +import org.onap.portalsdk.core.util.SystemProperties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mitre.openid.connect.model.DefaultUserInfo; +import org.mitre.openid.connect.model.UserInfo; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.util.StringUtils; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({StringUtils.class, EPUserUtils.class , SessionCookieUtil.class,SystemProperties.class}) +public class OpenIdConnectLoginStrategyTest { + + @InjectMocks + OpenIdConnectLoginStrategy OpenIdConnectLoginStrategy = new OpenIdConnectLoginStrategy(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + NullPointerException nullPointerException = new NullPointerException(); + + @Test + public void loginTest() throws Exception + { + PowerMockito.mockStatic(StringUtils.class); + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(SessionCookieUtil.class); + + UserInfo userInfo = new DefaultUserInfo(); + userInfo.setPreferredUsername("Test"); + userInfo.setEmail("test@gmail.com"); + userInfo.setName("first_name"); + userInfo.setFamilyName("last_name"); + Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo); + assertTrue(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse)); + } + + @Test + public void loginIfUserNullTest() throws Exception + { + PowerMockito.mockStatic(SystemProperties.class); + UserInfo userInfo = null; + Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo); +// Mockito.when(SystemProperties.getProperty("authentication_mechanism")).thenReturn("auth"); + assertFalse(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse)); + } + + @Test + public void loginIfUserIfAuthIsOIDCTest() throws Exception + { + PowerMockito.mockStatic(SystemProperties.class); + UserInfo userInfo = null; + Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo); + Mockito.when(SystemProperties.getProperty("authentication_mechanism")).thenReturn("OIDC"); + Mockito.when(SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL)).thenReturn("login_url"); + assertFalse(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse)); + } + + @Test + public void loginIfUserIfAuthNotNullTest() throws Exception + { + PowerMockito.mockStatic(SystemProperties.class); + UserInfo userInfo = null; + Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo); + Mockito.when(SystemProperties.getProperty("authentication_mechanism")).thenReturn("test"); + Mockito.when(SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL)).thenReturn("login_url"); + assertFalse(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse)); + } + + @Test + public void loginIfUserExceptionest() throws Exception + { + PowerMockito.mockStatic(SystemProperties.class); + UserInfo userInfo = null; + Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo); + Mockito.when(SystemProperties.getProperty("authentication_mechanism")).thenThrow(nullPointerException); + Mockito.when(SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL)).thenReturn("login_url"); + assertFalse(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse)); + } + @Test(expected = Exception.class) + public void doLoginTest() throws Exception + { + OpenIdConnectLoginStrategy.doLogin(mockedRequest, mockedResponse); + } + + @Test(expected = PortalAPIException.class) + public void getUserIdTest() throws Exception + { + OpenIdConnectLoginStrategy.getUserId(mockedRequest); + } + +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/authentication/SessionTimeoutInterceptorTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/authentication/SessionTimeoutInterceptorTest.java new file mode 100644 index 00000000..11b54bf7 --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/authentication/SessionTimeoutInterceptorTest.java @@ -0,0 +1,92 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.authentication; + +import static org.junit.Assert.assertFalse; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.authentication.LoginStrategy; +import org.onap.portalapp.authentication.SimpleLoginStrategy; +import org.onap.portalapp.controller.EPFusionBaseController; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.interceptor.SessionTimeoutInterceptor; +import org.springframework.web.method.HandlerMethod; + +public class SessionTimeoutInterceptorTest { + + + @Mock + LoginStrategy loginStrategy = new SimpleLoginStrategy(); + + @Mock + EPFusionBaseController ePFusionBaseController = new EPFusionBaseController() { + }; + + @Mock + HandlerMethod handlerMethod; + + @InjectMocks + SessionTimeoutInterceptor sessionTimeoutInterceptor = new SessionTimeoutInterceptor(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + @Test + public void preHandleTest() throws Exception{ + assertFalse(sessionTimeoutInterceptor.preHandle(mockedRequest, mockedResponse, ePFusionBaseController)); + } + + @Test + public void preHandleTestIfMethodIsinstanceOfHandlerMethod() throws Exception{ + assertFalse(sessionTimeoutInterceptor.preHandle(mockedRequest, mockedResponse, handlerMethod)); + } +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/authentication/SimpleLoginStrategyTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/authentication/SimpleLoginStrategyTest.java new file mode 100644 index 00000000..1a3972f6 --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/authentication/SimpleLoginStrategyTest.java @@ -0,0 +1,171 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.authentication; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +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.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.authentication.SimpleLoginStrategy; +import org.onap.portalapp.command.EPLoginBean; +import org.onap.portalapp.portal.domain.EPUser; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.service.EPLoginService; +import org.onap.portalapp.portal.service.EPRoleFunctionService; +import org.onap.portalapp.portal.service.EPRoleService; +import org.onap.portalapp.util.EPUserUtils; +import org.onap.portalapp.util.SessionCookieUtil; +import org.onap.portalsdk.core.menu.MenuProperties; +import org.onap.portalsdk.core.onboarding.exception.PortalAPIException; +import org.onap.portalsdk.core.util.SystemProperties; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.util.StringUtils; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ StringUtils.class, EPUserUtils.class, SessionCookieUtil.class, SystemProperties.class, + SessionCookieUtil.class, MenuProperties.class }) +public class SimpleLoginStrategyTest { + + @InjectMocks + SimpleLoginStrategy simpleLoginStrategy = new SimpleLoginStrategy(); + + @Mock + EPLoginService loginService; + @Mock + EPRoleService roleService; + @Mock + EPRoleFunctionService ePRoleFunctionService; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + NullPointerException nullPointerException = new NullPointerException(); + + @Test(expected = Exception.class) + public void loginTest() throws Exception { + PowerMockito.mockStatic(MenuProperties.class); + PowerMockito.mockStatic(SessionCookieUtil.class); + PowerMockito.mockStatic(StringUtils.class); + Mockito.when(SessionCookieUtil.getUserIdFromCookie(mockedRequest, mockedResponse)).thenReturn("guestT"); + Mockito.when(StringUtils.isEmpty("guestT")).thenReturn(false); + EPLoginBean commandBean = new EPLoginBean(); + EPUser user = new EPUser(); + commandBean.setUser(user); + commandBean.setOrgUserId("guestT"); + Mockito.when(mockedRequest.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY)) + .thenReturn("menu_properties_filename"); + Mockito.when(loginService.findUser(commandBean, "menu_properties_filename", null)).thenReturn(commandBean); + assertTrue(simpleLoginStrategy.login(mockedRequest, mockedResponse)); + } + + @Test + public void loginIfUserEmptyTest() throws Exception { + PowerMockito.mockStatic(MenuProperties.class); + PowerMockito.mockStatic(SessionCookieUtil.class); + PowerMockito.mockStatic(StringUtils.class); + Mockito.when(SessionCookieUtil.getUserIdFromCookie(mockedRequest, mockedResponse)).thenReturn("guestT"); + Mockito.when(StringUtils.isEmpty("guestT")).thenReturn(true); + EPLoginBean commandBean = new EPLoginBean(); + EPUser user = new EPUser(); + commandBean.setUser(user); + commandBean.setOrgUserId("guestT"); + assertFalse(simpleLoginStrategy.login(mockedRequest, mockedResponse)); + } + + @Test + public void loginIfAuthIsBothTest() throws Exception { + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(SessionCookieUtil.class); + PowerMockito.mockStatic(StringUtils.class); + Mockito.when(SessionCookieUtil.getUserIdFromCookie(mockedRequest, mockedResponse)).thenReturn("guestT"); + Mockito.when(StringUtils.isEmpty("guestT")).thenReturn(true); + Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("BOTH"); + assertFalse(simpleLoginStrategy.login(mockedRequest, mockedResponse)); + } + + @Test + public void loginIfAuthIsNotNullTest() throws Exception { + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(SessionCookieUtil.class); + PowerMockito.mockStatic(StringUtils.class); + Mockito.when(SessionCookieUtil.getUserIdFromCookie(mockedRequest, mockedResponse)).thenReturn("guestT"); + Mockito.when(StringUtils.isEmpty("guestT")).thenReturn(true); + Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("Test"); + assertFalse(simpleLoginStrategy.login(mockedRequest, mockedResponse)); + } + + @Test + public void loginExceptionTest() throws Exception { + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(SessionCookieUtil.class); + PowerMockito.mockStatic(StringUtils.class); + Mockito.when(SessionCookieUtil.getUserIdFromCookie(mockedRequest, mockedResponse)).thenReturn("guestT"); + Mockito.when(StringUtils.isEmpty("guestT")).thenReturn(true); + Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)) + .thenThrow(nullPointerException); + assertFalse(simpleLoginStrategy.login(mockedRequest, mockedResponse)); + } + + @Test(expected = Exception.class) + public void doLoginTest() throws Exception { + simpleLoginStrategy.doLogin(mockedRequest, mockedResponse); + } + + @Test(expected = PortalAPIException.class) + public void getUserIdTest() throws Exception { + simpleLoginStrategy.getUserId(mockedRequest); + } +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/AppsOSControllerTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/AppsOSControllerTest.java new file mode 100644 index 00000000..565bfdbf --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/AppsOSControllerTest.java @@ -0,0 +1,189 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.controller; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.portal.controller.AppsOSController; +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.MockEPUser; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.service.AdminRolesService; +import org.onap.portalapp.portal.service.EPAppService; +import org.onap.portalapp.portal.service.PersUserAppService; +import org.onap.portalapp.portal.service.UserService; +import org.onap.portalapp.util.EPUserUtils; + +public class AppsOSControllerTest { + + @Mock + AdminRolesService adminRolesService; + + @Mock + EPAppService appService; + + @Mock + PersUserAppService persUserAppService; + + @Mock + UserService userService; + + @Mock + EPUserUtils ePUserUtils; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @InjectMocks + AppsOSController appsOSController = new AppsOSController(); + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + NullPointerException nullPointerException = new NullPointerException(); + MockEPUser mockUser = new MockEPUser(); + + @Test + public void saveNewUserIfUserISNullTest() { + PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>(); + expectedportalRestResponse.setMessage("failure"); + expectedportalRestResponse.setResponse("New User cannot be null or empty"); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR); + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + PortalRestResponse<String> actualPortalRestResponse = appsOSController.saveNewUser(mockedRequest, null); + assertEquals(expectedportalRestResponse, actualPortalRestResponse); + } + + @Test + @Ignore + public void saveNewUserIfUserNOtNullTest() { + PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>(); + expectedportalRestResponse.setMessage("failure"); + expectedportalRestResponse.setResponse("UnAuthorized"); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR); + EPUser user = mockUser.mockEPUser(); + EPUser user1 = mockUser.mockEPUser(); + user1.setLoginId("guest"); + user.setLoginId("guestT"); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user1); + Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false); + Mockito.when(adminRolesService.isAccountAdmin(user)).thenReturn(false); + PortalRestResponse<String> actualPortalRestResponse = appsOSController.saveNewUser(mockedRequest, user); + assertEquals(expectedportalRestResponse, actualPortalRestResponse); + } + + @Test + public void saveNewUserAndLoggedInUserIdSameTest() throws Exception { + PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>(); + expectedportalRestResponse.setMessage("Success"); + expectedportalRestResponse.setResponse(""); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.OK); + EPUser user = mockUser.mockEPUser(); + user.setLoginId("guestT"); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false); + Mockito.when(adminRolesService.isAccountAdmin(user)).thenReturn(false); + Mockito.when(mockedRequest.getParameter("isCheck")).thenReturn("test"); + Mockito.when(userService.saveNewUser(user, "test")).thenReturn("Success"); + PortalRestResponse<String> actualPortalRestResponse = appsOSController.saveNewUser(mockedRequest, user); + assertEquals(expectedportalRestResponse, actualPortalRestResponse); + } + + @Test + public void saveNewUserexceptionest() throws Exception { + PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>(); + expectedportalRestResponse.setMessage("failure"); + expectedportalRestResponse.setResponse(null); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR); + EPUser user = mockUser.mockEPUser(); + user.setLoginId("guestT"); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false); + Mockito.when(adminRolesService.isAccountAdmin(user)).thenReturn(false); + Mockito.when(mockedRequest.getParameter("isCheck")).thenReturn("test"); + Mockito.when(userService.saveNewUser(user, "test")).thenThrow(nullPointerException); + PortalRestResponse<String> actualPortalRestResponse = appsOSController.saveNewUser(mockedRequest, user); + assertEquals(expectedportalRestResponse, actualPortalRestResponse); + } + + @Test + public void getCurrentUserProfileTest() { + String loginId = "guestT"; + EPUser user = mockUser.mockEPUser(); + List<EPUser> expectedList = new ArrayList<EPUser>(); + expectedList.add(user); + Mockito.when(userService.getUserByUserId(loginId)).thenReturn(expectedList); + String expectedString = appsOSController.getCurrentUserProfile(mockedRequest, loginId); + assertEquals("{\"firstName\":\"test\",\"lastName\":\"test\"}", expectedString); + } + + @Test + public void getCurrentUserProfileExceptionTest() { + String loginId = "guestT"; + EPUser user = mockUser.mockEPUser(); + List<EPUser> expectedList = new ArrayList<EPUser>(); + expectedList.add(user); + Mockito.when(userService.getUserByUserId(loginId)).thenThrow(nullPointerException); + String expectedString = appsOSController.getCurrentUserProfile(mockedRequest, loginId); + assertEquals("{}", expectedString); + } + +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/DashboardSearchResultControllerTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/DashboardSearchResultControllerTest.java new file mode 100644 index 00000000..d2a0c4ea --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/DashboardSearchResultControllerTest.java @@ -0,0 +1,360 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.controller; + +import static org.junit.Assert.assertEquals; + +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.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.portal.controller.DashboardSearchResultController; +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.ecomp.model.SearchResultItem; +import org.onap.portalapp.portal.framework.MockEPUser; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.service.DashboardSearchService; +import org.onap.portalapp.portal.transport.CommonWidget; +import org.onap.portalapp.portal.transport.CommonWidgetMeta; +import org.onap.portalapp.util.EPUserUtils; + +public class DashboardSearchResultControllerTest { + + @Mock + DashboardSearchService searchService; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @InjectMocks + DashboardSearchResultController dashboardSearchResultController = new DashboardSearchResultController(); + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + NullPointerException nullPointerException = new NullPointerException(); + MockEPUser mockUser = new MockEPUser(); + + @Test + public void getWidgetDataTest() { + PortalRestResponse<CommonWidgetMeta> ecpectedPortalRestResponse = new PortalRestResponse<CommonWidgetMeta>(); + ecpectedPortalRestResponse.setMessage("success"); + ecpectedPortalRestResponse.setResponse(new CommonWidgetMeta()); + ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.OK); + CommonWidgetMeta commonWidgetMeta = new CommonWidgetMeta(); + Mockito.when(searchService.getWidgetData("test")).thenReturn(commonWidgetMeta); + PortalRestResponse<CommonWidgetMeta> actualPortalRestResponse = dashboardSearchResultController + .getWidgetData(mockedRequest, "test"); + assertEquals(ecpectedPortalRestResponse.getStatus(), actualPortalRestResponse.getStatus()); + } + + @Test + public void saveWidgetDataBulkIfCatrgoryNullTest() { + PortalRestResponse<String> ecpectedPortalRestResponse = new PortalRestResponse<String>(); + ecpectedPortalRestResponse.setMessage("ERROR"); + ecpectedPortalRestResponse.setResponse("Category cannot be null or empty"); + ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR); + CommonWidgetMeta commonWidgetMeta = new CommonWidgetMeta(); + commonWidgetMeta.setCategory(null); + PortalRestResponse<String> actualPortalRestResponse = dashboardSearchResultController + .saveWidgetDataBulk(commonWidgetMeta); + assertEquals(ecpectedPortalRestResponse, actualPortalRestResponse); + } + + @Test + public void saveWidgetDataBulkTest() { + PortalRestResponse<String> ecpectedPortalRestResponse = new PortalRestResponse<String>(); + ecpectedPortalRestResponse.setMessage("success"); + ecpectedPortalRestResponse.setResponse(null); + ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.OK); + CommonWidgetMeta commonWidgetMeta = new CommonWidgetMeta(); + commonWidgetMeta.setCategory("test"); + List<CommonWidget> items = new ArrayList<>(); + + CommonWidget commonWidget = new CommonWidget(); + commonWidget.setId((long) 1); + commonWidget.setEventDate("2017-06-06"); + items.add(commonWidget); + commonWidgetMeta.setItems(items); + PortalRestResponse<String> actualPortalRestResponse = dashboardSearchResultController + .saveWidgetDataBulk(commonWidgetMeta); + assertEquals(ecpectedPortalRestResponse, actualPortalRestResponse); + } + + @Test + public void saveWidgetDataBulkExceptionTest() { + PortalRestResponse<String> ecpectedPortalRestResponse = new PortalRestResponse<String>(); + ecpectedPortalRestResponse.setMessage("java.text.ParseException: Unparseable date: \"date\""); + ecpectedPortalRestResponse.setResponse(null); + ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR); + CommonWidgetMeta commonWidgetMeta = new CommonWidgetMeta(); + commonWidgetMeta.setCategory("test"); + List<CommonWidget> items = new ArrayList<>(); + + CommonWidget commonWidget = new CommonWidget(); + commonWidget.setId((long) 1); + commonWidget.setEventDate("date"); + items.add(commonWidget); + commonWidgetMeta.setItems(items); + PortalRestResponse<String> actualPortalRestResponse = dashboardSearchResultController + .saveWidgetDataBulk(commonWidgetMeta); + assertEquals(ecpectedPortalRestResponse, actualPortalRestResponse); + } + + @Test + public void saveWidgetDataIfCatagoryNullTest() { + PortalRestResponse<String> ecpectedPortalRestResponse = new PortalRestResponse<String>(); + ecpectedPortalRestResponse.setMessage("ERROR"); + ecpectedPortalRestResponse.setResponse("Cateogry cannot be null or empty"); + ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR); + CommonWidget commonWidget = new CommonWidget(); + commonWidget.setCategory(null); + PortalRestResponse<String> actualPortalRestResponse = dashboardSearchResultController + .saveWidgetData(commonWidget); + assertEquals(ecpectedPortalRestResponse, actualPortalRestResponse); + } + + @Test + public void saveWidgetDataTest() { + PortalRestResponse<String> ecpectedPortalRestResponse = new PortalRestResponse<String>(); + ecpectedPortalRestResponse.setMessage("success"); + ecpectedPortalRestResponse.setResponse(null); + ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.OK); + CommonWidget commonWidget = new CommonWidget(); + commonWidget.setCategory("test"); + commonWidget.setId((long) 1); + commonWidget.setEventDate("2017-06-06"); + PortalRestResponse<String> actualPortalRestResponse = dashboardSearchResultController + .saveWidgetData(commonWidget); + assertEquals(ecpectedPortalRestResponse, actualPortalRestResponse); + } + + @Test + public void saveWidgetDataExceptionTest() { + PortalRestResponse<String> ecpectedPortalRestResponse = new PortalRestResponse<String>(); + ecpectedPortalRestResponse.setMessage("java.text.ParseException: Unparseable date: \"date\""); + ecpectedPortalRestResponse.setResponse(null); + ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR); + CommonWidget commonWidget = new CommonWidget(); + commonWidget.setCategory("test"); + commonWidget.setId((long) 1); + commonWidget.setEventDate("date"); + PortalRestResponse<String> actualPortalRestResponse = dashboardSearchResultController + .saveWidgetData(commonWidget); + assertEquals(ecpectedPortalRestResponse, actualPortalRestResponse); + } + + @Test + public void deleteWidgetDataTest() + { + PortalRestResponse<String> ecpectedPortalRestResponse = new PortalRestResponse<String>(); + ecpectedPortalRestResponse.setMessage("success"); + ecpectedPortalRestResponse.setResponse(null); + ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.OK); + CommonWidget commonWidget = new CommonWidget(); + Mockito.when(searchService.deleteWidgetData(commonWidget)).thenReturn("test"); + PortalRestResponse<String> actualPortalRestResponse = dashboardSearchResultController + .deleteWidgetData(commonWidget); + assertEquals(ecpectedPortalRestResponse.getStatus(), actualPortalRestResponse.getStatus()); + } + + @Test + public void searchPortalIfUserIsTest() + { + EPUser user = null; + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + String searchString = "test"; + PortalRestResponse<Map<String, List<SearchResultItem>>> expectedResult = new PortalRestResponse<Map<String, List<SearchResultItem>>>(); + expectedResult.setMessage("searchPortal: User object is null? - check logs"); + expectedResult.setResponse(new HashMap<String, List<SearchResultItem>>()); + expectedResult.setStatus(PortalRestStatusEnum.ERROR); + PortalRestResponse<Map<String, List<SearchResultItem>>> actualResult = dashboardSearchResultController + .searchPortal(mockedRequest, searchString); + System.out.println(actualResult); + + assertEquals(expectedResult, actualResult); + } + + + @Test + public void getActiveUsersTest() { + List<String> expectedActiveUsers = new ArrayList<String>(); + EPUser user = mockUser.mockEPUser(); + ; + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + String userId = user.getOrgUserId(); + Mockito.when(searchService.getRelatedUsers(userId)).thenReturn(expectedActiveUsers); + List<String> actualOnlineUsers = dashboardSearchResultController.getActiveUsers(mockedRequest); + assertEquals(expectedActiveUsers, actualOnlineUsers); + + } + + @Test + public void getActiveUsersExceptionTest() { + List<String> expectedActiveUsers = new ArrayList<String>(); + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + String userId = user.getOrgUserId(); + Mockito.when(searchService.getRelatedUsers(userId)).thenThrow(nullPointerException); + List<String> actualOnlineUsers = dashboardSearchResultController.getActiveUsers(mockedRequest); + assertEquals(expectedActiveUsers, actualOnlineUsers); + + } + + @Test + public void activeUsersTest() { + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + PortalRestResponse<List<String>> expectedResult = new PortalRestResponse<List<String>>(); + expectedResult.setMessage("success"); + expectedResult.setResponse(new ArrayList<>()); + expectedResult.setStatus(PortalRestStatusEnum.OK); + PortalRestResponse<List<String>> actualResult = dashboardSearchResultController.activeUsers(mockedRequest); + + assertEquals(actualResult, expectedResult); + + } + + @Test + public void activeUsersIfUserNullTest() { + EPUser user = null; + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + PortalRestResponse<List<String>> expectedResult = new PortalRestResponse<List<String>>(); + expectedResult.setMessage("User object is null? - check logs"); + expectedResult.setResponse(new ArrayList<>()); + expectedResult.setStatus(PortalRestStatusEnum.ERROR); + PortalRestResponse<List<String>> actualResult = dashboardSearchResultController.activeUsers(mockedRequest); + assertEquals(actualResult, expectedResult); + + } + + @Test + public void activeUsersExceptionTest() { + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + PortalRestResponse<List<String>> expectedResult = new PortalRestResponse<List<String>>(); + expectedResult.setMessage("null - check logs."); + expectedResult.setResponse(new ArrayList<>()); + expectedResult.setStatus(PortalRestStatusEnum.ERROR); + Mockito.when(searchService.getRelatedUsers(user.getLoginId())).thenThrow(nullPointerException); + PortalRestResponse<List<String>> actualResult = dashboardSearchResultController.activeUsers(mockedRequest); + assertEquals(actualResult, expectedResult); + + } + + @Test + public void searchPortalIfSearchStringNullTest() { + EPUser user = mockUser.mockEPUser(); + ; + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + String searchString = null; + + PortalRestResponse<Map<String, List<SearchResultItem>>> expectedResult = new PortalRestResponse<Map<String, List<SearchResultItem>>>(); + expectedResult.setMessage("searchPortal: String string is null"); + expectedResult.setResponse(new HashMap<String, List<SearchResultItem>>()); + expectedResult.setStatus(PortalRestStatusEnum.ERROR); + + PortalRestResponse<Map<String, List<SearchResultItem>>> actualResult = dashboardSearchResultController + .searchPortal(mockedRequest, searchString); + assertEquals(expectedResult, actualResult); + } + + @Test + public void searchPortalIfSearchTest() { + EPUser user = mockUser.mockEPUser(); + ; + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + String searchString = "test"; + List<SearchResultItem> searchResultItemList = new ArrayList<SearchResultItem>(); + SearchResultItem searchResultItem = new SearchResultItem(); + + searchResultItem.setId((long) 1); + searchResultItem.setCategory("test"); + searchResultItem.setName("test_name"); + searchResultItem.setTarget("test_target"); + searchResultItem.setUuid("test_UUId"); + searchResultItemList.add(searchResultItem); + Map<String, List<SearchResultItem>> expectedResultMap = new HashMap<String, List<SearchResultItem>>(); + expectedResultMap.put(searchString, searchResultItemList); + + PortalRestResponse<Map<String, List<SearchResultItem>>> expectedResult = new PortalRestResponse<Map<String, List<SearchResultItem>>>(); + expectedResult.setMessage("success"); + expectedResult.setResponse(expectedResultMap); + expectedResult.setStatus(PortalRestStatusEnum.OK); + + Mockito.when(searchService.searchResults(user.getLoginId(), searchString)).thenReturn(expectedResultMap); + PortalRestResponse<Map<String, List<SearchResultItem>>> actualResult = dashboardSearchResultController + .searchPortal(mockedRequest, searchString); + assertEquals(expectedResult, actualResult); + + } + + @Test + public void searchPortalIfSearchExcptionTest() { + EPUser user = mockUser.mockEPUser(); + ; + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + String searchString = "test"; + + PortalRestResponse<Map<String, List<SearchResultItem>>> expectedResult = new PortalRestResponse<Map<String, List<SearchResultItem>>>(); + expectedResult.setMessage("null - check logs."); + expectedResult.setResponse(new HashMap<String, List<SearchResultItem>>()); + expectedResult.setStatus(PortalRestStatusEnum.ERROR); + + Mockito.when(searchService.searchResults(user.getLoginId(), searchString)).thenThrow(nullPointerException); + PortalRestResponse<Map<String, List<SearchResultItem>>> actualResult = dashboardSearchResultController + .searchPortal(mockedRequest, searchString); + assertEquals(expectedResult, actualResult); + } + +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/ECOMPLogoutControllerTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/ECOMPLogoutControllerTest.java new file mode 100644 index 00000000..20f3fe7e --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/ECOMPLogoutControllerTest.java @@ -0,0 +1,114 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.controller; + +import static org.junit.Assert.*; + +import javax.servlet.ServletRequestWrapper; +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.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.stubbing.OngoingStubbing; +import org.onap.portalapp.controller.ECOMPLogoutController; +import org.onap.portalapp.portal.framework.MockEPUser; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.service.DashboardSearchService; +import org.onap.portalapp.portal.service.DashboardSearchServiceImpl; +import org.onap.portalapp.portal.utils.EPCommonSystemProperties; +import org.onap.portalapp.util.EPUserUtils; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.core.NamedThreadLocal; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import org.springframework.web.servlet.ModelAndView; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({EPUserUtils.class, EPCommonSystemProperties.class,RequestContextHolder.class,RequestAttributes.class}) +public class ECOMPLogoutControllerTest { + + @Mock + DashboardSearchService searchService = new DashboardSearchServiceImpl(); + + @InjectMocks + ECOMPLogoutController ecompLogoutController = new ECOMPLogoutController(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + NullPointerException nullPointerException = new NullPointerException(); + + MockEPUser mockUser = new MockEPUser(); + + @Mock + RequestContextHolder requestContextHolder; + + @Mock + RequestAttributes requestAttributes; + + @Test + public void logOutTest() throws Exception{ + ModelAndView actualData = new ModelAndView("redirect:login.htm"); + ModelAndView expedtedData = null; + ThreadLocal<RequestAttributes> requestAttributesHolder = + new NamedThreadLocal<RequestAttributes>("Request attributes"); + RequestAttributes requestAttributes = new ServletRequestAttributes(mockedRequest); + PowerMockito.mockStatic(RequestContextHolder.class); + PowerMockito.mockStatic(RequestAttributes.class); + Mockito.when((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).thenReturn((ServletRequestAttributes) requestAttributes); + expedtedData = ecompLogoutController.logOut(mockedRequest, mockedResponse); + assertEquals(actualData.getViewName(),expedtedData.getViewName()); + } + +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/ExternalAppsRestfulControllerOSTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/ExternalAppsRestfulControllerOSTest.java new file mode 100644 index 00000000..e8811121 --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/ExternalAppsRestfulControllerOSTest.java @@ -0,0 +1,199 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.controller; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +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.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.portal.controller.ExternalAppsRestfulController; +import org.onap.portalapp.portal.domain.EPUser; +import org.onap.portalapp.portal.framework.MockEPUser; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.service.AdminRolesService; +import org.onap.portalapp.portal.service.EPLoginService; +import org.onap.portalapp.portal.service.FunctionalMenuService; +import org.onap.portalapp.portal.transport.FavoritesFunctionalMenuItemJson; +import org.onap.portalapp.portal.transport.FunctionalMenuItem; +import org.onap.portalapp.portal.utils.EPCommonSystemProperties; +import org.onap.portalapp.portal.utils.EPSystemProperties; +import org.onap.portalapp.portal.utils.EcompPortalUtils; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.slf4j.MDC; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ MDC.class, EPSystemProperties.class , EcompPortalUtils.class}) +public class ExternalAppsRestfulControllerOSTest { + + + @InjectMocks + ExternalAppsRestfulController externalAppsRestfulController = new ExternalAppsRestfulController(); + @Mock + FunctionalMenuService functionalMenuService; + + @Mock + EPLoginService epLoginService; + + @Mock + AdminRolesService adminRolesService; + + + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + NullPointerException nullPointerException = new NullPointerException(); + + MockEPUser mockUser = new MockEPUser(); + + @Test + public void getFavoritesForUserTest() throws Exception + { + PowerMockito.mockStatic(EPSystemProperties.class); + PowerMockito.mockStatic(MDC.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + EPUser epUser = mockUser.mockEPUser(); + epUser.setId((long) 1); + epUser.setLoginId("guestT"); + String loginId = "guestT"; + Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test"); + List<FavoritesFunctionalMenuItemJson> favorites = new ArrayList<FavoritesFunctionalMenuItemJson>(); + FavoritesFunctionalMenuItemJson favoritesFunctionalMenuItemJson = new FavoritesFunctionalMenuItemJson(); + favorites.add(favoritesFunctionalMenuItemJson); + Mockito.when(mockedRequest.getHeader(EPCommonSystemProperties.MDC_LOGIN_ID)).thenReturn("Login_URL"); + Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test"); + Mockito.when(epLoginService.findUserWithoutPwd("Login_URL")).thenReturn(epUser); + Mockito.when(functionalMenuService.getFavoriteItems(epUser.getId())).thenReturn(favorites); + List<FavoritesFunctionalMenuItemJson> actaulFavorites = externalAppsRestfulController + .getFavoritesForUser(mockedRequest, mockedResponse); + assertEquals(actaulFavorites.size(), 1); + } + + @Test + public void getFunctionalMenuItemsForUserIfSuperAdminTest() throws Exception { + PowerMockito.mockStatic(EPSystemProperties.class); + PowerMockito.mockStatic(MDC.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + EPUser epUser = mockUser.mockEPUser(); + epUser.setId((long) 1); + epUser.setLoginId("guestT"); + String loginId = "guestT"; + Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test"); + Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser); + List<FunctionalMenuItem> expectedList = new ArrayList<FunctionalMenuItem>(); + FunctionalMenuItem functionalMenuItem = new FunctionalMenuItem(); + expectedList.add(functionalMenuItem); + Mockito.when(mockedRequest.getHeader("LoginId")).thenReturn("guestT"); + Mockito.when(adminRolesService.isSuperAdmin(epUser)).thenReturn(true); + Mockito.when(functionalMenuService.getFunctionalMenuItems()).thenReturn(expectedList); + List<FunctionalMenuItem> actualList = externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest, + mockedResponse); + assertNull(actualList.get(0).menuId); + } + + @Test(expected = Exception.class) + public void getFunctionalMenuItemsForUserIfUSerNullTest() throws + Exception + { + PowerMockito.mockStatic(EPSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(MDC.class); + EPUser epUser = null; + String loginId = "guestT"; + Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test"); + Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser); + externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest, + mockedResponse); + } + + @Test + public void getFunctionalMenuItemsForUserTest() throws Exception { + PowerMockito.mockStatic(EPSystemProperties.class); + PowerMockito.mockStatic(MDC.class); + EPUser epUser = mockUser.mockEPUser(); + epUser.setId((long) 1); + epUser.setLoginId("guestT"); + String loginId = "guestT"; + Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test"); + Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser); + List<FunctionalMenuItem> expectedList = new ArrayList<FunctionalMenuItem>(); + FunctionalMenuItem functionalMenuItem = new FunctionalMenuItem(); + expectedList.add(functionalMenuItem); + Mockito.when(mockedRequest.getHeader("LoginId")).thenReturn("guestT"); + Mockito.when(adminRolesService.isSuperAdmin(epUser)).thenReturn(false); + Mockito.when(functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId())) + .thenReturn(expectedList); + List<FunctionalMenuItem> actualList = externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest, + mockedResponse); + assertNull(actualList.get(0).menuId); + } + + @Test(expected = Exception.class) + public void getFavoritesForUserIfUserNullTest() throws Exception { + List<FavoritesFunctionalMenuItemJson> favorites = new ArrayList<FavoritesFunctionalMenuItemJson>(); + FavoritesFunctionalMenuItemJson favoritesFunctionalMenuItemJson = new FavoritesFunctionalMenuItemJson(); + favorites.add(favoritesFunctionalMenuItemJson); + PowerMockito.mockStatic(EPSystemProperties.class); + PowerMockito.mockStatic(MDC.class); + Mockito.when(mockedRequest.getHeader(EPSystemProperties.MDC_LOGIN_ID)).thenReturn("Login_URL"); + Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test"); + EPUser epUser = null; + externalAppsRestfulController.getFavoritesForUser(mockedRequest, mockedResponse); + } + + +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/LoginControllerTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/LoginControllerTest.java new file mode 100644 index 00000000..983584e9 --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/LoginControllerTest.java @@ -0,0 +1,121 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.controller; + +import static org.junit.Assert.*; + +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.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.controller.LoginController; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.service.EPLoginService; +import org.onap.portalapp.portal.service.EPRoleFunctionService; +import org.onap.portalapp.portal.service.EPRoleService; +import org.onap.portalapp.portal.service.SharedContextService; +import org.onap.portalapp.service.EPProfileService; +import org.onap.portalsdk.core.util.SystemProperties; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.web.servlet.ModelAndView; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(SystemProperties.class) +public class LoginControllerTest { + + @Mock + EPProfileService service; + @Mock + EPLoginService loginService; + @Mock + SharedContextService sharedContextService; + @Mock + EPRoleService roleService; + @Mock + EPRoleFunctionService ePRoleFunctionService; + + @InjectMocks + LoginController loginController = new LoginController(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + NullPointerException nullPointerException = new NullPointerException(); + + @Test + public void loginIfAuthNullTest() + { + PowerMockito.mockStatic(SystemProperties.class); + Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn(null); + ModelAndView result = loginController.login(mockedRequest); + assertEquals(result.getViewName(),"openIdLogin") ; + } + + @Test + public void loginIfAuthOIDCTest() + { + PowerMockito.mockStatic(SystemProperties.class); + Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC"); + ModelAndView result = loginController.login(mockedRequest); + assertEquals(result.getViewName(),"login") ; + } + + @Test + public void loginTest() + { + PowerMockito.mockStatic(SystemProperties.class); + Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("Test"); + ModelAndView result = loginController.login(mockedRequest); + assertEquals(result.getViewName(),"login") ; + } +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/ONAPLoginControllerTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/ONAPLoginControllerTest.java new file mode 100644 index 00000000..ab01a0ce --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/ONAPLoginControllerTest.java @@ -0,0 +1,128 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.controller.ONAPLoginController; +import org.onap.portalapp.portal.framework.MockEPUser; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalsdk.core.auth.LoginStrategy; +import org.onap.portalsdk.core.service.LoginService; +import org.onap.portalsdk.core.service.ProfileService; +import org.onap.portalsdk.core.service.ProfileServiceImpl; +import org.springframework.web.servlet.ModelAndView; + +public class ONAPLoginControllerTest { + + @Mock + ProfileService ProfileService = new ProfileServiceImpl(); + + @Mock + LoginService mockLoginService; + + @Mock + LoginStrategy loginStrategy ; + + @InjectMocks + ONAPLoginController oNAPLoginController = new ONAPLoginController(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + NullPointerException nullPointerException = new NullPointerException(); + + MockEPUser mockUser = new MockEPUser(); + + + @Test + public void doLogin() throws Exception + { + ModelAndView expectedModelandView = null; + ModelAndView actualModelandView = null; + Mockito.when(loginStrategy.doLogin(mockedRequest, mockedResponse)).thenReturn(expectedModelandView); + actualModelandView= oNAPLoginController.doLogin(mockedRequest, mockedResponse); + oNAPLoginController.setViewName("test"); + assertEquals("test", oNAPLoginController.getViewName()); + assertEquals(actualModelandView,expectedModelandView); + } + + + @Test + public void ViewTest() throws Exception + { + ModelAndView expectedModelandView = new ModelAndView(); + expectedModelandView.setViewName("testView"); + ModelAndView actualModelandView = null; + Mockito.when(loginStrategy.doLogin(mockedRequest, mockedResponse)).thenReturn(expectedModelandView); + actualModelandView= oNAPLoginController.doLogin(mockedRequest, mockedResponse); + assertEquals(actualModelandView.getViewName(),expectedModelandView.getViewName()); + } + @Test + public void getJessionIdTest() throws Exception + { + assertNull(oNAPLoginController.getJessionId(mockedRequest)); + } + + @Test + public void getLoginServiceTest() throws Exception + { + LoginService expectedLoginService = oNAPLoginController.getLoginService(); + assertEquals(mockLoginService,expectedLoginService); + } + + + +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/ONAPWelcomeControllerTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/ONAPWelcomeControllerTest.java new file mode 100644 index 00000000..c9c18b7a --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/ONAPWelcomeControllerTest.java @@ -0,0 +1,101 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.controller; + +import static org.junit.Assert.*; + +import java.security.Principal; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.controller.ONAPWelcomeController; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.springframework.ui.Model; +import org.springframework.web.servlet.ModelAndView; + +public class ONAPWelcomeControllerTest { + + @InjectMocks + ONAPWelcomeController oNAPWelcomeController = new ONAPWelcomeController(); + + @Mock + Principal p; + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + NullPointerException nullPointerException = new NullPointerException(); + + @Test + public void getIndexPageTest() + { + assertEquals(oNAPWelcomeController.getIndexPage(mockedRequest), "/index"); + } + + @Test + public void getEcompSinglePageTest() + { + assertEquals(oNAPWelcomeController.getEcompSinglePage(mockedRequest, mockedResponse), "forward:/index.html"); + } + + @Test + public void userTest() + { + assertEquals(oNAPWelcomeController.user(null), "oid-user"); + } + + @Test + public void loginTest() + { + ModelAndView expectedView = new ModelAndView(); + expectedView.setViewName("openIdLogin"); + ModelAndView md = oNAPWelcomeController.login(p); + System.out.println(md.getViewName()); + assertEquals(md.getViewName(), expectedView.getViewName()); + } +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/PortalAdminControllerOSTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/PortalAdminControllerOSTest.java new file mode 100644 index 00000000..a8d8e498 --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/controller/PortalAdminControllerOSTest.java @@ -0,0 +1,242 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.portal.controller.PortalAdminController; +import org.onap.portalapp.portal.domain.EPRole; +import org.onap.portalapp.portal.domain.EPUser; +import org.onap.portalapp.portal.framework.MockEPUser; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.service.AdminRolesService; +import org.onap.portalapp.portal.service.AdminRolesServiceImpl; +import org.onap.portalapp.portal.service.PortalAdminService; +import org.onap.portalapp.portal.service.PortalAdminServiceImpl; +import org.onap.portalapp.portal.transport.FieldsValidator; +import org.onap.portalapp.portal.transport.PortalAdmin; +import org.onap.portalapp.portal.utils.EcompPortalUtils; +import org.onap.portalapp.util.EPUserUtils; +import org.onap.portalsdk.core.service.AuditService; +import org.onap.portalsdk.core.service.AuditServiceImpl; + +public class PortalAdminControllerOSTest { + + @InjectMocks + PortalAdminController portalAdminController = new PortalAdminController(); + + @Mock + AdminRolesService adminRolesService = new AdminRolesServiceImpl(); + + @Mock + PortalAdminService portalAdminService = new PortalAdminServiceImpl(); + + @Mock + AuditService auditService = new AuditServiceImpl(); + + + @Mock + EcompPortalUtils ecompPortalUtils = new EcompPortalUtils(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + NullPointerException nullPointerException = new NullPointerException(); + + @Mock + EPUserUtils ePUserUtils = new EPUserUtils(); + + MockEPUser mockUser = new MockEPUser(); + + @Test + public void deletePortalAdminIfUserIsSuperAdminTest() + { + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false); + Long userInfo = (long) 12; + assertNull(portalAdminController.deletePortalAdmin(mockedRequest, userInfo, mockedResponse)); + + } + + @Test + public void deletePortalAdminTest() + { + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + + FieldsValidator expectedFieldValidator = new FieldsValidator(); + expectedFieldValidator.setHttpStatusCode((long) 200); + expectedFieldValidator.setFields(null); + expectedFieldValidator.setErrorCode(null); + FieldsValidator actualFieldValidator = new FieldsValidator(); + Long userInfo = (long) 12; + Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(true); + Mockito.when(portalAdminService.deletePortalAdmin((long) 12)).thenReturn(expectedFieldValidator); + actualFieldValidator = portalAdminController.deletePortalAdmin(mockedRequest, userInfo, mockedResponse); + assertEquals(actualFieldValidator,expectedFieldValidator); + + } + + @Test + public void deletePortalAdminWithNoUserInfoTest() + { + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false); + Long userInfo = null; + assertNull(portalAdminController.deletePortalAdmin(mockedRequest, userInfo, mockedResponse)); + } + + @Test + public void getRolesByAppExceptionTest() + { + EPUser user = mockUser.mockEPUser();; + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + List<EPRole> expectedRoleList = new ArrayList<EPRole>(); + EPRole ePRole = new EPRole(); + expectedRoleList.add(ePRole); + Long appId = (long) 1; + Mockito.when(adminRolesService.getRolesByApp(user, appId)).thenThrow(nullPointerException); + assertNull(portalAdminController.getRolesByApp(mockedRequest, appId, mockedResponse)); + } + + @Test + public void getRolesByAppIfUserNullTest() + { + EPUser user = null; + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Long appId = (long) 1; + assertNull(portalAdminController.getRolesByApp(mockedRequest, appId, mockedResponse)); + } + + @Test + public void getRolesByAppTest() + { + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + List<EPRole> expectedRoleList = new ArrayList<EPRole>(); + EPRole ePRole = new EPRole(); + expectedRoleList.add(ePRole); + Long appId = (long) 1; + Mockito.when(adminRolesService.getRolesByApp(user, appId)).thenReturn(expectedRoleList); + List<EPRole> actualRoleList = portalAdminController.getRolesByApp(mockedRequest, appId, mockedResponse); + assertEquals(actualRoleList,expectedRoleList); + } + + @Test + public void createPortalAdminIfUserNullTest() + { + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null); + assertNull(portalAdminController.createPortalAdmin(mockedRequest, "guestT", mockedResponse)); + } + + + @Test + public void createPortalAdminIfUserIsSuperAdminTest() + { + + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + FieldsValidator expectedFieldValidator = new FieldsValidator(); + expectedFieldValidator.setHttpStatusCode((long) 200); + expectedFieldValidator.setFields(null); + expectedFieldValidator.setErrorCode(null); + FieldsValidator actualFieldValidator = new FieldsValidator(); + Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(true); + Mockito.when(portalAdminService.createPortalAdmin("guestT")).thenReturn(expectedFieldValidator); + actualFieldValidator = portalAdminController.createPortalAdmin(mockedRequest, "guestT", mockedResponse); + assertEquals(actualFieldValidator,expectedFieldValidator); + } + + @Test + public void createPortalAdminIfUserIsNotSuperAdminTest() + { + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false); + assertNull(portalAdminController.createPortalAdmin(mockedRequest, "guestT", mockedResponse)); + } + + @Test + public void getPortalAdminsIfUserNullTest() + { + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null); + assertNull(portalAdminController.getPortalAdmins(mockedRequest, mockedResponse)); + } + + @Test + public void getPortalAdminsIfUserAdminTest() + { + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + List<PortalAdmin> portalAdmins = new ArrayList<>(); + Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(true); + Mockito.when(portalAdminService.getPortalAdmins()).thenReturn(portalAdmins); + List<PortalAdmin> actualortalAdmins = portalAdminController.getPortalAdmins(mockedRequest, mockedResponse); + assertEquals(actualortalAdmins,portalAdmins); + } + + @Test + public void getPortalAdminIfUserIsNotSuperAdminTest() + { + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false); + assertNull(portalAdminController.getPortalAdmins(mockedRequest, mockedResponse)); + } +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/framework/MockEPUser.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/framework/MockEPUser.java new file mode 100644 index 00000000..99c96547 --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/framework/MockEPUser.java @@ -0,0 +1,102 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.framework; + +import java.util.Date; + +import org.onap.portalapp.portal.domain.EPUser; + +public class MockEPUser { + + public EPUser mockEPUser() { + + EPUser ePUser = new EPUser(); + ePUser.setOrgId(null); + ePUser.setManagerId(null); + ePUser.setFirstName("test"); + ePUser.setLastName("test"); + ePUser.setMiddleInitial(null); + ePUser.setPhone(null); + ePUser.setFax(null); + ePUser.setCellular(null); + ePUser.setEmail(null); + ePUser.setAddressId(null); + ePUser.setAlertMethodCd(null); + ePUser.setHrid(null); + ePUser.setOrgUserId("guestT"); + ePUser.setOrgCode(null); + ePUser.setAddress1(null); + ePUser.setAddress2(null); + ePUser.setCity(null); + ePUser.setState(null); + ePUser.setZipCode(null); + ePUser.setCountry(null); + ePUser.setOrgManagerUserId(null); + ePUser.setLocationClli(null); + ePUser.setBusinessCountryCode(null); + ePUser.setBusinessCountryName(null); + ePUser.setBusinessUnit(null); + ePUser.setBusinessUnitName(null); + ePUser.setDepartment(null); + ePUser.setDepartmentName(null); + ePUser.setCompanyCode(null); + ePUser.setCompany(null); + ePUser.setZipCodeSuffix(null); + ePUser.setJobTitle(null); + ePUser.setCommandChain(null); + ePUser.setSiloStatus(null); + ePUser.setCostCenter(null); + ePUser.setFinancialLocCode(null); + + ePUser.setLoginId(null); + ePUser.setLoginPwd(null); + Date date = new Date(); + ePUser.setLastLoginDate(date); + ePUser.setActive(true); + ePUser.setInternal(false); + ePUser.setSelectedProfileId(null); + ePUser.setTimeZoneId(null); + ePUser.setOnline(true); + ePUser.setChatId(null); + ePUser.setUserApps(null); + ePUser.setPseudoRoles(null); + + ePUser.setId((long) -1); + return ePUser; + } +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/framework/MockitoTestSuite.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/framework/MockitoTestSuite.java new file mode 100644 index 00000000..219bc860 --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/framework/MockitoTestSuite.java @@ -0,0 +1,93 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.framework; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class MockitoTestSuite { + + + public MockHttpServletRequestWrapper mockedRequest = new MockHttpServletRequestWrapper( + Mockito.mock(HttpServletRequest.class)); + public HttpServletResponse mockedResponse = Mockito.mock(HttpServletResponse.class); + + public MockHttpServletRequestWrapper getMockedRequest() { + return mockedRequest; + } + + public HttpServletResponse getMockedResponse() { + return mockedResponse; + } + + public class MockHttpServletRequestWrapper extends HttpServletRequestWrapper { + + HttpSession session = Mockito.mock(HttpSession.class); + + public MockHttpServletRequestWrapper(HttpServletRequest request) { + super(request); + + } + + @Override + public HttpSession getSession() { + + return session; + } + + @Override + public HttpSession getSession(boolean create) { + + return session; + } + + } + @Test + public void test() + { + assert(true); + } +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/listener/HealthMonitorTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/listener/HealthMonitorTest.java new file mode 100644 index 00000000..929c8e36 --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/listener/HealthMonitorTest.java @@ -0,0 +1,55 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.listener; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.portalapp.portal.listener.HealthMonitor; + +public class HealthMonitorTest { + + @Test + public void initialFlagsTest() { + assertEquals(false, HealthMonitor.isBackEndUp()); + assertEquals(false, HealthMonitor.isFrontEndUp()); + assertEquals(false, HealthMonitor.isDatabaseUp()); + assertEquals(false, HealthMonitor.isUebUp()); + } + +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/service/RemoteWebServiceCallServiceImplTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/service/RemoteWebServiceCallServiceImplTest.java new file mode 100644 index 00000000..ad208f39 --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/service/RemoteWebServiceCallServiceImplTest.java @@ -0,0 +1,186 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.service; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.onap.portalsdk.core.util.SystemProperties; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.portal.domain.EPApp; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.service.AppsCacheService; +import org.onap.portalapp.portal.utils.EcompPortalUtils; +import org.onap.portalapp.service.RemoteWebServiceCallServiceImpl; +import org.onap.portalsdk.core.onboarding.util.CipherUtil; +import org.onap.portalsdk.core.service.DataAccessService; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ CipherUtil.class , SystemProperties.class}) +public class RemoteWebServiceCallServiceImplTest { + + + @InjectMocks + RemoteWebServiceCallServiceImpl remoteWebServiceCallServiceImpl = new RemoteWebServiceCallServiceImpl(); + + @Mock + AppsCacheService appCacheService; + + @Mock + DataAccessService dataAccessService; + + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + NullPointerException nullPointerException = new NullPointerException(); + + @Test + public void verifyRESTCredentialTest() throws Exception + { + PowerMockito.mockStatic(CipherUtil.class); + PowerMockito.mockStatic(SystemProperties.class); + String criteria= " where ueb_key = 'requestUebKey'"; + List<EPApp> appList = new ArrayList<>(); + EPApp app = new EPApp(); + app.setAppPassword("password"); + appList.add(app); + Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList); + String secretKey = null; + Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(secretKey); + Mockito.when(CipherUtil.decryptPKC("password", + secretKey == null ? null : secretKey)).thenReturn("pwd"); + assertFalse(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","requestPassword")); + } + + @Test + public void verifyRESTCredentialExceptionTest() throws Exception + { + PowerMockito.mockStatic(CipherUtil.class); + PowerMockito.mockStatic(SystemProperties.class); + String criteria= " where ueb_key = 'requestUebKey'"; + List<EPApp> appList = new ArrayList<>(); + EPApp app = new EPApp(); + app.setAppPassword("password"); + app.setUsername("requestAppName"); + appList.add(app); + Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList); + String secretKey = null; + Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(secretKey); + Mockito.when(CipherUtil.decryptPKC("password", + secretKey == null ? null : secretKey)).thenReturn("pwd"); + assertTrue(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","pwd")); + } + + @Test + public void verifyRESTCredentialIfAppNullTest() throws Exception + { + PowerMockito.mockStatic(CipherUtil.class); + PowerMockito.mockStatic(SystemProperties.class); + String criteria= " where ueb_key = 'requestUebKey'"; + List<EPApp> appList = new ArrayList<>(); + EPApp app = new EPApp(); + app.setAppPassword("password"); + app.setUsername("requestAppName"); + appList.add(app); + Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(null); + String secretKey = null; + Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(secretKey); + Mockito.when(CipherUtil.decryptPKC("password", + secretKey == null ? null : secretKey)).thenReturn("pwd"); + assertFalse(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","pwd")); + } + + @Test + public void verifyAppKeyCredentialIfKeyIsNullTest() throws Exception + { + assertFalse(remoteWebServiceCallServiceImpl.verifyAppKeyCredential(null)); + } + + @Test + public void verifyAppKeyCredentialTest() throws Exception + { + PowerMockito.mockStatic(CipherUtil.class); + PowerMockito.mockStatic(SystemProperties.class); + StringBuffer criteria = new StringBuffer("where ueb_key = 'requestUebKey'"); +// String criteria= " where ueb_key = 'requestUebKey'"; + List<EPApp> appList = new ArrayList<>(); + EPApp app = new EPApp(); + app.setAppPassword("password"); + app.setUsername("requestAppName"); + appList.add(app); + Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(null); + assertFalse(remoteWebServiceCallServiceImpl.verifyAppKeyCredential("test")); + } + + @Test + public void verifyAppKeyCredentialSuccessTest() throws Exception + { + PowerMockito.mockStatic(CipherUtil.class); + PowerMockito.mockStatic(SystemProperties.class); + String criteria= " where ueb_key = 'test'"; + List<EPApp> appList = new ArrayList<>(); + EPApp app = new EPApp(); + app.setAppPassword("password"); + app.setUsername("requestAppName"); + appList.add(app); + Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList); + assertTrue(remoteWebServiceCallServiceImpl.verifyAppKeyCredential("test")); + } +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/service/SearchServiceImplTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/service/SearchServiceImplTest.java new file mode 100644 index 00000000..339f4ebb --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/service/SearchServiceImplTest.java @@ -0,0 +1,228 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.service; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +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.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.portal.domain.EPUser; +import org.onap.portalapp.portal.framework.MockEPUser; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.service.SearchServiceImpl; +import org.onap.portalapp.portal.service.UserService; +import org.onap.portalapp.portal.transport.UserWithNameSurnameTitle; +import org.onap.portalapp.portal.utils.EcompPortalUtils; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ EcompPortalUtils.class}) +public class SearchServiceImplTest { + + @InjectMocks + SearchServiceImpl searchServiceImpl = new SearchServiceImpl(); + + @Mock + UserService userService; + + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + NullPointerException nullPointerException = new NullPointerException(); + MockEPUser mockUser = new MockEPUser(); + + @Test + public void searchUsersInPhoneBookTest() + { + PowerMockito.mockStatic(EcompPortalUtils.class); + List<String> list = new ArrayList<>(); + String str = "Test"; + list.add(str); + Mockito.when(EcompPortalUtils.parsingByRegularExpression("Test", " ")).thenReturn(list); + assertEquals(searchServiceImpl.searchUsersInPhoneBook("Test"), "[]"); + } + + @Test + public void searchUsersInFnTableToFindUserIdTest() + { + PowerMockito.mockStatic(EcompPortalUtils.class); + List<String> list = new ArrayList<>(); + String str = "Test"; + String str2 = "Test new"; + String str1 = "Test new1"; + list.add(str); + list.add(str1); + list.add(str2); + Mockito.when(EcompPortalUtils.parsingByRegularExpression("Test", " ")).thenReturn(list); + List<EPUser> userList = new ArrayList(); + EPUser user = mockUser.mockEPUser(); + user.setLastName("Test new"); + userList.add(user); + Mockito.when( this.userService.getUserByFirstLastName("Test","Test new")).thenReturn(userList); + String result = searchServiceImpl.searchUsersInPhoneBook("Test"); + assertEquals("[{\"orgUserId\":\"guestT\",\"firstName\":\"test\",\"lastName\":\"Test new\",\"jobTitle\":null}]" , result); + } + + + + @Test + public void searchUsersInFnTableFirstNameTest() + { + PowerMockito.mockStatic(EcompPortalUtils.class); + List<String> list = new ArrayList<>(); + String str = "TestTT"; + String str2 = "Test new1"; + String str1 = "Test new"; + String str3 = "Test new2"; + list.add(str); + list.add(str1); + list.add(str2); + list.add(str3); + Mockito.when(EcompPortalUtils.parsingByRegularExpression("TestTT", " ")).thenReturn(list); + List<EPUser> userList = new ArrayList(); + EPUser user = mockUser.mockEPUser(); + user.setLastName("Test new"); + user.setFirstName(null); + userList.add(user); + Mockito.when( this.userService.getUserByFirstLastName("TestTT","Test new")).thenReturn(userList); + assertEquals(searchServiceImpl.searchUsersInPhoneBook("TestTT"), "[]"); } + + + @Test + public void searchUsersInFnTableLastNameTest() + { + PowerMockito.mockStatic(EcompPortalUtils.class); + List<String> list = new ArrayList<>(); + String str = "Test"; + String str2 = "Test new"; + String str1 = "Test new1"; + list.add(str); + list.add(str1); + list.add(str2); + Mockito.when(EcompPortalUtils.parsingByRegularExpression("Test", " ")).thenReturn(list); + List<EPUser> userList = new ArrayList(); + EPUser user = mockUser.mockEPUser(); + user.setLastName(null); + userList.add(user); + Mockito.when( this.userService.getUserByFirstLastName("Test","Test new")).thenReturn(userList); + assertEquals(searchServiceImpl.searchUsersInPhoneBook("Test"), "[]"); } + + + + @Test + public void searchUserByUserIdTest() + { + List<EPUser> userList = new ArrayList(); + EPUser user = mockUser.mockEPUser(); + user.setLastName("Test new"); + userList.add(user); + List<EPUser> foundUsers = new ArrayList<EPUser>(); + Mockito.when(this.userService.getUserByUserId("guestT")).thenReturn(userList); + + EPUser expectedUser = searchServiceImpl.searchUserByUserId("guestT"); + assertEquals(user, expectedUser); + } + + @Test + public void searchUserByUserIdExceptionTest() + { + Mockito.when(this.userService.getUserByUserId("guestT")).thenThrow(nullPointerException); + assertNull(searchServiceImpl.searchUserByUserId("guestT")); + + } + + @Test + public void searchUsersByUserIdTest() + { + + List<EPUser> userList = new ArrayList(); + EPUser user = mockUser.mockEPUser(); + user.setLastName("Test new"); + userList.add(user); + Mockito.when(this.userService.getUserByUserId("guestT")).thenReturn(userList); + List<UserWithNameSurnameTitle> foundUsers = searchServiceImpl.searchUsersByUserId(user); + assertEquals(foundUsers.size(), 1); + + } + + @Test + public void searchUsersByUserIdExceptionTest() + { + EPUser user = mockUser.mockEPUser(); + user.setLastName("Test new"); + Mockito.when(this.userService.getUserByUserId("guestT")).thenThrow(nullPointerException); + List<UserWithNameSurnameTitle> foundUsers = searchServiceImpl.searchUsersByUserId(user); + assertEquals(foundUsers.size(), 0); + + } + + @Test + public void searchUsersByNameExceptionTest() + { + EPUser user = mockUser.mockEPUser(); + user.setLastName("test"); + user.setFirstName("test"); + + Mockito.when(this.userService.getUserByFirstLastName("test","test")).thenThrow(nullPointerException); + List<UserWithNameSurnameTitle> foundUsers = searchServiceImpl.searchUsersByName(user); + assertEquals(foundUsers.size(), 0); + + } +} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/service/UserServiceImplTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/service/UserServiceImplTest.java new file mode 100644 index 00000000..f4afb0fe --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/service/UserServiceImplTest.java @@ -0,0 +1,101 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.service; +//package org.openecomp.portalapp.portal.service; +// +//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.Mock; +//import org.mockito.Mockito; +//import org.mockito.MockitoAnnotations; +//import org.openecomp.portalapp.portal.framework.MockitoTestSuite; +//import org.openecomp.portalapp.portal.utils.EPSystemProperties; +//import org.openecomp.portalsdk.core.service.DataAccessService; +//import org.openecomp.portalsdk.core.util.SystemProperties; +//import org.powermock.api.mockito.PowerMockito; +// +//import java.io.BufferedReader; +//import java.io.IOException; +//import java.io.InputStreamReader; +//import java.io.UnsupportedEncodingException; +//import java.net.HttpURLConnection; +//import org.powermock.core.classloader.annotations.PrepareForTest; +//import org.powermock.modules.junit4.PowerMockRunner; +// +// +//@RunWith(PowerMockRunner.class) +//@PrepareForTest({ SystemProperties.class , EPSystemProperties.class , SystemProperties.class}) +//public class UserServiceImplTest { +// +// +// @InjectMocks +// UserServiceImpl userServiceImpl = new UserServiceImpl(); +// +// @Mock +// DataAccessService dataAccessService; +// +// @Mock +// HttpURLConnection con; +// +// @Before +// public void setup() { +// MockitoAnnotations.initMocks(this); +// } +// +// MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); +// +// HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); +// HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); +// +// @Test +// public void getUserByUserIdTest() throws UnsupportedEncodingException, IOException +// { +// BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); +// PowerMockito.mockStatic(SystemProperties.class); +// PowerMockito.mockStatic(EPSystemProperties.class); +// Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC"); +// Mockito.when(EPSystemProperties.getProperty(EPSystemProperties.AUTH_USER_SERVER)).thenReturn("http://www.google.com"); +// Mockito.when(new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"))).thenReturn(reader).thenReturn(reader); +// userServiceImpl.getUserByUserId("guestT"); +// } +//} diff --git a/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/utils/EcompPortalUtilsTest.java b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/utils/EcompPortalUtilsTest.java new file mode 100644 index 00000000..532c7204 --- /dev/null +++ b/ecomp-portal-BE-os/src/test/java/org/onap/portalapp/portal/utils/EcompPortalUtilsTest.java @@ -0,0 +1,52 @@ +/*- + * ============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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.utils; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.portalapp.portal.utils.EcompPortalUtils; + +public class EcompPortalUtilsTest { + + + @Test + public void legitimateUserIdFailureTest() { + assertEquals(false, EcompPortalUtils.legitimateUserId("1#@23456")); + } +} |