Adding test cases to test unregistering a device management service from the device management repository

revert-dabc3590
prabathabey 10 years ago
parent 233f9e8d1f
commit 9e695ecc85

@ -35,19 +35,26 @@ public class DeviceManagementRepository {
public void addDeviceManagementProvider(DeviceManagerService provider) { public void addDeviceManagementProvider(DeviceManagerService provider) {
String deviceType = provider.getProviderType(); String deviceType = provider.getProviderType();
providers.put(deviceType, provider);
try { try {
DeviceManagerUtil.registerDeviceType(deviceType); 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) { } catch (DeviceManagementException e) {
log.error("Exception occured while registering the device type.",e); log.error("Exception occured while registering the device type.",e);
} }
providers.remove(deviceType);
} }
public DeviceManagerService getDeviceManagementProvider(String type) { public DeviceManagerService getDeviceManagementProvider(String type) {
return providers.get(type); return providers.get(type);
} }
public Map<String, DeviceManagerService> getProviders() {
return providers;
}
} }

@ -37,4 +37,6 @@ public interface DeviceTypeDAO {
Integer getDeviceTypeIdByDeviceTypeName(String type) throws DeviceManagementDAOException; Integer getDeviceTypeIdByDeviceTypeName(String type) throws DeviceManagementDAOException;
void removeDeviceType(DeviceType deviceType) throws DeviceManagementDAOException;
} }

@ -124,6 +124,11 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
return deviceTypeId; return deviceTypeId;
} }
@Override
public void removeDeviceType(DeviceType deviceType) throws DeviceManagementDAOException {
}
private Connection getConnection() throws DeviceManagementDAOException { private Connection getConnection() throws DeviceManagementDAOException {
try { try {
return dataSource.getConnection(); return dataSource.getConnection();

@ -87,24 +87,47 @@ public final class DeviceManagerUtil {
/** /**
* Adds a new device type to the database if it does not exists. * 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 * @return status of the operation
*/ */
public static boolean registerDeviceType(String deviceTypeName) throws DeviceManagementException{ public static boolean registerDeviceType(String deviceType) throws DeviceManagementException {
boolean status = false; boolean status;
try { try {
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceTypeName); Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType);
if(deviceTypeId == null){ if (deviceTypeId == null) {
DeviceType deviceType = new DeviceType(); DeviceType dt = new DeviceType();
deviceType.setName(deviceTypeName); dt.setName(deviceType);
deviceTypeDAO.addDeviceType(deviceType); deviceTypeDAO.addDeviceType(dt);
} }
status = true; status = true;
} catch (DeviceManagementDAOException e) { } 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); throw new DeviceManagementException(msg, e);
} }
return status; 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);
}
}
} }

@ -16,20 +16,42 @@
package org.wso2.carbon.device.mgt.core; package org.wso2.carbon.device.mgt.core;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService;
public class DeviceManagementRepositoryTests { public class DeviceManagementRepositoryTests {
private DeviceManagementRepository repository;
@BeforeClass
public void initRepository() {
this.repository = new DeviceManagementRepository();
}
@Test @Test
public void testAddDeviceManagementService() { public void testAddDeviceManagementService() {
DeviceManagerService sourceProvider = new TestDeviceManagerService(); DeviceManagerService sourceProvider = new TestDeviceManagerService();
DeviceManagementRepository repository = new DeviceManagementRepository(); this.getRepository().addDeviceManagementProvider(sourceProvider);
repository.addDeviceManagementProvider(sourceProvider);
DeviceManagerService targetProvider = DeviceManagerService targetProvider =
repository.getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); this.getRepository().getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST);
Assert.assertEquals(targetProvider.getProviderType(), sourceProvider.getProviderType()); 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;
}
} }

Loading…
Cancel
Save