X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ecomp-portal-BE-common%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fportalapp%2Fportal%2Fcontroller%2FRoleManageControllerTest.java;h=bbc842a64ebd1f8a4d9b730af0f054a0463ae6be;hb=HEAD;hp=e63ea438c37b9235857410552105d6460fe6b55d;hpb=21a8761f684745bb300e075c7e98ad897ace9eed;p=portal.git 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 e63ea438..bbc842a6 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 @@ -4,6 +4,8 @@ * =================================================================== * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * =================================================================== + * Modifications Copyright (c) 2019 Samsung + * =================================================================== * * Unless otherwise specified, all software contained herein is licensed * under the Apache License, Version 2.0 (the "License"); @@ -33,26 +35,31 @@ * * ============LICENSE_END============================================ * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * */ package org.onap.portalapp.portal.controller; import static org.junit.Assert.assertEquals; - +import static org.junit.Assert.assertNull; +import java.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; import java.util.Map; import java.util.SortedSet; 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,54 +70,66 @@ 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.domain.EpAppType; import org.onap.portalapp.portal.ecomp.model.PortalRestResponse; import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum; +import org.onap.portalapp.portal.ecomp.model.UploadRoleFunctionExtSystem; import org.onap.portalapp.portal.framework.MockitoTestSuite; import org.onap.portalapp.portal.service.AdminRolesService; import org.onap.portalapp.portal.service.EPAppService; import org.onap.portalapp.portal.service.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,83 +141,90 @@ public class RoleManageControllerTest { HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); NullPointerException nullPointerException = new NullPointerException(); MockEPUser mockUser = new MockEPUser(); - - + + private DelegatingServletInputStream dsi; + @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 - { - Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); - CentralV2Role answer = new CentralV2Role(); + public void getRoleIfRoleIdNullTest() throws Exception { + EPApp app = mockApp(); + app.setRolesInAAF(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 apps = new ArrayList<>(); + apps.add(app); + Mockito.when(externalAccessRolesService.getApp(app.getUebKey())).thenReturn(apps); + ResponseEntity result = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(apps.get(0))).thenReturn(result); + CentralV2Role answer = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role(); Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(answer); List finalRoleFunctionList = new ArrayList<>(); Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenReturn(finalRoleFunctionList); 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 appList = new ArrayList<>(); - appList.add(CentralApp()); + appList.add(app); ResponseEntity 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(); + CentralV2Role answer = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role(); 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(); + CentralV2Role answer = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role(); Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(answer); List finalRoleFunctionList = new ArrayList<>(); Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenReturn(finalRoleFunctionList); StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); - Mockito.when(mockedResponse.getWriter()).thenReturn(writer); - CentralV2Role currentRole = new CentralV2Role(); - SortedSet parentRoles = new TreeSet<>(); - CentralV2Role centralV2Role = new CentralV2Role(); - centralV2Role.setName("test"); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + CentralV2Role currentRole = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role(); + SortedSet parentRoles = new TreeSet<>(); + CentralV2Role centralV2Role = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role(); + centralV2Role.setName("test"); parentRoles.add(centralV2Role); currentRole.setParentRoles(parentRoles); Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(currentRole); @@ -208,41 +234,51 @@ public class RoleManageControllerTest { List appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity 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"); app.setImageUrl("test"); - app.setDescription("test"); - app.setNotes("test"); - app.setUrl("test"); + app.setAppDescription("test"); + app.setAppNotes("test"); + app.setLandingPage("test"); app.setId((long) 1); app.setAppRestEndpoint("test"); - app.setAlternateUrl("test"); + app.setAlternateLandingPage("test"); app.setName("test"); app.setMlAppName("test"); app.setMlAppAdminId("test"); - app.setUsername("test"); - app.setAppPassword("test"); + app.setAppBasicAuthUsername("test"); + app.setAppBasicAuthPassword("test"); app.setOpen(false); app.setEnabled(false); app.setUebKey("test"); app.setUebSecret("test"); app.setUebTopicName("test"); - app.setAppType(1); + app.setAppType(EpAppType.GUI); + app.setModeOfIntegration("test"); + app.setAppAck(false); + app.setUsesCadi(false); return app; - + } + @Test public void getRolesTest() throws Exception { EPApp app = mockApp(); - app.setCentralAuth(true); + app.setRolesInAAF(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 answer = new ArrayList<>(); Mockito.when(appService.getApp((long) 1)).thenReturn(app); Mockito.when(externalAccessRolesService.getRolesForApp("test")).thenReturn(answer); @@ -252,7 +288,7 @@ public class RoleManageControllerTest { List appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity 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,7 +304,16 @@ public class RoleManageControllerTest { @Test public void getRoleFunctionListTest() throws Exception { - Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); + EPApp app = mockApp(); + app.setRolesInAAF(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(adminRolesService.isAccountAdminOfApplication(user, app)).thenReturn(true); + + Mockito.when(appService.getApp((long) 1)).thenReturn(app); List answer = new ArrayList<>(); Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenReturn(answer); StringWriter sw = new StringWriter(); @@ -277,7 +322,7 @@ public class RoleManageControllerTest { List appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity 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,17 +335,72 @@ public class RoleManageControllerTest { @Test public void saveRoleFunctionTest() throws Exception { - Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); + EPApp app = mockApp(); + app.setRolesInAAF(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(adminRolesService.isAccountAdminOfApplication(user, app)).thenReturn(true); + Mockito.when(appService.getApp((long) 1)).thenReturn(app); 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); + 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 userList = new ArrayList<>(); + userList.add(user); + List appList = new ArrayList<>(); + appList.add(app); + Mockito.when(externalAccessRolesService.getUser("guestT")).thenReturn(userList); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + ResponseEntity response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); + PortalRestResponse actual = roleManageController.saveRoleFunction(mockedRequest, mockedResponse, + addNewFunc, (long) 1); + PortalRestResponse expected = new PortalRestResponse(PortalRestStatusEnum.OK, + "Saved Successfully!", "Success"); + assertEquals(expected, actual); + } + + @Test + public void saveRoleFunctionXSSTest() 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("“>"); + addNewFunc.setType("Test"); + addNewFunc.setAction("Test"); + addNewFunc.setName("Test"); + CentralV2RoleFunction roleFunction = mockCentralRoleFunction(); + 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); + 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 userList = new ArrayList<>(); userList.add(user); List appList = new ArrayList<>(); @@ -310,9 +410,13 @@ public class RoleManageControllerTest { PrintWriter writer = new PrintWriter(sw); Mockito.when(mockedResponse.getWriter()).thenReturn(writer); ResponseEntity 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 actual = roleManageController.saveRoleFunction(mockedRequest, mockedResponse, + addNewFunc, (long) 1); + PortalRestResponse expected = new PortalRestResponse(PortalRestStatusEnum.ERROR, + "Data is not valid", "ERROR"); + assertEquals(expected, actual); } @Test @@ -332,29 +436,69 @@ public class RoleManageControllerTest { addNewFunc.setCode("Test"); addNewFunc.setName("Test"); Mockito.when(appService.getApp((long) 1)).thenReturn(NonCentralApp()); - String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}"; roleManageController.saveRoleFunction(mockedRequest, mockedResponse, addNewFunc, (long) 1); } @Test public void removeRoleFunctionTest() throws Exception { + EPApp app = mockApp(); + app.setRolesInAAF(true); + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); EPUser user = mockUser.mockEPUser(); Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); - Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp()); - String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}"; + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + Mockito.when(adminRolesService.isAccountAdminOfApplication(user, app)).thenReturn(true); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(appService.getApp((long) 1)).thenReturn(app); + 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); Mockito.when(externalAccessRolesService.deleteCentralRoleFunction(Matchers.anyString(), Matchers.anyObject())) .thenReturn(true); List appList = new ArrayList<>(); + appList.add(app); + ResponseEntity response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); + PortalRestResponse actual = roleManageController.removeRoleFunction(mockedRequest, mockedResponse, + roleFun, (long) 1); + PortalRestResponse expected = new PortalRestResponse(PortalRestStatusEnum.OK, + "Deleted Successfully!", "Success"); + assertEquals(expected, actual); + } + + @Test + public void removeRoleFunctionXSSTest() 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 = ""; + CentralV2RoleFunction roleFunction = mockCentralRoleFunction(); + 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); + Mockito.when(externalAccessRolesService.deleteCentralRoleFunction(Matchers.anyString(), Matchers.anyObject())) + .thenReturn(true); + List appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity 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 actual = roleManageController.removeRoleFunction(mockedRequest, mockedResponse, + roleFun, (long) 1); + PortalRestResponse expected = new PortalRestResponse(PortalRestStatusEnum.ERROR, + "Data is not valid", "ERROR"); + assertEquals(expected, actual); } @Test @@ -373,7 +517,7 @@ public class RoleManageControllerTest { List appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity 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); } @@ -381,7 +525,7 @@ public class RoleManageControllerTest { @Test public void removeRoleFunctionIfAppNotCentralizedTest() throws Exception { EPApp app = mockApp(); - app.setCentralAuth(false); + app.setRolesInAAF(false); Mockito.when(appService.getApp((long) 1)).thenReturn(app); String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}"; roleManageController.removeRoleFunction(mockedRequest, mockedResponse, roleFun, (long) 1); @@ -389,9 +533,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); @@ -399,7 +546,7 @@ public class RoleManageControllerTest { List appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity 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 actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l); PortalRestResponse portalRestResponse = new PortalRestResponse<>(); @@ -411,6 +558,9 @@ public class RoleManageControllerTest { @Test public void syncRolesException() throws Exception { + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(adminRolesService.isAccountAdminOfApplication(user, null)).thenReturn(true); Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException); PortalRestResponse actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l); PortalRestResponse portalRestResponse = new PortalRestResponse<>(); @@ -419,9 +569,24 @@ public class RoleManageControllerTest { portalRestResponse.setStatus(PortalRestStatusEnum.ERROR); assertEquals(portalRestResponse, actual); } - + + @Test + public void syncRolesUserNullException() throws Exception { + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null); + Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException); + PortalRestResponse actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l); + PortalRestResponse portalRestResponse = new PortalRestResponse<>(); + portalRestResponse.setMessage("Unauthorized User"); + portalRestResponse.setResponse("Failure"); + 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(); @@ -432,7 +597,7 @@ public class RoleManageControllerTest { List appList = new ArrayList<>(); appList.add(CentralApp()); ResponseEntity 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 actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l); PortalRestResponse portalRestResponse = new PortalRestResponse<>(); @@ -444,7 +609,10 @@ public class RoleManageControllerTest { @Test public void syncRolesFunctionsException() throws Exception { - Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException); + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + Mockito.when(adminRolesService.isAccountAdminOfApplication(user, null)).thenReturn(true); + Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException); PortalRestResponse actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l); PortalRestResponse portalRestResponse = new PortalRestResponse<>(); portalRestResponse.setMessage(null); @@ -452,18 +620,32 @@ public class RoleManageControllerTest { portalRestResponse.setStatus(PortalRestStatusEnum.ERROR); assertEquals(portalRestResponse, actual); } - + + @Test + public void syncRolesFunctionsUserNullException() throws Exception { + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null); + Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException); + PortalRestResponse actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l); + PortalRestResponse portalRestResponse = new PortalRestResponse<>(); + portalRestResponse.setMessage("Unauthorized User"); + portalRestResponse.setResponse("Failure"); + 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 epuserList = new ArrayList<>(); List appList = new ArrayList<>(); appList.add(CentralApp()); @@ -471,6 +653,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); @@ -480,14 +663,16 @@ public class RoleManageControllerTest { Mockito.when(externalAccessRolesService.getUser(Matchers.anyString())).thenReturn(epuserList); Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList); ResponseEntity response = new ResponseEntity<>(HttpStatus.OK); - Mockito.when( externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); Map expedtedResponse = new HashMap(); expedtedResponse.put("restCallStatus", " Unauthorized user"); - Map actualResponse = roleManageController.removeRole(mockedRequest, mockedResponse, (long) 1, (long) 1); + expedtedResponse.put("availableRoles", new ArrayList<>()); + Map 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(); @@ -498,28 +683,366 @@ 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 epuserList = new ArrayList<>(); + List 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 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 childRolesJson = new ArrayList<>(); + List 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()); + dsi = new DelegatingServletInputStream( + new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8))); + Mockito.when(mockedRequest.getInputStream()).thenReturn(dsi); + 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 actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId()); + final Map expected = new HashMap<>(); + expected.put("role", + new CentralV2Role.CentralV2RoleBuilder().setId(null).setName("test").createCentralV2Role()); + 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 epuserList = new ArrayList<>(); + List 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 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 childRolesJson = new ArrayList<>(); + List 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()); + dsi = new DelegatingServletInputStream( + new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8))); + Mockito.when(mockedRequest.getInputStream()).thenReturn(dsi); + 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.CentralV2RoleBuilder().setId(1l).setName("test1").createCentralV2Role(); + cenV2Role.setActive(true); + Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(cenV2Role); + Map actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId()); + final Map expected = new HashMap<>(); + expected.put("status", "Success"); + assertEquals(expected.get("status"), actual.get("status")); + } - public CentralV2RoleFunction mockCentralRoleFunction() - { + @SuppressWarnings("unchecked") + @Test + public void saveRoleInvalidRoleExceptionTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + List epuserList = new ArrayList<>(); + List 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 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 childRolesJson = new ArrayList<>(); + List 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()); + dsi = new DelegatingServletInputStream( + new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8))); + Mockito.when(mockedRequest.getInputStream()).thenReturn(dsi); + 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 actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId()); + final Map 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 epuserList = new ArrayList<>(); + List 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 response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Map actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId()); + final Map expected = new HashMap<>(); + expected.put("error", " Unauthorized user"); + assertEquals(expected.get("error"), actual.get("error")); + } + + @Test + public void toggleRoleTest() throws Exception { + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + List epuserList = new ArrayList<>(); + List 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 response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + CentralV2Role role = new CentralV2Role.CentralV2RoleBuilder().setId(1l).setName("test").createCentralV2Role(); + 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 actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l); + final Map 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 epuserList = new ArrayList<>(); + List 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 response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + Map actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l); + final Map 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 epuserList = new ArrayList<>(); + List 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 epuserList = new ArrayList<>(); + List 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 response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response); + CentralV2Role role = new CentralV2Role.CentralV2RoleBuilder().setId(1l).setName("test").createCentralV2Role(); + 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 actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l); + final Map expected = new HashMap<>(); + expected.put("restcallStatus", "Toggle Role Failed"); + assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus")); + } + + @Test + public void getAvailableChildRolesTest() throws Exception { + List 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 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 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 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 actual = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, user.getOrgUserId()); + assertEquals(cenApps.size(), actual.size()); + } + + @Test + public void getCentralizedAppRolesXSSTest() throws IOException { + String id = (""); + List actual = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, id); + assertNull(actual); + } + + @Test + public void getCentralizedAppRolesExceptionTest() throws IOException { + PowerMockito.mockStatic(EPUserUtils.class); + EPUser user = mockUser.mockEPUser(); + Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user); + List 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 actual = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, user.getOrgUserId()); + assertNull(actual); + } + + @Test + public void bulkUploadRoleFuncUserNullTest() { + UploadRoleFunctionExtSystem data = Mockito.mock(UploadRoleFunctionExtSystem.class); + Mockito.when(appService.getApp(127L)).thenReturn(null); + PortalRestResponse response = roleManageController.bulkUploadRoleFunc(mockedRequest, mockedResponse, data, 127L); + assertEquals(PortalRestStatusEnum.ERROR, response.getStatus()); + assertEquals("Unauthorized User", response.getMessage()); + assertEquals("Failure", response.getResponse()); + } + + 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(); - app.setCentralAuth(true); + + public EPApp CentralApp() { + EPApp app = mockApp(); + app.setRolesInAAF(true); app.setNameSpace("com.test"); return app; } - - public EPApp NonCentralApp() - { - EPApp app = mockApp(); - app.setCentralAuth(false); + + public EPApp NonCentralApp() { + EPApp app = mockApp(); + app.setRolesInAAF(false); return app; } + }