From bed56a60db795848a20ee2c3ed6f28553c023c83 Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 28 Sep 2017 17:41:34 +0530 Subject: [PATCH 1/2] Adding Integration tests for core and fixing bugs --- .../core/dao/impl/AbstractDeviceDAOImpl.java | 9 ++- .../DeviceManagementProviderServiceTest.java | 62 +++++++++---------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index decb956f1e..75c7ac7340 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -122,7 +122,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + "t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + - "AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC"; + "AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC, e.STATUS ASC"; + // Status adeed as an orderby clause to fix a bug : when an existing device is + // re-enrolled, earlier enrollment is marked as removed and a new enrollment is added. + // However, both enrollments share the same time stamp. When retrieving the device + // due to same timestamp, enrollment information is incorrect, intermittently. Hence + // status also should be taken into consideration when ordering. This should not present a + // problem for other status transitions, as there would be an intermediary 'removed' + // state in between. stmt = conn.prepareStatement(sql); stmt.setString(1, deviceIdentifier.getType()); stmt.setString(2, deviceIdentifier.getId()); 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 144a3a4e6b..c70fe720a9 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 @@ -125,39 +125,39 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes } } -// @Test(dependsOnMethods = {"testReEnrollmentofSameDeviceUnderSameUser"}) -// public void testReEnrollmentofSameDeviceWithOtherUser() { -// -// EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); -// enrolmentInfo.setDateOfEnrolment(new Date().getTime()); -// enrolmentInfo.setDateOfLastUpdate(new Date().getTime()); -// enrolmentInfo.setOwner("user1"); -// enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); -// enrolmentInfo.setStatus(EnrolmentInfo.Status.CREATED); -// -// Device alternateDevice = TestDataHolder.generateDummyDeviceData("12345", DEVICE_TYPE, -// enrolmentInfo); -// -// try { -// Device retrievedDevice1 = deviceMgtService.getDevice(new DeviceIdentifier("12345", DEVICE_TYPE)); -// -// deviceMgtService.enrollDevice(alternateDevice); -// Device retrievedDevice2 = deviceMgtService.getDevice(new DeviceIdentifier(alternateDevice -// .getDeviceIdentifier(), alternateDevice.getType())); -// -// log.info(retrievedDevice1.getEnrolmentInfo().getOwner()); -// log.info(retrievedDevice2.getEnrolmentInfo().getOwner()); -// -// Assert.assertFalse(retrievedDevice1.getEnrolmentInfo().getOwner().equalsIgnoreCase -// (retrievedDevice2.getEnrolmentInfo().getOwner())); -// } catch (DeviceManagementException e) { -// String msg = "Error Occured while enrolling device"; -// Assert.fail(msg, e); -// } -// } + @Test(dependsOnMethods = {"testReEnrollmentofSameDeviceUnderSameUser"}) + public void testReEnrollmentofSameDeviceWithOtherUser() { + EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); + enrolmentInfo.setDateOfEnrolment(new Date().getTime()); + enrolmentInfo.setDateOfLastUpdate(new Date().getTime()); + enrolmentInfo.setOwner("user1"); + enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); + enrolmentInfo.setStatus(EnrolmentInfo.Status.CREATED); - @Test(dependsOnMethods = {"testReEnrollmentofSameDeviceUnderSameUser"}) + Device alternateDevice = TestDataHolder.generateDummyDeviceData("12345", DEVICE_TYPE, + enrolmentInfo); + + try { + Device retrievedDevice1 = deviceMgtService.getDevice(new DeviceIdentifier("12345", DEVICE_TYPE)); + + deviceMgtService.enrollDevice(alternateDevice); + Device retrievedDevice2 = deviceMgtService.getDevice(new DeviceIdentifier(alternateDevice + .getDeviceIdentifier(), alternateDevice.getType())); + + log.info(retrievedDevice1.getEnrolmentInfo().getOwner()); + log.info(retrievedDevice2.getEnrolmentInfo().getOwner()); + + Assert.assertFalse(retrievedDevice1.getEnrolmentInfo().getOwner().equalsIgnoreCase + (retrievedDevice2.getEnrolmentInfo().getOwner())); + } catch (DeviceManagementException e) { + String msg = "Error Occured while enrolling device"; + Assert.fail(msg, e); + } + } + + + @Test(dependsOnMethods = {"testReEnrollmentofSameDeviceWithOtherUser"}) public void testDisenrollment() { Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE); From 43462b173aca95feb7d4f20a1ad81a659a0ee388 Mon Sep 17 00:00:00 2001 From: Ace Date: Fri, 29 Sep 2017 21:26:26 +0530 Subject: [PATCH 2/2] Adding integration tests for Device Retrieval --- .../core/dao/impl/AbstractDeviceDAOImpl.java | 2 +- .../DeviceManagementProviderServiceImpl.java | 1 + .../mgt/core/common/TestDataHolder.java | 23 +++ .../DeviceManagementProviderServiceTest.java | 134 +++++++++++++++++- 4 files changed, 157 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 75c7ac7340..1dc588f0e1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -128,7 +128,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { // However, both enrollments share the same time stamp. When retrieving the device // due to same timestamp, enrollment information is incorrect, intermittently. Hence // status also should be taken into consideration when ordering. This should not present a - // problem for other status transitions, as there would be an intermediary 'removed' + // problem for other status transitions, as there would be an intermediary removed // state in between. stmt = conn.prepareStatement(sql); stmt.setString(1, deviceIdentifier.getType()); 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 e5d065a196..4cc02ee680 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 @@ -1741,6 +1741,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (requireDeviceInfo) { this.getAllDeviceInfo(userDevices); } + devices.addAll(userDevices); } return devices; } 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 a23fdbc638..41b3c312a5 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 @@ -20,6 +20,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.app.mgt.Application; +import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; @@ -55,6 +56,28 @@ public class TestDataHolder { return device; } + public static DeviceInfo generateDummyDeviceInfo() { + DeviceInfo deviceInfo = new DeviceInfo(); + deviceInfo.setIMEI("IMEI-12345"); + deviceInfo.setIMSI("IMSI-12344"); + deviceInfo.setDeviceModel("DUMMY_MODEL"); + deviceInfo.setVendor("Google"); + deviceInfo.setOsVersion("Oreo"); + deviceInfo.setOsBuildDate("24-05-2017"); + deviceInfo.setBatteryLevel(25.0); + deviceInfo.setInternalTotalMemory(1.5); + deviceInfo.setInternalAvailableMemory(2.5); + deviceInfo.setExternalTotalMemory(16.76); + deviceInfo.setExternalAvailableMemory(4.56); + deviceInfo.setConnectionType("CON_TYPE"); + deviceInfo.setSsid("SSID"); + deviceInfo.setCpuUsage(23.5); + deviceInfo.setTotalRAMMemory(1.5); + deviceInfo.setAvailableRAMMemory(2.33); + deviceInfo.setPluggedIn(true); + return deviceInfo; + } + public static Notification getNotification(int notificationId, String status, String deviceId, String description, String deviceName, int operationId, String deviceType) { 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 791907ff66..9c0dac543d 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,14 +23,28 @@ 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.common.InvalidDeviceException; +import org.wso2.carbon.device.mgt.common.TransactionManagementException; +import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; 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.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; +import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException; 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.device.mgt.core.operation.TestNotificationStrategy; +import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.registry.core.config.RegistryContext; import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.internal.RegistryDataHolder; @@ -40,6 +54,14 @@ import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import java.io.InputStream; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; import java.util.List; public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest { @@ -47,7 +69,7 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes 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"; + private DeviceDetailsDAO deviceDetailsDAO = DeviceManagementDAOFactory.getDeviceDetailsDAO(); DeviceManagementProviderService deviceMgtService; @@ -65,7 +87,6 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); - } private RegistryService getRegistryService() throws RegistryException { @@ -320,5 +341,114 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes } } + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void testGetDevice() { + try { + Device device = deviceMgtService.getDevice(new DeviceIdentifier("12345",DEVICE_TYPE)); + Assert.assertTrue(device.getDeviceIdentifier().equalsIgnoreCase("12345")); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void testGetDeviceWithInfo() { + try { + Device device = deviceMgtService.getDevice(new DeviceIdentifier("12345", DEVICE_TYPE) + , true); + Assert.assertTrue(device.getDeviceInfo() != null); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void testGetDeviceWithOutInfo() { + try { + Device device = deviceMgtService.getDevice(new DeviceIdentifier("12345", DEVICE_TYPE) + , false); + Assert.assertTrue(device.getDeviceInfo() == null); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void testGetAllDevicesOfRole() { + try { + List devices = deviceMgtService.getAllDevicesOfRole("admin"); + Assert.assertTrue(devices.size() > 0); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void testDeviceByOwner() { + try { + Device device = deviceMgtService.getDevice(new DeviceIdentifier("12345", + DEVICE_TYPE), "admin", true); + Assert.assertTrue(device != null); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void testDeviceByDate() { + try { + Device initialDevice = deviceMgtService.getDevice(new DeviceIdentifier("12345", + DEVICE_TYPE)); + + DeviceManagementDAOFactory.beginTransaction(); + + //Device details table will be reffered when looking for last updated time + //This dao entry is to mimic a device info operation + deviceDetailsDAO.addDeviceInformation(initialDevice.getId(), TestDataHolder + .generateDummyDeviceInfo()); + } catch (DeviceManagementException e) { + e.printStackTrace(); + } catch (TransactionManagementException e) { + e.printStackTrace(); + } catch (DeviceDetailsMgtDAOException e) { + e.printStackTrace(); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + try { + Device device = deviceMgtService.getDevice(new DeviceIdentifier("12345", + DEVICE_TYPE), yesterday()); + Assert.assertTrue(device != null); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + @Test(dependsOnMethods = {"testDeviceByDate"}) + public void testDeviceByDateAndOwner() { + try { + Device device = deviceMgtService.getDevice(new DeviceIdentifier("12345", + DEVICE_TYPE), "admin", yesterday(), true); + Assert.assertTrue(device != null); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating the device status"; + Assert.fail(msg, e); + } + } + + private Date yesterday() { + final Calendar cal = Calendar.getInstance(); + cal.add(Calendar.DATE, -1); + return cal.getTime(); + } + } \ No newline at end of file