|
|
@ -21,27 +21,24 @@ package org.wso2.carbon.device.mgt.core.dao;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.testng.Assert;
|
|
|
|
import org.testng.Assert;
|
|
|
|
import org.testng.annotations.AfterClass;
|
|
|
|
|
|
|
|
import org.testng.annotations.BeforeClass;
|
|
|
|
import org.testng.annotations.BeforeClass;
|
|
|
|
import org.testng.annotations.Test;
|
|
|
|
import org.testng.annotations.Test;
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.OwnerShip;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
|
|
|
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.TestUtils;
|
|
|
|
import org.wso2.carbon.device.mgt.core.TestUtils;
|
|
|
|
|
|
|
|
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.dto.DeviceType;
|
|
|
|
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
|
|
|
|
|
|
|
|
|
|
|
|
import java.sql.*;
|
|
|
|
import java.sql.*;
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest {
|
|
|
|
public class DevicePersistTests extends BaseDeviceManagementTest {
|
|
|
|
|
|
|
|
|
|
|
|
DeviceDAO deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
|
|
|
|
DeviceDAO deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
|
|
|
|
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
|
|
|
|
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(DeviceManagementDAOTests.class);
|
|
|
|
private static final Log log = LogFactory.getLog(DevicePersistTests.class);
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
@BeforeClass
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -51,7 +48,7 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testAddDeviceTypeTest() {
|
|
|
|
public void testAddDeviceTypeTest() {
|
|
|
|
DeviceType deviceType = this.loadDummyDeviceType();
|
|
|
|
DeviceType deviceType = TestDataHolder.generateDeviceTypeData(TestDataHolder.TEST_DEVICE_TYPE);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DeviceManagementDAOFactory.openConnection();
|
|
|
|
DeviceManagementDAOFactory.openConnection();
|
|
|
|
deviceTypeDAO.addDeviceType(deviceType);
|
|
|
|
deviceTypeDAO.addDeviceType(deviceType);
|
|
|
@ -69,7 +66,7 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest {
|
|
|
|
|
|
|
|
|
|
|
|
int targetTypeId = -1;
|
|
|
|
int targetTypeId = -1;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
targetTypeId = this.getDeviceTypeId();
|
|
|
|
targetTypeId = this.getDeviceTypeId(TestDataHolder.TEST_DEVICE_TYPE);
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
String msg = "Error occurred while retrieving target device type id";
|
|
|
|
String msg = "Error occurred while retrieving target device type id";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
@ -78,20 +75,21 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest {
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull(targetTypeId, "Device Type Id is null");
|
|
|
|
Assert.assertNotNull(targetTypeId, "Device Type Id is null");
|
|
|
|
deviceType.setId(targetTypeId);
|
|
|
|
deviceType.setId(targetTypeId);
|
|
|
|
|
|
|
|
TestDataHolder.initialTestDeviceType = deviceType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test(dependsOnMethods = {"testAddDeviceTypeTest"})
|
|
|
|
@Test(dependsOnMethods = {"testAddDeviceTypeTest"})
|
|
|
|
public void testAddDeviceTest() {
|
|
|
|
public void testAddDeviceTest() {
|
|
|
|
DeviceType deviceType = this.loadDummyDeviceType();
|
|
|
|
|
|
|
|
deviceType.setId(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int tenantId = -1234;
|
|
|
|
int tenantId = TestDataHolder.SUPER_TENANT_ID;
|
|
|
|
Device device = this.loadDummyDevice();
|
|
|
|
Device device = TestDataHolder.generateDummyDeviceData("ios");
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DeviceManagementDAOFactory.openConnection();
|
|
|
|
DeviceManagementDAOFactory.openConnection();
|
|
|
|
int deviceId = deviceDAO.addDevice(deviceType.getId(), device, tenantId);
|
|
|
|
int deviceId = deviceDAO.addDevice(TestDataHolder.initialTestDeviceType.getId(), device, tenantId);
|
|
|
|
device.setId(deviceId);
|
|
|
|
device.setId(deviceId);
|
|
|
|
deviceDAO.addEnrollment(device, tenantId);
|
|
|
|
deviceDAO.addEnrollment(device, tenantId);
|
|
|
|
|
|
|
|
TestDataHolder.initialTestDevice = device;
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
String msg = "Error occurred while adding '" + device.getType() + "' device with the identifier '" +
|
|
|
|
String msg = "Error occurred while adding '" + device.getType() + "' device with the identifier '" +
|
|
|
|
device.getDeviceIdentifier() + "'";
|
|
|
|
device.getDeviceIdentifier() + "'";
|
|
|
@ -107,7 +105,8 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest {
|
|
|
|
|
|
|
|
|
|
|
|
int targetId = -1;
|
|
|
|
int targetId = -1;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
targetId = this.getDeviceId();
|
|
|
|
targetId = this.getDeviceId(TestDataHolder.initialTestDevice.getDeviceIdentifier(),
|
|
|
|
|
|
|
|
TestDataHolder.SUPER_TENANT_ID);
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
String msg = "Error occurred while retrieving device id";
|
|
|
|
String msg = "Error occurred while retrieving device id";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
@ -117,26 +116,7 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest {
|
|
|
|
device.getType() + "' carrying the identifier '" + device.getDeviceIdentifier() + "', is null");
|
|
|
|
device.getType() + "' carrying the identifier '" + device.getDeviceIdentifier() + "', is null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void addDeviceEnrolment() {
|
|
|
|
private int getDeviceId(String deviceIdentification, int tenantId) throws DeviceManagementDAOException {
|
|
|
|
Device device = this.loadDummyDevice();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
DeviceManagementDAOFactory.openConnection();
|
|
|
|
|
|
|
|
deviceDAO.addEnrollment(device, -1234);
|
|
|
|
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred while adding enrolment configuration upon '" + device.getType() +
|
|
|
|
|
|
|
|
"' device with the identifier '" + device.getDeviceIdentifier() + "'";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
DeviceManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
|
|
|
|
log.warn("Error occurred while closing the connection", e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int getDeviceId() throws DeviceManagementDAOException {
|
|
|
|
|
|
|
|
Connection conn = null;
|
|
|
|
Connection conn = null;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
ResultSet rs = null;
|
|
|
|
ResultSet rs = null;
|
|
|
@ -147,8 +127,8 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest {
|
|
|
|
conn = getDataSource().getConnection();
|
|
|
|
conn = getDataSource().getConnection();
|
|
|
|
String sql = "SELECT ID FROM DM_DEVICE WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?";
|
|
|
|
String sql = "SELECT ID FROM DM_DEVICE WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?";
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
stmt.setString(1, "111");
|
|
|
|
stmt.setString(1, deviceIdentification);
|
|
|
|
stmt.setInt(2, -1234);
|
|
|
|
stmt.setInt(2, tenantId);
|
|
|
|
|
|
|
|
|
|
|
|
rs = stmt.executeQuery();
|
|
|
|
rs = stmt.executeQuery();
|
|
|
|
if (rs.next()) {
|
|
|
|
if (rs.next()) {
|
|
|
@ -163,13 +143,13 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private int getDeviceTypeId() throws DeviceManagementDAOException {
|
|
|
|
private int getDeviceTypeId(String deviceName) throws DeviceManagementDAOException {
|
|
|
|
int id = -1;
|
|
|
|
int id = -1;
|
|
|
|
Connection conn = null;
|
|
|
|
Connection conn = null;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
String sql = "SELECT ID, NAME FROM DM_DEVICE_TYPE WHERE NAME = ?";
|
|
|
|
String sql = "SELECT ID, NAME FROM DM_DEVICE_TYPE WHERE NAME = ?";
|
|
|
|
|
|
|
|
|
|
|
|
DeviceType deviceType = this.loadDummyDeviceType();
|
|
|
|
DeviceType deviceType = TestDataHolder.generateDeviceTypeData("ios");
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Assert.assertNotNull(getDataSource(), "Data Source is not initialized properly");
|
|
|
|
Assert.assertNotNull(getDataSource(), "Data Source is not initialized properly");
|
|
|
|
conn = getDataSource().getConnection();
|
|
|
|
conn = getDataSource().getConnection();
|
|
|
@ -191,11 +171,14 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Test(dependsOnMethods = "testAddDeviceTest")
|
|
|
|
@Test(dependsOnMethods = "testAddDeviceTest")
|
|
|
|
public void testSetEnrolmentStatus() {
|
|
|
|
public void testSetEnrolmentStatus() {
|
|
|
|
Device device = this.loadDummyDevice();
|
|
|
|
|
|
|
|
|
|
|
|
Device device = TestDataHolder.initialTestDevice;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DeviceManagementDAOFactory.openConnection();
|
|
|
|
DeviceManagementDAOFactory.openConnection();
|
|
|
|
DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
|
|
|
|
DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
|
|
|
|
deviceDAO.setEnrolmentStatus(deviceId, device.getEnrolmentInfo().getOwner(), Status.ACTIVE, -1234);
|
|
|
|
deviceDAO.setEnrolmentStatus(deviceId, device.getEnrolmentInfo().getOwner(), Status.ACTIVE,
|
|
|
|
|
|
|
|
TestDataHolder.SUPER_TENANT_ID);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
String msg = "Error occurred while setting enrolment status";
|
|
|
|
String msg = "Error occurred while setting enrolment status";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
@ -209,24 +192,26 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Status target = null;
|
|
|
|
Status target = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
target = this.getEnrolmentStatus();
|
|
|
|
target = this.getEnrolmentStatus(device.getDeviceIdentifier(), device.getType(),
|
|
|
|
|
|
|
|
TestDataHolder.SUPER_TENANT_ID);
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
String msg = "Error occurred while retrieving the target enrolment status";
|
|
|
|
String msg = "Error occurred while retrieving the target enrolment status";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull(target, "Enrolment status retrieved for the device carrying its identifier as '" +
|
|
|
|
Assert.assertNotNull(target, "Enrolment status retrieved for the device carrying its identifier as '" +
|
|
|
|
device.getDeviceIdentifier() + "' is null");
|
|
|
|
device.getDeviceIdentifier() + "' is null");
|
|
|
|
Assert.assertEquals(target, Status.ACTIVE, "Enrolment status retrieved is not as same as what's configured");
|
|
|
|
Assert.assertEquals(target, Status.ACTIVE, "Enrolment status retrieved is not as same as what's configured");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Status getEnrolmentStatus() throws DeviceManagementDAOException {
|
|
|
|
private Status getEnrolmentStatus(String identifier, String deviceType, int tenantId)
|
|
|
|
Device device = this.loadDummyDevice();
|
|
|
|
throws DeviceManagementDAOException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Device device = TestDataHolder.generateDummyDeviceData("ios");
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DeviceManagementDAOFactory.openConnection();
|
|
|
|
DeviceManagementDAOFactory.openConnection();
|
|
|
|
DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
|
|
|
|
DeviceIdentifier deviceId = new DeviceIdentifier(identifier, deviceType);
|
|
|
|
return deviceDAO.getEnrolmentStatus(deviceId, device.getEnrolmentInfo().getOwner(), -1234);
|
|
|
|
return deviceDAO.getEnrolmentStatus(deviceId, device.getEnrolmentInfo().getOwner(), tenantId);
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while retrieving the current status of the " +
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while retrieving the current status of the " +
|
|
|
|
"enrolment", e);
|
|
|
|
"enrolment", e);
|
|
|
@ -238,24 +223,4 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Device loadDummyDevice() {
|
|
|
|
|
|
|
|
Device device = new Device();
|
|
|
|
|
|
|
|
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
|
|
|
|
|
|
|
enrolmentInfo.setDateOfEnrolment(new Date().getTime());
|
|
|
|
|
|
|
|
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
|
|
|
|
|
|
|
|
enrolmentInfo.setOwner("admin");
|
|
|
|
|
|
|
|
enrolmentInfo.setOwnership(OwnerShip.BYOD);
|
|
|
|
|
|
|
|
enrolmentInfo.setStatus(Status.CREATED);
|
|
|
|
|
|
|
|
device.setEnrolmentInfo(enrolmentInfo);
|
|
|
|
|
|
|
|
device.setDescription("Test Description");
|
|
|
|
|
|
|
|
device.setDeviceIdentifier("1234");
|
|
|
|
|
|
|
|
device.setType(this.loadDummyDeviceType().getName());
|
|
|
|
|
|
|
|
return device;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private DeviceType loadDummyDeviceType() {
|
|
|
|
|
|
|
|
return new DeviceType("iOS");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|