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) {
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<String, DeviceManagerService> getProviders() {
return providers;
}
}

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

@ -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();

@ -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);
}
}
}

@ -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;
}
}

Loading…
Cancel
Save