diff options
Diffstat (limited to 'ecomp-portal-BE-common/src/test/java')
6 files changed, 1050 insertions, 85 deletions
diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/HealthCheckControllerTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/HealthCheckControllerTest.java new file mode 100644 index 00000000..92c0b21e --- /dev/null +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/HealthCheckControllerTest.java @@ -0,0 +1,76 @@ +/*- + * ============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 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.MockitoAnnotations; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +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 HealthCheckControllerTest { + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @InjectMocks + HealthCheckController healthCheckController = new HealthCheckController(); + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + @Test + public void healthCheckFailTest() { + PowerMockito.mockStatic(EcompPortalUtils.class); + healthCheckController.healthCheck(mockedRequest, mockedResponse); + } +} diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/RoleManageControllerTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/RoleManageControllerTest.java index a4970f82..d7f64c1d 100644 --- a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/RoleManageControllerTest.java +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/RoleManageControllerTest.java @@ -38,9 +38,15 @@ package org.onap.portalapp.portal.controller; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.PrintWriter; +import java.io.StringReader; import java.io.StringWriter; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -51,8 +57,10 @@ import java.util.TreeSet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.json.simple.JSONObject; 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; @@ -63,6 +71,7 @@ import org.onap.portalapp.controller.core.RoleFunctionListController; import org.onap.portalapp.controller.core.RoleListController; import org.onap.portalapp.portal.core.MockEPUser; import org.onap.portalapp.portal.domain.CentralV2RoleFunction; +import org.onap.portalapp.portal.domain.CentralizedApp; import org.onap.portalapp.portal.domain.EPApp; import org.onap.portalapp.portal.domain.EPUser; import org.onap.portalapp.portal.ecomp.model.PortalRestResponse; @@ -73,44 +82,54 @@ import org.onap.portalapp.portal.service.EPAppService; import org.onap.portalapp.portal.service.ExternalAccessRolesService; import org.onap.portalapp.portal.transport.CentralV2Role; import org.onap.portalapp.portal.transport.ExternalRequestFieldsValidator; +import org.onap.portalapp.portal.utils.EcompPortalUtils; import org.onap.portalapp.util.EPUserUtils; +import org.onap.portalsdk.core.domain.Role; import org.onap.portalsdk.core.service.AuditService; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.mock.web.DelegatingServletInputStream; import org.springframework.web.servlet.ModelAndView; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ EPUserUtils.class, EcompPortalUtils.class }) public class RoleManageControllerTest { @Mock - RoleController roleController; + RoleController roleController; @Mock - RoleListController roleListController; + RoleListController roleListController; @Mock - RoleFunctionListController roleFunctionListController; - + RoleFunctionListController roleFunctionListController; @Mock ExternalAccessRolesService externalAccessRolesService; @Mock EPUserUtils ePUserUtils = new EPUserUtils(); - + @Mock ExternalAccessRolesService externalAccessRolesService1 = null; - + @InjectMocks - RoleManageController roleManageController = new RoleManageController(); - + RoleManageController roleManageController = new RoleManageController(); + @Mock EPAppService appService; @Mock AuditService auditService; - + @Mock AdminRolesService adminRolesService; - + @Before public void setup() { MockitoAnnotations.initMocks(this); @@ -122,41 +141,46 @@ public class RoleManageControllerTest { HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); NullPointerException nullPointerException = new NullPointerException(); MockEPUser mockUser = new MockEPUser(); - - + @Test - public void removeRoleRoleFunctionTest() throws Exception - { - + public void removeRoleRoleFunctionTest() throws Exception { + ModelAndView modelandView = new ModelAndView("login.htm"); Mockito.when(roleController.removeRoleFunction(mockedRequest, mockedResponse)).thenReturn(modelandView); - ModelAndView expectedModelandView = roleManageController.removeRoleRoleFunction(mockedRequest, mockedResponse); + ModelAndView expectedModelandView = roleManageController.removeRoleRoleFunction(mockedRequest, mockedResponse); assertEquals(expectedModelandView, modelandView); } - + @Test - public void addRoleRoRoleFunctionTest() throws Exception - { + public void addRoleRoRoleFunctionTest() throws Exception { ModelAndView modelandView = new ModelAndView("login.htm"); Mockito.when(roleController.addRoleFunction(mockedRequest, mockedResponse)).thenReturn(modelandView); - ModelAndView expectedModelandView = roleManageController.addRoleRoRoleFunction(mockedRequest, mockedResponse); + ModelAndView expectedModelandView = roleManageController.addRoleRoRoleFunction(mockedRequest, mockedResponse); assertEquals(expectedModelandView, modelandView); } - + @Test - public void removeChildRoleTest() throws Exception - { + public void removeChildRoleTest() throws Exception { ModelAndView modelandView = new ModelAndView("login.htm"); Mockito.when(roleController.removeChildRole(mockedRequest, mockedResponse)).thenReturn(modelandView); - ModelAndView expectedModelandView = roleManageController.removeChildRole(mockedRequest, mockedResponse); + ModelAndView expectedModelandView = roleManageController.removeChildRole(mockedRequest, mockedResponse); assertEquals(expectedModelandView, modelandView); } - - + @Test - public void getRoleIfRoleIdNullTest() throws Exception - { + public void getRoleIfRoleIdNullTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); + Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true); + List<EPApp> apps = new ArrayList<>(); + apps.add(CentralApp()); + Mockito.when(externalAccessRolesService.getApp(CentralApp().getUebKey())).thenReturn(apps); + ResponseEntity<String> result = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(apps.get(0))).thenReturn(result); CentralV2Role answer = new CentralV2Role(); Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(answer); List<CentralV2RoleFunction> finalRoleFunctionList = new ArrayList<>(); @@ -164,29 +188,27 @@ public class RoleManageControllerTest { Mockito.when(externalAccessRolesService.getRolesForApp("test")).thenReturn(null); StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); - Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); List<EPApp> appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); - Mockito.when( externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); - roleManageController.getRole(mockedRequest, mockedResponse, (long)1,null); - -} - + roleManageController.getRole(mockedRequest, mockedResponse, (long) 1, null); + } + @Test(expected = Exception.class) - public void getRoleExceptionTest() throws Exception - { + public void getRoleExceptionTest() throws Exception { Mockito.when(appService.getApp((long) 1)).thenReturn(mockApp()); CentralV2Role answer = new CentralV2Role(); Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(answer); Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenThrow(nullPointerException); - roleManageController.getRole(mockedRequest, mockedResponse, (long)1,null); - -} + roleManageController.getRole(mockedRequest, mockedResponse, (long) 1, null); + + } + @Test - public void getRoleIfRoleIdNotNullTest() throws Exception - { + public void getRoleIfRoleIdNotNullTest() throws Exception { Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); CentralV2Role answer = new CentralV2Role(); Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(answer); @@ -194,11 +216,11 @@ public class RoleManageControllerTest { Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenReturn(finalRoleFunctionList); StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); - Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); CentralV2Role currentRole = new CentralV2Role(); - SortedSet<CentralV2Role> parentRoles = new TreeSet<>(); + SortedSet<CentralV2Role> parentRoles = new TreeSet<>(); CentralV2Role centralV2Role = new CentralV2Role(); - centralV2Role.setName("test"); + centralV2Role.setName("test"); parentRoles.add(centralV2Role); currentRole.setParentRoles(parentRoles); Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(currentRole); @@ -208,13 +230,12 @@ public class RoleManageControllerTest { List<EPApp> appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); - Mockito.when( externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); - roleManageController.getRole(mockedRequest, mockedResponse, (long)1,(long)1); -} - - public EPApp mockApp() - { + roleManageController.getRole(mockedRequest, mockedResponse, (long) 1, (long) 1); + } + + public EPApp mockApp() { EPApp app = new EPApp(); app.setName("Test"); @@ -237,12 +258,20 @@ public class RoleManageControllerTest { app.setUebTopicName("test"); app.setAppType(1); return app; - + } + @Test public void getRolesTest() throws Exception { EPApp app = mockApp(); app.setCentralAuth(true); + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + Mockito.when(appService.getApp((long) 1)).thenReturn(app); + Mockito.when(adminRolesService.isAccountAdminOfApplication(user, app)).thenReturn(true); List<CentralV2Role> answer = new ArrayList<>(); Mockito.when(appService.getApp((long) 1)).thenReturn(app); Mockito.when(externalAccessRolesService.getRolesForApp("test")).thenReturn(answer); @@ -252,7 +281,7 @@ public class RoleManageControllerTest { List<EPApp> appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); - Mockito.when( externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); roleManageController.getRoles(mockedRequest, mockedResponse, (long) 1); } @@ -268,6 +297,12 @@ public class RoleManageControllerTest { @Test public void getRoleFunctionListTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true); Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); List<CentralV2RoleFunction> answer = new ArrayList<>(); Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenReturn(answer); @@ -277,7 +312,7 @@ public class RoleManageControllerTest { List<EPApp> appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); - Mockito.when( externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); roleManageController.getRoleFunctionList(mockedRequest, mockedResponse, (long) 1); } @@ -290,16 +325,27 @@ public class RoleManageControllerTest { @Test public void saveRoleFunctionTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true); Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); Mockito.doNothing().when(roleFunctionListController).saveRoleFunction(mockedRequest, mockedResponse, "test"); CentralV2RoleFunction addNewFunc = new CentralV2RoleFunction(); addNewFunc.setCode("Test"); + addNewFunc.setType("Test"); + addNewFunc.setAction("Test"); addNewFunc.setName("Test"); CentralV2RoleFunction roleFunction = mockCentralRoleFunction(); - Mockito.when(externalAccessRolesService.getRoleFunction("Test", "test")).thenReturn(roleFunction); + roleFunction.setCode("Test|Test|Test"); + Mockito.when(externalAccessRolesService.getRoleFunction("Test|Test|Test", "test")).thenReturn(roleFunction); Mockito.when(externalAccessRolesService.saveCentralRoleFunction(Matchers.anyObject(), Matchers.anyObject())) .thenReturn(true); - EPUser user = mockUser.mockEPUser(); + Mockito.when(EcompPortalUtils.getFunctionCode(roleFunction.getCode())).thenReturn("Test"); + Mockito.when(EcompPortalUtils.getFunctionType(roleFunction.getCode())).thenReturn("Test"); + Mockito.when(EcompPortalUtils.getFunctionAction(roleFunction.getCode())).thenReturn("Test"); Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); List<EPUser> userList = new ArrayList<>(); userList.add(user); @@ -310,9 +356,13 @@ public class RoleManageControllerTest { PrintWriter writer = new PrintWriter(sw); Mockito.when(mockedResponse.getWriter()).thenReturn(writer); ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); - Mockito.when( externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); - roleManageController.saveRoleFunction(mockedRequest, mockedResponse, addNewFunc, (long) 1); + PortalRestResponse<String> actual = roleManageController.saveRoleFunction(mockedRequest, mockedResponse, + addNewFunc, (long) 1); + PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.OK, + "Saved Successfully!", "Success"); + assertEquals(expected, actual); } @Test @@ -337,12 +387,17 @@ public class RoleManageControllerTest { @Test public void removeRoleFunctionTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); EPUser user = mockUser.mockEPUser(); Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); - String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}"; + String roleFun = "{\"name\":\"Test\",\"type\":\"Test\",\"action\":\"Test\", \"code\":\"Test\"}"; CentralV2RoleFunction roleFunction = mockCentralRoleFunction(); - Mockito.when(externalAccessRolesService.getRoleFunction("Test", "test")).thenReturn(roleFunction); + Mockito.when(externalAccessRolesService.getRoleFunction("Test|Test|Test", "test")).thenReturn(roleFunction); StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); Mockito.when(mockedResponse.getWriter()).thenReturn(writer); @@ -351,9 +406,13 @@ public class RoleManageControllerTest { List<EPApp> appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); - Mockito.when( externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); - roleManageController.removeRoleFunction(mockedRequest, mockedResponse, roleFun, (long) 1); + PortalRestResponse<String> actual = roleManageController.removeRoleFunction(mockedRequest, mockedResponse, + roleFun, (long) 1); + PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.OK, + "Deleted Successfully!", "Success"); + assertEquals(expected, actual); } @Test @@ -372,7 +431,7 @@ public class RoleManageControllerTest { List<EPApp> appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); - Mockito.when( externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); roleManageController.removeRoleFunction(mockedRequest, mockedResponse, roleFun, (long) 1); } @@ -388,9 +447,12 @@ public class RoleManageControllerTest { @Test public void syncRolesTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + EPUser user = mockUser.mockEPUser(); EPApp app = mockApp(); app.setId((long) 1); - EPUser user = mockUser.mockEPUser(); Mockito.when(appService.getApp(1l)).thenReturn(app); Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); @@ -398,7 +460,7 @@ public class RoleManageControllerTest { List<EPApp> appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); - Mockito.when( externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); PortalRestResponse<String> actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l); PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>(); @@ -418,9 +480,12 @@ public class RoleManageControllerTest { portalRestResponse.setStatus(PortalRestStatusEnum.ERROR); assertEquals(portalRestResponse, actual); } - + @Test public void syncRolesFunctionsTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); EPApp app = mockApp(); app.setId((long) 1); EPUser user = mockUser.mockEPUser(); @@ -431,7 +496,7 @@ public class RoleManageControllerTest { List<EPApp> appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); - Mockito.when( externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); PortalRestResponse<String> actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l); PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>(); @@ -451,18 +516,20 @@ public class RoleManageControllerTest { portalRestResponse.setStatus(PortalRestStatusEnum.ERROR); assertEquals(portalRestResponse, actual); } - + @Test - public void addeChildRoleTest() throws Exception - { + public void addChildRoleTest() throws Exception { ModelAndView modelandView = new ModelAndView("login.htm"); Mockito.when(roleController.addChildRole(mockedRequest, mockedResponse)).thenReturn(modelandView); - ModelAndView expectedModelandView = roleManageController.addChildRole(mockedRequest, mockedResponse); + ModelAndView expectedModelandView = roleManageController.addChildRole(mockedRequest, mockedResponse); assertEquals(expectedModelandView, modelandView); } - + @Test public void removeRoleTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); List<EPUser> epuserList = new ArrayList<>(); List<EPApp> appList = new ArrayList<>(); appList.add(CentralApp()); @@ -470,6 +537,7 @@ public class RoleManageControllerTest { epuserList.add(user); Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); + Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); Mockito.when(mockedResponse.getWriter()).thenReturn(writer); @@ -479,14 +547,16 @@ public class RoleManageControllerTest { Mockito.when(externalAccessRolesService.getUser(Matchers.anyString())).thenReturn(epuserList); Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); - Mockito.when( externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); Map<String, Object> expedtedResponse = new HashMap<String, Object>(); expedtedResponse.put("restCallStatus", " Unauthorized user"); - Map<String, Object> actualResponse = roleManageController.removeRole(mockedRequest, mockedResponse, (long) 1, (long) 1); + expedtedResponse.put("availableRoles", new ArrayList<>()); + Map<String, Object> actualResponse = roleManageController.removeRole(mockedRequest, mockedResponse, (long) 1, + (long) 1); assertEquals(actualResponse.size(), expedtedResponse.size()); } - + @Test(expected = Exception.class) public void removeRoleExceptionTest() throws Exception { EPUser user = mockUser.mockEPUser(); @@ -497,28 +567,344 @@ public class RoleManageControllerTest { Mockito.when(mockedResponse.getWriter()).thenReturn(writer); roleManageController.removeRole(mockedRequest, mockedResponse, (long) 1, (long) 1); } + + @SuppressWarnings("unchecked") + @Test + public void saveRoleNewTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + List<EPUser> epuserList = new ArrayList<>(); + List<EPApp> appList = new ArrayList<>(); + appList.add(CentralApp()); + EPUser user = mockUser.mockEPUser(); + epuserList.add(user); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); + Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); + Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); + ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + JSONObject roleJson = new JSONObject(); + roleJson.put("name", "test"); + JSONObject roleJson2 = new JSONObject(); + List<JSONObject> childRolesJson = new ArrayList<>(); + List<JSONObject> roleFunctions = new ArrayList<>(); + roleJson2.put("role", roleJson); + roleJson2.put("childRoles", childRolesJson); + roleJson2.put("roleFunctions", roleFunctions); + ObjectMapper mapper = new ObjectMapper(); + JsonNode actualObj = mapper.readTree(roleJson2.toString()); + Mockito.when(mockedRequest.getInputStream()).thenReturn(new DelegatingServletInputStream( + new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8)))); + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString()))); + Mockito.when(mockedRequest.getContentType()).thenReturn("application/json"); + Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8"); + Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(new Role()); + ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, ""); + Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(), Matchers.any())) + .thenReturn(externalRequestFieldsValidator); + Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId()); + final Map<String, Object> expected = new HashMap<>(); + expected.put("role", new CentralV2Role(null, "test")); + expected.put("status", "Success"); + assertEquals(expected, actual); + } + + @SuppressWarnings("unchecked") + @Test + public void saveRoleUpdateTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + List<EPUser> epuserList = new ArrayList<>(); + List<EPApp> appList = new ArrayList<>(); + appList.add(CentralApp()); + EPUser user = mockUser.mockEPUser(); + epuserList.add(user); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); + Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); + Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); + ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + JSONObject roleJson = new JSONObject(); + roleJson.put("id", 1); + roleJson.put("name", "test"); + roleJson.put("active", true); + JSONObject roleJson2 = new JSONObject(); + List<JSONObject> childRolesJson = new ArrayList<>(); + List<JSONObject> roleFunctions = new ArrayList<>(); + JSONObject roleFunction = new JSONObject(); + roleFunction.put("code", "test"); + roleFunction.put("name", "test"); + roleFunction.put("type", "test"); + roleFunction.put("action", "test"); + roleFunctions.add(roleFunction); + roleJson.put("roleFunctions", roleFunctions); + roleJson2.put("role", roleJson); + roleJson2.put("childRoles", childRolesJson); + roleJson2.put("roleFunctions", roleFunctions); + ObjectMapper mapper = new ObjectMapper(); + JsonNode actualObj = mapper.readTree(roleJson2.toString()); + Mockito.when(mockedRequest.getInputStream()).thenReturn(new DelegatingServletInputStream( + new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8)))); + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString()))); + Mockito.when(mockedRequest.getContentType()).thenReturn("application/json"); + Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8"); + Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(new Role()); + ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, ""); + Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(), Matchers.any())) + .thenReturn(externalRequestFieldsValidator); + CentralV2Role cenV2Role = new CentralV2Role(1l, "test1"); + cenV2Role.setActive(true); + Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(cenV2Role); + Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId()); + final Map<String, Object> expected = new HashMap<>(); + expected.put("status", "Success"); + assertEquals(expected.get("status"), actual.get("status")); + } + + @SuppressWarnings("unchecked") + @Test + public void saveRoleInvalidRoleExceptionTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + List<EPUser> epuserList = new ArrayList<>(); + List<EPApp> appList = new ArrayList<>(); + appList.add(CentralApp()); + EPUser user = mockUser.mockEPUser(); + epuserList.add(user); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); + Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); + Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); + ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + JSONObject roleJson = new JSONObject(); + roleJson.put("id", 1); + roleJson.put("name", "test%%"); + roleJson.put("active", true); + JSONObject roleJson2 = new JSONObject(); + List<JSONObject> childRolesJson = new ArrayList<>(); + List<JSONObject> roleFunctions = new ArrayList<>(); + JSONObject roleFunction = new JSONObject(); + roleFunction.put("code", "test"); + roleFunction.put("name", "test"); + roleFunction.put("type", "test"); + roleFunction.put("action", "test"); + roleFunctions.add(roleFunction); + roleJson.put("roleFunctions", roleFunctions); + roleJson2.put("role", roleJson); + roleJson2.put("childRoles", childRolesJson); + roleJson2.put("roleFunctions", roleFunctions); + ObjectMapper mapper = new ObjectMapper(); + JsonNode actualObj = mapper.readTree(roleJson2.toString()); + Mockito.when(mockedRequest.getInputStream()).thenReturn(new DelegatingServletInputStream( + new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8)))); + Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString()))); + Mockito.when(mockedRequest.getContentType()).thenReturn("application/json"); + Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8"); + Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId()); + final Map<String, Object> expected = new HashMap<>(); + expected.put("error", "Invalid role name found for 'test%%'. Any one of the following characters '%,(),=,:,comma, and double quotes' are not allowed"); + assertEquals(expected.get("error"), actual.get("error")); + } + + @Test + public void saveRoleUnauthorizedUserExceptionTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + List<EPUser> epuserList = new ArrayList<>(); + List<EPApp> appList = new ArrayList<>(); + appList.add(CentralApp()); + EPUser user = mockUser.mockEPUser(); + epuserList.add(user); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); + Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(false); + Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); + ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId()); + final Map<String, Object> expected = new HashMap<>(); + expected.put("error", " Unauthorized user"); + assertEquals(expected.get("error"), actual.get("error")); + } - public CentralV2RoleFunction mockCentralRoleFunction() - { + @Test + public void toggleRoleTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + List<EPUser> epuserList = new ArrayList<>(); + List<EPApp> appList = new ArrayList<>(); + appList.add(CentralApp()); + EPUser user = mockUser.mockEPUser(); + epuserList.add(user); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); + Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); + Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); + ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + CentralV2Role role = new CentralV2Role(1l, "test"); + role.setActive(true); + Role currentRole = new Role(); + currentRole.setName("test"); + Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(role); + Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(currentRole); + ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, ""); + Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(),Matchers.any())).thenReturn(externalRequestFieldsValidator); + Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l); + final Map<String, Object> expected = new HashMap<>(); + expected.put("restcallStatus", "Success"); + assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus")); + } + + @Test + public void toggleRoleUnauthorizedUserExceptionTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + List<EPUser> epuserList = new ArrayList<>(); + List<EPApp> appList = new ArrayList<>(); + appList.add(CentralApp()); + EPUser user = mockUser.mockEPUser(); + epuserList.add(user); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); + Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(false); + Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); + ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l); + final Map<String, Object> expected = new HashMap<>(); + expected.put("restcallStatus", " Unauthorized user"); + assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus")); + } + + @Test(expected = NullPointerException.class) + public void toggleRoleExceptionTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + List<EPUser> epuserList = new ArrayList<>(); + List<EPApp> appList = new ArrayList<>(); + appList.add(CentralApp()); + EPUser user = mockUser.mockEPUser(); + epuserList.add(user); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.doThrow(new NullPointerException()).when(appService).getApp((long) 1); + roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l); + } + + @Test + public void toggleRoleFailedTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + List<EPUser> epuserList = new ArrayList<>(); + List<EPApp> appList = new ArrayList<>(); + appList.add(CentralApp()); + EPUser user = mockUser.mockEPUser(); + epuserList.add(user); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); + Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); + Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); + ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + CentralV2Role role = new CentralV2Role(1l, "test"); + role.setActive(true); + Role currentRole = new Role(); + currentRole.setName("test"); + Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(role); + Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(currentRole); + ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(false, ""); + Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(),Matchers.any())).thenReturn(externalRequestFieldsValidator); + Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l); + final Map<String, Object> expected = new HashMap<>(); + expected.put("restcallStatus", "Toggle Role Failed"); + assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus")); + } + + @Test + public void getAvailableChildRolesTest() throws Exception { + List<CentralV2Role> centralV2Roles = new ArrayList<>(); + CentralV2Role centralV2Role = new CentralV2Role(); + centralV2Role.setName("test"); + centralV2Role.setId(1l); + CentralV2Role centralV2Role2 = new CentralV2Role(); + centralV2Role2.setName("test"); + centralV2Role2.setId(1l); + centralV2Roles.add(centralV2Role); + centralV2Roles.add(centralV2Role2); + SortedSet<CentralV2RoleFunction> roleFuns = new TreeSet<>(); + CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test", "test"); + roleFuns.add(centralV2RoleFunction); + CentralV2Role currentRole = new CentralV2Role(); + currentRole.setName("test"); + currentRole.setId(1l); + currentRole.setRoleFunctions(roleFuns); + Mockito.when(externalAccessRolesService.getRolesForApp(CentralApp().getUebKey())).thenReturn(centralV2Roles); + Mockito.when(externalAccessRolesService.getRoleInfo(1l, CentralApp().getUebKey())).thenReturn(currentRole); + List<CentralV2Role> actual = roleManageController.getAvailableChildRoles(CentralApp().getUebKey(), 1l); + assertEquals(new ArrayList<>().size(), actual.size()); + } + + @Test + public void getCentralizedAppRolesTest() throws IOException { + PowerMockito.mockStatic(EPUserUtils.class); + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + List<CentralizedApp> cenApps = new ArrayList<>(); + CentralizedApp centralizedApp = new CentralizedApp(); + centralizedApp.setAppId(1); + centralizedApp.setAppName("Test"); + cenApps.add(centralizedApp); + Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); + Mockito.when(externalAccessRolesService.getCentralizedAppsOfUser(Matchers.anyString())).thenReturn(cenApps); + List<CentralizedApp> actual = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, user.getOrgUserId()); + assertEquals(cenApps.size(), actual.size()); + } + + @Test + public void getCentralizedAppRolesExceptionTest() throws IOException { + PowerMockito.mockStatic(EPUserUtils.class); + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + List<CentralizedApp> cenApps = new ArrayList<>(); + CentralizedApp centralizedApp = new CentralizedApp(); + centralizedApp.setAppId(1); + centralizedApp.setAppName("Test"); + cenApps.add(centralizedApp); + Mockito.when(adminRolesService.isAccountAdmin(Matchers.anyObject())).thenReturn(false); + List<CentralizedApp> actual = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, user.getOrgUserId()); + assertNull(actual); + } + + public CentralV2RoleFunction mockCentralRoleFunction() { CentralV2RoleFunction roleFunction = new CentralV2RoleFunction(); roleFunction.setCode("Test"); roleFunction.setName("Test"); roleFunction.setAppId((long) 1); return roleFunction; } - - public EPApp CentralApp() - { - EPApp app = mockApp(); + + public EPApp CentralApp() { + EPApp app = mockApp(); app.setCentralAuth(true); app.setNameSpace("com.test"); return app; } - - public EPApp NonCentralApp() - { - EPApp app = mockApp(); + + public EPApp NonCentralApp() { + EPApp app = mockApp(); app.setCentralAuth(false); return app; } + } diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/logging/aop/EPEELFLoggerAdviceTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/logging/aop/EPEELFLoggerAdviceTest.java new file mode 100644 index 00000000..5f8319cf --- /dev/null +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/logging/aop/EPEELFLoggerAdviceTest.java @@ -0,0 +1,158 @@ +/*- + * ============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.logging.aop; + +import static org.junit.Assert.assertNotEquals; + +import java.util.ArrayList; +import java.util.List; + +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.core.MockEPUser; +import org.onap.portalapp.portal.domain.EPUser; +import org.onap.portalapp.portal.utils.EPCommonSystemProperties; +import org.onap.portalapp.portal.utils.EcompPortalUtils; +import org.onap.portalapp.util.EPUserUtils; +import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.portalsdk.core.util.SystemProperties.SecurityEventTypeEnum; +import org.onap.portalsdk.core.web.support.AppUtils; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpSession; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, + EPCommonSystemProperties.class }) +public class EPEELFLoggerAdviceTest { + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Mock + SecurityEventTypeEnum securityEventTypeEnum; + + @InjectMocks + EPEELFLoggerAdvice epEELFLoggerAdvice = new EPEELFLoggerAdvice(); + + @Test + @SuppressWarnings("static-access") + public void getCurrentDateTimeUTCTest(){ + String actual = epEELFLoggerAdvice.getCurrentDateTimeUTC(); + assertNotEquals("", actual); + } + + MockEPUser mockUser = new MockEPUser(); + + @Test + public void loadServletRequestBasedDefaultsTest(){ + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(AppUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + EPUser user = mockUser.mockEPUser(); + MockHttpSession session = new MockHttpSession(); + session.setAttribute("user_attribute_name", user); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setServletPath("http:test.com"); + request.setSession(session); + request.addHeader("user-agent", "Mozilla/5.0"); + Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user_attribute_name"); + epEELFLoggerAdvice.loadServletRequestBasedDefaults(request, securityEventTypeEnum); + } + + @Test + public void afterTest(){ + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(AppUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + EPUser user = mockUser.mockEPUser(); + MockHttpSession session = new MockHttpSession(); + session.setAttribute("user_attribute_name", user); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setServletPath("http:test.com"); + request.setSession(session); + request.addHeader("user-agent", "Mozilla/5.0"); + Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user_attribute_name"); + List<Object> args = new ArrayList<>(); + args.add("testClassName"); + args.add("testMethodName"); + Object[] passOnArgs = args.toArray(new Object[args.size()]); + List<Object> args2 = new ArrayList<>(); + args2.add(request); + Object[] httpArgs = args2.toArray(new Object[args2.size()]); + epEELFLoggerAdvice.after(securityEventTypeEnum, "200", "200", httpArgs, null, passOnArgs); + } + + @Test + public void BeforeTest(){ + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(AppUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + EPUser user = mockUser.mockEPUser(); + MockHttpSession session = new MockHttpSession(); + session.setAttribute("user_attribute_name", user); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setServletPath("http:test.com"); + request.setSession(session); + request.addHeader("user-agent", "Mozilla/5.0"); + Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user_attribute_name"); + List<Object> args = new ArrayList<>(); + args.add("testClassName"); + args.add("testMethodName"); + Object[] passOnArgs = args.toArray(new Object[args.size()]); + List<Object> args2 = new ArrayList<>(); + args2.add(request); + Object[] httpArgs = args2.toArray(new Object[args2.size()]); + epEELFLoggerAdvice.before(securityEventTypeEnum, httpArgs, passOnArgs); + } +} diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/SearchServiceImplTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/SearchServiceImplTest.java new file mode 100644 index 00000000..61244ddd --- /dev/null +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/SearchServiceImplTest.java @@ -0,0 +1,141 @@ +/*- + * ============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.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; +import java.util.List; + +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.portal.core.MockEPUser; +import org.onap.portalapp.portal.domain.EPUser; +import org.onap.portalapp.portal.utils.EPCommonSystemProperties; +import org.onap.portalapp.portal.utils.EcompPortalUtils; +import org.onap.portalapp.portal.utils.PortalConstants; +import org.onap.portalsdk.core.command.support.SearchResult; +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; + + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ EcompPortalUtils.class, PortalConstants.class, SystemProperties.class, + EPCommonSystemProperties.class }) +public class SearchServiceImplTest { + + @InjectMocks + SearchServiceImpl searchServiceImpl = new SearchServiceImpl(); + + @Mock + EPLdapService epLdapService; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockEPUser mockUser = new MockEPUser(); + + @SuppressWarnings("unchecked") + @Test + public void searchUsersInPhoneBookTest() throws Exception { + PowerMockito.mockStatic(EcompPortalUtils.class); + List<String> tokens = new ArrayList<>(); + tokens.add("ts1234"); + tokens.add("TestName"); + EPUser user = new EPUser(); + user.setOrgUserId("ts1234"); + user.setFirstName("TestName"); + Mockito.when(EcompPortalUtils.parsingByRegularExpression(Matchers.anyString(), Matchers.anyString())).thenReturn(tokens); + SearchResult searchResult = new SearchResult(); + searchResult.add(user); + Mockito.when(epLdapService.searchPost(Matchers.any(EPUser.class), Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt())).thenReturn(searchResult); + String actual = searchServiceImpl.searchUsersInPhoneBook("ts1234"); + assertNotEquals("[]", actual); + } + + @SuppressWarnings("unchecked") + @Test + public void searchUsersInPhoneBookFirtAndLastNameTest() throws Exception { + PowerMockito.mockStatic(EcompPortalUtils.class); + List<String> tokens = new ArrayList<>(); + tokens.add("ts1234"); + tokens.add("TestFirstName"); + tokens.add("TestLastName"); + EPUser user = new EPUser(); + user.setOrgUserId("ts1234"); + user.setFirstName("TestFirstName"); + user.setLastName("TestLastName"); + Mockito.when(EcompPortalUtils.parsingByRegularExpression(Matchers.anyString(), Matchers.anyString())).thenReturn(tokens); + SearchResult searchResult = new SearchResult(); + searchResult.add(user); + Mockito.when(epLdapService.searchPost(Matchers.any(EPUser.class), Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt())).thenReturn(searchResult); + String actual = searchServiceImpl.searchUsersInPhoneBook("ts1234"); + assertNotEquals("[]", actual); + } + + @SuppressWarnings("unchecked") + @Test + public void searchUserByUserIdTest() throws Exception { + EPUser user = new EPUser(); + user.setOrgUserId("ts1234"); + SearchResult searchResult = new SearchResult(); + searchResult.add(user); + Mockito.when(epLdapService.searchPost(Matchers.any(EPUser.class), Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt())).thenReturn(searchResult); + EPUser actual = searchServiceImpl.searchUserByUserId("ts1234"); + assertEquals(user, actual); + } + + @Test + public void searchUserByUserIdExceptionTest() throws Exception { + Mockito.doThrow(new NullPointerException()).when(epLdapService).searchPost(Matchers.any(EPUser.class), Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt()); + EPUser actual = searchServiceImpl.searchUserByUserId("ts1234"); + assertNull(actual); + } +} diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/ueb/EPUebHelperTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/ueb/EPUebHelperTest.java new file mode 100644 index 00000000..226d9709 --- /dev/null +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/ueb/EPUebHelperTest.java @@ -0,0 +1,157 @@ +/*- + * ============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.ueb; + +import static org.junit.Assert.assertFalse; + +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +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.EcompApp; +import org.onap.portalapp.portal.service.EPAppCommonServiceImpl; +import org.onap.portalapp.portal.utils.PortalConstants; +import org.onap.portalsdk.core.onboarding.util.PortalApiConstants; +import org.onap.portalsdk.core.onboarding.util.PortalApiProperties; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import com.att.nsa.apiClient.http.HttpClient; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({PortalApiProperties.class, HttpClient.class, URL.class, PortalConstants.class}) +public class EPUebHelperTest { + + @Mock + EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl(); + + @Mock + SessionFactory sessionFactory; + + @Mock + Session session; + + @Mock + Transaction transaction; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + Mockito.when(sessionFactory.openSession()).thenReturn(session); + } + + @InjectMocks + EPUebHelper epUebHelper = new EPUebHelper(); + + @Test + public void refreshPublisherForPortalListTest() { + List<EcompApp> ecompApps = new ArrayList<>(); + EcompApp ecompApp = new EcompApp(); + ecompApp.setCentralAuth(true); + ecompApp.setId(1l); + ecompApp.setName("test"); + ecompApp.setEnabled(true); + ecompApp.setUebTopicName("ECOMP-PORTAL-INBOX"); + ecompApps.add(ecompApp); + Mockito.when(epAppCommonServiceImpl.getEcompAppAppsFullList()).thenReturn(ecompApps); + epUebHelper.refreshPublisherList(); + } + + @Test + public void refreshPublisherForPartnersListTest() { + PowerMockito.mockStatic(PortalConstants.class); + PowerMockito.mockStatic(PortalApiProperties.class); + List<EcompApp> ecompApps = new ArrayList<>(); + EcompApp ecompApp = new EcompApp(); + ecompApp.setCentralAuth(true); + ecompApp.setId(2l); + ecompApp.setName("test"); + ecompApp.setEnabled(true); + ecompApp.setUebTopicName("Test"); + ecompApps.add(ecompApp); + Mockito.when(epAppCommonServiceImpl.getEcompAppAppsFullList()).thenReturn(ecompApps); + Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME)).thenReturn("ecomp_portal_inbox_name"); + Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_INBOUND_MAILBOX_NAME)).thenReturn("ueb_app_mailbox_name"); + Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("ueb_app_key"); + Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_SECRET)).thenReturn("ueb_app_secret"); + Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_CONSUMER_GROUP_NAME)).thenReturn("ueb_app_consumer_group_name"); + Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_URL_LIST)).thenReturn("ueb_url_list"); + epUebHelper.refreshPublisherList(); + } + + @Test + public void refreshPublisherForExceptionListTest() { + List<EcompApp> ecompApps = new ArrayList<>(); + EcompApp ecompApp = new EcompApp(); + ecompApp.setCentralAuth(true); + ecompApp.setId(2l); + ecompApp.setName("test"); + ecompApp.setEnabled(true); + ecompApp.setUebTopicName("Test"); + ecompApps.add(ecompApp); + Mockito.doThrow(new NullPointerException()).when(epAppCommonServiceImpl).getEcompAppAppsFullList(); + epUebHelper.refreshPublisherList(); + } + + @Test + public void checkAvailabilityConectionRefusedTest() throws Exception { + PowerMockito.mockStatic(PortalConstants.class); + PowerMockito.mockStatic(PortalApiProperties.class); + Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_URL_LIST)).thenReturn("localhost"); + URL u = PowerMockito.mock(URL.class); + String url = "http://localhost:3904/topics/null"; + PowerMockito.whenNew(URL.class).withArguments(url).thenReturn(u); + HttpURLConnection huc = PowerMockito.mock(HttpURLConnection.class); + PowerMockito.when(u.openConnection()).thenReturn(huc); + PowerMockito.when(huc.getResponseCode()).thenReturn(200); + boolean actual = epUebHelper.checkAvailability(); + assertFalse(actual); + } +} diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/utils/EcompPortalUtilsTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/utils/EcompPortalUtilsTest.java index efc1dab1..2af02c32 100644 --- a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/utils/EcompPortalUtilsTest.java +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/utils/EcompPortalUtilsTest.java @@ -39,11 +39,26 @@ package org.onap.portalapp.portal.utils; 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.Test; -import org.onap.portalapp.portal.utils.EcompPortalUtils; +import org.onap.portalapp.portal.core.MockEPUser; +import org.onap.portalapp.portal.domain.EPUser; +import org.onap.portalapp.portal.framework.MockitoTestSuite; public class EcompPortalUtilsTest { + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + MockEPUser mockUser = new MockEPUser(); + + @Test public void legitimateAttuidSuccessTest() { assertEquals(true, EcompPortalUtils.legitimateUserId("mm016f")); @@ -53,4 +68,36 @@ public class EcompPortalUtilsTest { public void legitimateAttuidFailureTest() { assertEquals(false, EcompPortalUtils.legitimateUserId("1#@23456")); } + + + @Test + public void parsingByRegularExpressionTest() { + List<String> expected = new ArrayList<>(); + expected.add("test"); + expected.add("123"); + assertEquals(expected,EcompPortalUtils.parsingByRegularExpression("test 123"," ")); + } + + @Test + public void jsonErrorMessageResponseTest() { + String expected = "{\"error\":{\"code\":" + 200 + "," + "\"message\":\"" + "test" + "\"}}"; + assertEquals(expected,EcompPortalUtils.jsonErrorMessageResponse(200, "test")); + } + + @Test + public void jsonMessageResponseTest() { + String expected = "{\"message\":\"test\"}"; + assertEquals(expected,EcompPortalUtils.jsonMessageResponse("test")); + } + + @Test + public void logAndSerializeObjectTest() { + EcompPortalUtils.logAndSerializeObject("test", "test", EcompPortalUtils.class); + } + + @Test + public void setBadPermissionsForEmptyUserTest() { + EcompPortalUtils.setBadPermissions(new EPUser(), mockedResponse, "test"); + } + } |