From 2ef3cfb39a00eba6feea82c4b96be24bed34408d Mon Sep 17 00:00:00 2001 From: Ace Date: Tue, 3 Oct 2017 15:15:18 +0530 Subject: [PATCH] Including additional unit tests for devicemt-core --- .../device/mgt/core/TestDeviceManager.java | 5 +- .../core/common/BaseDeviceManagementTest.java | 20 +++++ .../mgt/core/common/TestDataHolder.java | 4 +- .../DeviceManagementProviderServiceTest.java | 85 +++++++++++++++++++ 4 files changed, 111 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java index c81b4e4eb3..010b47b8d0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java @@ -108,7 +108,10 @@ public class TestDeviceManager implements DeviceManager { @Override public License getLicense(String languageCode) throws LicenseManagementException { - return null; + License testLicense = new License(); + testLicense.setText("This is a dummy license for test device type."); + testLicense.setLanguage("ENG"); + return testLicense; } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java index 51789b1fef..21261bef2b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java @@ -18,6 +18,9 @@ */ package org.wso2.carbon.device.mgt.core.common; +import org.apache.axis2.AxisFault; +import org.apache.axis2.context.ConfigurationContext; +import org.apache.axis2.context.ConfigurationContextFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tomcat.jdbc.pool.PoolProperties; @@ -41,12 +44,14 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import org.wso2.carbon.email.sender.core.service.EmailSenderServiceImpl; import org.wso2.carbon.registry.core.config.RegistryContext; import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.internal.RegistryDataHolder; import org.wso2.carbon.registry.core.jdbc.realm.InMemoryRealmService; import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; +import org.wso2.carbon.utils.ConfigurationContextService; import javax.sql.DataSource; import javax.xml.bind.JAXBContext; @@ -89,6 +94,8 @@ public abstract class BaseDeviceManagementTest { DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl()); DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(new GroupManagementProviderServiceImpl()); DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); + DeviceManagementDataHolder.getInstance().setEmailSenderService(new TestEmailSenderService()); + DeviceManagementDataHolder.getInstance().setConfigurationContextService(getConfigContextService()); } private RegistryService getRegistryService() throws RegistryException { @@ -101,6 +108,19 @@ public abstract class BaseDeviceManagementTest { return context.getEmbeddedRegistryService(); } + private ConfigurationContextService getConfigContextService() throws RegistryException { + ConfigurationContext context = + null; + try { + context = ConfigurationContextFactory.createConfigurationContextFromFileSystem + ("src/test/resources/carbon-home/repository/conf/axis2/axis2.xml"); + } catch (AxisFault axisFault) { + axisFault.printStackTrace(); + } + ConfigurationContextService service = new ConfigurationContextService(context, null); + return service; + } + @BeforeClass public abstract void init() throws Exception; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java index a6868bd89a..355ec9a55a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java @@ -62,8 +62,8 @@ public class TestDataHolder { deviceInfo.setIMEI("IMEI-12345"); deviceInfo.setIMSI("IMSI-12344"); deviceInfo.setDeviceModel("DUMMY_MODEL"); - deviceInfo.setVendor("Google"); - deviceInfo.setOsVersion("Oreo"); + deviceInfo.setVendor("WSO2"); + deviceInfo.setOsVersion("OREO"); deviceInfo.setOsBuildDate("24-05-2017"); deviceInfo.setBatteryLevel(25.0); deviceInfo.setInternalTotalMemory(1.5); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java index d2425c4cdd..94487a0774 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java @@ -24,7 +24,10 @@ 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.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.TransactionManagementException; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; +import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; @@ -48,6 +51,7 @@ import java.io.InputStream; import java.util.Calendar; import java.util.Date; import java.util.List; +import java.util.Properties; public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest { @@ -429,6 +433,87 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes } } + @Test + public void testGetAvaliableDeviceTypes() { + try { + List deviceTypes = deviceMgtService.getAvailableDeviceTypes(); + Assert.assertTrue(!deviceTypes.isEmpty()); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void testGetAllDevices() { + try { + List devices = deviceMgtService.getAllDevices(); + Assert.assertTrue(!devices.isEmpty()); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = {"testDeviceByDate"}) + public void testGetAllDevicesWithInfo() { + try { + List devices = deviceMgtService.getAllDevices(true); + Assert.assertTrue(!devices.isEmpty()); + Assert.assertTrue(devices.get(0).getDeviceInfo() != null); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = {"testDeviceByDate"}) + public void testGetLicense() { + try { + License license = deviceMgtService.getLicense(DEVICE_TYPE, "ENG"); + Assert.assertTrue(license.getLanguage().equalsIgnoreCase("ENG")); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + @Test + public void testSendRegistrationEmailNoMetaInfo() { + try { + deviceMgtService.sendRegistrationEmail(null); + Assert.assertTrue(false); + } catch (ConfigurationManagementException e) { + Assert.assertTrue(false, "Error in sending registration email : Configration " + + "related error" + e.getMessage()); + } catch (DeviceManagementException e) { + Assert.assertTrue(true, "Device Management Exception thrown when meta info for " + + "email sending is not available" + e.getMessage()); + } + } + + @Test + public void testSendRegistrationEmailSuccessFlow() { + try { + String recipient = "test-user@wso2.com"; + Properties props = new Properties(); + props.setProperty("first-name", "Test"); + props.setProperty("username", "User"); + props.setProperty("password", "!@#$$$%"); + + EmailMetaInfo metaInfo = new EmailMetaInfo(recipient, props); + + deviceMgtService.sendRegistrationEmail(metaInfo); + Assert.assertTrue(true); + } catch (ConfigurationManagementException e) { + Assert.assertTrue(false, "Error in sending registration email : Configration " + + "related error" + e.getMessage()); + } catch (DeviceManagementException e) { + Assert.assertTrue(false, "Error in sending registration email" + + e.getMessage()); + } + } + private Date yesterday() { final Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1);