From 8a6c208b012872bcb766da1bb6e31061612848bb Mon Sep 17 00:00:00 2001 From: charitha Date: Wed, 27 Sep 2017 15:46:07 +0530 Subject: [PATCH] Add additional test cases for grouping --- .../GroupManagementProviderServiceImpl.java | 73 +-------- .../GroupManagementProviderServiceTest.java | 145 ++++++++++-------- 2 files changed, 89 insertions(+), 129 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 13f87ad01f..69efaa107d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -259,7 +259,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid if (log.isDebugEnabled()) { log.debug("Get groups"); } - List deviceGroups = new ArrayList<>(); + List deviceGroups; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); @@ -293,7 +293,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.debug("Get groups with pagination " + request.toString()); } request = DeviceManagerUtil.validateGroupListPageSize(request); - List deviceGroups = new ArrayList<>(); + List deviceGroups; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); @@ -345,14 +345,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid for (DeviceGroup deviceGroup : deviceGroups) { groups.put(deviceGroup.getGroupId(), deviceGroup); } -// } catch (UserStoreException e) { -// String msg = "Error occurred while getting user store manager."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while retrieving all groups accessible to user."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (UserStoreException | SQLException | GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups accessible to user."; log.error(msg, e); @@ -368,16 +360,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } private List getGroupIds(String username) throws GroupManagementException { - if (username == null || username.isEmpty()) { - String msg = "Received empty user name for getGroupIds"; - log.error(msg); - throw new GroupManagementException(msg); - } if (log.isDebugEnabled()) { log.debug("Get groups Ids of owner '" + username + "'"); } UserStoreManager userStoreManager; - List deviceGroupIds = new ArrayList<>(); + List deviceGroupIds; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) @@ -386,14 +373,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.openConnection(); deviceGroupIds = this.groupDAO.getOwnGroupIds(username, tenantId); deviceGroupIds.addAll(this.groupDAO.getGroupIds(roleList, tenantId)); -// } catch (UserStoreException e) { -// String msg = "Error occurred while getting user store manager."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while retrieving all groups accessible to user."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (UserStoreException | SQLException | GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups accessible to user."; log.error(msg, e); @@ -421,7 +400,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } request = DeviceManagerUtil.validateGroupListPageSize(request); List allDeviceGroupIdsOfUser = getGroupIds(currentUser); - List allMatchingGroups = new ArrayList<>(); + List allMatchingGroups; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); @@ -430,10 +409,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while retrieving all groups in tenant"; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getGroups"; log.error(msg, e); @@ -460,10 +435,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while retrieving all groups in tenant"; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; log.error(msg, e); @@ -490,10 +461,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while retrieving all groups in tenant"; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getGroupCount"; log.error(msg, e); @@ -527,18 +494,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid count = groupDAO.getOwnGroupsCount(username, tenantId); count += groupDAO.getGroupsCount(roleList, tenantId); return count; -// } catch (UserStoreException e) { -// String msg = "Error occurred while getting user store manager."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (UserStoreException | GroupManagementDAOException | SQLException e) { String msg = "Error occurred while retrieving group count of user '" + username + "'"; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getGroupCount for username '" + username + "'"; log.error(msg, e); @@ -619,10 +578,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while retrieving all groups in tenant"; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getRoles for groupId: " + groupId; log.error(msg, e); @@ -651,14 +606,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while getting devices in group."; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); -// } catch (DeviceManagementException e) { -// String msg = "Error occurred while validating the limit of the devices to be returned"; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getDevices for groupId: " + groupId; log.error(msg, e); @@ -684,10 +631,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while retrieving all groups in tenant"; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getDeviceCount for groupId: " + groupId; log.error(msg, e); @@ -838,18 +781,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.openConnection(); return groupDAO.getGroups(device.getId(), PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); -// } catch (DeviceManagementException e) { -// String msg = "Error occurred while retrieving the device details."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (DeviceManagementException | GroupManagementDAOException | SQLException e) { String msg = "Error occurred while retrieving device groups."; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening database connection."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getGroups"; log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java index 6a13e27b70..994ef33e8e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java @@ -42,7 +42,7 @@ import java.util.List; public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest { - GroupManagementProviderService groupManagementProviderService; + private GroupManagementProviderService groupManagementProviderService; private static final String DEFAULT_ADMIN_ROLE = "admin"; private static final String[] DEFAULT_ADMIN_PERMISSIONS = {"/permission/device-mgt/admin/groups", "/permission/device-mgt/user/groups"}; @@ -54,6 +54,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest RealmService realmService = new InMemoryRealmService(); DeviceManagementDataHolder.getInstance().setRealmService(realmService); realmService.getTenantManager().getSuperTenantDomain(); + DeviceConfigurationManager.getInstance().initConfig(); } @Test(expectedExceptions = {GroupManagementException.class, GroupAlreadyExistException.class}) @@ -71,24 +72,32 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test public void createGroup() throws GroupManagementException, GroupAlreadyExistException { - groupManagementProviderService.createGroup(TestUtils.createDeviceGroup1(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); - groupManagementProviderService.createGroup(TestUtils.createDeviceGroup2(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); - groupManagementProviderService.createGroup(TestUtils.createDeviceGroup3(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); - groupManagementProviderService.createGroup(TestUtils.createDeviceGroup4(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); } @Test(dependsOnMethods = ("createGroup")) public void updateGroup() throws GroupManagementException, GroupNotExistException { - DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName()); deviceGroup.setName(deviceGroup.getName() + "_UPDATED"); groupManagementProviderService.updateGroup(deviceGroup, deviceGroup.getGroupId()); } + @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class}) + public void getGroupNull() throws GroupManagementException, GroupNotExistException { + groupManagementProviderService.getGroup(null); + } + + // Rename again to use in different place. + @Test(dependsOnMethods = ("updateGroup")) + public void updateGroupSecondTime() throws GroupManagementException, GroupNotExistException { + DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName() + "_UPDATED"); + deviceGroup.setName(TestUtils.createDeviceGroup1().getName()); + groupManagementProviderService.updateGroup(deviceGroup, deviceGroup.getGroupId()); + } + @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class, GroupNotExistException.class}) public void updateGroupError() throws GroupManagementException, GroupNotExistException { groupManagementProviderService.updateGroup(null, 1); @@ -96,7 +105,6 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class, GroupNotExistException.class}) public void updateGroupErrorNotExist() throws GroupManagementException, GroupNotExistException { - DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup2().getName()); deviceGroup.setName(deviceGroup.getName() + "_UPDATED"); groupManagementProviderService.updateGroup(deviceGroup, 6); @@ -141,8 +149,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class}) public void getGroupsByUsernameError() throws GroupManagementException { - String username = null; - groupManagementProviderService.getGroups(username); + groupManagementProviderService.getGroups((String) null); } @Test(dependsOnMethods = ("createGroup")) @@ -158,12 +165,19 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest } @Test(dependsOnMethods = ("createGroup")) - public void getGroupsByUsernameAndPagination(String username, GroupPaginationRequest paginationRequest) + public void getGroupsByUsernameAndPagination() throws GroupManagementException { - PaginationResult result = groupManagementProviderService.getGroups(username, paginationRequest); + PaginationResult result = groupManagementProviderService.getGroups("admin", TestUtils.createPaginationRequest()); Assert.assertNotNull(result); } + + @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class}) + public void getGroupsByUsernameAndPaginationError() + throws GroupManagementException { + groupManagementProviderService.getGroups(null, TestUtils.createPaginationRequest()); + } + @Test(dependsOnMethods = ("createGroup")) public void getGroupCount() throws GroupManagementException { int x = groupManagementProviderService.getGroupCount(); @@ -176,27 +190,28 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest Assert.assertNotNull(x); } -// @Test -// public void manageGroupSharing() throws GroupManagementException, RoleDoesNotExistException, UserStoreException { -// groupManagementProviderService.manageGroupSharing(0, null); -// List newRoles = new ArrayList<>(); -// newRoles.add("TEST_ROLE_1"); -// newRoles.add("TEST_ROLE_2"); -// newRoles.add("TEST_ROLE_3"); -// -// UserStoreManager userStoreManager = -// DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm( -// -1234).getUserStoreManager(); -// Permission[] permissions = new Permission[1]; -// Permission perm = new Permission("/admin/test/perm", "add"); -//// perm.setAction("execute.ui"); -//// perm.setResourceId("/admin/test/perm"); -// permissions[0] = perm; -// -// userStoreManager.addRole("TEST_ROLE_1", null, permissions); -// -// groupManagementProviderService.manageGroupSharing(1, newRoles); -// } + @Test(dependsOnMethods = ("updateGroupSecondTime")) + public void manageGroupSharing() throws GroupManagementException, RoleDoesNotExistException, UserStoreException { + groupManagementProviderService.manageGroupSharing(0, null); + List newRoles = new ArrayList<>(); + newRoles.add("TEST_ROLE_1"); + newRoles.add("TEST_ROLE_2"); + newRoles.add("TEST_ROLE_3"); + + UserStoreManager userStoreManager = + DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm( + -1234).getUserStoreManager(); + Permission[] permissions = new Permission[1]; + Permission perm = new Permission("/admin/test/perm", "add"); + permissions[0] = perm; + + userStoreManager.addRole("TEST_ROLE_1", null, permissions); + userStoreManager.addRole("TEST_ROLE_2", null, permissions); + userStoreManager.addRole("TEST_ROLE_3", null, permissions); + + groupManagementProviderService.manageGroupSharing(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup1().getName()).getGroupId(), newRoles); + } @Test(dependsOnMethods = ("createGroup")) public void getRoles() throws GroupManagementException { @@ -216,26 +231,30 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest Assert.assertEquals(0, x); } -// @Test(dependsOnMethods = ("createGroup")) -// public void addDevices() throws GroupManagementException, DeviceNotFoundException { -// -// DeviceCacheConfiguration configuration = new DeviceCacheConfiguration(); -// configuration.setEnabled(false); -// -// DeviceConfigurationManager.getInstance().getDeviceManagementConfig().setDeviceCacheConfiguration(configuration); -// -// List list = TestUtils.getDeviceIdentifiersList(); -// groupManagementProviderService.addDevices(1, list); -// groupManagementProviderService.addDevices(2, list); -// groupManagementProviderService.addDevices(3, list); -// } -// -// @Test(dependsOnMethods = ("addDevices")) -// public void removeDevice() throws GroupManagementException, DeviceNotFoundException { -// List list = TestUtils.getDeviceIdentifiersList(); -// groupManagementProviderService.removeDevice(2, list); -// groupManagementProviderService.removeDevice(3, list); -// } + @Test(dependsOnMethods = ("createGroup")) + public void addDevices() throws GroupManagementException, DeviceNotFoundException { + + DeviceCacheConfiguration configuration = new DeviceCacheConfiguration(); + configuration.setEnabled(false); + + DeviceConfigurationManager.getInstance().getDeviceManagementConfig().setDeviceCacheConfiguration(configuration); + List list = TestUtils.getDeviceIdentifiersList(); + groupManagementProviderService.addDevices(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup1().getName()).getGroupId(), list); + groupManagementProviderService.addDevices(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup2().getName()).getGroupId(), list); + groupManagementProviderService.addDevices(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup3().getName()).getGroupId(), list); + } + + @Test(dependsOnMethods = ("addDevices")) + public void removeDevice() throws GroupManagementException, DeviceNotFoundException { + List list = TestUtils.getDeviceIdentifiersList(); + groupManagementProviderService.removeDevice(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup2().getName()).getGroupId(), list); + groupManagementProviderService.removeDevice(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup3().getName()).getGroupId(), list); + } @Test(dependsOnMethods = ("createGroup")) public void getGroupsByUsernameAndPermissions() throws GroupManagementException { @@ -243,18 +262,24 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest Assert.assertNotNull(groups); } -// @Test(dependsOnMethods = ("addDevices")) -// public void getGroupsByDeviceIdentifier() throws GroupManagementException { -// DeviceIdentifier identifier = new DeviceIdentifier(); -// identifier.setId("12345"); -// identifier.setType("Test"); -// List groups = groupManagementProviderService.getGroups(identifier); -// Assert.assertNull(groups); -// } + @Test(dependsOnMethods = ("addDevices")) + public void getGroupsByDeviceIdentifier() throws GroupManagementException { + DeviceIdentifier identifier = new DeviceIdentifier(); + identifier.setId("12345"); + identifier.setType("Test"); + List groups = groupManagementProviderService.getGroups(identifier); + Assert.assertNotNull(groups); + } @Test public void createDefaultGroup() throws GroupManagementException { groupManagementProviderService.createDefaultGroup("BYOD"); } + + @Test(dependsOnMethods = ("createDefaultGroup")) + public void createDefaultGroupTwice() throws GroupManagementException { + groupManagementProviderService.createDefaultGroup("BYOD"); + } + }