From 845749df14496ab3b2d699a84da678a17dd31685 Mon Sep 17 00:00:00 2001 From: megala21 Date: Tue, 10 Oct 2017 20:24:46 +0530 Subject: [PATCH] Adding test cases and improving existing test cases --- .../type/template/DeviceTypeManager.java | 7 +- .../dao/PropertyBasedPluginDAOImpl.java | 11 +- .../type/template/util/DeviceTypeUtils.java | 25 +- .../DeviceTypeManagerNegativeTest.java | 221 +++++++++++++++++- ...rviceAndDeviceTypeGeneratorServceTest.java | 33 +++ .../mgt/extensions/utils/UtilsTest.java | 41 ++++ .../wrong-default-platform-configuration.xml | 23 ++ .../carbon-home/repository/conf/registry.xml | 2 +- .../test/resources/device-types/android.xml | 8 + .../device-types/defective-devicetype.xml | 9 - .../device-types/defective-devicetype3.xml | 61 +++++ 11 files changed, 410 insertions(+), 31 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/carbon-home/repository/conf/etc/device-mgt-plugin-configs/mobile/wrong-default-platform-configuration.xml create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/defective-devicetype3.xml 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 1f1ee0d868..6e2aff8b35 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 @@ -83,9 +83,7 @@ public class DeviceTypeManager implements DeviceManager { if (deviceTypeConfiguration.getFeatures() != null && deviceTypeConfiguration.getFeatures(). getFeature() != null) { List features = deviceTypeConfiguration.getFeatures().getFeature(); - if (features != null) { - featureManager = new ConfigurationBasedFeatureManager(features); - } + featureManager = new ConfigurationBasedFeatureManager(features); } if (deviceTypeConfiguration.getDeviceAuthorizationConfig() != null) { requiredDeviceTypeAuthorization = deviceTypeConfiguration.getDeviceAuthorizationConfig(). @@ -119,7 +117,8 @@ public class DeviceTypeManager implements DeviceManager { try { defaultPlatformConfiguration = this.getDefaultConfiguration(); } catch (DeviceManagementException e) { - String msg = "Error occurred while default platform configuration"; + String msg = + "Error occurred while getting default platform configuration for the device type " + deviceType; throw new DeviceTypeDeployerPayloadException(msg, e); } 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 ceedeb7454..0e1502f34a 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 @@ -126,14 +126,13 @@ public class PropertyBasedPluginDAOImpl implements PluginDAO { } public boolean updateDevice(Device device) throws DeviceTypeMgtPluginException { - boolean status = false; - Connection conn = null; + Connection conn; PreparedStatement stmt = null; try { conn = deviceTypeDAOHandler.getConnection(); stmt = conn.prepareStatement( - "UPDATE DM_DEVICE_PROPERTIES SET PROPERTY_VALUE = ? WHERE DEVICE_TYPE_NAME = ? AND " + - "DEVICE_IDENTIFICATION = ? AND PROPERTY_NAME = ? AND TENANT_ID= ?"); + "UPDATE DM_DEVICE_PROPERTIES SET PROPERTY_VALUE = ? WHERE DEVICE_TYPE_NAME = ? AND " + + "DEVICE_IDENTIFICATION = ? AND PROPERTY_NAME = ? AND TENANT_ID= ?"); for (Device.Property property : device.getProperties()) { if (!deviceProps.contains(property.getName())) { @@ -149,8 +148,8 @@ public class PropertyBasedPluginDAOImpl implements PluginDAO { stmt.executeBatch(); return true; } catch (SQLException e) { - String msg = "Error occurred while modifying the device '" + - device.getDeviceIdentifier() + "' data on" + deviceType; + String msg = "Error occurred while modifying the device '" + device.getDeviceIdentifier() + "' data on" + + deviceType; log.error(msg, e); throw new DeviceTypeMgtPluginException(msg, e); } finally { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/util/DeviceTypeUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/util/DeviceTypeUtils.java index d409d22b38..f6dd19a8f4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/util/DeviceTypeUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/util/DeviceTypeUtils.java @@ -25,6 +25,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeMgtPluginException; import org.wso2.carbon.device.mgt.extensions.internal.DeviceTypeExtensionDataHolder; import org.wso2.carbon.registry.api.RegistryException; +import org.wso2.carbon.registry.api.RegistryService; import org.wso2.carbon.registry.api.Resource; import org.wso2.carbon.registry.core.Registry; @@ -101,25 +102,29 @@ public class DeviceTypeUtils { public static Registry getConfigurationRegistry() throws DeviceTypeMgtPluginException { try { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - return DeviceTypeExtensionDataHolder.getInstance().getRegistryService() - .getConfigSystemRegistry(tenantId); + org.wso2.carbon.registry.core.service.RegistryService registryService = DeviceTypeExtensionDataHolder + .getInstance().getRegistryService(); + return registryService == null ? null : registryService.getConfigSystemRegistry(tenantId); } catch (RegistryException e) { throw new DeviceTypeMgtPluginException("Error in retrieving conf registry instance: " + e.getMessage(), e); } } public static boolean putRegistryResource(String path, Resource resource) throws DeviceTypeMgtPluginException { - boolean status; try { - DeviceTypeUtils.getConfigurationRegistry().beginTransaction(); - DeviceTypeUtils.getConfigurationRegistry().put(path, resource); - DeviceTypeUtils.getConfigurationRegistry().commitTransaction(); - status = true; + Registry registry = getConfigurationRegistry(); + if (registry == null) { + return false; + } else { + registry.beginTransaction(); + registry.put(path, resource); + registry.commitTransaction(); + return true; + } } catch (RegistryException e) { - throw new DeviceTypeMgtPluginException("Error occurred while persisting registry resource : " + - e.getMessage(), e); + throw new DeviceTypeMgtPluginException( + "Error occurred while persisting registry resource : " + e.getMessage(), e); } - return status; } public static Resource getRegistryResource(String path) throws DeviceTypeMgtPluginException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerNegativeTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerNegativeTest.java index d2d9060d12..7d8de52148 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerNegativeTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerNegativeTest.java @@ -18,13 +18,26 @@ package org.wso2.carbon.device.mgt.extensions.device.type.template; +import org.h2.jdbcx.JdbcDataSource; +import org.mockito.Mockito; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import org.wso2.carbon.base.MultitenantConstants; +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.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.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.PropertyBasedPluginDAOImpl; import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeDeployerPayloadException; +import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeMgtPluginException; import org.wso2.carbon.device.mgt.extensions.utils.Utils; import org.xml.sax.SAXException; @@ -32,7 +45,11 @@ import javax.xml.bind.JAXBException; import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; import java.net.URL; +import java.sql.Connection; +import java.util.ArrayList; +import java.util.List; /** * This class tests the negative scenarios in {@link DeviceTypeManager} initialization; @@ -40,15 +57,22 @@ import java.net.URL; public class DeviceTypeManagerNegativeTest { private DeviceTypeConfiguration defectiveDeviceTypeConfiguration1; private DeviceTypeConfiguration defectiveDeviceTypeConfiguration2; + private DeviceTypeConfiguration defectiveDeviceTypeConfiguration3; private DeviceTypeConfiguration androidDeviceTypeConfiguration; private DeviceTypeConfigIdentifier deviceTypeConfigIdentifier; + private DeviceTypeManager androidDeviceTypeManager; + private DeviceTypeDAOHandler deviceTypeDAOHandler; private final String DEFECTIVE_DEVICE_TYPE = "defectiveDeviceType"; private final String TABLE_NAME = "DEFECTIVE_DEVICE"; + private DeviceIdentifier deviceIdentifier; + private final String ANDROID_DEVICE_TYPE = "android"; + private PropertyBasedPluginDAOImpl propertyBasedPluginDAO; + private Device sampleDevice; @BeforeTest public void setup() throws SAXException, JAXBException, ParserConfigurationException, DeviceTypeConfigurationException, - IOException { + IOException, NoSuchFieldException, IllegalAccessException { ClassLoader classLoader = getClass().getClassLoader(); URL resourceUrl = classLoader.getResource(Utils.DEVICE_TYPE_FOLDER + "defective-devicetype.xml"); File configurationFile = null; @@ -76,6 +100,37 @@ public class DeviceTypeManagerNegativeTest { if (configurationFile != null) { androidDeviceTypeConfiguration = Utils.getDeviceTypeConfiguration(configurationFile.getAbsoluteFile()); } + + resourceUrl = classLoader.getResource(Utils.DEVICE_TYPE_FOLDER + "defective-devicetype3.xml"); + if (resourceUrl != null) { + configurationFile = new File(resourceUrl.getFile()); + } + if (configurationFile != null) { + defectiveDeviceTypeConfiguration3 = Utils + .getDeviceTypeConfiguration(configurationFile.getAbsoluteFile()); + } + createDefectiveDeviceTypeManager(); + deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId("test"); + deviceIdentifier.setType(ANDROID_DEVICE_TYPE); + + DeviceDetails deviceDetails = new DeviceDetails(); + Properties properties = new Properties(); + List propertyList = new ArrayList<>(); + propertyList.add("test"); + properties.addProperties(propertyList); + + deviceDetails.setProperties(properties); + propertyBasedPluginDAO = new PropertyBasedPluginDAOImpl(deviceDetails, + deviceTypeDAOHandler, ANDROID_DEVICE_TYPE); + sampleDevice = new Device(); + sampleDevice.setDeviceIdentifier("test"); + List deviceProperties = new ArrayList<>(); + Device.Property property = new Device.Property(); + property.setName("test"); + property.setValue("test"); + deviceProperties.add(property); + sampleDevice.setProperties(deviceProperties); } @Test(description = "This test case tests the behaviour of the DeviceTypeManager creation without defining the " @@ -121,4 +176,168 @@ public class DeviceTypeManagerNegativeTest { new DeviceTypeManager(deviceTypeConfigIdentifier, androidDeviceTypeConfiguration); } + + @Test(description = "This test case tests the behaviour of the DeviceTypeManager creation when having a " + + "defective platform configuration ", expectedExceptions = {DeviceTypeDeployerPayloadException.class}, + expectedExceptionsMessageRegExp = "Error occurred while getting default platform configuration for the " + + "device type wrong *") + public void testWithDefectivePlatformConfiguration() { + DeviceTypeConfigIdentifier wrongDeviceTypeConfigIdentifier = new DeviceTypeConfigIdentifier("wrong", + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + new DeviceTypeManager(wrongDeviceTypeConfigIdentifier, androidDeviceTypeConfiguration); + } + + @Test(description = "This test case tests the behaviour of the DeviceTypeManager creation when having a " + + "defective platform configuration ", expectedExceptions = {DeviceTypeDeployerPayloadException.class}, + expectedExceptionsMessageRegExp = "Error while looking up the data source:.*") + public void testWithoutDeviceSpecificTable() { + new DeviceTypeManager(deviceTypeConfigIdentifier, defectiveDeviceTypeConfiguration3); + } + + @Test(description = "This test case tests the behaviour of the isEnrolled when the relevant tables are not there", + expectedExceptions = {DeviceManagementException.class}, + expectedExceptionsMessageRegExp = "Error while checking the enrollment status of android device.*") + public void testIsEnrolled() throws DeviceManagementException { + androidDeviceTypeManager.isEnrolled(deviceIdentifier); + } + + @Test(description = "This test case tests the behaviour of the modifyEnrollment when the relevant tables " + + "are not there", + expectedExceptions = {DeviceManagementException.class}, + expectedExceptionsMessageRegExp = "Error while updating the enrollment of the.*") + public void testModifyEnrollment() throws DeviceManagementException { + Device device = new Device(); + device.setDeviceIdentifier(deviceIdentifier.getId()); + device.setType(deviceIdentifier.getType()); + androidDeviceTypeManager.modifyEnrollment(device); + } + + @Test(description = "This test case tests the behaviour of the getAllDevices when the relevant tables " + + "are not there", + expectedExceptions = {DeviceManagementException.class}, + expectedExceptionsMessageRegExp = "Error occurred while fetching all.*") + public void testGetAllDevices() throws DeviceManagementException { + androidDeviceTypeManager.getAllDevices(); + } + + @Test(description = "This test case tests the behaviour of the updateDeviceInfo when the relevant tables " + + "are not there", + expectedExceptions = {DeviceManagementException.class}, + expectedExceptionsMessageRegExp = "Error occurred while fetching the.*") + public void testUpdateDeviceInfo() throws DeviceManagementException { + Device device = new Device(); + device.setDeviceIdentifier(deviceIdentifier.getId()); + device.setType(deviceIdentifier.getType()); + androidDeviceTypeManager.updateDeviceInfo(deviceIdentifier, device); + } + + @Test(description = ("This test case tests the behaviour of the enrollDevice when the relevant tables are not " + + "there"), expectedExceptions = {DeviceManagementException.class}, + expectedExceptionsMessageRegExp = "Error while enrolling the.*", dependsOnMethods = {"testIsEnrolled"}) + public void testEnrollDevice() throws DeviceManagementException { + Device device = new Device(); + device.setDeviceIdentifier(deviceIdentifier.getId()); + device.setType(deviceIdentifier.getType()); + Mockito.doReturn(false).when(androidDeviceTypeManager).isEnrolled(Mockito.any()); + androidDeviceTypeManager.enrollDevice(device); + } + + @Test(description = ("This test case tests the behaviour of the updateDeviceInfo when the relevant tables are not " + + "there"), expectedExceptions = {DeviceManagementException.class}, + expectedExceptionsMessageRegExp = "Error occurred while updating the.*", dependsOnMethods = + {"testUpdateDeviceInfo"}) + public void testUpdateDeviceWithMock() throws DeviceManagementException { + Mockito.doReturn(new Device()).when(androidDeviceTypeManager).getDevice(Mockito.any()); + androidDeviceTypeManager.updateDeviceInfo(deviceIdentifier, sampleDevice); + } + + @Test(description = "This test case tests the behaviour of addDevice when the relevant tables are not available", + expectedExceptions = { DeviceTypeMgtPluginException.class }, + expectedExceptionsMessageRegExp = "Error occurred while adding the device .*") + public void testAddDevice() throws DeviceTypeMgtPluginException { + propertyBasedPluginDAO.addDevice(sampleDevice); + } + + @Test(description = "This test case tests the behaviour of getDevice when the relevant tables are not available", + expectedExceptions = { DeviceTypeMgtPluginException.class }, + expectedExceptionsMessageRegExp = "Error occurred while fetching device .*") + public void testGetPropertyBasedDevice() throws DeviceTypeMgtPluginException { + propertyBasedPluginDAO.getDevice("id"); + } + + @Test(description = "This test case tests the behaviour of the getAllDevices method of the PropertyBasedPuginDAO", + expectedExceptions = {DeviceTypeMgtPluginException.class}, expectedExceptionsMessageRegExp = "Error " + + "occurred while fetching all.*") + public void testGetAllPropertyBasedDevices() throws DeviceTypeMgtPluginException { + propertyBasedPluginDAO.getAllDevices(); + } + + @Test(description = "This test case tests the behaviour of the updateDevice method of the PropertyBasedPuginDAO", + expectedExceptions = {DeviceTypeMgtPluginException.class}, expectedExceptionsMessageRegExp = "Error " + + "occurred while modifying the device.*") + public void testUpdateDevice() throws DeviceTypeMgtPluginException { + propertyBasedPluginDAO.updateDevice(sampleDevice); + } + + /** + * To create a defective device type manager for testing. + * @throws NoSuchFieldException No Such Field Exception. + * @throws SAXException SAX Exception. + * @throws JAXBException JAXB Exception + * @throws ParserConfigurationException Parser Configuration Exception. + * @throws DeviceTypeConfigurationException Device Type Configuration Exception. + * @throws IOException IO Exception. + * @throws IllegalAccessException Illegal Access Exception. + */ + private void createDefectiveDeviceTypeManager() + throws NoSuchFieldException, SAXException, JAXBException, ParserConfigurationException, + DeviceTypeConfigurationException, IOException, IllegalAccessException { + Field datasourceField = DeviceTypeDAOHandler.class.getDeclaredField("dataSource"); + datasourceField.setAccessible(true); + Field currentConnection = DeviceTypeDAOHandler.class.getDeclaredField("currentConnection"); + currentConnection.setAccessible(true); + Field deviceTypePluginDAOField = DeviceTypePluginDAOManager.class.getDeclaredField("deviceTypePluginDAO"); + deviceTypePluginDAOField.setAccessible(true); + Field deviceTypeDAOHandlerField = DeviceTypePluginDAOManager.class.getDeclaredField("deviceTypeDAOHandler"); + deviceTypeDAOHandlerField.setAccessible(true); + + ClassLoader classLoader = getClass().getClassLoader(); + URL resourceUrl = classLoader.getResource(Utils.DEVICE_TYPE_FOLDER + "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); + + deviceTypeDAOHandler = Mockito + .mock(DeviceTypeDAOHandler.class, Mockito.CALLS_REAL_METHODS); + + JdbcDataSource dataSource = new JdbcDataSource(); + dataSource.setURL("jdbc:h2:mem:notexist;DB_CLOSE_DELAY=-1"); + dataSource.setUser("sa"); + dataSource.setPassword("sa"); + datasourceField.set(deviceTypeDAOHandler, dataSource); + currentConnection.set(deviceTypeDAOHandler, new ThreadLocal()); + + DeviceDAODefinition deviceDAODefinition = Utils.getDeviceDAODefinition(androidDeviceConfiguration); + DeviceTypePluginDAOImpl deviceTypePluginDAO = new DeviceTypePluginDAOImpl(deviceDAODefinition, + deviceTypeDAOHandler); + DeviceTypePluginDAOManager deviceTypePluginDAOManager = Mockito + .mock(DeviceTypePluginDAOManager.class, Mockito.CALLS_REAL_METHODS); + deviceTypePluginDAOField.set(deviceTypePluginDAOManager, deviceTypePluginDAO); + deviceTypeDAOHandlerField.set(deviceTypePluginDAOManager, deviceTypeDAOHandler); + + Field deviceTypePluginDAOManagerField = DeviceTypeManager.class.getDeclaredField("deviceTypePluginDAOManager"); + deviceTypePluginDAOManagerField.setAccessible(true); + deviceTypePluginDAOManagerField.set(androidDeviceTypeManager, deviceTypePluginDAOManager); + + Field propertiesExist = DeviceTypeManager.class.getDeclaredField("propertiesExist"); + propertiesExist.setAccessible(true); + Field deviceType = DeviceTypeManager.class.getDeclaredField("deviceType"); + deviceType.setAccessible(true); + + deviceType.set(androidDeviceTypeManager, ANDROID_DEVICE_TYPE); + propertiesExist.set(androidDeviceTypeManager, true); + } } 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 7c4ff9ec2c..d34ed73a3b 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 @@ -42,9 +42,13 @@ import javax.xml.bind.JAXBException; import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * This test case contains the tests for {@link HTTPDeviceTypeManagerService} and {@link DeviceTypeGeneratorServiceImpl} @@ -142,6 +146,28 @@ public class HttpDeviceTypeManagerServiceAndDeviceTypeGeneratorServceTest { httpDeviceTypeManagerService.getDeviceManager().enrollDevice(null); } + @Test(description = "This test case tests the getDeviceTypeConfiguration method", + dependsOnMethods = {"testPopulateDeviceManagementService"}) + public void testGetDeviceTypeConfiguration() + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Method getDeviceTypeConfiguration = HTTPDeviceTypeManagerService.class + .getDeclaredMethod("getDeviceTypeConfiguration", String.class, DeviceTypeMetaDefinition.class); + getDeviceTypeConfiguration.setAccessible(true); + List properties = new ArrayList<>(); + properties.add("test"); + deviceTypeMetaDefinition.setProperties(properties); + Map mapProperties = new HashMap<>(); + mapProperties.put("test", "test"); + PushNotificationConfig pushNotificationConfig = new PushNotificationConfig("push", true, mapProperties); + deviceTypeMetaDefinition.setPushNotificationConfig(pushNotificationConfig); + DeviceTypeConfiguration deviceTypeConfiguration = (DeviceTypeConfiguration) getDeviceTypeConfiguration + .invoke(httpDeviceTypeManagerService, "android", deviceTypeMetaDefinition); + Assert.assertEquals(deviceTypeMetaDefinition.getProperties().size(), + deviceTypeConfiguration.getDeviceDetails().getProperties().getProperty().size(), "Number of " + + "properties added in device-type meta definition is not equal to the properties added in " + + "the DeviceType Configuration"); + } + /** * To create a sample device type meta defintion. * @throws SAXException SAX Exception. @@ -180,6 +206,13 @@ public class HttpDeviceTypeManagerServiceAndDeviceTypeGeneratorServceTest { commonFeature.setCode(feature.getCode()); commonFeature.setDescription(feature.getDescription()); commonFeature.setName(feature.getName()); + org.wso2.carbon.device.mgt.common.Feature.MetadataEntry metadataEntry = new org.wso2.carbon.device.mgt + .common.Feature.MetadataEntry(); + metadataEntry.setId(1); + metadataEntry.setValue("test"); + List metadataEntries = new ArrayList<>(); + metadataEntries.add(metadataEntry); + commonFeature.setMetadataEntries(metadataEntries); features.add(commonFeature); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/utils/UtilsTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/utils/UtilsTest.java index 01651d3b2c..a682ab99bb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/utils/UtilsTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/utils/UtilsTest.java @@ -23,10 +23,15 @@ import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import org.wso2.carbon.base.MultitenantConstants; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; +import org.wso2.carbon.device.mgt.extensions.device.type.template.DeviceTypeConfigIdentifier; +import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeMgtPluginException; import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceSchemaInitializer; +import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceTypeUtils; import org.wso2.carbon.device.mgt.extensions.license.mgt.file.FileSystemBasedLicenseManager; +import org.wso2.carbon.device.mgt.extensions.license.mgt.registry.RegistryBasedLicenseManager; import java.io.File; import java.lang.reflect.InvocationTargetException; @@ -81,4 +86,40 @@ public class UtilsTest { public void testFileBasedLicenseManagerAddLicense() throws LicenseManagementException { fileSystemBasedLicenseManager.addLicense("test", null); } + + @Test(description = "This test case tests the DeviceTypeConfigIdentifier equals method") + public void testDeviceTypeConfigIdentifier() { + DeviceTypeConfigIdentifier deviceTypeConfigIdentifier = new DeviceTypeConfigIdentifier("sample", + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + DeviceTypeConfigIdentifier clonedDeviceTypeConfigIdentifier = new DeviceTypeConfigIdentifier("sample", + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + Assert.assertTrue(deviceTypeConfigIdentifier.equals(clonedDeviceTypeConfigIdentifier), + "Clone device type config identifier gives wrong results for equals check."); + Assert.assertFalse(deviceTypeConfigIdentifier.equals(null), + "Device Type config identifier object comparison gives wrong results."); + } + + @Test(description = "This test cases tests the registry based license addition") + public void testAddRegistryBasedLicense() throws LicenseManagementException { + String newLicenseString = "New License"; + License fileBasedLicense = fileSystemBasedLicenseManager.getLicense("test", "en_US"); + RegistryBasedLicenseManager registryBasedLicenseManager = new RegistryBasedLicenseManager(); + registryBasedLicenseManager.addLicense("test", fileBasedLicense); + Assert.assertEquals(fileBasedLicense.getText(), + registryBasedLicenseManager.getLicense("test", "en_US").getText(), "Registry license addition failed"); + fileBasedLicense.setText(newLicenseString); + registryBasedLicenseManager.addLicense("test", fileBasedLicense); + Assert.assertEquals(registryBasedLicenseManager.getLicense("test", "en_US").getText(), newLicenseString, + "Registry license update failed"); + } + + @Test(description = "This test case tests the GetConfigurationRegistry method when the registry service is " + + "unavailable", dependsOnMethods = {"testDeviceSchemaInitializer", "testDeviceTypeConfigIdentifier", + "testAddRegistryBasedLicense"}, + expectedExceptions = {DeviceTypeMgtPluginException.class}, + expectedExceptionsMessageRegExp = "Error in retrieving conf registry instance:.*") + public void testGetConfigurationRegistry() throws DeviceTypeMgtPluginException { + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(4); + DeviceTypeUtils.getConfigurationRegistry(); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/carbon-home/repository/conf/etc/device-mgt-plugin-configs/mobile/wrong-default-platform-configuration.xml b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/carbon-home/repository/conf/etc/device-mgt-plugin-configs/mobile/wrong-default-platform-configuration.xml new file mode 100644 index 0000000000..645805d7a8 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/carbon-home/repository/conf/etc/device-mgt-plugin-configs/mobile/wrong-default-platform-configuration.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/carbon-home/repository/conf/registry.xml b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/carbon-home/repository/conf/registry.xml index 0bb97a7dc9..036b3dda06 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/carbon-home/repository/conf/registry.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/carbon-home/repository/conf/registry.xml @@ -24,7 +24,7 @@ wso2registry false - true + false / diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/android.xml b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/android.xml index 176469c329..0320698ff4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/android.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/android.xml @@ -51,6 +51,11 @@ true + + + true + + + + + + + defective + + + + + + + Control Bulb + Control Bulb on Arduino Uno + + + state + + + + + + + true + + + + en_US + 1.0.0 + This is license text + + + + + jdbc/MobileAndroidDM_DS + + + +
+
+
+ +