From f766011f9b42850747cbc76035177d129afb793c Mon Sep 17 00:00:00 2001 From: charitha Date: Fri, 6 Oct 2017 13:36:56 +0530 Subject: [PATCH] Add isMappedToGroup method for device group service --- .../GroupManagementProviderService.java | 15 +++++-- .../GroupManagementProviderServiceImpl.java | 42 ++++++++++++++++- .../device/mgt/core/search/util/Utils.java | 7 +-- .../GroupManagementProviderServiceTest.java | 45 +++++++++++++++---- 4 files changed, 91 insertions(+), 18 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index 0187e8dca63..27aa73f3584 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -26,8 +26,8 @@ import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; -import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; import java.util.List; @@ -215,10 +215,19 @@ public interface GroupManagementProviderService { /** * Checks for the default group existence and create group based on device ownership. - * @param groupName - * @return + * @param groupName of the group + * @return DeviceGroup object * @throws GroupManagementException */ DeviceGroup createDefaultGroup(String groupName) throws GroupManagementException; + /** + * Check device is belonging to a Device Group. + * + * @param groupId of Device Group. + * @param deviceIdentifier of the device. + * @throws GroupManagementException on errors. + */ + boolean isDeviceMappedToGroup(int groupId, DeviceIdentifier deviceIdentifier) throws GroupManagementException; + } 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 69efaa107df..f55e8fb0c92 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 @@ -30,7 +30,12 @@ import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.TransactionManagementException; -import org.wso2.carbon.device.mgt.common.group.mgt.*; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; import org.wso2.carbon.device.mgt.core.dao.GroupDAO; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; @@ -825,4 +830,39 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return defaultGroup; } } + + /** + * {@inheritDoc} + */ + @Override + public boolean isDeviceMappedToGroup(int groupId, DeviceIdentifier deviceIdentifier) + throws GroupManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + Device device; + try { + device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(). + getDevice(deviceIdentifier, false); + if (device == null) { + throw new GroupManagementException("Device not found for id '" + deviceIdentifier.getId() + + "' type '" + deviceIdentifier.getType() + "'"); + } + } catch (DeviceManagementException e) { + throw new GroupManagementException("Device management exception occurred when retrieving device. " + + e.getMessage(), e); + } + + try{ + GroupManagementDAOFactory.openConnection(); + return this.groupDAO.isDeviceMappedToGroup(groupId, device.getId(), tenantId); + } catch (GroupManagementDAOException e) { + throw new GroupManagementException("Error occurred when checking device, group mapping between device id '" + + deviceIdentifier.getId() + "' and group id '" + groupId + "'", e); + } catch (SQLException e) { + throw new GroupManagementException("Error occurred when opening db connection to check device, group " + + "mapping between device id '" + deviceIdentifier.getId() + + "' and group id '" + groupId + "'", e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/Utils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/Utils.java index 58c6f621ac8..9e02b3f9708 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/Utils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/Utils.java @@ -33,10 +33,7 @@ public class Utils { DeviceInfo deviceInfo = new DeviceInfo(); - deviceInfo.setIMSI("e6f236ac82537a8e"); deviceInfo.setSsid("FAFDA"); - - deviceInfo.setAvailableRAMMemory(1.24); deviceInfo.setBatteryLevel(27.3); deviceInfo.setConnectionType("GSM"); @@ -44,8 +41,6 @@ public class Utils { deviceInfo.setDeviceModel("SM-T520"); deviceInfo.setExternalAvailableMemory(2.45); deviceInfo.setExternalTotalMemory(16.23); - deviceInfo.setIMEI("e6f236ac82537a8e"); - deviceInfo.setIMSI("GT-0WDA"); deviceInfo.setInternalAvailableMemory(3.56); deviceInfo.setInternalTotalMemory(7.89); deviceInfo.setMobileSignalStrength(0.67); @@ -67,6 +62,8 @@ public class Utils { propertyMap.put("MEMORY_THRESHOLD", "100663296"); propertyMap.put("CPU_IOW", "12"); propertyMap.put("CPU_IRQ", "1"); + propertyMap.put("IMEI", "e6f236ac82537a8e"); + propertyMap.put("IMSI", "432659632123654845"); deviceInfo.setDeviceDetailsMap(propertyMap); 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 994ef33e8e1..56fe67888c6 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 @@ -16,20 +16,26 @@ * under the License. */ - package org.wso2.carbon.device.mgt.core.service; - import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import org.wso2.carbon.device.mgt.common.*; -import org.wso2.carbon.device.mgt.common.group.mgt.*; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; +import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; +import org.wso2.carbon.device.mgt.common.PaginationResult; +import org.wso2.carbon.device.mgt.common.TransactionManagementException; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; import org.wso2.carbon.device.mgt.core.TestUtils; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.cache.DeviceCacheConfiguration; -import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.registry.core.jdbc.realm.InMemoryRealmService; import org.wso2.carbon.user.api.Permission; @@ -65,7 +71,6 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test(expectedExceptions = {GroupManagementException.class, GroupAlreadyExistException.class, TransactionManagementException.class}) public void createGroupError() throws GroupManagementException, GroupAlreadyExistException, TransactionManagementException { - GroupManagementDAOFactory.beginTransaction(); groupManagementProviderService.createGroup(TestUtils.createDeviceGroup4(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); } @@ -125,7 +130,6 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test(dependsOnMethods = ("createGroup")) public void getGroup() throws GroupManagementException { - DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup3().getName()); Assert.assertNotNull(groupManagementProviderService.getGroup(deviceGroup.getGroupId())); } @@ -160,8 +164,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class}) public void getGroupsByPaginationError() throws GroupManagementException { - GroupPaginationRequest request = null; - groupManagementProviderService.getGroups(request); + groupManagementProviderService.getGroups((GroupPaginationRequest) null); } @Test(dependsOnMethods = ("createGroup")) @@ -281,5 +284,29 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest groupManagementProviderService.createDefaultGroup("BYOD"); } + @Test(dependsOnMethods = {"createGroup", "addDevices", "updateGroupSecondTime"}) + public void checkDeviceBelongsToGroup() throws GroupManagementException { + List list = TestUtils.getDeviceIdentifiersList(); + boolean isMapped = groupManagementProviderService + .isDeviceMappedToGroup(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup1().getName()).getGroupId(), list.get(0)); + Assert.assertEquals(isMapped, true); + } + + @Test + public void checkDeviceBelongsToNonExistingGroup() throws GroupManagementException { + List list = TestUtils.getDeviceIdentifiersList(); + boolean isMapped = groupManagementProviderService + .isDeviceMappedToGroup(1500, list.get(0)); + Assert.assertEquals(isMapped, false); + } + + + @Test(dependsOnMethods = {"createGroup", "updateGroupSecondTime"}, expectedExceptions = {GroupManagementException.class}) + public void checkNullDeviceBelongsToGroup() throws GroupManagementException { + groupManagementProviderService.isDeviceMappedToGroup(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup1().getName()).getGroupId(), null); + } + }