From 444bccecfbe6570bba177c25424b4291cd422172 Mon Sep 17 00:00:00 2001 From: megala21 Date: Thu, 28 Sep 2017 20:55:06 +0530 Subject: [PATCH] Refactoring --- .../type/template/DeviceTypeManager.java | 12 ++ .../template/dao/DeviceTypePluginDAOImpl.java | 32 ---- .../device/type/template/dao/PluginDAO.java | 2 - .../dao/PropertyBasedPluginDAOImpl.java | 32 +--- .../DeviceTypeManagerServiceTest.java | 10 +- .../type/template/DeviceTypeManagerTest.java | 150 ++++++++++++------ ...rviceAndDeviceTypeGeneratorServceTest.java | 42 ++--- .../device/mgt/extensions/utils/Utils.java | 1 + 8 files changed, 146 insertions(+), 135 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java index b0c4f8312e..1f0915bd90 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java @@ -189,6 +189,9 @@ public class DeviceTypeManager implements DeviceManager { @Override public boolean saveConfiguration(PlatformConfiguration tenantConfiguration) throws DeviceManagementException { + if (tenantConfiguration == null) { + throw new DeviceManagementException("Platform configuration is null. Cannot save the configuration"); + } try { if (log.isDebugEnabled()) { log.debug("Persisting " + deviceType + " configurations in Registry"); @@ -246,6 +249,9 @@ public class DeviceTypeManager implements DeviceManager { @Override public boolean enrollDevice(Device device) throws DeviceManagementException { + if (device == null) { + throw new DeviceManagementException("Device is null. Cannot enroll the device."); + } if (propertiesExist) { boolean status = false; boolean isEnrolled = this.isEnrolled( @@ -313,6 +319,9 @@ public class DeviceTypeManager implements DeviceManager { @Override public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { + if (deviceId == null) { + throw new DeviceManagementException("Cannot check the enrollment status of a null device"); + } if (propertiesExist) { boolean isEnrolled = false; try { @@ -347,6 +356,9 @@ public class DeviceTypeManager implements DeviceManager { @Override public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + if (deviceId == null) { + throw new DeviceManagementException("Cannot get the device. DeviceIdentifier is null"); + } if (propertiesExist) { Device device; try { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypePluginDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypePluginDAOImpl.java index e705661daa..755cb0397b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypePluginDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypePluginDAOImpl.java @@ -44,7 +44,6 @@ public class DeviceTypePluginDAOImpl implements PluginDAO { private String selectDBQueryForGetDevice; private String createDBqueryForAddDevice; private String updateDBQueryForUpdateDevice; - private String deleteDBQueryToRemoveDevicd; private String selectDBQueryToGetAllDevice; public DeviceTypePluginDAOImpl(DeviceDAODefinition deviceDAODefinition, @@ -158,33 +157,6 @@ public class DeviceTypePluginDAOImpl implements PluginDAO { 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 getAllDevices() throws DeviceTypeMgtPluginException { Connection conn; PreparedStatement stmt = null; @@ -264,10 +236,6 @@ public class DeviceTypePluginDAOImpl implements PluginDAO { updateDBQueryForUpdateDevice = "UPDATE " + deviceDAODefinition.getDeviceTableName() + " SET " + getDeviceTableColumnNamesForUpdateQuery() + " WHERE " + deviceDAODefinition.getPrimaryKey() + " = ?"; - deleteDBQueryToRemoveDevicd = - "DELETE FROM " + deviceDAODefinition.getDeviceTableName() + " WHERE " + deviceDAODefinition - .getPrimaryKey() + " = ?"; - selectDBQueryToGetAllDevice = "SELECT " + getDeviceTableColumnNames() + "," + deviceDAODefinition.getPrimaryKey() + " FROM " + deviceDAODefinition.getDeviceTableName(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PluginDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PluginDAO.java index 2ffd37f8f4..47606fe6a6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PluginDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PluginDAO.java @@ -30,7 +30,5 @@ public interface PluginDAO { boolean updateDevice(Device device) throws DeviceTypeMgtPluginException; - boolean deleteDevice(String deviceId) throws DeviceTypeMgtPluginException; - List getAllDevices() throws DeviceTypeMgtPluginException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java index 227c352a82..ceedeb7454 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java @@ -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 getAllDevices() throws DeviceTypeMgtPluginException { Connection conn; PreparedStatement stmt = null; @@ -220,7 +190,7 @@ public class PropertyBasedPluginDAOImpl implements PluginDAO { log.debug( "All device details have fetched from " + deviceType + " table."); } - return Arrays.asList((Device[])deviceMap.values().toArray()); + return new ArrayList<>(deviceMap.values()); } catch (SQLException e) { String msg = "Error occurred while fetching all " + deviceType + " device data'"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerServiceTest.java index 2e1b1a52ac..286f21c931 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerServiceTest.java @@ -305,12 +305,12 @@ public class DeviceTypeManagerServiceTest { DeviceTypeConfigurationException, JAXBException { ClassLoader classLoader = getClass().getClassLoader(); URL resourceUrl = classLoader.getResource("arduino.xml"); - File raspberrypiConfiguration = null; + File arduinoConfiguration = null; if (resourceUrl != null) { - raspberrypiConfiguration = new File(resourceUrl.getFile()); + arduinoConfiguration = new File(resourceUrl.getFile()); } - arduinoDeviceTypeConfiguration = Utils.getDeviceTypeConfiguration(raspberrypiConfiguration); - arduinoDeviceTypeManagerService = new DeviceTypeManagerService(new - DeviceTypeConfigIdentifier("arduino", "carbon.super"), arduinoDeviceTypeConfiguration); + arduinoDeviceTypeConfiguration = Utils.getDeviceTypeConfiguration(arduinoConfiguration); + arduinoDeviceTypeManagerService = new DeviceTypeManagerService( + new DeviceTypeConfigIdentifier("arduino", "carbon.super"), arduinoDeviceTypeConfiguration); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerTest.java index fa76609776..d896eaa73d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerTest.java @@ -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.DeviceManagementException; 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.DeviceTypeConfiguration; 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.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.registry.core.exceptions.RegistryException; import org.xml.sax.SAXException; @@ -47,6 +51,7 @@ import java.net.URL; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -54,10 +59,18 @@ import java.util.List; */ public class DeviceTypeManagerTest { private DeviceTypeManager androidDeviceTypeManager; + private DeviceTypeManager customDeviceTypeManager; private DeviceIdentifier nonExistingDeviceIdentifier; private Device sampleDevice1; private Device sampleDevice2; + private Device customDevice; 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") public void setup() throws NoSuchFieldException, IllegalAccessException, IOException, SQLException, SAXException, @@ -67,26 +80,24 @@ public class DeviceTypeManagerTest { androidDeviceType = "android"; File androidDatabaseScript = null; javax.sql.DataSource dataSource = null; - File carbonHome = new File("src/test/resources/carbon-home"); + File androidConfiguration = null; if (resourceUrl != null) { androidDatabaseScript = new File(resourceUrl.getFile()); } - if (carbonHome.exists()) { - System.setProperty("carbon.home", carbonHome.getAbsolutePath()); - } resourceUrl = classLoader.getResource("android.xml"); - File androidConfiguration = null; + if (resourceUrl != null) { androidConfiguration = new File(resourceUrl.getFile()); } DeviceTypeConfiguration androidDeviceConfiguration = Utils.getDeviceTypeConfiguration(androidConfiguration); androidDeviceTypeManager = Mockito.mock(DeviceTypeManager.class, Mockito.CALLS_REAL_METHODS); + customDeviceTypeManager = Mockito.mock(DeviceTypeManager.class, Mockito.CALLS_REAL_METHODS); if (androidDatabaseScript != null) { - dataSource = Utils.createDataTables("deviceType", androidDatabaseScript.getAbsolutePath()); + dataSource = Utils.createDataTables("customDeviceType", androidDatabaseScript.getAbsolutePath()); } - DeviceTypePluginDAOManager deviceTypePluginDAOManager = createMockDeviceTypePluginDAOManager(dataSource, + DeviceTypePluginDAOManager deviceTypePluginDAOManager = createandroidDeviceTypePluginDAOManager(dataSource, androidDeviceConfiguration); Field deviceTypePluginDAOManagerField = DeviceTypeManager.class.getDeclaredField("deviceTypePluginDAOManager"); deviceTypePluginDAOManagerField.setAccessible(true); @@ -96,9 +107,25 @@ public class DeviceTypeManagerTest { propertiesExist.setAccessible(true); Field deviceType = DeviceTypeManager.class.getDeclaredField("deviceType"); 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); 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", @@ -106,10 +133,17 @@ public class DeviceTypeManagerTest { public void testIsEnrolled() throws DeviceManagementException { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(sampleDevice2.getDeviceIdentifier(), sampleDevice2.getType()); - Assert.assertTrue(!androidDeviceTypeManager.isEnrolled(nonExistingDeviceIdentifier), - "Device with " + "NON-Existing ID is not enrolled, but this shows as enrolled"); + DeviceIdentifier nonExistingCustomDeviceIdentifier = new DeviceIdentifier(sampleDevice2.getDeviceIdentifier(), + 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), "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 = @@ -120,20 +154,37 @@ public class DeviceTypeManagerTest { Assert.assertNull(androidDeviceTypeManager.getDevice(nonExistingDeviceIdentifier), "Non existing sampleDevice was retrieved"); 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") public void testEnrollDevice() throws DeviceManagementException { - Assert.assertTrue(androidDeviceTypeManager.enrollDevice(sampleDevice1)); - Assert.assertTrue(!androidDeviceTypeManager.enrollDevice(sampleDevice2)); + Assert.assertTrue(androidDeviceTypeManager.enrollDevice(sampleDevice1), "New android device enrollment failed"); + Assert.assertFalse(androidDeviceTypeManager.enrollDevice(sampleDevice2), + "Modification to existing android " + "device enrollment failed"); + Assert.assertTrue(customDeviceTypeManager.enrollDevice(customDevice), "Custom device type enrollment failed."); + List 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 = {"testEnrollDevice"}) public void testGetAllDevices() throws DeviceManagementException { 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") @@ -147,6 +198,7 @@ public class DeviceTypeManagerTest { "Platform Configuration saved and retrieved correctly in " + "DeviceType Manager"); Assert.assertEquals(actualPlatformConfiguration.getType(), androidDeviceType, "Platform Configuration saved and " + "retrieved correctly in DeviceType Manager"); + Assert.assertNull(customDeviceTypeManager.getConfiguration()); } @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); List list = new ArrayList<>(); @@ -190,6 +242,21 @@ public class DeviceTypeManagerTest { sampleDevice2 = new Device("testdevice1", androidDeviceType, "test", "testdevice", null, null, list); } + /** + * To create a sample custom device. + */ + private void createCustomDevice () { + List 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. * @param dataSource DataSource for the DAO layer @@ -198,13 +265,8 @@ public class DeviceTypeManagerTest { * @throws NoSuchFieldException No Such Field 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 { - Field datasourceField = DeviceTypeDAOHandler.class.getDeclaredField("dataSource"); - datasourceField.setAccessible(true); - Field currentConnection = DeviceTypeDAOHandler.class.getDeclaredField("currentConnection"); - currentConnection.setAccessible(true); - DeviceTypeDAOHandler deviceTypeDAOHandler = Mockito .mock(DeviceTypeDAOHandler.class, Mockito.CALLS_REAL_METHODS); datasourceField.set(deviceTypeDAOHandler, dataSource); @@ -213,22 +275,25 @@ public class DeviceTypeManagerTest { DeviceDAODefinition deviceDAODefinition = Utils.getDeviceDAODefinition(androidDeviceConfiguration); DeviceTypePluginDAOImpl deviceTypePluginDAO = new DeviceTypePluginDAOImpl(deviceDAODefinition, deviceTypeDAOHandler); - DeviceTypePluginDAOManager deviceTypePluginDAOManager = Mockito .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); deviceTypeDAOHandlerField.set(deviceTypePluginDAOManager, deviceTypeDAOHandler); 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 { - String deviceType = "new_property_device_type"; ClassLoader classLoader = getClass().getClassLoader(); URL resourceUrl = classLoader.getResource("h2.sql"); File cdmDataScript = null; @@ -237,37 +302,28 @@ public class DeviceTypeManagerTest { cdmDataScript = new File(resourceUrl.getFile()); } 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(); - List propertyList = new ArrayList<>(); - propertyList.add("custom_property"); - propertyList.add("custom_property"); + propertyList.addAll(Arrays.asList(customDeviceTypeProperties)); Properties properties = new Properties(); + properties.addProperties(propertyList); + deviceDetails.setProperties(properties); DeviceTypeDAOHandler deviceTypeDAOHandler = Mockito .mock(DeviceTypeDAOHandler.class, Mockito.CALLS_REAL_METHODS); datasourceField.set(deviceTypeDAOHandler, dataSource); currentConnection.set(deviceTypeDAOHandler, new ThreadLocal()); - - PluginDAO deviceTypePluginDAO = new PropertyBasedPluginDAOImpl(deviceDetails, deviceTypeDAOHandler, deviceType); + PluginDAO deviceTypePluginDAO = new PropertyBasedPluginDAOImpl(deviceDetails, deviceTypeDAOHandler, + customDeviceType); DeviceTypePluginDAOManager deviceTypePluginDAOManager = Mockito .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); deviceTypeDAOHandlerField.set(deviceTypePluginDAOManager, deviceTypeDAOHandler); - + return deviceTypePluginDAOManager; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/HttpDeviceTypeManagerServiceAndDeviceTypeGeneratorServceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/HttpDeviceTypeManagerServiceAndDeviceTypeGeneratorServceTest.java index ab7df25260..14e87a6703 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/HttpDeviceTypeManagerServiceAndDeviceTypeGeneratorServceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/HttpDeviceTypeManagerServiceAndDeviceTypeGeneratorServceTest.java @@ -21,34 +21,19 @@ package org.wso2.carbon.device.mgt.extensions.device.type.template; import org.testng.Assert; import org.testng.annotations.BeforeTest; 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.DeviceIdentifier; 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.push.notification.PushNotificationConfig; 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.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.Operation; 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.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.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.service.RegistryService; -import org.wso2.carbon.registry.core.session.UserRegistry; -import org.wso2.carbon.utils.FileUtil; import org.xml.sax.SAXException; import javax.xml.bind.JAXBException; @@ -59,8 +44,6 @@ import java.net.URL; import java.util.ArrayList; 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} */ @@ -69,7 +52,6 @@ public class HttpDeviceTypeManagerServiceAndDeviceTypeGeneratorServceTest { private HTTPDeviceTypeManagerService httpDeviceTypeManagerService; private DeviceTypeGeneratorServiceImpl deviceTypeGeneratorService; private String androidSenseDeviceType = "androidsense"; - private String sampleDeviceType = "sample"; @BeforeTest 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") public void testPopulateDeviceManagementService() { + String sampleDeviceType = "sample"; DeviceManagementService deviceManagementService = deviceTypeGeneratorService .populateDeviceManagementService(sampleDeviceType, deviceTypeMetaDefinition); 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. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/utils/Utils.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/utils/Utils.java index 0e6c37cd1b..910a5986c5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/utils/Utils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/utils/Utils.java @@ -106,6 +106,7 @@ public class Utils { dataSource.setUser("sa"); dataSource.setPassword("sa"); + File file = new File(scriptFilePath); final String LOAD_DATA_QUERY = "RUNSCRIPT FROM '" + file.getCanonicalPath() + "'";