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 decb956f1ed..1dc588f0e13 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/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 ab31f82599f..4c1454015b6 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 a23fdbc638a..41b3c312a56 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 4cb26bc170a..d2425c4cdde 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,11 +23,16 @@ 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.TransactionManagementException; 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; @@ -40,6 +45,8 @@ import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import java.io.InputStream; +import java.util.Calendar; +import java.util.Date; import java.util.List; public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest { @@ -47,7 +54,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 +72,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 { @@ -170,39 +176,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); try { @@ -320,5 +326,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