Refactoring

revert-70aa11f8
megala21 7 years ago
parent 80f7d45372
commit 444bccecfb

@ -189,6 +189,9 @@ public class DeviceTypeManager implements DeviceManager {
@Override @Override
public boolean saveConfiguration(PlatformConfiguration tenantConfiguration) public boolean saveConfiguration(PlatformConfiguration tenantConfiguration)
throws DeviceManagementException { throws DeviceManagementException {
if (tenantConfiguration == null) {
throw new DeviceManagementException("Platform configuration is null. Cannot save the configuration");
}
try { try {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Persisting " + deviceType + " configurations in Registry"); log.debug("Persisting " + deviceType + " configurations in Registry");
@ -246,6 +249,9 @@ public class DeviceTypeManager implements DeviceManager {
@Override @Override
public boolean enrollDevice(Device device) throws DeviceManagementException { public boolean enrollDevice(Device device) throws DeviceManagementException {
if (device == null) {
throw new DeviceManagementException("Device is null. Cannot enroll the device.");
}
if (propertiesExist) { if (propertiesExist) {
boolean status = false; boolean status = false;
boolean isEnrolled = this.isEnrolled( boolean isEnrolled = this.isEnrolled(
@ -313,6 +319,9 @@ public class DeviceTypeManager implements DeviceManager {
@Override @Override
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
if (deviceId == null) {
throw new DeviceManagementException("Cannot check the enrollment status of a null device");
}
if (propertiesExist) { if (propertiesExist) {
boolean isEnrolled = false; boolean isEnrolled = false;
try { try {
@ -347,6 +356,9 @@ public class DeviceTypeManager implements DeviceManager {
@Override @Override
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
if (deviceId == null) {
throw new DeviceManagementException("Cannot get the device. DeviceIdentifier is null");
}
if (propertiesExist) { if (propertiesExist) {
Device device; Device device;
try { try {

@ -44,7 +44,6 @@ public class DeviceTypePluginDAOImpl implements PluginDAO {
private String selectDBQueryForGetDevice; private String selectDBQueryForGetDevice;
private String createDBqueryForAddDevice; private String createDBqueryForAddDevice;
private String updateDBQueryForUpdateDevice; private String updateDBQueryForUpdateDevice;
private String deleteDBQueryToRemoveDevicd;
private String selectDBQueryToGetAllDevice; private String selectDBQueryToGetAllDevice;
public DeviceTypePluginDAOImpl(DeviceDAODefinition deviceDAODefinition, public DeviceTypePluginDAOImpl(DeviceDAODefinition deviceDAODefinition,
@ -158,33 +157,6 @@ public class DeviceTypePluginDAOImpl implements PluginDAO {
return status; return status;
} }
public boolean deleteDevice(String deviceId) throws DeviceTypeMgtPluginException {
boolean status = false;
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = deviceTypeDAOHandler.getConnection();
stmt = conn.prepareStatement(deleteDBQueryToRemoveDevicd);
stmt.setString(1, deviceId);
int rows = stmt.executeUpdate();
if (rows > 0) {
status = true;
if (log.isDebugEnabled()) {
log.debug("device " + deviceId + " data has deleted from the " +
deviceDAODefinition.getDeviceTableName() + " table.");
}
}
} catch (SQLException e) {
String msg =
"Error occurred while deleting " + deviceDAODefinition.getDeviceTableName() + " device " + deviceId;
log.error(msg, e);
throw new DeviceTypeMgtPluginException(msg, e);
} finally {
DeviceTypeUtils.cleanupResources(stmt, null);
}
return status;
}
public List<Device> getAllDevices() throws DeviceTypeMgtPluginException { public List<Device> getAllDevices() throws DeviceTypeMgtPluginException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -264,10 +236,6 @@ public class DeviceTypePluginDAOImpl implements PluginDAO {
updateDBQueryForUpdateDevice = "UPDATE " + deviceDAODefinition.getDeviceTableName() + " SET " updateDBQueryForUpdateDevice = "UPDATE " + deviceDAODefinition.getDeviceTableName() + " SET "
+ getDeviceTableColumnNamesForUpdateQuery() + " WHERE " + deviceDAODefinition.getPrimaryKey() + " = ?"; + getDeviceTableColumnNamesForUpdateQuery() + " WHERE " + deviceDAODefinition.getPrimaryKey() + " = ?";
deleteDBQueryToRemoveDevicd =
"DELETE FROM " + deviceDAODefinition.getDeviceTableName() + " WHERE " + deviceDAODefinition
.getPrimaryKey() + " = ?";
selectDBQueryToGetAllDevice = selectDBQueryToGetAllDevice =
"SELECT " + getDeviceTableColumnNames() + "," + deviceDAODefinition.getPrimaryKey() + " FROM " "SELECT " + getDeviceTableColumnNames() + "," + deviceDAODefinition.getPrimaryKey() + " FROM "
+ deviceDAODefinition.getDeviceTableName(); + deviceDAODefinition.getDeviceTableName();

@ -30,7 +30,5 @@ public interface PluginDAO {
boolean updateDevice(Device device) throws DeviceTypeMgtPluginException; boolean updateDevice(Device device) throws DeviceTypeMgtPluginException;
boolean deleteDevice(String deviceId) throws DeviceTypeMgtPluginException;
List<Device> getAllDevices() throws DeviceTypeMgtPluginException; List<Device> getAllDevices() throws DeviceTypeMgtPluginException;
} }

@ -158,36 +158,6 @@ public class PropertyBasedPluginDAOImpl implements PluginDAO {
} }
} }
public boolean deleteDevice(String deviceId) throws DeviceTypeMgtPluginException {
boolean status = false;
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = deviceTypeDAOHandler.getConnection();
stmt = conn.prepareStatement("DELETE FROM DM_DEVICE_PROPERTIES WHERE DEVICE_TYPE_NAME = ? " +
"AND DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?");
stmt.setString(1, deviceType);
stmt.setString(2, deviceId);
stmt.setInt(3, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true));
int rows = stmt.executeUpdate();
if (rows > 0) {
status = true;
if (log.isDebugEnabled()) {
log.debug("device " + deviceId + " data has deleted from the " +
deviceType + " table.");
}
}
} catch (SQLException e) {
String msg =
"Error occurred while deleting " + deviceType + " device " + deviceId;
log.error(msg, e);
throw new DeviceTypeMgtPluginException(msg, e);
} finally {
DeviceTypeUtils.cleanupResources(stmt, null);
}
return status;
}
public List<Device> getAllDevices() throws DeviceTypeMgtPluginException { public List<Device> getAllDevices() throws DeviceTypeMgtPluginException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -220,7 +190,7 @@ public class PropertyBasedPluginDAOImpl implements PluginDAO {
log.debug( log.debug(
"All device details have fetched from " + deviceType + " table."); "All device details have fetched from " + deviceType + " table.");
} }
return Arrays.asList((Device[])deviceMap.values().toArray()); return new ArrayList<>(deviceMap.values());
} catch (SQLException e) { } catch (SQLException e) {
String msg = String msg =
"Error occurred while fetching all " + deviceType + " device data'"; "Error occurred while fetching all " + deviceType + " device data'";

@ -305,12 +305,12 @@ public class DeviceTypeManagerServiceTest {
DeviceTypeConfigurationException, JAXBException { DeviceTypeConfigurationException, JAXBException {
ClassLoader classLoader = getClass().getClassLoader(); ClassLoader classLoader = getClass().getClassLoader();
URL resourceUrl = classLoader.getResource("arduino.xml"); URL resourceUrl = classLoader.getResource("arduino.xml");
File raspberrypiConfiguration = null; File arduinoConfiguration = null;
if (resourceUrl != null) { if (resourceUrl != null) {
raspberrypiConfiguration = new File(resourceUrl.getFile()); arduinoConfiguration = new File(resourceUrl.getFile());
} }
arduinoDeviceTypeConfiguration = Utils.getDeviceTypeConfiguration(raspberrypiConfiguration); arduinoDeviceTypeConfiguration = Utils.getDeviceTypeConfiguration(arduinoConfiguration);
arduinoDeviceTypeManagerService = new DeviceTypeManagerService(new arduinoDeviceTypeManagerService = new DeviceTypeManagerService(
DeviceTypeConfigIdentifier("arduino", "carbon.super"), arduinoDeviceTypeConfiguration); new DeviceTypeConfigIdentifier("arduino", "carbon.super"), arduinoDeviceTypeConfiguration);
} }
} }

@ -26,12 +26,16 @@ import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DataSource;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceDetails; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceDetails;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceTypeConfiguration; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceTypeConfiguration;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Properties; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Properties;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.exception.DeviceTypeConfigurationException; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.exception.DeviceTypeConfigurationException;
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.*; import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceDAODefinition;
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypeDAOHandler;
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOImpl;
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOManager;
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.PluginDAO;
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.PropertyBasedPluginDAOImpl;
import org.wso2.carbon.device.mgt.extensions.utils.Utils; import org.wso2.carbon.device.mgt.extensions.utils.Utils;
import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@ -47,6 +51,7 @@ import java.net.URL;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
@ -54,10 +59,18 @@ import java.util.List;
*/ */
public class DeviceTypeManagerTest { public class DeviceTypeManagerTest {
private DeviceTypeManager androidDeviceTypeManager; private DeviceTypeManager androidDeviceTypeManager;
private DeviceTypeManager customDeviceTypeManager;
private DeviceIdentifier nonExistingDeviceIdentifier; private DeviceIdentifier nonExistingDeviceIdentifier;
private Device sampleDevice1; private Device sampleDevice1;
private Device sampleDevice2; private Device sampleDevice2;
private Device customDevice;
private String androidDeviceType; private String androidDeviceType;
private String customDeviceType = "customDeviceType";
private Field datasourceField;
private Field currentConnection;
private Field deviceTypePluginDAOField;
private Field deviceTypeDAOHandlerField;
private String[] customDeviceTypeProperties = {"custom_property", "custom_property2"};
@BeforeTest(description = "Mocking the classes for testing") @BeforeTest(description = "Mocking the classes for testing")
public void setup() throws NoSuchFieldException, IllegalAccessException, IOException, SQLException, SAXException, public void setup() throws NoSuchFieldException, IllegalAccessException, IOException, SQLException, SAXException,
@ -67,26 +80,24 @@ public class DeviceTypeManagerTest {
androidDeviceType = "android"; androidDeviceType = "android";
File androidDatabaseScript = null; File androidDatabaseScript = null;
javax.sql.DataSource dataSource = null; javax.sql.DataSource dataSource = null;
File carbonHome = new File("src/test/resources/carbon-home"); File androidConfiguration = null;
if (resourceUrl != null) { if (resourceUrl != null) {
androidDatabaseScript = new File(resourceUrl.getFile()); androidDatabaseScript = new File(resourceUrl.getFile());
} }
if (carbonHome.exists()) {
System.setProperty("carbon.home", carbonHome.getAbsolutePath());
}
resourceUrl = classLoader.getResource("android.xml"); resourceUrl = classLoader.getResource("android.xml");
File androidConfiguration = null;
if (resourceUrl != null) { if (resourceUrl != null) {
androidConfiguration = new File(resourceUrl.getFile()); androidConfiguration = new File(resourceUrl.getFile());
} }
DeviceTypeConfiguration androidDeviceConfiguration = Utils.getDeviceTypeConfiguration(androidConfiguration); DeviceTypeConfiguration androidDeviceConfiguration = Utils.getDeviceTypeConfiguration(androidConfiguration);
androidDeviceTypeManager = Mockito.mock(DeviceTypeManager.class, Mockito.CALLS_REAL_METHODS); androidDeviceTypeManager = Mockito.mock(DeviceTypeManager.class, Mockito.CALLS_REAL_METHODS);
customDeviceTypeManager = Mockito.mock(DeviceTypeManager.class, Mockito.CALLS_REAL_METHODS);
if (androidDatabaseScript != null) { if (androidDatabaseScript != null) {
dataSource = Utils.createDataTables("deviceType", androidDatabaseScript.getAbsolutePath()); dataSource = Utils.createDataTables("customDeviceType", androidDatabaseScript.getAbsolutePath());
} }
DeviceTypePluginDAOManager deviceTypePluginDAOManager = createMockDeviceTypePluginDAOManager(dataSource, DeviceTypePluginDAOManager deviceTypePluginDAOManager = createandroidDeviceTypePluginDAOManager(dataSource,
androidDeviceConfiguration); androidDeviceConfiguration);
Field deviceTypePluginDAOManagerField = DeviceTypeManager.class.getDeclaredField("deviceTypePluginDAOManager"); Field deviceTypePluginDAOManagerField = DeviceTypeManager.class.getDeclaredField("deviceTypePluginDAOManager");
deviceTypePluginDAOManagerField.setAccessible(true); deviceTypePluginDAOManagerField.setAccessible(true);
@ -96,9 +107,25 @@ public class DeviceTypeManagerTest {
propertiesExist.setAccessible(true); propertiesExist.setAccessible(true);
Field deviceType = DeviceTypeManager.class.getDeclaredField("deviceType"); Field deviceType = DeviceTypeManager.class.getDeclaredField("deviceType");
deviceType.setAccessible(true); deviceType.setAccessible(true);
datasourceField = DeviceTypeDAOHandler.class.getDeclaredField("dataSource");
datasourceField.setAccessible(true);
currentConnection = DeviceTypeDAOHandler.class.getDeclaredField("currentConnection");
currentConnection.setAccessible(true);
deviceTypePluginDAOField = DeviceTypePluginDAOManager.class.getDeclaredField("deviceTypePluginDAO");
deviceTypePluginDAOField.setAccessible(true);
deviceTypeDAOHandlerField = DeviceTypePluginDAOManager.class.getDeclaredField("deviceTypeDAOHandler");
deviceTypeDAOHandlerField.setAccessible(true);
deviceType.set(androidDeviceTypeManager, androidDeviceType); deviceType.set(androidDeviceTypeManager, androidDeviceType);
propertiesExist.set(androidDeviceTypeManager, true); propertiesExist.set(androidDeviceTypeManager, true);
createDevice(); createAndroidDevice();
DeviceTypePluginDAOManager propertyBasedPluginDAOManager = createPluginBasedDeviceTypeManager();
deviceTypePluginDAOManagerField.set(customDeviceTypeManager, propertyBasedPluginDAOManager);
deviceType.set(customDeviceTypeManager, customDeviceType);
propertiesExist.set(customDeviceTypeManager, true);
createCustomDevice();
} }
@Test(description = "This test case tests IsEnrolled method of the DeviceTypeManager", @Test(description = "This test case tests IsEnrolled method of the DeviceTypeManager",
@ -106,10 +133,17 @@ public class DeviceTypeManagerTest {
public void testIsEnrolled() throws DeviceManagementException { public void testIsEnrolled() throws DeviceManagementException {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(sampleDevice2.getDeviceIdentifier(), DeviceIdentifier deviceIdentifier = new DeviceIdentifier(sampleDevice2.getDeviceIdentifier(),
sampleDevice2.getType()); sampleDevice2.getType());
Assert.assertTrue(!androidDeviceTypeManager.isEnrolled(nonExistingDeviceIdentifier), DeviceIdentifier nonExistingCustomDeviceIdentifier = new DeviceIdentifier(sampleDevice2.getDeviceIdentifier(),
"Device with " + "NON-Existing ID is not enrolled, but this shows as enrolled"); customDevice.getType());
Assert.assertFalse(androidDeviceTypeManager.isEnrolled(nonExistingDeviceIdentifier),
"Device with NON-Existing ID is not enrolled, but this shows as enrolled");
Assert.assertTrue(androidDeviceTypeManager.isEnrolled(deviceIdentifier), Assert.assertTrue(androidDeviceTypeManager.isEnrolled(deviceIdentifier),
"Enrolled device is shown as un-enrolled"); "Enrolled device is shown as un-enrolled");
Assert.assertFalse(customDeviceTypeManager.isEnrolled(nonExistingCustomDeviceIdentifier),
"Custom device type manager returns an non-existing device as enrolled");
Assert.assertTrue(customDeviceTypeManager.isEnrolled(new DeviceIdentifier(customDeviceType, customDeviceType))
, "Enrolled device is shown as un-enrolled in custom device type manager");
} }
@Test(description = "This test case tests the getDevcie method of the DeviceTypeManager", dependsOnMethods = @Test(description = "This test case tests the getDevcie method of the DeviceTypeManager", dependsOnMethods =
@ -120,20 +154,37 @@ public class DeviceTypeManagerTest {
Assert.assertNull(androidDeviceTypeManager.getDevice(nonExistingDeviceIdentifier), Assert.assertNull(androidDeviceTypeManager.getDevice(nonExistingDeviceIdentifier),
"Non existing sampleDevice was retrieved"); "Non existing sampleDevice was retrieved");
Assert.assertNotNull(androidDeviceTypeManager.getDevice(existingDeviceIdntifier), Assert.assertNotNull(androidDeviceTypeManager.getDevice(existingDeviceIdntifier),
"Existing sampleDevice was retrieved"); "Existing sampleDevice was not retrieved");
Device customDevice1 = customDeviceTypeManager
.getDevice(new DeviceIdentifier(customDeviceType, customDeviceType));
Assert.assertEquals(customDevice1.getProperties().size(), 2,
"GetDevice call" + " failed in custom deviceTypeManager");
} }
@Test(description = "This test case tests the enrollment of the device") @Test(description = "This test case tests the enrollment of the device")
public void testEnrollDevice() throws DeviceManagementException { public void testEnrollDevice() throws DeviceManagementException {
Assert.assertTrue(androidDeviceTypeManager.enrollDevice(sampleDevice1)); Assert.assertTrue(androidDeviceTypeManager.enrollDevice(sampleDevice1), "New android device enrollment failed");
Assert.assertTrue(!androidDeviceTypeManager.enrollDevice(sampleDevice2)); Assert.assertFalse(androidDeviceTypeManager.enrollDevice(sampleDevice2),
"Modification to existing android " + "device enrollment failed");
Assert.assertTrue(customDeviceTypeManager.enrollDevice(customDevice), "Custom device type enrollment failed.");
List<Device.Property> properties = customDevice.getProperties();
Device.Property property = new Device.Property();
property.setName("test");
property.setValue("test");
properties.add(property);
customDevice.setProperties(properties);
Assert.assertFalse(customDeviceTypeManager.enrollDevice(customDevice),
"Custom device type re-enrollment " + "failed.");
} }
@Test(description = "This test case tests the get all devices method of the DeviceTypeManager", dependsOnMethods @Test(description = "This test case tests the get all devices method of the DeviceTypeManager", dependsOnMethods
= {"testEnrollDevice"}) = {"testEnrollDevice"})
public void testGetAllDevices() throws DeviceManagementException { public void testGetAllDevices() throws DeviceManagementException {
Assert.assertEquals(androidDeviceTypeManager.getAllDevices().size(), 1, Assert.assertEquals(androidDeviceTypeManager.getAllDevices().size(), 1,
"All the added devices are not fetched " + "from the database"); "All the added devices are not fetched from the database");
Assert.assertEquals(customDeviceTypeManager.getAllDevices().size(), 1,
"All the added devices are not fetched from the database");
} }
@Test(description = "This test case tests the addition of platform configuration and retrieval of the same") @Test(description = "This test case tests the addition of platform configuration and retrieval of the same")
@ -147,6 +198,7 @@ public class DeviceTypeManagerTest {
"Platform Configuration saved and retrieved correctly in " + "DeviceType Manager"); "Platform Configuration saved and retrieved correctly in " + "DeviceType Manager");
Assert.assertEquals(actualPlatformConfiguration.getType(), androidDeviceType, Assert.assertEquals(actualPlatformConfiguration.getType(), androidDeviceType,
"Platform Configuration saved and " + "retrieved correctly in DeviceType Manager"); "Platform Configuration saved and " + "retrieved correctly in DeviceType Manager");
Assert.assertNull(customDeviceTypeManager.getConfiguration());
} }
@Test (description = "This test case tests the getDefaultConfiguration method") @Test (description = "This test case tests the getDefaultConfiguration method")
@ -169,9 +221,9 @@ public class DeviceTypeManagerTest {
} }
/** /**
* To create a sample sampleDevice to add to DAO Layer. * To create sample android devices to add to DAO Layer.
*/ */
private void createDevice() { private void createAndroidDevice() {
nonExistingDeviceIdentifier = new DeviceIdentifier("NON-EXISTING", androidDeviceType); nonExistingDeviceIdentifier = new DeviceIdentifier("NON-EXISTING", androidDeviceType);
List<Device.Property> list = new ArrayList<>(); List<Device.Property> list = new ArrayList<>();
@ -190,6 +242,21 @@ public class DeviceTypeManagerTest {
sampleDevice2 = new Device("testdevice1", androidDeviceType, "test", "testdevice", null, null, list); sampleDevice2 = new Device("testdevice1", androidDeviceType, "test", "testdevice", null, null, list);
} }
/**
* To create a sample custom device.
*/
private void createCustomDevice () {
List<Device.Property> list = new ArrayList<>();
for(String customProperty : customDeviceTypeProperties) {
Device.Property property = new Device.Property();
property.setName(customProperty);
property.setValue(customProperty);
list.add(property);
}
customDevice = new Device(customDeviceType, customDeviceType, customDeviceType, customDeviceType, null,
null, list);
}
/* /*
* To create a mock sampleDevice type plugin dao manager. * To create a mock sampleDevice type plugin dao manager.
* @param dataSource DataSource for the DAO layer * @param dataSource DataSource for the DAO layer
@ -198,13 +265,8 @@ public class DeviceTypeManagerTest {
* @throws NoSuchFieldException No Such Field Exception * @throws NoSuchFieldException No Such Field Exception
* @throws IllegalAccessException Illegal Access Exception * @throws IllegalAccessException Illegal Access Exception
*/ */
private DeviceTypePluginDAOManager createMockDeviceTypePluginDAOManager(javax.sql.DataSource dataSource, private DeviceTypePluginDAOManager createandroidDeviceTypePluginDAOManager(javax.sql.DataSource dataSource,
DeviceTypeConfiguration androidDeviceConfiguration) throws NoSuchFieldException, IllegalAccessException { DeviceTypeConfiguration androidDeviceConfiguration) throws NoSuchFieldException, IllegalAccessException {
Field datasourceField = DeviceTypeDAOHandler.class.getDeclaredField("dataSource");
datasourceField.setAccessible(true);
Field currentConnection = DeviceTypeDAOHandler.class.getDeclaredField("currentConnection");
currentConnection.setAccessible(true);
DeviceTypeDAOHandler deviceTypeDAOHandler = Mockito DeviceTypeDAOHandler deviceTypeDAOHandler = Mockito
.mock(DeviceTypeDAOHandler.class, Mockito.CALLS_REAL_METHODS); .mock(DeviceTypeDAOHandler.class, Mockito.CALLS_REAL_METHODS);
datasourceField.set(deviceTypeDAOHandler, dataSource); datasourceField.set(deviceTypeDAOHandler, dataSource);
@ -213,22 +275,25 @@ public class DeviceTypeManagerTest {
DeviceDAODefinition deviceDAODefinition = Utils.getDeviceDAODefinition(androidDeviceConfiguration); DeviceDAODefinition deviceDAODefinition = Utils.getDeviceDAODefinition(androidDeviceConfiguration);
DeviceTypePluginDAOImpl deviceTypePluginDAO = new DeviceTypePluginDAOImpl(deviceDAODefinition, DeviceTypePluginDAOImpl deviceTypePluginDAO = new DeviceTypePluginDAOImpl(deviceDAODefinition,
deviceTypeDAOHandler); deviceTypeDAOHandler);
DeviceTypePluginDAOManager deviceTypePluginDAOManager = Mockito DeviceTypePluginDAOManager deviceTypePluginDAOManager = Mockito
.mock(DeviceTypePluginDAOManager.class, Mockito.CALLS_REAL_METHODS); .mock(DeviceTypePluginDAOManager.class, Mockito.CALLS_REAL_METHODS);
Field deviceTypePluginDAOField = DeviceTypePluginDAOManager.class.getDeclaredField("deviceTypePluginDAO");
deviceTypePluginDAOField.setAccessible(true);
Field deviceTypeDAOHandlerField = DeviceTypePluginDAOManager.class.getDeclaredField("deviceTypeDAOHandler");
deviceTypeDAOHandlerField.setAccessible(true);
deviceTypePluginDAOField.set(deviceTypePluginDAOManager, deviceTypePluginDAO); deviceTypePluginDAOField.set(deviceTypePluginDAOManager, deviceTypePluginDAO);
deviceTypeDAOHandlerField.set(deviceTypePluginDAOManager, deviceTypeDAOHandler); deviceTypeDAOHandlerField.set(deviceTypePluginDAOManager, deviceTypeDAOHandler);
return deviceTypePluginDAOManager; return deviceTypePluginDAOManager;
} }
private void createPluginBasedDeviceTypeManager() /**
* To create a plugin based device type manager.
*
* @return Plugin based device type manager.
* @throws IOException IO Exception.
* @throws SQLException SQL Exception
* @throws NoSuchFieldException No Such File Exception.
* @throws IllegalAccessException Illegal Access Exception.
*/
private DeviceTypePluginDAOManager createPluginBasedDeviceTypeManager()
throws IOException, SQLException, NoSuchFieldException, IllegalAccessException { throws IOException, SQLException, NoSuchFieldException, IllegalAccessException {
String deviceType = "new_property_device_type";
ClassLoader classLoader = getClass().getClassLoader(); ClassLoader classLoader = getClass().getClassLoader();
URL resourceUrl = classLoader.getResource("h2.sql"); URL resourceUrl = classLoader.getResource("h2.sql");
File cdmDataScript = null; File cdmDataScript = null;
@ -237,37 +302,28 @@ public class DeviceTypeManagerTest {
cdmDataScript = new File(resourceUrl.getFile()); cdmDataScript = new File(resourceUrl.getFile());
} }
if (cdmDataScript != null) { if (cdmDataScript != null) {
dataSource = Utils.createDataTables("deviceType", cdmDataScript.getAbsolutePath()); dataSource = Utils.createDataTables(customDeviceType, cdmDataScript.getAbsolutePath());
} }
Field datasourceField = DeviceTypeDAOHandler.class.getDeclaredField("dataSource");
datasourceField.setAccessible(true);
Field currentConnection = DeviceTypeDAOHandler.class.getDeclaredField("currentConnection");
currentConnection.setAccessible(true);
DeviceDetails deviceDetails = new DeviceDetails(); DeviceDetails deviceDetails = new DeviceDetails();
List<String> propertyList = new ArrayList<>(); List<String> propertyList = new ArrayList<>();
propertyList.add("custom_property"); propertyList.addAll(Arrays.asList(customDeviceTypeProperties));
propertyList.add("custom_property");
Properties properties = new Properties(); Properties properties = new Properties();
properties.addProperties(propertyList);
deviceDetails.setProperties(properties);
DeviceTypeDAOHandler deviceTypeDAOHandler = Mockito DeviceTypeDAOHandler deviceTypeDAOHandler = Mockito
.mock(DeviceTypeDAOHandler.class, Mockito.CALLS_REAL_METHODS); .mock(DeviceTypeDAOHandler.class, Mockito.CALLS_REAL_METHODS);
datasourceField.set(deviceTypeDAOHandler, dataSource); datasourceField.set(deviceTypeDAOHandler, dataSource);
currentConnection.set(deviceTypeDAOHandler, new ThreadLocal<Connection>()); currentConnection.set(deviceTypeDAOHandler, new ThreadLocal<Connection>());
PluginDAO deviceTypePluginDAO = new PropertyBasedPluginDAOImpl(deviceDetails, deviceTypeDAOHandler,
PluginDAO deviceTypePluginDAO = new PropertyBasedPluginDAOImpl(deviceDetails, deviceTypeDAOHandler, deviceType); customDeviceType);
DeviceTypePluginDAOManager deviceTypePluginDAOManager = Mockito DeviceTypePluginDAOManager deviceTypePluginDAOManager = Mockito
.mock(DeviceTypePluginDAOManager.class, Mockito.CALLS_REAL_METHODS); .mock(DeviceTypePluginDAOManager.class, Mockito.CALLS_REAL_METHODS);
Field deviceTypePluginDAOField = DeviceTypePluginDAOManager.class.getDeclaredField("deviceTypePluginDAO");
deviceTypePluginDAOField.setAccessible(true);
Field deviceTypeDAOHandlerField = DeviceTypePluginDAOManager.class.getDeclaredField("deviceTypeDAOHandler");
deviceTypeDAOHandlerField.setAccessible(true);
deviceTypePluginDAOField.set(deviceTypePluginDAOManager, deviceTypePluginDAO); deviceTypePluginDAOField.set(deviceTypePluginDAOManager, deviceTypePluginDAO);
deviceTypeDAOHandlerField.set(deviceTypePluginDAOManager, deviceTypeDAOHandler); deviceTypeDAOHandlerField.set(deviceTypePluginDAOManager, deviceTypeDAOHandler);
return deviceTypePluginDAOManager;
} }
} }

@ -21,34 +21,19 @@ package org.wso2.carbon.device.mgt.extensions.device.type.template;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.context.internal.OSGiDataHolder;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeMetaDefinition; import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeMetaDefinition;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceTypeConfiguration; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceTypeConfiguration;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Feature; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Feature;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Operation;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.PushNotificationProvider; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.PushNotificationProvider;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.exception.DeviceTypeConfigurationException; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.exception.DeviceTypeConfigurationException;
import org.wso2.carbon.device.mgt.extensions.internal.DeviceTypeExtensionDataHolder;
import org.wso2.carbon.device.mgt.extensions.license.mgt.registry.RegistryBasedLicenseManager;
import org.wso2.carbon.device.mgt.extensions.utils.Utils; import org.wso2.carbon.device.mgt.extensions.utils.Utils;
import org.wso2.carbon.governance.api.util.GovernanceArtifactConfiguration;
import org.wso2.carbon.governance.api.util.GovernanceUtils;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.utils.FileUtil;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
@ -59,8 +44,6 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.wso2.carbon.governance.api.util.GovernanceUtils.getGovernanceArtifactConfiguration;
/** /**
* This test case contains the tests for {@link HTTPDeviceTypeManagerService} and {@link DeviceTypeGeneratorServiceImpl} * This test case contains the tests for {@link HTTPDeviceTypeManagerService} and {@link DeviceTypeGeneratorServiceImpl}
*/ */
@ -69,7 +52,6 @@ public class HttpDeviceTypeManagerServiceAndDeviceTypeGeneratorServceTest {
private HTTPDeviceTypeManagerService httpDeviceTypeManagerService; private HTTPDeviceTypeManagerService httpDeviceTypeManagerService;
private DeviceTypeGeneratorServiceImpl deviceTypeGeneratorService; private DeviceTypeGeneratorServiceImpl deviceTypeGeneratorService;
private String androidSenseDeviceType = "androidsense"; private String androidSenseDeviceType = "androidsense";
private String sampleDeviceType = "sample";
@BeforeTest @BeforeTest
public void setup() throws RegistryException, IOException, SAXException, ParserConfigurationException, public void setup() throws RegistryException, IOException, SAXException, ParserConfigurationException,
@ -100,6 +82,7 @@ public class HttpDeviceTypeManagerServiceAndDeviceTypeGeneratorServceTest {
@Test(description = "This test case tests the populate device management service method") @Test(description = "This test case tests the populate device management service method")
public void testPopulateDeviceManagementService() { public void testPopulateDeviceManagementService() {
String sampleDeviceType = "sample";
DeviceManagementService deviceManagementService = deviceTypeGeneratorService DeviceManagementService deviceManagementService = deviceTypeGeneratorService
.populateDeviceManagementService(sampleDeviceType, deviceTypeMetaDefinition); .populateDeviceManagementService(sampleDeviceType, deviceTypeMetaDefinition);
Assert.assertEquals(deviceManagementService.getType(), sampleDeviceType, Assert.assertEquals(deviceManagementService.getType(), sampleDeviceType,
@ -107,6 +90,29 @@ public class HttpDeviceTypeManagerServiceAndDeviceTypeGeneratorServceTest {
} }
@Test(description = "This test case tests the negative scenarios when saving the platform configurations",
expectedExceptions = {DeviceManagementException.class})
public void testSaveConfiguration() throws DeviceManagementException {
httpDeviceTypeManagerService.getDeviceManager().saveConfiguration(null);
}
@Test(description = "This test case tests the negative scenarios when getting a device",
expectedExceptions = {DeviceManagementException.class})
public void testGetDevice() throws DeviceManagementException {
httpDeviceTypeManagerService.getDeviceManager().getDevice(null);
}
@Test(description = "This test case tests the negative scenario when checking whether a device has enrolled",
expectedExceptions = {DeviceManagementException.class})
public void testIsEnrolled() throws DeviceManagementException {
httpDeviceTypeManagerService.getDeviceManager().isEnrolled(null);
}
@Test(description = "This test case tests the negative scenario when enrolling a device",
expectedExceptions = {DeviceManagementException.class})
public void testEnroll() throws DeviceManagementException {
httpDeviceTypeManagerService.getDeviceManager().enrollDevice(null);
}
/** /**
* To create a sample device type meta defintion. * To create a sample device type meta defintion.

@ -106,6 +106,7 @@ public class Utils {
dataSource.setUser("sa"); dataSource.setUser("sa");
dataSource.setPassword("sa"); dataSource.setPassword("sa");
File file = new File(scriptFilePath); File file = new File(scriptFilePath);
final String LOAD_DATA_QUERY = "RUNSCRIPT FROM '" + file.getCanonicalPath() + "'"; final String LOAD_DATA_QUERY = "RUNSCRIPT FROM '" + file.getCanonicalPath() + "'";

Loading…
Cancel
Save