Add application update and unit tests

4.x.x
manoj 9 years ago
parent 6b459afd84
commit da61c08fac

@ -46,6 +46,7 @@ import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceStub;
import org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO; import org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -171,10 +172,32 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem
DeviceIdentifier deviceIdentifier, List<Application> applications) throws ApplicationManagementException { DeviceIdentifier deviceIdentifier, List<Application> applications) throws ApplicationManagementException {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId);
List<Integer> applicationIds = applicationDAO.addApplications(applications, tenantId);
List<Application> installedAppList = getApplicationListForDevice(deviceIdentifier);
List<Application> appsToAdd = new ArrayList<Application>();
List<Integer> appIdsToRemove = new ArrayList<Integer>();
for(Application installedApp:installedAppList){
if (!applications.contains(installedApp)){
appIdsToRemove.add(installedApp.getId());
}
}
for(Application application:applications){
if (!installedAppList.contains(application)){
appsToAdd.add(application);
}
}
List<Integer> applicationIds = applicationDAO.addApplications(appsToAdd, tenantId);
applicationMappingDAO.addApplicationMappings(device.getId(), applicationIds, tenantId); applicationMappingDAO.addApplicationMappings(device.getId(), applicationIds, tenantId);
applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove,tenantId);
} catch (DeviceManagementDAOException deviceDaoEx) { } catch (DeviceManagementDAOException deviceDaoEx) {
String errorMsg = "Error occurred saving application list to the device"; String errorMsg = "Error occurred saving application list to the device";
log.error(errorMsg + ":" + deviceIdentifier.toString()); log.error(errorMsg + ":" + deviceIdentifier.toString());

@ -28,7 +28,7 @@ public interface ApplicationDAO {
List<Integer> addApplications(List<Application> applications, int tenantId) throws DeviceManagementDAOException; List<Integer> addApplications(List<Application> applications, int tenantId) throws DeviceManagementDAOException;
int removeApplication(String applicationName, int tenantId) throws DeviceManagementDAOException; List<Integer> removeApplications(List<Application> apps, int tenantId) throws DeviceManagementDAOException;
Application getApplication(String identifier, int tenantId) throws DeviceManagementDAOException; Application getApplication(String identifier, int tenantId) throws DeviceManagementDAOException;

@ -26,8 +26,9 @@ public interface ApplicationMappingDAO {
int addApplicationMapping(int deviceId, int applicationId, int tenantId) throws DeviceManagementDAOException; int addApplicationMapping(int deviceId, int applicationId, int tenantId) throws DeviceManagementDAOException;
List<Integer> addApplicationMappings(int deviceId, List<Integer> applicationIds, List<Integer> addApplicationMappings(int deviceId, List<Integer> applicationIds, int tenantId)
int tenantId) throws DeviceManagementDAOException; throws DeviceManagementDAOException;
int removeApplicationMapping(int deviceId, int applicationId, int tenantId) throws DeviceManagementDAOException; void removeApplicationMapping(int deviceId, List<Integer> appIdList, int tenantId)
throws DeviceManagementDAOException;
} }

@ -86,10 +86,11 @@ public class ApplicationDAOImpl implements ApplicationDAO {
conn = this.getConnection(); conn = this.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, CATEGORY, " + stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, CATEGORY, " +
"VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES,APP_IDENTIFIER) " + "VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES,APP_IDENTIFIER) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?)"); "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?)", Statement.RETURN_GENERATED_KEYS);
for (Application application : applications) { for (Application application : applications) {
stmt.setString(1, application.getName()); stmt.setString(1, application.getName());
stmt.setString(2, application.getPlatform()); stmt.setString(2, application.getPlatform());
stmt.setString(3, application.getCategory()); stmt.setString(3, application.getCategory());
@ -117,33 +118,38 @@ public class ApplicationDAOImpl implements ApplicationDAO {
} }
@Override @Override
public int removeApplication(String applicationName, int tenantId) throws DeviceManagementDAOException { public List<Integer> removeApplications(List<Application> apps, int tenantId) throws DeviceManagementDAOException {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
int applicationId = -1; int applicationId = -1;
List<Integer> applicationIds = new ArrayList<Integer>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
conn.setAutoCommit(false); conn.setAutoCommit(false);
stmt = conn.prepareStatement("DELETE DM_APPLICATION WHERE NAME = ? AND TENANT_ID = ?"); stmt = conn.prepareStatement("DELETE DM_APPLICATION WHERE APP_IDENTIFIER = ? AND TENANT_ID = ?",
stmt.setString(1, applicationName); Statement.RETURN_GENERATED_KEYS);
stmt.setInt(2, tenantId);
stmt.execute();
conn.commit();
for(Application app:apps){
stmt.setString(1, app.getApplicationIdentifier());
stmt.setInt(2, tenantId);
stmt.addBatch();
}
stmt.executeBatch();
rs = stmt.getGeneratedKeys(); rs = stmt.getGeneratedKeys();
if (rs.next()) { if (rs.next()) {
applicationId = rs.getInt(1); applicationIds.add(rs.getInt(1));
} }
return applicationId; return applicationIds;
} catch (SQLException e) { } catch (SQLException e) {
try { try {
conn.rollback(); conn.rollback();
} catch (SQLException e1) { } catch (SQLException e1) {
log.warn("Error occurred while roll-backing the transaction", e); log.warn("Error occurred while roll-backing the transaction", e);
} }
throw new DeviceManagementDAOException("Error occurred while removing application '" + throw new DeviceManagementDAOException("Error occurred while removing bulk application list", e);
applicationName + "'", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }

@ -102,8 +102,9 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
} }
@Override @Override
public int removeApplicationMapping(int deviceId, int applicationId, public void removeApplicationMapping(int deviceId, List<Integer> appIdList, int tenantId)
int tenantId) throws DeviceManagementDAOException { throws DeviceManagementDAOException {
Connection conn; Connection conn;
ResultSet rs; ResultSet rs;
int mappingId = -1; int mappingId = -1;
@ -112,16 +113,14 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
String sql = "DELETE DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " + String sql = "DELETE DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " +
"APPLICATION_ID = ? AND TENANT_ID = ?"; "APPLICATION_ID = ? AND TENANT_ID = ?";
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
for(Integer appId:appIdList){
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, applicationId); stmt.setInt(2, appId);
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.execute(); stmt.addBatch();
rs = stmt.getGeneratedKeys();
if (rs.next()) {
mappingId = rs.getInt(1);
} }
return mappingId; stmt.executeBatch();
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e); throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e);
} }

@ -53,21 +53,37 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
private DeviceTypeDAO deviceTypeDAO; private DeviceTypeDAO deviceTypeDAO;
private EnrolmentDAO enrolmentDAO; private EnrolmentDAO enrolmentDAO;
private DeviceManagementPluginRepository pluginRepository; private DeviceManagementPluginRepository pluginRepository;
private boolean isTest = false;
private static Log log = LogFactory.getLog(DeviceManagementProviderServiceImpl.class); private static Log log = LogFactory.getLog(DeviceManagementProviderServiceImpl.class);
private int tenantId;
public DeviceManagementProviderServiceImpl() { public DeviceManagementProviderServiceImpl() {
this.pluginRepository = new DeviceManagementPluginRepository(); this.pluginRepository = new DeviceManagementPluginRepository();
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); initDataAccessObjects();
this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
this.enrolmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
/* Registering a listener to retrieve events when some device management service plugin is installed after /* Registering a listener to retrieve events when some device management service plugin is installed after
* the component is done getting initialized */ * the component is done getting initialized */
DeviceManagementServiceComponent.registerPluginInitializationListener(this); DeviceManagementServiceComponent.registerPluginInitializationListener(this);
} }
/**
* This constructor calls from unit tests
* @param pluginRepo
*/
public DeviceManagementProviderServiceImpl(DeviceManagementPluginRepository pluginRepo, boolean test){
this.pluginRepository = pluginRepo;
initDataAccessObjects();
isTest = test;
}
private void initDataAccessObjects() {
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
this.enrolmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
}
@Override @Override
public String getProviderType() { public String getProviderType() {
return null; return null;
@ -87,8 +103,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public boolean enrollDevice(Device device) throws DeviceManagementException { public boolean enrollDevice(Device device) throws DeviceManagementException {
DeviceManager dms =
this.getPluginRepository().getDeviceManagementService(device.getType()); DeviceManager dms = this.getPluginRepository().getDeviceManagementService(device.getType());
boolean status = dms.enrollDevice(device); boolean status = dms.enrollDevice(device);
try { try {
if (dms.isClaimable(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()))) { if (dms.isClaimable(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()))) {
@ -96,7 +112,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} else { } else {
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE);
} }
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = getTenantId();
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
@ -687,4 +703,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
} }
public int getTenantId() {
ThreadLocal<Integer> tenantId = new ThreadLocal<Integer>();
int tenant = 0;
if (isTest){
tenant = org.wso2.carbon.device.mgt.core.common.;
}else{
tenant = CarbonContext.getThreadLocalCarbonContext().getTenantId();
}
return tenant;
}
} }

@ -22,12 +22,13 @@ import org.wso2.carbon.device.mgt.common.app.mgt.Application;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.core.common.TestDataHolder;
import java.util.List; import java.util.List;
public class TestDeviceManagementService implements DeviceManagementService { public class TestDeviceManagementService implements DeviceManagementService {
public static final String DEVICE_TYPE_TEST = "Test"; public static final String DEVICE_TYPE_TEST = TestDataHolder.TEST_DEVICE_TYPE;
@Override @Override
public String getProviderType() { public String getProviderType() {

@ -16,7 +16,7 @@
* under the License. * under the License.
* *
*/ */
package org.wso2.carbon.device.mgt.core.dao; package org.wso2.carbon.device.mgt.core.common;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -28,7 +28,7 @@ import org.testng.annotations.BeforeSuite;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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.DataSourceConfig; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -41,10 +41,10 @@ import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
public abstract class BaseDeviceManagementDAOTest { public abstract class BaseDeviceManagementTest {
private DataSource dataSource; private DataSource dataSource;
private static final Log log = LogFactory.getLog(BaseDeviceManagementDAOTest.class); private static final Log log = LogFactory.getLog(BaseDeviceManagementTest.class);
@BeforeSuite @BeforeSuite
public void setupDataSource() throws Exception { public void setupDataSource() throws Exception {

@ -24,33 +24,25 @@ import org.testng.Assert;
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.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.Application;
import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest;
import org.wso2.carbon.device.mgt.core.common.TestDataHolder;
import java.util.Properties; public class ApplicationPersistenceTests extends BaseDeviceManagementTest {
public class ApplicationPersistenceDAOTests extends BaseDeviceManagementDAOTest { private static final Log log = LogFactory.getLog(ApplicationPersistenceTests.class);
private static final Log log = LogFactory.getLog(ApplicationPersistenceDAOTests.class);
private ApplicationDAO applicationDAO = DeviceManagementDAOFactory.getApplicationDAO(); private ApplicationDAO applicationDAO = DeviceManagementDAOFactory.getApplicationDAO();
@Test @Test
public void testAddApplication() { public void testAddApplication() {
/* Initializing source application bean to be tested */ /* Initializing source application bean to be tested */
Properties properties = new Properties();
Application source = new Application();
source.setName("SimpleCalculator");
source.setCategory("TestCategory");
source.setApplicationIdentifier("com.simple.calculator");
source.setType("TestType");
source.setVersion("1.0.0");
source.setImageUrl("http://test.org/image/");
source.setLocationUrl("http://test.org/location/");
/* Adding dummy application to the application store */ /* Adding dummy application to the application store */
String testAppIdentifier = "test sample1";
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
applicationDAO.addApplication(source, -1234); applicationDAO.addApplication(TestDataHolder.generateApplicationDummyData(testAppIdentifier), -1234);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
log.error("Error occurred while adding application '" + source.getName() + "'", e); log.error("Error occurred while adding application test sample1", e);
} finally { } finally {
try { try {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
@ -61,22 +53,22 @@ public class ApplicationPersistenceDAOTests extends BaseDeviceManagementDAOTest
/* Retrieving the application by its name */ /* Retrieving the application by its name */
Application target = null; Application target = null;
try { try {
target = this.getApplication(source.getApplicationIdentifier(), -1234); target = this.getApplication(testAppIdentifier, -1234);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving application info"; String msg = "Error occurred while retrieving application info";
log.error(msg, e); log.error(msg, e);
Assert.fail(msg, e); Assert.fail(msg, e);
} }
Assert.assertEquals(target.getApplicationIdentifier(), source.getApplicationIdentifier(), "Application added is not as same as " + Assert.assertEquals(target.getApplicationIdentifier(), testAppIdentifier, "Application added is not as same as " +
"what's " + "what's " +
"retrieved"); "retrieved");
} }
private Application getApplication(String packageName, int tenantId) throws DeviceManagementDAOException { private Application getApplication(String appIdentifier, int tenantId) throws DeviceManagementDAOException {
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
return applicationDAO.getApplication(packageName, tenantId); return applicationDAO.getApplication(appIdentifier, tenantId);
} finally { } finally {
try { try {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();

@ -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");
}
} }

@ -24,18 +24,17 @@ import org.testng.Assert;
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.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest;
import org.wso2.carbon.device.mgt.core.common.TestDataHolder;
import java.util.Date; public class EnrolmentPersistenceTests extends BaseDeviceManagementTest {
public class EnrolmentPersistenceDAOTests extends BaseDeviceManagementDAOTest { private static final Log log = LogFactory.getLog(EnrolmentPersistenceTests.class);
private static final Log log = LogFactory.getLog(EnrolmentPersistenceDAOTests.class);
private EnrolmentDAO enrolmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO(); private EnrolmentDAO enrolmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
@Test @Test
public void testAddEnrolment() { public void testAddEnrolment() {
int deviceId = 1234; int deviceId = TestDataHolder.initialTestDevice.getId();
String owner = "admin"; String owner = "admin";
/* Initializing source enrolment configuration bean to be tested */ /* Initializing source enrolment configuration bean to be tested */
@ -46,7 +45,7 @@ public class EnrolmentPersistenceDAOTests extends BaseDeviceManagementDAOTest {
/* Adding dummy enrolment configuration to the device management metadata store */ /* Adding dummy enrolment configuration to the device management metadata store */
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
enrolmentDAO.addEnrollment(deviceId, source, -1234); enrolmentDAO.addEnrollment(deviceId, source, TestDataHolder.SUPER_TENANT_ID);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
log.error("Error occurred while adding enrollment", e); log.error("Error occurred while adding enrollment", e);
} finally { } finally {
@ -59,7 +58,7 @@ public class EnrolmentPersistenceDAOTests extends BaseDeviceManagementDAOTest {
/* Retrieving the enrolment associated with the given deviceId and owner */ /* Retrieving the enrolment associated with the given deviceId and owner */
EnrolmentInfo target = null; EnrolmentInfo target = null;
try { try {
target = this.getEnrolmentConfig(deviceId, owner, -1234); target = this.getEnrolmentConfig(deviceId, owner, TestDataHolder.SUPER_TENANT_ID);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving application info"; String msg = "Error occurred while retrieving application info";
log.error(msg, e); log.error(msg, e);

@ -24,11 +24,16 @@
<test name="DAO Unit Tests" preserve-order="true"> <test name="DAO Unit Tests" preserve-order="true">
<classes> <classes>
<class name="org.wso2.carbon.device.mgt.core.dao.BaseDeviceManagementDAOTest"/> <class name="org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest"/>
<class name="org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOTests"/> <class name="org.wso2.carbon.device.mgt.core.dao.DevicePersistTests"/>
<class name="org.wso2.carbon.device.mgt.core.DeviceManagementRepositoryTests"/> <class name="org.wso2.carbon.device.mgt.core.DeviceManagementRepositoryTests"/>
<class name="org.wso2.carbon.device.mgt.core.DeviceManagementConfigTests"/> <class name="org.wso2.carbon.device.mgt.core.DeviceManagementConfigTests"/>
<class name="org.wso2.carbon.device.mgt.core.dao.ApplicationPersistenceDAOTests"/> <class name="org.wso2.carbon.device.mgt.core.dao.ApplicationPersistenceTests"/>
</classes>
</test>
<test name="Service Unit Tests" preserve-order="true">
<classes>
<class name="org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceTest"/>
</classes> </classes>
</test> </test>
</suite> </suite>
Loading…
Cancel
Save