From 9e695ecc85ef663145f8643db3bffd95dc8c20e4 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Sun, 18 Jan 2015 13:53:26 +0530 Subject: [PATCH] Adding test cases to test unregistering a device management service from the device management repository --- .../mgt/core/DeviceManagementRepository.java | 15 +++++-- .../device/mgt/core/dao/DeviceTypeDAO.java | 2 + .../mgt/core/dao/impl/DeviceTypeDAOImpl.java | 5 +++ .../mgt/core/util/DeviceManagerUtil.java | 41 +++++++++++++++---- .../core/DeviceManagementRepositoryTests.java | 28 +++++++++++-- 5 files changed, 75 insertions(+), 16 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java index 9cf5ed8f3..3e7b41aae 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java @@ -35,19 +35,26 @@ public class DeviceManagementRepository { public void addDeviceManagementProvider(DeviceManagerService provider) { String deviceType = provider.getProviderType(); - providers.put(deviceType, provider); try { DeviceManagerUtil.registerDeviceType(deviceType); + } catch (DeviceManagementException e) { + log.error("Exception occurred while registering the device type.",e); + } + providers.put(deviceType, provider); + } + + public void removeDeviceManagementProvider(DeviceManagerService provider) { + String deviceType = provider.getProviderType(); + try { + DeviceManagerUtil.unregisterDeviceType(deviceType); } catch (DeviceManagementException e) { log.error("Exception occured while registering the device type.",e); } + providers.remove(deviceType); } public DeviceManagerService getDeviceManagementProvider(String type) { return providers.get(type); } - public Map getProviders() { - return providers; - } } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java index 29c08d9ca..afeb42eaa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java @@ -37,4 +37,6 @@ public interface DeviceTypeDAO { Integer getDeviceTypeIdByDeviceTypeName(String type) throws DeviceManagementDAOException; + void removeDeviceType(DeviceType deviceType) throws DeviceManagementDAOException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java index 049ed0424..98e6a98c2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java @@ -124,6 +124,11 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { return deviceTypeId; } + @Override + public void removeDeviceType(DeviceType deviceType) throws DeviceManagementDAOException { + + } + private Connection getConnection() throws DeviceManagementDAOException { try { return dataSource.getConnection(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index ab71bdc5c..b973ebd06 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -87,24 +87,47 @@ public final class DeviceManagerUtil { /** * Adds a new device type to the database if it does not exists. * - * @param deviceTypeName device type + * @param deviceType device type * @return status of the operation */ - public static boolean registerDeviceType(String deviceTypeName) throws DeviceManagementException{ - boolean status = false; + public static boolean registerDeviceType(String deviceType) throws DeviceManagementException { + boolean status; try { DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceTypeName); - if(deviceTypeId == null){ - DeviceType deviceType = new DeviceType(); - deviceType.setName(deviceTypeName); - deviceTypeDAO.addDeviceType(deviceType); + Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType); + if (deviceTypeId == null) { + DeviceType dt = new DeviceType(); + dt.setName(deviceType); + deviceTypeDAO.addDeviceType(dt); } status = true; } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while registering the device type " + deviceTypeName; + String msg = "Error occurred while registering the device type " + deviceType; throw new DeviceManagementException(msg, e); } return status; } + + /** + * Unregisters an existing device type from the device management metadata repository. + * + * @param deviceType device type + * @return status of the operation + */ + public static boolean unregisterDeviceType(String deviceType) throws DeviceManagementException { + try { + DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); + Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType); + if (deviceTypeId == null) { + DeviceType dt = new DeviceType(); + dt.setName(deviceType); + deviceTypeDAO.removeDeviceType(dt); + } + return true; + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while registering the device type " + deviceType; + throw new DeviceManagementException(msg, e); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java index b657a9f03..bc4707b86 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java @@ -16,20 +16,42 @@ package org.wso2.carbon.device.mgt.core; import org.testng.Assert; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; public class DeviceManagementRepositoryTests { + private DeviceManagementRepository repository; + + @BeforeClass + public void initRepository() { + this.repository = new DeviceManagementRepository(); + } + @Test public void testAddDeviceManagementService() { DeviceManagerService sourceProvider = new TestDeviceManagerService(); - DeviceManagementRepository repository = new DeviceManagementRepository(); - repository.addDeviceManagementProvider(sourceProvider); + this.getRepository().addDeviceManagementProvider(sourceProvider); DeviceManagerService targetProvider = - repository.getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); + this.getRepository().getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); + Assert.assertEquals(targetProvider.getProviderType(), sourceProvider.getProviderType()); } + @Test(dependsOnMethods = "testAddDeviceManagementService") + public void testRemoveDeviceManagementService() { + DeviceManagerService sourceProvider = new TestDeviceManagerService(); + this.getRepository().removeDeviceManagementProvider(sourceProvider); + + DeviceManagerService targetProvider = + this.getRepository().getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); + Assert.assertNull(targetProvider); + } + + private DeviceManagementRepository getRepository() { + return repository; + } + }