X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ecomp-portal-BE-common%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fportalapp%2Fportal%2Fservice%2FExternalAccessRolesServiceImplTest.java;h=0331633c508b0eeacb88afee71c3d8e126992141;hb=8fbf2846655dbad0e19789e510c51127ad35104d;hp=172b94217680c44172241b6a32810af78b945376;hpb=f59a56b7c12badbb41bb2155b6de47e9c9c48bcd;p=portal.git diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/ExternalAccessRolesServiceImplTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/ExternalAccessRolesServiceImplTest.java index 172b9421..0331633c 100644 --- a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/ExternalAccessRolesServiceImplTest.java +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/ExternalAccessRolesServiceImplTest.java @@ -33,7 +33,7 @@ * * ============LICENSE_END============================================ * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * */ package org.onap.portalapp.portal.service; @@ -78,18 +78,11 @@ import org.onap.portalapp.portal.domain.EPAppRoleFunction; import org.onap.portalapp.portal.domain.EPRole; import org.onap.portalapp.portal.domain.EPUser; import org.onap.portalapp.portal.domain.EPUserApp; +import org.onap.portalapp.portal.ecomp.model.UploadRoleFunctionExtSystem; import org.onap.portalapp.portal.exceptions.InactiveApplicationException; import org.onap.portalapp.portal.exceptions.InvalidUserException; import org.onap.portalapp.portal.framework.MockitoTestSuite; -import org.onap.portalapp.portal.transport.BulkUploadRoleFunction; -import org.onap.portalapp.portal.transport.BulkUploadUserRoles; -import org.onap.portalapp.portal.transport.CentralRole; -import org.onap.portalapp.portal.transport.CentralUser; -import org.onap.portalapp.portal.transport.CentralV2Role; -import org.onap.portalapp.portal.transport.EcompUserRoles; -import org.onap.portalapp.portal.transport.ExternalRequestFieldsValidator; -import org.onap.portalapp.portal.transport.GlobalRoleWithApplicationRoleFunction; -import org.onap.portalapp.portal.transport.LocalRole; +import org.onap.portalapp.portal.transport.*; import org.onap.portalapp.portal.utils.EPCommonSystemProperties; import org.onap.portalapp.portal.utils.EcompPortalUtils; import org.onap.portalapp.portal.utils.PortalConstants; @@ -114,7 +107,7 @@ import org.springframework.web.client.RestTemplate; import com.fasterxml.jackson.databind.ObjectMapper; @RunWith(PowerMockRunner.class) -@PrepareForTest({ EcompPortalUtils.class, Criterion.class, Restrictions.class, SystemProperties.class, +@PrepareForTest({ EcompPortalUtils.class, Criterion.class, EPUserUtils.class, Restrictions.class, SystemProperties.class, EPCommonSystemProperties.class }) public class ExternalAccessRolesServiceImplTest { @Mock @@ -141,6 +134,13 @@ public class ExternalAccessRolesServiceImplTest { @Before public void setup() { MockitoAnnotations.initMocks(this); + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(PortalConstants.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(Restrictions.class); + PowerMockito.mockStatic(Criterion.class); Mockito.when(sessionFactory.openSession()).thenReturn(session); Mockito.when(session.beginTransaction()).thenReturn(transaction); } @@ -256,9 +256,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void addRoleTest() throws Exception { HttpHeaders headers = new HttpHeaders(); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); - PowerMockito.mockStatic(EPCommonSystemProperties.class); String uebKey = "test-ueb-key"; Role role = new Role(); role.setId((long) 25); @@ -294,9 +291,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void addRoleMethodNotAllowedTest() throws Exception { HttpHeaders headers = new HttpHeaders(); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); - PowerMockito.mockStatic(EPCommonSystemProperties.class); Role role = new Role(); role.setId((long) 25); EPApp app = mockApp(); @@ -353,9 +347,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void deleteCentralRoleFunctionTest() throws Exception { - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); - PowerMockito.mockStatic(EPCommonSystemProperties.class); final Map params = new HashMap<>(); EPApp app = mockApp(); params.put("functionCode", "menu_fun_code"); @@ -488,7 +479,6 @@ public class ExternalAccessRolesServiceImplTest { Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers); Mockito.doNothing().when(dataAccessService).deleteDomainObjects(EPAppRoleFunction.class, "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null); - boolean returnedValue = externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app); assertTrue(returnedValue); } @@ -510,9 +500,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void getRoleFunctionTest() throws Exception { - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); List appList = new ArrayList<>(); appList.add(app); @@ -533,14 +520,11 @@ public class ExternalAccessRolesServiceImplTest { .thenReturn(getRoleFuncList); CentralV2RoleFunction actual = externalAccessRolesServiceImpl.getRoleFunction("test_type|type_code|*", app.getUebKey()); - assertEquals(getCenRole.getCode(), actual.getCode()); + assertEquals("type_code", actual.getCode()); } @Test public void getRoleFunctionMutilpleFilterTest() throws Exception { - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); List appList = new ArrayList<>(); appList.add(app); @@ -554,6 +538,7 @@ public class ExternalAccessRolesServiceImplTest { Mockito.when(EcompPortalUtils.getFunctionCode("test_type_1|type_code_1|*")).thenReturn("type_code_1"); Mockito.when(EcompPortalUtils.getFunctionType("test_type|type_code|*")).thenReturn("test_type"); Mockito.when(EcompPortalUtils.getFunctionAction("test_type|type_code|*")).thenReturn("*"); + Mockito.when(EcompPortalUtils.encodeFunctionCode("type_code")).thenReturn("type_code"); List getRoleFuncList = new ArrayList<>(); CentralV2RoleFunction getCenRole = new CentralV2RoleFunction("test_type|type_code|*", "test_name"); CentralV2RoleFunction getCenRole2 = new CentralV2RoleFunction("test_type_1|type_code_1|*", "test_name_1"); @@ -737,13 +722,23 @@ public class ExternalAccessRolesServiceImplTest { ecompUserRoles.setOrgUserId("guestT"); ecompUserRoles.setRoleId((long) 1); ecompUserRoles.setRoleName("test"); + ecompUserRoles.setFunctionCode("test_type|test_instance|test_action"); + ecompUserRoles.setFunctionName("test1"); EcompUserRoles ecompUserRoles2 = new EcompUserRoles(); ecompUserRoles2.setOrgUserId("guestT"); ecompUserRoles2.setRoleId((long) 2); ecompUserRoles2.setRoleName("test new"); + ecompUserRoles2.setFunctionCode("test_instance2"); + ecompUserRoles2.setFunctionName("test2"); userList.add(ecompUserRoles); userList.add(ecompUserRoles2); - Mockito.when(dataAccessService.executeNamedQuery("ApplicationUserRoles", appParams, null)).thenReturn(userList); + Mockito.when(EcompPortalUtils.getFunctionCode(ecompUserRoles.getFunctionCode())).thenReturn("test_instance"); + Mockito.when(EPUserUtils.decodeFunctionCode("test_instance")).thenReturn("test_instance"); + Mockito.when(EcompPortalUtils.getFunctionCode(ecompUserRoles2.getFunctionCode())).thenReturn("test_instance2"); + Mockito.when(EPUserUtils.decodeFunctionCode("test_instance2")).thenReturn("test_instance2"); + Mockito.when(EcompPortalUtils.getFunctionType("test_type|test_instance|test_action")).thenReturn("test_type"); + Mockito.when(EcompPortalUtils.getFunctionAction("test_type|test_instance|test_action")).thenReturn("test_action"); + Mockito.when(dataAccessService.executeNamedQuery("ApplicationUserRoles", appParams, null)).thenReturn(userList); List usersfinalList = externalAccessRolesServiceImpl.getAllAppUsers(app.getUebKey()); assertEquals(usersfinalList.get(0).getRoles().size(), 2); } @@ -816,8 +811,8 @@ public class ExternalAccessRolesServiceImplTest { globalRoles.add(globalRole); Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles); List expected = new ArrayList<>(); - CentralV2Role cenV2Role = new CentralV2Role(); - CentralV2Role cenV2Role2 = new CentralV2Role(); + CentralV2Role cenV2Role = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role(); + CentralV2Role cenV2Role2 = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role(); expected.add(cenV2Role); expected.add(cenV2Role2); List actual = externalAccessRolesServiceImpl.getRolesForApp(app.getUebKey()); @@ -915,7 +910,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void saveRoleForPortalApplicationNewTest() throws Exception { - PowerMockito.mockStatic(EcompPortalUtils.class); EPApp app = mockApp(); app.setId(1l); Role addRoleTest = new Role(); @@ -956,9 +950,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void saveRoleForPortalApplicationUpdateTest() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); app.setId(1l); Role addRoleTest = new Role(); @@ -1082,9 +1073,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void saveRoleExitsInDbButNotInExtAuthSystemTest() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); app.setId(1l); Role addRoleTest = new Role(); @@ -1183,9 +1171,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void saveGlobalRoleForPortalApplicationUpdateTest() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); app.setId(1l); Role addRoleTest = new Role(); @@ -1309,9 +1294,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void saveRoleForPartnerApplicationUpdateTest() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); app.setId(2l); Role addRoleTest = new Role(); @@ -1445,9 +1427,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void saveGlobalRoleFunctionsForPartnerApplicationUpdateTest() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); app.setId(2l); Role addRoleTest = new Role(); @@ -1588,9 +1567,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void syncRoleFunctionFromExternalAccessSystemTest() { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); app.setId(2l); JSONObject mockJsonObjectFinalPerm = new JSONObject(); @@ -1758,9 +1734,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void syncApplicationRolesWithEcompDBTest() { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); app.setId(2l); JSONObject mockJsonObjectRole = new JSONObject(); @@ -1780,8 +1753,8 @@ public class ExternalAccessRolesServiceImplTest { mockJsonObjectRole.put("name", "com.test.app.Test"); mockJsonObjectRole.put("perms", permsList); mockJsonObjectRole.put("description", - "{\"id\":\"2\",\"name\":\"test1\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"2\",\"appRoleId\":\"2\"}"); - mockJsonObjectRole2.put("name", "com.test.app.Test2"); + "Test role"); + mockJsonObjectRole2.put("name", "com.test.app.Test2_role"); List permsList2 = new ArrayList<>(); permsList2.add(mockJsonObjectPerm1); mockJsonObjectRole2.put("perms", permsList2); @@ -1794,19 +1767,26 @@ public class ExternalAccessRolesServiceImplTest { Matchers.>any(), Matchers.eq(String.class))).thenReturn(getResponse); List getCurrentRoleList = new ArrayList<>(); EPRole getEPRole = new EPRole(); - getEPRole.setName("Test"); + getEPRole.setName("Test role"); getEPRole.setId(2l); getEPRole.setAppId(app.getId()); getEPRole.setAppRoleId(2l); getEPRole.setActive(true); EPRole getEPRole2 = new EPRole(); - getEPRole2.setName("Test3"); + getEPRole2.setName("Test2_role"); getEPRole2.setId(3l); - getEPRole.setAppId(app.getId()); - getEPRole.setAppRoleId(3l); + getEPRole2.setAppId(app.getId()); + getEPRole2.setAppRoleId(3l); getEPRole2.setActive(true); + EPRole getEPRole3 = new EPRole(); + getEPRole3.setName("Test3_role"); + getEPRole3.setId(3l); + getEPRole3.setAppId(app.getId()); + getEPRole3.setAppRoleId(3l); + getEPRole3.setActive(true); getCurrentRoleList.add(getEPRole); getCurrentRoleList.add(getEPRole2); + getCurrentRoleList.add(getEPRole3); final Map appParams = new HashMap<>(); appParams.put("appId", app.getId()); Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null)) @@ -1841,10 +1821,10 @@ public class ExternalAccessRolesServiceImplTest { getV2RoleFunction.add(centralV2RoleFunction); final Map extRoleParams = new HashMap<>(); List roleListDeactivate = new ArrayList<>(); - extRoleParams.put(APP_ROLE_NAME_PARAM, "Test3"); + extRoleParams.put(APP_ROLE_NAME_PARAM, "Test3_role"); extRoleParams.put(APP_ID, app.getId().toString()); EPRole getEPRoleDeactivate = new EPRole(); - getEPRoleDeactivate.setName("Test3"); + getEPRoleDeactivate.setName("Test3_role"); getEPRoleDeactivate.setId(3l); getEPRoleDeactivate.setAppId(app.getId()); getEPRoleDeactivate.setAppRoleId(3l); @@ -1891,9 +1871,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void deleteDependencyRoleRecord() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); SQLQuery SqlQuery = Mockito.mock(SQLQuery.class); EPApp app = mockApp(); app.setId(2l); @@ -1934,9 +1911,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void deleteDependencyRoleRecordForPortal() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); SQLQuery SqlQuery = Mockito.mock(SQLQuery.class); EPApp app = mockApp(); app.setId(1l); @@ -1976,9 +1950,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void bulkUploadFunctionsTest() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); app.setId(2l); List appList = new ArrayList<>(); @@ -2018,9 +1989,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void bulkUploadRolesTest() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); app.setId(2l); List appList = new ArrayList<>(); @@ -2074,15 +2042,12 @@ public class ExternalAccessRolesServiceImplTest { Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), Matchers.>any(), Matchers.eq(String.class))).thenReturn(postResponse); Integer actual = externalAccessRolesServiceImpl.bulkUploadRoles(app.getUebKey()); - Integer expected = 2; + Integer expected = 3; assertEquals(expected, actual); } @Test public void bulkUploadUserRolesTest() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); app.setId(2l); EPUser user = mockUser.mockEPUser(); @@ -2150,12 +2115,6 @@ public class ExternalAccessRolesServiceImplTest { @SuppressWarnings("unchecked") @Test public void getActiveRolesTest() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); - PowerMockito.mockStatic(EPUserUtils.class); - PowerMockito.mockStatic(Restrictions.class); - PowerMockito.mockStatic(Criterion.class); EPApp app = mockApp(); app.setId(1l); List appList = new ArrayList<>(); @@ -2219,9 +2178,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void bulkUploadRolesFunctionsTest() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); List appList = new ArrayList<>(); final Map appUebkeyParams = new HashMap<>(); @@ -2274,9 +2230,6 @@ public class ExternalAccessRolesServiceImplTest { @Test public void bulkUploadPartnerRoleFunctionsTest() throws Exception { - PowerMockito.mockStatic(EPCommonSystemProperties.class); - PowerMockito.mockStatic(EcompPortalUtils.class); - PowerMockito.mockStatic(SystemProperties.class); EPApp app = mockApp(); List appList = new ArrayList<>(); final Map appUebkeyParams = new HashMap<>(); @@ -2284,32 +2237,64 @@ public class ExternalAccessRolesServiceImplTest { appUebkeyParams.put("appKey", app.getUebKey()); Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) .thenReturn(appList); - List roles = new ArrayList<>(); - Role role = new Role(); - role.setName("Test"); - role.setId(2l); - role.setActive(true); - Role role2 = new Role(); - role2.setName("Test2"); - role2.setId(3l); - role2.setActive(true); - roles.add(role); - roles.add(role2); - SortedSet roleFuncSet = new TreeSet<>(); - RoleFunction roleFunc = new RoleFunction(); - roleFunc.setName("Test Name"); - roleFunc.setCode("testcode"); - RoleFunction roleFunc2 = new RoleFunction(); - roleFunc2.setName("Test Name3"); - roleFunc2.setCode("menu_testcode2"); - roleFuncSet.add(roleFunc); - roleFuncSet.add(roleFunc2); - role.setRoleFunctions(roleFuncSet); - role2.setRoleFunctions(roleFuncSet); + List applicationRoles = new ArrayList<>(); + EPRole getEPRole = new EPRole(); + getEPRole.setName("Test"); + getEPRole.setId(2l); + getEPRole.setActive(true); + EPRole getEPRole2 = new EPRole(); + getEPRole2.setName("Test2"); + getEPRole2.setId(3l); + getEPRole2.setActive(true); + applicationRoles.add(getEPRole); + applicationRoles.add(getEPRole2); + final Map paramsRoles = new HashMap<>(); + paramsRoles.put("appId", app.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", paramsRoles, null)) + .thenReturn(applicationRoles); + final Map params = new HashMap<>(); + params.put("roleId", getEPRole.getId()); + List appRoleFunc = new ArrayList<>(); + BulkUploadRoleFunction bulkUploadRoleFunction = new BulkUploadRoleFunction(); + bulkUploadRoleFunction.setFunctionCd("testcode"); + bulkUploadRoleFunction.setFunctionName("test_name"); + BulkUploadRoleFunction bulkUploadRoleFunction2 = new BulkUploadRoleFunction(); + bulkUploadRoleFunction2.setFunctionCd("menu_testcode2"); + bulkUploadRoleFunction2.setFunctionName("test_name2"); + appRoleFunc.add(bulkUploadRoleFunction); + appRoleFunc.add(bulkUploadRoleFunction2); + Mockito.when(dataAccessService.executeNamedQuery("uploadPartnerRoleFunctions", params, null)) + .thenReturn(appRoleFunc); + final Map params2 = new HashMap<>(); + params2.put("roleId", getEPRole2.getId()); + List appRoleFunc2 = new ArrayList<>(); + appRoleFunc2.add(bulkUploadRoleFunction); + appRoleFunc2.add(bulkUploadRoleFunction2); + Mockito.when(dataAccessService.executeNamedQuery("uploadPartnerRoleFunctions", params2, null)) + .thenReturn(appRoleFunc2); ResponseEntity getResponse = new ResponseEntity<>(HttpStatus.OK); Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT), Matchers.>any(), Matchers.eq(String.class))).thenReturn(getResponse); - externalAccessRolesServiceImpl.bulkUploadPartnerRoleFunctions(app.getUebKey(), roles); + // GlobalRoleFunctionsTest + final Map partnerAppParams = new HashMap<>(); + partnerAppParams.put("appId", app.getId()); + Mockito.when(epAppCommonServiceImpl.getApp(1l)).thenReturn(app); + List globalRoleFuncsList = new ArrayList<>(); + GlobalRoleWithApplicationRoleFunction globalRoleFunc = new GlobalRoleWithApplicationRoleFunction(); + globalRoleFunc.setActive(true); + globalRoleFunc.setAppId(10l); + globalRoleFunc.setFunctionCd("test|test|test"); + globalRoleFunc.setRoleId(2l); + globalRoleFunc.setFunctionName("test"); + globalRoleFunc.setRoleName("global_test"); + globalRoleFuncsList.add(globalRoleFunc); + Mockito.when(dataAccessService.executeNamedQuery("getBulkUploadPartnerGlobalRoleFunctions", partnerAppParams, null)) + .thenReturn(globalRoleFuncsList); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.>any(), Matchers.eq(String.class))).thenReturn(getResponse); + Integer actual = externalAccessRolesServiceImpl.bulkUploadPartnerRoleFunctions(app.getUebKey()); + Integer expected = 5; + assertEquals(expected, actual); } @Test @@ -2372,7 +2357,7 @@ public class ExternalAccessRolesServiceImplTest { @Test public void convertV2CentralRoleListToOldVerisonCentralRoleListTest() { List v2CenRoleList = new ArrayList<>(); - CentralV2Role cenV2Role = new CentralV2Role(2l, "test1"); + CentralV2Role cenV2Role = new CentralV2Role.CentralV2RoleBuilder().setId(2l).setName("test1").createCentralV2Role(); CentralV2RoleFunction CentralV2Role = new CentralV2RoleFunction("testcode", "test_name"); SortedSet setV2RoleFuncs = new TreeSet<>(); setV2RoleFuncs.add(CentralV2Role); @@ -2381,4 +2366,174 @@ public class ExternalAccessRolesServiceImplTest { List actual = externalAccessRolesServiceImpl.convertV2CentralRoleListToOldVerisonCentralRoleList(v2CenRoleList); assertEquals(1, actual.size()); } + + @Test + public void bulkUploadRoleFuncTest() throws Exception { + EPApp app = mockApp(); + UploadRoleFunctionExtSystem data = new UploadRoleFunctionExtSystem(); + data.setRoleName("test"); + data.setType("test"); + data.setInstance("test"); + data.setIsGlobalRolePartnerFunc(false); + data.setAction("test"); + data.setName("test"); + ResponseEntity getResponse = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.>any(), Matchers.eq(String.class))).thenReturn(getResponse); + externalAccessRolesServiceImpl.bulkUploadRoleFunc(data, app); + } + + @Test + public void bulkUploadGlobalRoleFuncTest() throws Exception { + EPApp app = mockApp(); + EPApp portalApp = mockApp(); + portalApp.setId(1L); + Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(portalApp); + UploadRoleFunctionExtSystem data = new UploadRoleFunctionExtSystem(); + data.setRoleName("test"); + data.setType("test"); + data.setInstance("test"); + data.setIsGlobalRolePartnerFunc(true); + data.setAction("test"); + data.setName("test"); + ResponseEntity getResponse = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.>any(), Matchers.eq(String.class))).thenReturn(getResponse); + externalAccessRolesServiceImpl.bulkUploadRoleFunc(data, app); + } + + @Test(expected = HttpClientErrorException.class) + public void bulkUploadRoleFuncExcpetionTest() throws Exception { + UploadRoleFunctionExtSystem data = new UploadRoleFunctionExtSystem(); + data.setRoleName("test"); + data.setType("test"); + data.setInstance("test"); + data.setAction("test"); + data.setName("test"); + data.setInstance("test"); + EPApp app = mockApp(); + Mockito.doThrow(new HttpClientErrorException(HttpStatus.CONFLICT)).when(template).exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.>any(), Matchers.eq(String.class)); + externalAccessRolesServiceImpl.bulkUploadRoleFunc(data, app); + } + + @Test + public void syncApplicationUserRolesFromExtAuthSystemTest() throws Exception { + Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(new HttpHeaders()); + Mockito.when(EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)).thenReturn(true); + JSONObject mockJsonObjectRole = new JSONObject(); + JSONObject mockJsonObjectRole2 = new JSONObject(); + JSONObject mockJsonObjectRole3 = new JSONObject(); + mockJsonObjectRole.put("name", "com.test.app.test_role"); + mockJsonObjectRole2.put("name", "com.test.app2.test_role"); + mockJsonObjectRole3.put("name", "com.test.app2.Account_Administrator"); + List userRolesList = new ArrayList<>(); + JSONObject mockJsonObjectFinalUserRole = new JSONObject(); + userRolesList.add(mockJsonObjectRole); + userRolesList.add(mockJsonObjectRole2); + userRolesList.add(mockJsonObjectRole3); + mockJsonObjectFinalUserRole.put("role", userRolesList); + ResponseEntity getResponse = new ResponseEntity<>(mockJsonObjectFinalUserRole.toString(),HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), + Matchers.>any(), Matchers.eq(String.class))).thenReturn(getResponse); + List users = new ArrayList<>(); + EPUser user = mockUser.mockEPUser(); + user.setOrgUserId("test"); + users.add(user); + List apps = new ArrayList<>(); + EPApp app = mockApp(); + app.setNameSpace("com.test.app"); + app.setId(1l); + EPApp app2 = mockApp(); + app2.setNameSpace("com.test.app2"); + app2.setId(2l); + apps.add(app); + apps.add(app2); + Mockito.when(dataAccessService + .executeNamedQuery("getCentralizedApps", null, null)).thenReturn(apps); + HashMap userParams = new HashMap<>(); + userParams.put("org_user_id", "test"); + Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null)).thenReturn(users); + List currentUserAppRoles = new ArrayList<>(); + CentralizedAppRoles currentUserAppRole = new CentralizedAppRoles(); + currentUserAppRole.setAppId(1l); + currentUserAppRole.setAppNameSpace("com.test.app"); + currentUserAppRole.setRoleId(2l); + currentUserAppRole.setRoleName("test role"); + currentUserAppRoles.add(currentUserAppRole); + HashMap userParams2 = new HashMap<>(); + userParams2.put("orgUserId", user.getOrgUserId()); + Mockito.when(dataAccessService + .executeNamedQuery("getUserCentralizedAppRoles", userParams2, null)).thenReturn(currentUserAppRoles); + List centralizedAppRoles = new ArrayList<>(); + CentralizedAppRoles centralizedAppRole = new CentralizedAppRoles(); + centralizedAppRole.setAppId(1l); + centralizedAppRole.setAppNameSpace("com.test.app"); + centralizedAppRole.setRoleId(2l); + centralizedAppRole.setRoleName("test role"); + CentralizedAppRoles centralizedAppRole2 = new CentralizedAppRoles(); + centralizedAppRole2.setAppId(1l); + centralizedAppRole2.setAppNameSpace("com.test.app2"); + centralizedAppRole2.setRoleId(2l); + centralizedAppRole2.setRoleName("test role"); + centralizedAppRoles.add(centralizedAppRole); + centralizedAppRoles.add(centralizedAppRole2); + Mockito.when(dataAccessService + .executeNamedQuery("getAllCentralizedAppsRoles", null, null)).thenReturn(centralizedAppRoles); + externalAccessRolesServiceImpl.syncApplicationUserRolesFromExtAuthSystem(user.getOrgUserId()); + } + + @Test + public void updateAppRoleDescriptionTest() { + EPApp app = mockUpdateAppRoleDescription(); + ResponseEntity postResponse = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT), + Matchers.>any(), Matchers.eq(String.class))).thenReturn(postResponse); + Integer actual = externalAccessRolesServiceImpl.updateAppRoleDescription(app.getUebKey()); + Integer expected = 1; + assertEquals(expected, actual); + } + + @Test + public void updateAppRoleDescriptionExceptionTest() { + EPApp app = mockUpdateAppRoleDescription(); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT), + Matchers.>any(), Matchers.eq(String.class))).thenThrow(new HttpClientErrorException(HttpStatus.NOT_ACCEPTABLE)); + Integer actual = externalAccessRolesServiceImpl.updateAppRoleDescription(app.getUebKey()); + Integer expected = 0; + assertEquals(expected, actual); + } + + @Test + public void updateAppRoleDescriptionExceptionTest2() throws Exception { + EPApp app = mockUpdateAppRoleDescription(); + Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenThrow(new NullPointerException()); + Integer actual = externalAccessRolesServiceImpl.updateAppRoleDescription(app.getUebKey()); + Integer expected = 0; + assertEquals(expected, actual); + } + + private EPApp mockUpdateAppRoleDescription() { + EPApp app = mockApp(); + app.setId(2l); + List appList = new ArrayList<>(); + final Map appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + List epRoleList = new ArrayList<>(); + EPRole getEPRole = new EPRole(); + getEPRole.setName("Test"); + getEPRole.setId(2l); + getEPRole.setAppRoleId(2l); + getEPRole.setActive(true); + epRoleList.add(getEPRole); + final Map appParams = new HashMap<>(); + appParams.put("appId", app.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null)) + .thenReturn(epRoleList); + return app; + } + }