From 1298fd26f37cd451cbf1adbea68b98151662e796 Mon Sep 17 00:00:00 2001 From: Hima Elisetty Date: Fri, 12 Jan 2018 10:36:42 -0500 Subject: Introduce JUNIT Plumbing Issue-ID: PORTAL-161, PORTAL-160, PORTAL-159 Includes Change in encryption and decryption algorithm Change-Id: I5f379d6d9ac25536744152ac1d61223a52f1ce7f Signed-off-by: Hima Elisetty --- ecomp-sdk/epsdk-app-common/README.md | 5 + .../db-scripts/EcompSdkDMLMySql_2_1_Common.sql | 30 ++ .../EcompSdkMySql_Rollback_2_1_to_1710_Common.sql | 10 + .../EcompSdkMySql_Upgrade_1710_to_2_1_Common.sql | 36 +++ ecomp-sdk/epsdk-app-common/pom.xml | 7 + .../controller/core/FavoritesController.java | 2 +- .../controller/core/FuncMenuController.java | 2 +- .../test/java/org/onap/portalapp/SanityTest.java | 57 ---- .../controller/AngularAdminControllerTest.java | 93 ++++++ .../controller/CollaborationControllerTest.java | 63 ----- .../org/onap/portalapp/controller/NetMapTest.java | 56 ---- .../core/BootstrapSampleControllerTest.java | 64 +++++ .../controller/core/RoleControllerTest.java | 311 +++++++++++++++++++++ .../core/RoleFunctionListControllerTest.java | 249 +++++++++++++++++ .../controller/core/RoleListControllerTest.java | 209 ++++++++++++++ .../core/SingleSignOnControllerTest.java | 297 ++++++++++++++++++++ .../onap/portalapp/framework/MockitoTestSuite.java | 89 ++++++ .../onap/portalapp/service/ProfileServiceTest.java | 73 ----- .../services/WorkflowScheduleServiceTest.java | 75 ----- 19 files changed, 1402 insertions(+), 326 deletions(-) create mode 100644 ecomp-sdk/epsdk-app-common/db-scripts/EcompSdkMySql_Rollback_2_1_to_1710_Common.sql create mode 100644 ecomp-sdk/epsdk-app-common/db-scripts/EcompSdkMySql_Upgrade_1710_to_2_1_Common.sql delete mode 100644 ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/SanityTest.java create mode 100644 ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/AngularAdminControllerTest.java delete mode 100644 ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/CollaborationControllerTest.java delete mode 100644 ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/NetMapTest.java create mode 100644 ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/BootstrapSampleControllerTest.java create mode 100644 ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/RoleControllerTest.java create mode 100644 ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/RoleFunctionListControllerTest.java create mode 100644 ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/RoleListControllerTest.java create mode 100644 ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/SingleSignOnControllerTest.java create mode 100644 ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/framework/MockitoTestSuite.java delete mode 100644 ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/service/ProfileServiceTest.java delete mode 100644 ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/workflow/services/WorkflowScheduleServiceTest.java (limited to 'ecomp-sdk/epsdk-app-common') diff --git a/ecomp-sdk/epsdk-app-common/README.md b/ecomp-sdk/epsdk-app-common/README.md index e72d9420..2ba60d3b 100644 --- a/ecomp-sdk/epsdk-app-common/README.md +++ b/ecomp-sdk/epsdk-app-common/README.md @@ -24,6 +24,11 @@ AngularJS version 1.5.0. ### ONAP Distributions +Version 2.1.0 +- PORTAL 161 Add the JUNIT plumbing for PortalSDK +- PORTAL 160 Prevent unauthorized access to SamplePages, Report Pages, Role +- PORTAL 159 Change the encryption algorithm to PKCS5PADDING + Version 1.4.0 - PORTAL-19 Rename Java package base to org.onap - PORTAL-42 Use OParent as parent POM diff --git a/ecomp-sdk/epsdk-app-common/db-scripts/EcompSdkDMLMySql_2_1_Common.sql b/ecomp-sdk/epsdk-app-common/db-scripts/EcompSdkDMLMySql_2_1_Common.sql index 18de88fd..7617e4e8 100644 --- a/ecomp-sdk/epsdk-app-common/db-scripts/EcompSdkDMLMySql_2_1_Common.sql +++ b/ecomp-sdk/epsdk-app-common/db-scripts/EcompSdkDMLMySql_2_1_Common.sql @@ -136,6 +136,36 @@ INSERT INTO fn_restricted_url (restricted_url, function_cd) VALUES ('tab3.htm',' INSERT INTO fn_restricted_url (restricted_url, function_cd) VALUES ('tab4.htm','menu_tab'); INSERT INTO fn_restricted_url (restricted_url, function_cd) VALUES ('raptor.htm','view_reports'); INSERT INTO fn_restricted_url (restricted_url, function_cd) VALUES ('raptor_blob_extract.htm','view_reports'); +INSERT INTO fn_restricted_url VALUES('admin','menu_admin'); +INSERT INTO fn_restricted_url VALUES('get_role','menu_admin'); +INSERT INTO fn_restricted_url VALUES('get_role_functions','menu_admin'); +INSERT INTO fn_restricted_url VALUES('role_list/*','menu_admin'); +INSERT INTO fn_restricted_url VALUES('role_function_list/*','menu_admin'); +INSERT INTO fn_restricted_url VALUES('addRole','menu_admin'); +INSERT INTO fn_restricted_url VALUES('addRoleFunction','menu_admin'); +INSERT INTO fn_restricted_url VALUES('removeRole','menu_admin'); +INSERT INTO fn_restricted_url VALUES('removeRoleFunction','menu_admin'); +INSERT INTO fn_restricted_url VALUES('profile/*','menu_admin'); +INSERT INTO fn_restricted_url VALUES('samplePage','menu_sample'); +INSERT INTO fn_restricted_url VALUES('workflows','menu_admin'); +INSERT INTO fn_restricted_url VALUES('workflows/list','menu_admin'); +INSERT INTO fn_restricted_url VALUES('workflows/addWorkflow','menu_admin'); +INSERT INTO fn_restricted_url VALUES('workflows/saveCronJob','menu_admin'); +INSERT INTO fn_restricted_url VALUES('workflows/editWorkflow','menu_admin'); +INSERT INTO fn_restricted_url VALUES('workflows/removeWorkflow','menu_admin'); +INSERT INTO fn_restricted_url VALUES('workflows/removeAllWorkflows','menu_admin'); +INSERT INTO fn_restricted_url VALUES('role/saveRole.htm','menu_admin'); +INSERT INTO fn_restricted_url VALUES('post_search/process','menu_admin'); +INSERT INTO fn_restricted_url VALUES('post_search/search','menu_admin'); +INSERT INTO fn_restricted_url VALUES('post_search/search','menu_profile'); +INSERT INTO fn_restricted_url VALUES('report/wizard/retrieve_def_tab_wise_data/*','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/wizard/retrieve_form_tab_wise_data/*','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/wizard/retrieve_sql_tab_wise_data/*','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/wizard/security/*','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/wizard/copy_report/*','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/wizard/save_def_tab_data/*','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/wizard/retrieve_data/true','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/security/*','menu_reports'); -- fn_role Insert into fn_role (ROLE_ID,ROLE_NAME,ACTIVE_YN,PRIORITY) values (16,'Standard User','Y',5); diff --git a/ecomp-sdk/epsdk-app-common/db-scripts/EcompSdkMySql_Rollback_2_1_to_1710_Common.sql b/ecomp-sdk/epsdk-app-common/db-scripts/EcompSdkMySql_Rollback_2_1_to_1710_Common.sql new file mode 100644 index 00000000..14ef6f1e --- /dev/null +++ b/ecomp-sdk/epsdk-app-common/db-scripts/EcompSdkMySql_Rollback_2_1_to_1710_Common.sql @@ -0,0 +1,10 @@ +use ecomp_sdk; + +DELETE FROM fn_restricted_url +where RESTRICTED_URL in('admin', 'get_role', 'get_role_functions', 'role_list/*', 'role_function_list/*', 'addRole', 'addRoleFunction', 'removeRole', + 'removeRoleFunction', 'profile/*', 'samplePage','workflows', 'workflows/list', 'workflows/addWorkflow', 'workflows/saveCronJob', + 'workflows/editWorkflow', 'workflows/removeWorkflow', 'workflows/removeAllWorkflows','role/saveRole.htm','post_search/process', 'post_search/search', 'report/wizard/retrieve_def_tab_wise_data/*', + 'report/wizard/retrieve_form_tab_wise_data/*', 'report/wizard/retrieve_sql_tab_wise_data/*', 'report/wizard/security/*', 'report/wizard/copy_report/*', 'report/wizard/save_def_tab_data/*', 'report/wizard/retrieve_data/true', + 'report/security/*'); + +commit; \ No newline at end of file diff --git a/ecomp-sdk/epsdk-app-common/db-scripts/EcompSdkMySql_Upgrade_1710_to_2_1_Common.sql b/ecomp-sdk/epsdk-app-common/db-scripts/EcompSdkMySql_Upgrade_1710_to_2_1_Common.sql new file mode 100644 index 00000000..08f0d38f --- /dev/null +++ b/ecomp-sdk/epsdk-app-common/db-scripts/EcompSdkMySql_Upgrade_1710_to_2_1_Common.sql @@ -0,0 +1,36 @@ +use ecomp_sdk; + +SET FOREIGN_KEY_CHECKS=0; + +INSERT INTO fn_restricted_url VALUES('admin','menu_admin'); +INSERT INTO fn_restricted_url VALUES('get_role','menu_admin'); +INSERT INTO fn_restricted_url VALUES('get_role_functions','menu_admin'); +INSERT INTO fn_restricted_url VALUES('role_list/*','menu_admin'); +INSERT INTO fn_restricted_url VALUES('role_function_list/*','menu_admin'); +INSERT INTO fn_restricted_url VALUES('addRole','menu_admin'); +INSERT INTO fn_restricted_url VALUES('addRoleFunction','menu_admin'); +INSERT INTO fn_restricted_url VALUES('removeRole','menu_admin'); +INSERT INTO fn_restricted_url VALUES('removeRoleFunction','menu_admin'); +INSERT INTO fn_restricted_url VALUES('profile/*','menu_admin'); +INSERT INTO fn_restricted_url VALUES('samplePage','menu_sample'); +INSERT INTO fn_restricted_url VALUES('workflows','menu_admin'); +INSERT INTO fn_restricted_url VALUES('workflows/list','menu_admin'); +INSERT INTO fn_restricted_url VALUES('workflows/addWorkflow','menu_admin'); +INSERT INTO fn_restricted_url VALUES('workflows/saveCronJob','menu_admin'); +INSERT INTO fn_restricted_url VALUES('workflows/editWorkflow','menu_admin'); +INSERT INTO fn_restricted_url VALUES('workflows/removeWorkflow','menu_admin'); +INSERT INTO fn_restricted_url VALUES('workflows/removeAllWorkflows','menu_admin'); +INSERT INTO fn_restricted_url VALUES('role/saveRole.htm','menu_admin'); +INSERT INTO fn_restricted_url VALUES('post_search/process','menu_admin'); +INSERT INTO fn_restricted_url VALUES('post_search/search','menu_admin'); +INSERT INTO fn_restricted_url VALUES('post_search/search','menu_profile'); +INSERT INTO fn_restricted_url VALUES('report/wizard/retrieve_def_tab_wise_data/*','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/wizard/retrieve_form_tab_wise_data/*','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/wizard/retrieve_sql_tab_wise_data/*','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/wizard/security/*','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/wizard/copy_report/*','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/wizard/save_def_tab_data/*','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/wizard/retrieve_data/true','menu_reports'); +INSERT INTO fn_restricted_url VALUES('report/security/*','menu_reports'); + +commit; \ No newline at end of file diff --git a/ecomp-sdk/epsdk-app-common/pom.xml b/ecomp-sdk/epsdk-app-common/pom.xml index cb14aec0..a017ea0d 100644 --- a/ecomp-sdk/epsdk-app-common/pom.xml +++ b/ecomp-sdk/epsdk-app-common/pom.xml @@ -237,5 +237,12 @@ esapi 2.1.0 + + + org.jacoco + org.jacoco.agent + ${jacoco.version} + runtime + diff --git a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/FavoritesController.java b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/FavoritesController.java index da26fe69..82c13f10 100644 --- a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/FavoritesController.java +++ b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/FavoritesController.java @@ -109,7 +109,7 @@ public class FavoritesController extends RestrictedBaseController { appName = app.getName(); appUserName = app.getUsername(); try { - decryptedPwd = CipherUtil.decrypt(app.getAppPassword(), + decryptedPwd = CipherUtil.decryptPKC(app.getAppPassword(), SystemProperties.getProperty(SystemProperties.Decryption_Key)); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, diff --git a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/FuncMenuController.java b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/FuncMenuController.java index 3ad49860..716f8e03 100644 --- a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/FuncMenuController.java +++ b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/FuncMenuController.java @@ -172,7 +172,7 @@ public class FuncMenuController extends RestrictedBaseController { appName = app.getName(); appUserName = app.getUsername(); try { - decryptedPwd = CipherUtil.decrypt(app.getAppPassword(), + decryptedPwd = CipherUtil.decryptPKC(app.getAppPassword(), SystemProperties.getProperty(SystemProperties.Decryption_Key)); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/SanityTest.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/SanityTest.java deleted file mode 100644 index 479f83e0..00000000 --- a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/SanityTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * ============LICENSE_START========================================== - * ONAP Portal SDK - * =================================================================== - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * =================================================================== - * - * Unless otherwise specified, all software contained herein is licensed - * under the Apache License, Version 2.0 (the "License"); - * you may not use this software except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Unless otherwise specified, all documentation contained herein is licensed - * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); - * you may not use this documentation except in compliance with the License. - * You may obtain a copy of the License at - * - * https://creativecommons.org/licenses/by/4.0/ - * - * Unless required by applicable law or agreed to in writing, documentation - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================ - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - */ -package org.onap.portalapp; - -import org.junit.Assert; -import org.junit.Test; -import org.onap.portalapp.core.MockApplicationContextTestSuite; -import org.springframework.test.web.servlet.ResultActions; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; - -public class SanityTest extends MockApplicationContextTestSuite { - - @Test - public void testGetAvailableRoles() throws Exception { - - ResultActions ra =getMockMvc().perform(MockMvcRequestBuilders.get("/api/roles")); - //Assert.assertEquals(UrlAccessRestrictedException.class,ra.andReturn().getResolvedException().getClass()); - Assert.assertEquals("application/json",ra.andReturn().getResponse().getContentType()); - } - - -} diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/AngularAdminControllerTest.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/AngularAdminControllerTest.java new file mode 100644 index 00000000..02755b4a --- /dev/null +++ b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/AngularAdminControllerTest.java @@ -0,0 +1,93 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.controller.core; + +import static org.junit.Assert.*; + +import java.util.HashMap; +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.MockitoAnnotations; +import org.onap.portalapp.framework.MockitoTestSuite; +import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.web.support.UserUtils; +import org.springframework.web.servlet.ModelAndView; + +public class AngularAdminControllerTest { + + @InjectMocks + AngularAdminController angularAdminController = new AngularAdminController(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + NullPointerException nullPointerException = new NullPointerException(); + + User user = new User(); + + @Mock + UserUtils userUtils = new UserUtils(); + + @Test + public void viewTest() { + ModelAndView modelandView = new ModelAndView("user_profile_list"); + ModelAndView expectedModelandView = angularAdminController.view(); + assertEquals(expectedModelandView.getViewName(), modelandView.getViewName()); + } + + @Test + public void adminViewTest() { + ModelAndView expectedModelandView = angularAdminController.adminView(); + assertNull(expectedModelandView.getViewName()); + } +} + diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/CollaborationControllerTest.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/CollaborationControllerTest.java deleted file mode 100644 index e6e604cb..00000000 --- a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/CollaborationControllerTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * ============LICENSE_START========================================== - * ONAP Portal SDK - * =================================================================== - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * =================================================================== - * - * Unless otherwise specified, all software contained herein is licensed - * under the Apache License, Version 2.0 (the "License"); - * you may not use this software except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Unless otherwise specified, all documentation contained herein is licensed - * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); - * you may not use this documentation except in compliance with the License. - * You may obtain a copy of the License at - * - * https://creativecommons.org/licenses/by/4.0/ - * - * Unless required by applicable law or agreed to in writing, documentation - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================ - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - */ -package org.onap.portalapp.controller; - -import org.junit.Assert; -import org.junit.Test; -import org.onap.portalapp.core.MockApplicationContextTestSuite; -import org.onap.portalsdk.core.domain.User; -import org.onap.portalsdk.core.util.SystemProperties; -import org.springframework.test.web.servlet.ResultActions; -import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; - -public class CollaborationControllerTest extends MockApplicationContextTestSuite { - - @Test - public void testGetAvailableRoles() throws Exception { - MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/collaborate_list"); - User user = new User(); - user.setId(1l); - // user.setOrgUserId("abc"); - requestBuilder.sessionAttr(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user); - - ResultActions ra = getMockMvc().perform(requestBuilder); - Assert.assertEquals(2, ra.andReturn().getModelAndView().getModelMap().size()); - } - -} diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/NetMapTest.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/NetMapTest.java deleted file mode 100644 index 61973f64..00000000 --- a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/NetMapTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * ============LICENSE_START========================================== - * ONAP Portal SDK - * =================================================================== - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * =================================================================== - * - * Unless otherwise specified, all software contained herein is licensed - * under the Apache License, Version 2.0 (the "License"); - * you may not use this software except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Unless otherwise specified, all documentation contained herein is licensed - * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); - * you may not use this documentation except in compliance with the License. - * You may obtain a copy of the License at - * - * https://creativecommons.org/licenses/by/4.0/ - * - * Unless required by applicable law or agreed to in writing, documentation - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================ - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - */ -package org.onap.portalapp.controller; - -import org.junit.Assert; -import org.junit.Test; -import org.onap.portalapp.core.MockApplicationContextTestSuite; -import org.springframework.test.web.servlet.ResultActions; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; - -public class NetMapTest extends MockApplicationContextTestSuite { - - @Test - public void testGetNetMap() throws Exception { - ResultActions ra =getMockMvc().perform(MockMvcRequestBuilders.get("/net_map")); - //Assert.assertEquals(UrlAccessRestrictedException.class,ra.andReturn().getResolvedException().getClass()); - Assert.assertEquals("net_map_int",ra.andReturn().getModelAndView().getModel().get("frame_int")); - } - - -} diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/BootstrapSampleControllerTest.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/BootstrapSampleControllerTest.java new file mode 100644 index 00000000..2980c7ce --- /dev/null +++ b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/BootstrapSampleControllerTest.java @@ -0,0 +1,64 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.controller.core; + +import static org.junit.Assert.assertNull; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.MockitoAnnotations; +import org.springframework.web.servlet.ModelAndView; + +public class BootstrapSampleControllerTest { + + @InjectMocks + BootstrapSampleController bootstrapSampleController = new BootstrapSampleController(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void welcomeTest() { + ModelAndView expectedResluts = bootstrapSampleController.welcome(); + assertNull(expectedResluts.getViewName()); + } + +} diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/RoleControllerTest.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/RoleControllerTest.java new file mode 100644 index 00000000..81bbaee2 --- /dev/null +++ b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/RoleControllerTest.java @@ -0,0 +1,311 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.controller.core; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringReader; +import java.io.StringWriter; +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.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.framework.MockitoTestSuite; +import org.onap.portalsdk.core.domain.Role; +import org.onap.portalsdk.core.domain.RoleFunction; +import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.service.RoleService; +import org.onap.portalsdk.core.web.support.UserUtils; +import org.springframework.web.bind.ServletRequestUtils; +import org.springframework.web.servlet.ModelAndView; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class RoleControllerTest { + + @InjectMocks + RoleController roleController = new RoleController(); + + @Mock + RoleService roleService; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + NullPointerException nullPointerException = new NullPointerException(); + + @Mock + UserUtils userUtils = new UserUtils(); + + @Mock + ServletRequestUtils servletRequestUtils; + + @Mock + ObjectMapper mapper = new ObjectMapper(); + + @Test + public void roleTest() throws IOException { + roleController.setViewName("Test"); + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Role expectedRole = new Role(); + expectedRole.setId((long) 1); + Mockito.when(roleService.getRole(Matchers.anyString(), Matchers.anyLong())).thenReturn(expectedRole); + List roleFunctionList = new ArrayList(); + List roleList = new ArrayList(); + Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunctionList); + Mockito.when(roleService.getAvailableChildRoles(Matchers.anyString(), Matchers.anyLong())).thenReturn(roleList); + ModelAndView expectedResult = roleController.role(mockedRequest); + assertEquals(expectedResult.getViewName(), "Test"); + } + + @Test + public void roleExceptionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Role expectedRole = new Role(); + expectedRole.setId((long) 1); + Mockito.when(roleService.getRole(Matchers.anyString(), Matchers.anyLong())).thenReturn(expectedRole); + Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenThrow(nullPointerException); + ModelAndView expectedResult = roleController.role(mockedRequest); + assertNull(expectedResult.getViewName(), null); + } + + @Test + public void getRoleTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Role expectedRole = new Role(); + expectedRole.setId((long) 1); + Mockito.when(roleService.getRole(Matchers.anyString(), Matchers.anyLong())).thenReturn(expectedRole); + List roleFunctionList = new ArrayList(); + List roleList = new ArrayList(); + Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunctionList); + Mockito.when(roleService.getAvailableChildRoles(Matchers.anyString(), Matchers.anyLong())).thenReturn(roleList); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + roleController.getRole(mockedRequest, mockedResponse); + } + + @Test + public void getRoleExceptionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Role expectedRole = new Role(); + expectedRole.setId((long) 1); + Mockito.when(roleService.getRole(Matchers.anyString(), Matchers.anyLong())).thenReturn(expectedRole); + List roleFunctionList = new ArrayList(); + List roleList = new ArrayList(); + Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunctionList); + Mockito.when(roleService.getAvailableChildRoles(Matchers.anyString(), Matchers.anyLong())).thenReturn(roleList); + roleController.getRole(mockedRequest, mockedResponse); + } + + @Test + public void saveRoleTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String json = "{\"role\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"name\":\"test1\",\"active\":false,\"priority\":\"1\",\"roleFunctions\":[],\"childRoles\":[],\"editUrl\":\"/role.htm?role_id=1\",\"toggleActiveImage\":\"/static/fusion/images/inactive.png\",\"toggleActiveAltText\":\"Click to Activate Role\"},\"childRoles\":[],\"roleFunctions\":[]}"; + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json))); + Role role = new Role(); + role.setId((long) 1); + Mockito.when(roleService.getRole(user.getOrgUserId(), role.getId())).thenReturn(role); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + assertNull(roleController.saveRole(mockedRequest, mockedResponse)); + + } + + @Test + public void saveRoleWithRoleFunctionsTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String json = "{\"role\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"name\":\"test role modified test\",\"active\":true,\"priority\":null,\"roleFunctions\":[{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"code\":\"menu_test\",\"name\":\"Test Menu\",\"editUrl\":\"/role_function.htm?role_function_id=menu_test\",\"$$hashKey\":\"object:1476\"}],\"childRoles\":[],\"editUrl\":\"/role.htm?role_id=9356\",\"toggleActiveImage\":\"/static/fusion/images/active.png\",\"toggleActiveAltText\":\"Click to Deactivate Role\"},\"childRoles\":[],\"roleFunctions\":[{\"id\":9356,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"code\":\"menu_test\",\"name\":\"Test Menu\",\"editUrl\":\"/role_function.htm?role_function_id=menu_test\",\"$$hashKey\":\"object:1476\"}]}"; + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json))); + Role role = new Role(); + role.setId((long) 1); + Mockito.when(roleService.getRole(user.getOrgUserId(), role.getId())).thenReturn(role); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + assertNull(roleController.saveRole(mockedRequest, mockedResponse)); + } + + @Test + public void saveNewRoleTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String json = "{\"role\":{\"id\":null,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"name\":\"test1\",\"active\":false,\"priority\":\"1\",\"roleFunctions\":[],\"childRoles\":[],\"editUrl\":\"/role.htm?role_id=1\",\"toggleActiveImage\":\"/static/fusion/images/inactive.png\",\"toggleActiveAltText\":\"Click to Activate Role\"},\"childRoles\":[],\"roleFunctions\":[]}"; + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json))); + Role role = new Role(); + role.setId((long) 1); + Mockito.when(roleService.getRole(user.getOrgUserId(), role.getId())).thenReturn(role); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + List roleList = new ArrayList(); + Role role1 = new Role(); + role1.setName("TestRole1"); + Role role2 = new Role(); + role2.setName("TestRole2"); + roleList.add(role1); + roleList.add(role2); + Mockito.when(roleService.getAvailableRoles(user.getOrgUserId())).thenReturn(roleList); + assertNull(roleController.saveRole(mockedRequest, mockedResponse)); + } + + @Test + public void saveNewRoleExceptionTestIfRoleNameExistsTest() throws IOException { + + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String json = "{\"role\":{\"id\":null,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"name\":\"TestRole1\",\"active\":false,\"priority\":\"1\",\"roleFunctions\":[],\"childRoles\":[],\"editUrl\":\"/role.htm?role_id=1\",\"toggleActiveImage\":\"/static/fusion/images/inactive.png\",\"toggleActiveAltText\":\"Click to Activate Role\"},\"childRoles\":[],\"roleFunctions\":[]}"; + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json))); + Role role = new Role(); + role.setId((long) 1); + Mockito.when(roleService.getRole(user.getOrgUserId(), role.getId())).thenReturn(role); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + List roleList = new ArrayList(); + Role role1 = new Role(); + role1.setName("TestRole1"); + Role role2 = new Role(); + role2.setName("TestRole2"); + roleList.add(role1); + roleList.add(role2); + Mockito.when(roleService.getAvailableRoles(user.getOrgUserId())).thenReturn(roleList); + assertNull(roleController.saveRole(mockedRequest, mockedResponse)); + } + + @Test + public void removeRoleFunctionTest() throws IOException + { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String json = "{\"roleFunction\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"code\":\"menu_test\",\"name\":\"Test Menu\",\"editUrl\":\"/role_function.htm?role_function_id=menu_test\",\"$$hashKey\":\"object:1476\"}}"; + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json))); + Role expectedRole = new Role(); + expectedRole.setId((long) 1); + Mockito.when(roleService.getRole(Matchers.anyString(), Matchers.anyLong())).thenReturn(expectedRole); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + assertNull(roleController.removeRoleFunction(mockedRequest, mockedResponse)); + } + + @Test + public void removeRoleFunctionExceptionTest() throws IOException + { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String json = "{\"roleFunction\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"code\":\"menu_test\",\"name\":\"Test Menu\",\"editUrl\":\"/role_function.htm?role_function_id=menu_test\",\"$$hashKey\":\"object:1476\"}}"; + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json))); + Role expectedRole = new Role(); + expectedRole.setId((long) 1); + Mockito.when(roleService.getRole(Matchers.anyString(), Matchers.anyLong())).thenThrow(nullPointerException); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + assertNull(roleController.removeRoleFunction(mockedRequest, mockedResponse)); + } + + @Test + public void saveRoleFunctionTest() throws IOException + { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String json = "{\"roleFunction\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"code\":\"menu_test\",\"name\":\"Test Menu\",\"editUrl\":\"/role_function.htm?role_function_id=menu_test\",\"$$hashKey\":\"object:1476\"}}"; + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json))); + Role expectedRole = new Role(); + expectedRole.setId((long) 1); + Mockito.when(roleService.getRole(Matchers.anyString(), Matchers.anyLong())).thenReturn(expectedRole); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + assertNull(roleController.addRoleFunction(mockedRequest, mockedResponse)); + } + + @Test + public void saveRoleFunctionExceptionTest() throws IOException + { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String json = "{\"roleFunction\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"code\":\"menu_test\",\"name\":\"Test Menu\",\"editUrl\":\"/role_function.htm?role_function_id=menu_test\",\"$$hashKey\":\"object:1476\"}}"; + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json))); + Role expectedRole = new Role(); + expectedRole.setId((long) 1); + Mockito.when(roleService.getRole(Matchers.anyString(), Matchers.anyLong())).thenThrow(nullPointerException); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + assertNull(roleController.addRoleFunction(mockedRequest, mockedResponse)); + } +} diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/RoleFunctionListControllerTest.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/RoleFunctionListControllerTest.java new file mode 100644 index 00000000..d30dc1d6 --- /dev/null +++ b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/RoleFunctionListControllerTest.java @@ -0,0 +1,249 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.controller.core; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +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.framework.MockitoTestSuite; +import org.onap.portalsdk.core.domain.RoleFunction; +import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.service.RoleService; +import org.onap.portalsdk.core.web.support.UserUtils; +import org.springframework.web.bind.ServletRequestUtils; +import org.springframework.web.servlet.ModelAndView; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class RoleFunctionListControllerTest { + + @InjectMocks + RoleFunctionListController roleFunctionListController = new RoleFunctionListController(); + + @Mock + RoleService roleService; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + NullPointerException nullPointerException = new NullPointerException(); + + @Mock + UserUtils userUtils = new UserUtils(); + + @Mock + ServletRequestUtils servletRequestUtils; + + @Mock + ObjectMapper mapper = new ObjectMapper(); + + @Test + public void welcomeTest() throws IOException { + roleFunctionListController.setViewName("Test"); + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + List roleFunctionList = new ArrayList(); + Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunctionList); + ModelAndView expectedResult = roleFunctionListController.welcome(mockedRequest); + assertEquals(expectedResult.getViewName(), "Test"); + } + + @Test + public void welcomeExceptionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenThrow(nullPointerException); + ModelAndView expectedResult = roleFunctionListController.welcome(mockedRequest); + assertNull(expectedResult.getViewName()); + } + + @Test + public void getRoleFunctionListTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + List roleFunctionList = new ArrayList(); + Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunctionList); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + roleFunctionListController.getRoleFunctionList(mockedRequest, mockedResponse); + } + + @Test + public void getRoleFunctionListExceptionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenThrow(nullPointerException); + roleFunctionListController.getRoleFunctionList(mockedRequest, mockedResponse); + } + + @Test + public void saveRoleFunctionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}"; + RoleFunction roleFunction = new RoleFunction(); + Mockito.when(roleService.getRoleFunction(user.getOrgUserId(), "Test")).thenReturn(roleFunction); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + roleFunctionListController.saveRoleFunction(mockedRequest, mockedResponse, roleFun); + } + + @Test(expected = java.io.IOException.class) + public void saveRoleFunctionExceptionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}"; + Mockito.when(roleService.getRoleFunction(user.getOrgUserId(), "Test")).thenThrow(nullPointerException); + roleFunctionListController.saveRoleFunction(mockedRequest, mockedResponse, roleFun); + } + + @Test + public void addRoleFunctionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}"; + List roleFunList = new ArrayList<>(); + RoleFunction roleFun1 = new RoleFunction(); + roleFun1.setName("TestRoleFun1"); + roleFun1.setCode("TestRoleFunCode1"); + roleFunList.add(roleFun1); + Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunList); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + roleFunctionListController.addRoleFunction(mockedRequest, mockedResponse, roleFun); + } + + @Test + public void addRoleFunctionExsistsTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}"; + List roleFunList = new ArrayList<>(); + RoleFunction roleFun1 = new RoleFunction(); + roleFun1.setName("Test"); + roleFun1.setCode("Test"); + roleFunList.add(roleFun1); + Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunList); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + roleFunctionListController.addRoleFunction(mockedRequest, mockedResponse, roleFun); + } + + @Test(expected = java.io.IOException.class) + public void addRoleFunctionExceptionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}"; + List roleFunList = new ArrayList<>(); + RoleFunction roleFun1 = new RoleFunction(); + roleFun1.setName("Test"); + roleFun1.setCode("Test"); + roleFunList.add(roleFun1); + Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenThrow(nullPointerException); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + roleFunctionListController.addRoleFunction(mockedRequest, mockedResponse, roleFun); + } + + @Test + public void removeRoleFunctionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}"; + RoleFunction roleFun1 = new RoleFunction(); + roleFun1.setName("Test"); + roleFun1.setCode("Test"); + Mockito.when((roleService.getRoleFunction(user.getOrgUserId(), "Test"))).thenReturn(roleFun1); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + roleFunctionListController.removeRoleFunction(mockedRequest, mockedResponse, roleFun); + + } + + @Test(expected = java.io.IOException.class) + public void removeRoleFunctionExceptionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}"; + RoleFunction roleFun1 = new RoleFunction(); + roleFun1.setName("Test"); + roleFun1.setCode("Test"); + Mockito.when((roleService.getRoleFunction(user.getOrgUserId(), "Test"))).thenThrow(nullPointerException); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + roleFunctionListController.removeRoleFunction(mockedRequest, mockedResponse, roleFun); + } +} diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/RoleListControllerTest.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/RoleListControllerTest.java new file mode 100644 index 00000000..9a576761 --- /dev/null +++ b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/RoleListControllerTest.java @@ -0,0 +1,209 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ + +package org.onap.portalapp.controller.core; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringReader; +import java.io.StringWriter; +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.framework.MockitoTestSuite; +import org.onap.portalsdk.core.domain.Role; +import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.service.RoleService; +import org.onap.portalsdk.core.web.support.UserUtils; +import org.springframework.web.bind.ServletRequestUtils; +import org.springframework.web.servlet.ModelAndView; +import org.onap.portalapp.controller.core.RoleListController; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class RoleListControllerTest { + + @InjectMocks + RoleListController roleListController = new RoleListController(); + + @Mock + RoleService roleService; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + NullPointerException nullPointerException = new NullPointerException(); + + @Mock + UserUtils userUtils = new UserUtils(); + + @Mock + ServletRequestUtils servletRequestUtils; + + @Mock + ObjectMapper mapper = new ObjectMapper(); + + @Test + public void getRoleListTest() throws IOException { + roleListController.setViewName("Test"); + User user = new User(); + user.setOrgUserId("test12"); + List roleList = new ArrayList<>(); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(roleService.getAvailableRoles(user.getOrgUserId())).thenReturn(roleList); + ModelAndView expectedResult = roleListController.getRoleList(mockedRequest); + assertEquals(expectedResult.getViewName(), "Test"); + } + + @Test + public void getRoleListExceptionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(roleService.getAvailableRoles(user.getOrgUserId())).thenThrow(nullPointerException); + ModelAndView expectedResult = roleListController.getRoleList(mockedRequest); + assertNull(expectedResult.getViewName()); + } + + @Test + public void getRolesTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + List roleList = new ArrayList<>(); + Role role = new Role(); + role.setId((long) 1); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(roleService.getAvailableRoles(user.getOrgUserId())).thenReturn(roleList); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + roleListController.getRoles(mockedRequest, mockedResponse); + + } + + @Test + public void getRolesExceptionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(roleService.getAvailableRoles(user.getOrgUserId())).thenThrow(nullPointerException); + roleListController.getRoles(mockedRequest, mockedResponse); + } + + @Test + public void toggleRoleTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String json = "{\"role\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"name\":\"test1\",\"active\":false,\"priority\":\"1\",\"roleFunctions\":[],\"childRoles\":[],\"editUrl\":\"/role.htm?role_id=1\",\"toggleActiveImage\":\"/static/fusion/images/inactive.png\",\"toggleActiveAltText\":\"Click to Activate Role\"},\"childRoles\":[],\"roleFunctions\":[]}"; + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json))); + Role role = new Role(); + role.setActive(true); + Mockito.when(roleService.getRole(user.getOrgUserId(), (long) 1)).thenReturn(role); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + assertNull(roleListController.toggleRole(mockedRequest, mockedResponse)); + } + + @Test + public void toggleRoleExceptionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String json = "{\"role\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"name\":\"test1\",\"active\":false,\"priority\":\"1\",\"roleFunctions\":[],\"childRoles\":[],\"editUrl\":\"/role.htm?role_id=1\",\"toggleActiveImage\":\"/static/fusion/images/inactive.png\",\"toggleActiveAltText\":\"Click to Activate Role\"},\"childRoles\":[],\"roleFunctions\":[]}"; + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json))); + Role role = new Role(); + role.setActive(true); + Mockito.when(roleService.getRole(user.getOrgUserId(), (long) 1)).thenThrow(nullPointerException); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + assertNull(roleListController.toggleRole(mockedRequest, mockedResponse)); + } + + @Test + public void removeRoleTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String json = "{\"role\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"name\":\"test1\",\"active\":false,\"priority\":\"1\",\"roleFunctions\":[],\"childRoles\":[],\"editUrl\":\"/role.htm?role_id=1\",\"toggleActiveImage\":\"/static/fusion/images/inactive.png\",\"toggleActiveAltText\":\"Click to Activate Role\"},\"childRoles\":[],\"roleFunctions\":[]}"; + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json))); + Role role = new Role(); + role.setActive(true); + Mockito.when(roleService.getRole(user.getOrgUserId(), (long) 1)).thenReturn(role); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + List roleList = new ArrayList<>(); + Mockito.when(roleService.getAvailableRoles(user.getOrgUserId())).thenReturn(roleList); + assertNull(roleListController.removeRole(mockedRequest, mockedResponse)); + } + + @Test + public void removeRoleExceptionTest() throws IOException { + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + String json = "{\"role\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"name\":\"test1\",\"active\":false,\"priority\":\"1\",\"roleFunctions\":[],\"childRoles\":[],\"editUrl\":\"/role.htm?role_id=1\",\"toggleActiveImage\":\"/static/fusion/images/inactive.png\",\"toggleActiveAltText\":\"Click to Activate Role\"},\"childRoles\":[],\"roleFunctions\":[]}"; + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json))); + Role role = new Role(); + role.setActive(true); + Mockito.when(roleService.getRole(user.getOrgUserId(), (long) 1)).thenThrow(nullPointerException); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + assertNull(roleListController.removeRole(mockedRequest, mockedResponse)); + } +} diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/SingleSignOnControllerTest.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/SingleSignOnControllerTest.java new file mode 100644 index 00000000..f1d4aa05 --- /dev/null +++ b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/SingleSignOnControllerTest.java @@ -0,0 +1,297 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.controller.core; + +import static org.junit.Assert.assertEquals; + +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.framework.MockitoTestSuite; +import org.onap.portalsdk.core.auth.LoginStrategy; +import org.onap.portalsdk.core.command.LoginBean; +import org.onap.portalsdk.core.domain.RoleFunction; +import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.menu.MenuProperties; +import org.onap.portalsdk.core.onboarding.util.PortalApiConstants; +import org.onap.portalsdk.core.onboarding.util.PortalApiProperties; +import org.onap.portalsdk.core.service.LoginService; +import org.onap.portalsdk.core.service.RoleService; +import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.portalsdk.core.web.support.UserUtils; +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; +import org.springframework.web.util.WebUtils; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ URLDecoder.class, SystemProperties.class, URLEncoder.class, PortalApiProperties.class, WebUtils.class, + UserUtils.class }) +public class SingleSignOnControllerTest { + + @InjectMocks + SingleSignOnController singleSignOnController = new SingleSignOnController(); + + @Mock + RoleService roleService; + + @Mock + LoginService loginService; + + @Mock + LoginStrategy loginStrategy; + + @Mock + URLDecoder uRLDecoder; + + @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 = java.lang.SecurityException.class) + public void singleSignOnLoginExceptionTest() throws Exception { + Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("Test"); + PowerMockito.mockStatic(URLDecoder.class); + PowerMockito.mockStatic(SystemProperties.class); + Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("Test"); + Mockito.when(SystemProperties.containsProperty(SystemProperties.APP_BASE_URL)).thenReturn(true); + Mockito.when(SystemProperties.getProperty(SystemProperties.APP_BASE_URL)).thenReturn("http://TestUrl"); + Mockito.when(SystemProperties.getProperty(SystemProperties.COOKIE_DOMAIN)).thenReturn("te"); + singleSignOnController.singleSignOnLogin(mockedRequest); + + } + + @Test + public void singleSignOnLoginTest() throws Exception { + Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("Test"); + PowerMockito.mockStatic(URLDecoder.class); + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(URLEncoder.class); + PowerMockito.mockStatic(PortalApiProperties.class); + Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("Test"); + Mockito.when(SystemProperties.containsProperty(SystemProperties.APP_BASE_URL)).thenReturn(true); + Mockito.when(SystemProperties.getProperty(SystemProperties.APP_BASE_URL)).thenReturn("http://TestUrl"); + Mockito.when(SystemProperties.getProperty(SystemProperties.COOKIE_DOMAIN)).thenReturn("TestUrl"); + Mockito.when(URLEncoder.encode("http://TestUrl/Test", "UTF-8")).thenReturn("encodeTestUrl"); + Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("uebkey"); + Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL)) + .thenReturn("http://test.com/roles"); + ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest); + assertEquals(expectedResults.getViewName(), + "redirect:http://test.com/process_csp?uebAppKey=uebkey&redirectUrl=http%3A%2F%2FTestUrl%2FTest"); + } + + @Test + public void singleSignOnLoginIfUrlIsNotAppBasedTest() throws Exception { + Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("Test"); + PowerMockito.mockStatic(URLDecoder.class); + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(URLEncoder.class); + PowerMockito.mockStatic(PortalApiProperties.class); + Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("Test"); + Mockito.when(SystemProperties.containsProperty(SystemProperties.APP_BASE_URL)).thenReturn(false); + Mockito.when(SystemProperties.getProperty(SystemProperties.COOKIE_DOMAIN)).thenReturn("test.com"); + Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("uebkey"); + Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL)) + .thenReturn("http://test.com/roles"); + StringBuffer stringBuffer = new StringBuffer("http://test.com/testSDK"); + Mockito.when(mockedRequest.getRequestURL()).thenReturn(stringBuffer); + ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest); + assertEquals(expectedResults.getViewName(), + "redirect:http://test.com/process_csp?uebAppKey=uebkey&redirectUrl=http%3A%2F%2Ftest.com%2FtestSDK"); + } + + @Test + public void singleSignOnTest() throws Exception { + Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("http://Test.com"); + PowerMockito.mockStatic(URLDecoder.class); + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(WebUtils.class); + Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("http://Test.com"); + Mockito.when(mockedRequest.getParameter("redirectToPortal")).thenReturn(null); + Mockito.when(SystemProperties.containsProperty(SystemProperties.APP_BASE_URL)).thenReturn(true); + Mockito.when(SystemProperties.getProperty(SystemProperties.APP_BASE_URL)).thenReturn("http://TestUrl"); + Mockito.when(SystemProperties.getProperty(SystemProperties.COOKIE_DOMAIN)).thenReturn("Test.com"); + Mockito.when(WebUtils.getCookie(mockedRequest, "EPService")).thenReturn(new Cookie("test", "test")); + User user = new User(); + user.setOrgUserId("test12"); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest); + assertEquals(expectedResults.getViewName(), "redirect:http://Test.com"); + + } + + @Test + public void singleSignOnIfUserNullTest() throws Exception { + User user = null; + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("http://Test.com"); + PowerMockito.mockStatic(URLDecoder.class); + PowerMockito.mockStatic(WebUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + + Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("http://Test.com"); + Mockito.when(WebUtils.getCookie(mockedRequest, "EPService")).thenReturn(new Cookie("test", "test")); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("testauth"); + Mockito.when(loginStrategy.getUserId(mockedRequest)).thenReturn("test1234"); + Mockito.when(mockedRequest.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY)).thenReturn("test"); + LoginBean commandBean = new LoginBean(); + commandBean.setUserid("test1234"); + commandBean.setUser(null); + Mockito.when(loginService.findUser(Matchers.any(), Matchers.anyString(), Matchers.anyMap())) + .thenReturn(commandBean); + List roleFunctionList = new ArrayList<>(); + Mockito.when(roleService.getRoleFunctions("test1234")).thenReturn(roleFunctionList); + ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest); + assertEquals(expectedResults.getViewName(), "redirect:null?noUserError=Yes"); + } + + @Test + public void singleSignOnIfUserNotNullTest() throws Exception { + User user = null; + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("http://Test.com"); + PowerMockito.mockStatic(URLDecoder.class); + PowerMockito.mockStatic(WebUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(UserUtils.class); + + Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("http://Test.com"); + Mockito.when(WebUtils.getCookie(mockedRequest, "EPService")).thenReturn(new Cookie("test", "test")); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("testauth"); + Mockito.when(loginStrategy.getUserId(mockedRequest)).thenReturn("test1234"); + Mockito.when(mockedRequest.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY)).thenReturn("test"); + LoginBean commandBean = new LoginBean(); + commandBean.setUserid("test1234"); + User user1 = new User(); + user1.setId((long) 1); + commandBean.setUser(user1); + Mockito.when(loginService.findUser(Matchers.any(), Matchers.anyString(), Matchers.anyMap())) + .thenReturn(commandBean); + List roleFunctionList = new ArrayList<>(); + Mockito.when(roleService.getRoleFunctions("test1234")).thenReturn(roleFunctionList); + ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest); + assertEquals(expectedResults.getViewName(), "redirect:http://Test.com"); + } + + @Test + public void singleSignOnIfUserNotNullAndAuthNullTest() throws Exception { + User user = null; + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("http://Test.com"); + PowerMockito.mockStatic(URLDecoder.class); + PowerMockito.mockStatic(WebUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(UserUtils.class); + + Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("http://Test.com"); + Mockito.when(WebUtils.getCookie(mockedRequest, "EPService")).thenReturn(new Cookie("test", "test")); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn(null); + Mockito.when(loginStrategy.getUserId(mockedRequest)).thenReturn("test1234"); + Mockito.when(mockedRequest.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY)).thenReturn("test"); + LoginBean commandBean = new LoginBean(); + commandBean.setUserid("test1234"); + User user1 = new User(); + user1.setId((long) 1); + commandBean.setUser(user1); + Mockito.when(loginService.findUser(Matchers.any(), Matchers.anyString(), Matchers.anyMap())) + .thenReturn(commandBean); + List roleFunctionList = new ArrayList<>(); + Mockito.when(roleService.getRoleFunctions("test1234")).thenReturn(roleFunctionList); + ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest); + assertEquals(expectedResults.getViewName(), "redirect:http://Test.com"); + } + + @Test + public void singleSignOnIfUserNotNullAndAuthCSPTest() throws Exception { + singleSignOnController.setViewName("test"); + singleSignOnController.setWelcomeView("welcome"); + assertEquals(singleSignOnController.getViewName(), "test"); + assertEquals(singleSignOnController.getWelcomeView(), "welcome"); + User user = null; + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("http://Test.com"); + PowerMockito.mockStatic(URLDecoder.class); + PowerMockito.mockStatic(WebUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(UserUtils.class); + Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("http://Test.com"); + Mockito.when(WebUtils.getCookie(mockedRequest, "EPService")).thenReturn(new Cookie("test", "test")); + Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("CSP"); + Mockito.when(loginStrategy.getUserId(mockedRequest)).thenReturn("test1234"); + Mockito.when(mockedRequest.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY)).thenReturn("test"); + LoginBean commandBean = new LoginBean(); + commandBean.setUserid("test1234"); + User user1 = new User(); + user1.setId((long) 1); + commandBean.setUser(user1); + Mockito.when(loginService.findUser(Matchers.any(), Matchers.anyString(), Matchers.anyMap())) + .thenReturn(commandBean); + List roleFunctionList = new ArrayList<>(); + Mockito.when(roleService.getRoleFunctions("test1234")).thenReturn(roleFunctionList); + ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest); + assertEquals(expectedResults.getViewName(), "redirect:http://Test.com"); + } + +} diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/framework/MockitoTestSuite.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/framework/MockitoTestSuite.java new file mode 100644 index 00000000..baac323e --- /dev/null +++ b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/framework/MockitoTestSuite.java @@ -0,0 +1,89 @@ +/*- + * ============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.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; + } + + } + +} diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/service/ProfileServiceTest.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/service/ProfileServiceTest.java deleted file mode 100644 index 36d54ba6..00000000 --- a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/service/ProfileServiceTest.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * ============LICENSE_START========================================== - * ONAP Portal SDK - * =================================================================== - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * =================================================================== - * - * Unless otherwise specified, all software contained herein is licensed - * under the Apache License, Version 2.0 (the "License"); - * you may not use this software except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Unless otherwise specified, all documentation contained herein is licensed - * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); - * you may not use this documentation except in compliance with the License. - * You may obtain a copy of the License at - * - * https://creativecommons.org/licenses/by/4.0/ - * - * Unless required by applicable law or agreed to in writing, documentation - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================ - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - */ -package org.onap.portalapp.service; - -import java.util.List; - -import org.junit.Assert; -import org.junit.Test; -import org.onap.portalapp.core.MockApplicationContextTestSuite; -import org.onap.portalsdk.core.domain.Profile; -import org.onap.portalsdk.core.domain.User; -import org.onap.portalsdk.core.service.ProfileService; -import org.onap.portalsdk.core.service.UserProfileService; -import org.springframework.beans.factory.annotation.Autowired; - -public class ProfileServiceTest extends MockApplicationContextTestSuite { - - @Autowired - ProfileService service; - - @Autowired - UserProfileService userProfileService; - - @Test - public void testFindAll() throws Exception { - - List profiles = service.findAll(); - Assert.assertTrue(profiles.size() > 0); - } - - @Test - public void testFindAllActive() { - - List users = userProfileService.findAllActive(); - List activeUsers = userProfileService.findAllActive(); - Assert.assertTrue(users.size() - activeUsers.size() >= 0); - } -} diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/workflow/services/WorkflowScheduleServiceTest.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/workflow/services/WorkflowScheduleServiceTest.java deleted file mode 100644 index e26c35ab..00000000 --- a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/workflow/services/WorkflowScheduleServiceTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * ============LICENSE_START========================================== - * ONAP Portal SDK - * =================================================================== - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * =================================================================== - * - * Unless otherwise specified, all software contained herein is licensed - * under the Apache License, Version 2.0 (the "License"); - * you may not use this software except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Unless otherwise specified, all documentation contained herein is licensed - * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); - * you may not use this documentation except in compliance with the License. - * You may obtain a copy of the License at - * - * https://creativecommons.org/licenses/by/4.0/ - * - * Unless required by applicable law or agreed to in writing, documentation - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================ - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - */ -package org.onap.portalapp.workflow.services; - -import java.util.Calendar; - -import org.junit.Assert; -import org.junit.Test; -import org.onap.portalapp.core.MockApplicationContextTestSuite; -import org.onap.portalsdk.workflow.domain.WorkflowSchedule; -import org.onap.portalsdk.workflow.services.WorkflowScheduleService; -import org.springframework.beans.factory.annotation.Autowired; - -public class WorkflowScheduleServiceTest extends MockApplicationContextTestSuite { - - @Autowired - WorkflowScheduleService service; - - @Test - public void testFire() { - - final WorkflowSchedule ws = new WorkflowSchedule(); - ws.setId(999L); - ws.setWorkflowKey("test"); - ws.setCronDetails("0 38 13 3 5 ? 2016"); - final Calendar instance = Calendar.getInstance(); - instance.add(Calendar.YEAR, 3); - ws.setEndDateTime(instance.getTime()); - - ws.setStartDateTime(Calendar.getInstance().getTime()); - try { - service.saveWorkflowSchedule(ws); - Assert.assertTrue(true); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } - - } - -} -- cgit 1.2.3-korg