From 08ca9f45c53c0f9f6bd97f7d41f10af40849b72e Mon Sep 17 00:00:00 2001 From: Harshan Liyanage Date: Thu, 28 Sep 2017 17:32:45 +0530 Subject: [PATCH] Added unit tests to DeviceManagementProviderService class. --- .../DeviceManagementProviderService.java | 2 - .../DeviceManagementProviderServiceImpl.java | 5 - .../DeviceManagementProviderServiceTest.java | 166 ++++++++++++++++-- 3 files changed, 156 insertions(+), 17 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index bd5e6d5897d..fe77b941805 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -510,8 +510,6 @@ public interface DeviceManagementProviderService { boolean enrollDevice(Device device) throws DeviceManagementException; - PlatformConfiguration getConfiguration() throws DeviceManagementException; - boolean saveConfiguration(PlatformConfiguration configuration) throws DeviceManagementException; boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index da66d7c9b60..e5d065a196d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -129,11 +129,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return dms.saveConfiguration(configuration); } - @Override - public PlatformConfiguration getConfiguration() throws DeviceManagementException { - return null; - } - @Override public PlatformConfiguration getConfiguration(String deviceType) throws DeviceManagementException { DeviceManager dms = 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 144a3a4e6be..4cb26bc170a 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 @@ -23,12 +23,12 @@ import org.testng.annotations.Test; 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.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.registry.core.config.RegistryContext; @@ -40,13 +40,14 @@ import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import java.io.InputStream; -import java.util.Date; +import java.util.List; public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest { private static final Log log = LogFactory.getLog(DeviceManagementProviderServiceTest.class); private DeviceManagementProviderService providerService; private static final String DEVICE_TYPE = "RANDOM_DEVICE_TYPE"; + private static final String DEVICE_TYPE_2 = "RANDOM_DEVICE_TYPE"; DeviceManagementProviderService deviceMgtService; @@ -77,6 +78,17 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes return context.getEmbeddedRegistryService(); } + @Test + public void testGetAvailableDeviceTypes() { + try { + List deviceTypes = deviceMgtService.getDeviceTypes(); + Assert.assertTrue(deviceTypes.size() > 0); + } catch (DeviceManagementException e) { + String msg = "Error occurred while getting the device types"; + Assert.fail(msg, e); + } + } + @Test public void testNullDeviceEnrollment() { try { @@ -87,17 +99,50 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes } @Test - public void testSuccessfullDeviceEnrollment() { + public void testSuccessfulDeviceEnrollment() { Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE); try { boolean enrollmentStatus = deviceMgtService.enrollDevice(device); Assert.assertTrue(enrollmentStatus); } catch (DeviceManagementException e) { - String msg = "Error Occured while enrolling device"; + String msg = "Error occurred while enrolling device"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = "testSuccessfulDeviceEnrollment") + public void testIsEnrolled() { + try { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(TestDataHolder.initialDeviceIdentifier); + deviceIdentifier.setType(DEVICE_TYPE); + boolean enrollmentStatus = deviceMgtService.isEnrolled(deviceIdentifier); + Assert.assertTrue(enrollmentStatus); + } catch (DeviceManagementException e) { + String msg = "Error occurred while checking enrollment status."; + Assert.fail(msg, e); + } + } + + @Test + public void testIsEnrolledForNonExistingDevice() { + try { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId("34535235235235235"); + deviceIdentifier.setType(DEVICE_TYPE); + boolean enrollmentStatus = deviceMgtService.isEnrolled(deviceIdentifier); + Assert.assertFalse(enrollmentStatus); + } catch (DeviceManagementException e) { + String msg = "Error occurred while checking enrollment status."; Assert.fail(msg, e); } } + @Test(expectedExceptions = DeviceManagementException.class) + public void testIsEnrolledForNullDevice() throws DeviceManagementException { + deviceMgtService.isEnrolled(null); + } + @Test public void testNonExistentDeviceType() { Device device = TestDataHolder.generateDummyDeviceData("abc"); @@ -105,13 +150,13 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes boolean enrollmentStatus = deviceMgtService.enrollDevice(device); Assert.assertFalse(enrollmentStatus); } catch (DeviceManagementException e) { - String msg = "Error Occured while enrolling device"; + String msg = "Error occurred while enrolling device"; Assert.fail(msg, e); } } - @Test(dependsOnMethods = {"testSuccessfullDeviceEnrollment"}) + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) public void testReEnrollmentofSameDeviceUnderSameUser() { Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE); @@ -120,7 +165,7 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes Assert.assertTrue(enrollment); } catch (DeviceManagementException e) { - String msg = "Error Occured while enrolling device"; + String msg = "Error occurred while enrolling device"; Assert.fail(msg, e); } } @@ -160,7 +205,6 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes @Test(dependsOnMethods = {"testReEnrollmentofSameDeviceUnderSameUser"}) public void testDisenrollment() { Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE); - try { boolean disenrollmentStatus = deviceMgtService.disenrollDevice(new DeviceIdentifier (device @@ -170,9 +214,111 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes Assert.assertTrue(disenrollmentStatus); } catch (DeviceManagementException e) { - String msg = "Error Occured while enrolling device"; + String msg = "Error occurred while enrolling device"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void testGetDeviceCount() { + try { + int count = deviceMgtService.getDeviceCount(); + Assert.assertTrue(count > 0); + } catch (DeviceManagementException e) { + String msg = "Error occurred while getting the device count"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void testGetDeviceCountForUser() { + try { + int count = deviceMgtService.getDeviceCount(TestDataHolder.OWNER); + Assert.assertTrue(count > 0); + } catch (DeviceManagementException e) { + String msg = "Error occurred while getting the device count"; Assert.fail(msg, e); } } -} + @Test + public void testGetDeviceCountForNonExistingUser() { + try { + int count = deviceMgtService.getDeviceCount("ABCD"); + Assert.assertEquals(count, 0); + } catch (DeviceManagementException e) { + String msg = "Error occurred while getting the device count"; + Assert.fail(msg, e); + } + } + + @Test(expectedExceptions = DeviceManagementException.class) + public void testGetDeviceCountForNullUser() throws DeviceManagementException { + deviceMgtService.getDeviceCount(null); + } + + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void testIsActive() { + try { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(TestDataHolder.initialDeviceIdentifier); + deviceIdentifier.setType(DEVICE_TYPE); + Assert.assertTrue(deviceMgtService.isActive(deviceIdentifier)); + } catch (DeviceManagementException e) { + String msg = "Error occurred while checking the device status"; + Assert.fail(msg, e); + } + } + + @Test + public void testIsActiveForNonExistingDevice() { + try { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId("34535235235235235"); + deviceIdentifier.setType("TEST_TYPE"); + Assert.assertFalse(deviceMgtService.isActive(deviceIdentifier)); + } catch (DeviceManagementException e) { + String msg = "Error occurred while checking the device status"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void testSetActive() { + try { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(TestDataHolder.initialDeviceIdentifier); + deviceIdentifier.setType(DEVICE_TYPE); + Assert.assertFalse(deviceMgtService.setActive(deviceIdentifier, true)); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + @Test + public void testSetActiveForNonExistingDevice() { + try { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId("34535235235235235"); + deviceIdentifier.setType("TEST_TYPE"); + Assert.assertFalse(deviceMgtService.setActive(deviceIdentifier, true)); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status for non-existing device"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void testGetDeviceEnrolledTenants() { + try { + List tenants = deviceMgtService.getDeviceEnrolledTenants(); + Assert.assertEquals(tenants.size(), 1); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + +} \ No newline at end of file