diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index 69a7e8eb8a..444ca6051e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -46,6 +46,7 @@ import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceStub; import org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO; import java.rmi.RemoteException; +import java.util.ArrayList; import java.util.List; /** @@ -171,10 +172,32 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem DeviceIdentifier deviceIdentifier, List applications) throws ApplicationManagementException { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); - List applicationIds = applicationDAO.addApplications(applications, tenantId); + + List installedAppList = getApplicationListForDevice(deviceIdentifier); + List appsToAdd = new ArrayList(); + List appIdsToRemove = new ArrayList(); + + for(Application installedApp:installedAppList){ + if (!applications.contains(installedApp)){ + appIdsToRemove.add(installedApp.getId()); + } + } + + for(Application application:applications){ + if (!installedAppList.contains(application)){ + appsToAdd.add(application); + } + } + + + List applicationIds = applicationDAO.addApplications(appsToAdd, tenantId); applicationMappingDAO.addApplicationMappings(device.getId(), applicationIds, tenantId); + + applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove,tenantId); + } catch (DeviceManagementDAOException deviceDaoEx) { String errorMsg = "Error occurred saving application list to the device"; log.error(errorMsg + ":" + deviceIdentifier.toString()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java index eb729199fc..5cc8b3c7d6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java @@ -28,7 +28,7 @@ public interface ApplicationDAO { List addApplications(List applications, int tenantId) throws DeviceManagementDAOException; - int removeApplication(String applicationName, int tenantId) throws DeviceManagementDAOException; + List removeApplications(List apps, int tenantId) throws DeviceManagementDAOException; Application getApplication(String identifier, int tenantId) throws DeviceManagementDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java index 327e80f3fa..372ea626ec 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java @@ -26,8 +26,9 @@ public interface ApplicationMappingDAO { int addApplicationMapping(int deviceId, int applicationId, int tenantId) throws DeviceManagementDAOException; - List addApplicationMappings(int deviceId, List applicationIds, - int tenantId) throws DeviceManagementDAOException; + List addApplicationMappings(int deviceId, List applicationIds, int tenantId) + throws DeviceManagementDAOException; - int removeApplicationMapping(int deviceId, int applicationId, int tenantId) throws DeviceManagementDAOException; + void removeApplicationMapping(int deviceId, List appIdList, int tenantId) + throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java index 465fdb3f98..f73c1038e5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java @@ -86,10 +86,11 @@ public class ApplicationDAOImpl implements ApplicationDAO { conn = this.getConnection(); stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, CATEGORY, " + "VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES,APP_IDENTIFIER) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?)"); + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?)", Statement.RETURN_GENERATED_KEYS); for (Application application : applications) { + stmt.setString(1, application.getName()); stmt.setString(2, application.getPlatform()); stmt.setString(3, application.getCategory()); @@ -117,33 +118,38 @@ public class ApplicationDAOImpl implements ApplicationDAO { } @Override - public int removeApplication(String applicationName, int tenantId) throws DeviceManagementDAOException { + public List removeApplications(List apps, int tenantId) throws DeviceManagementDAOException { + Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; int applicationId = -1; + List applicationIds = new ArrayList(); + try { conn = this.getConnection(); conn.setAutoCommit(false); - stmt = conn.prepareStatement("DELETE DM_APPLICATION WHERE NAME = ? AND TENANT_ID = ?"); - stmt.setString(1, applicationName); - stmt.setInt(2, tenantId); - stmt.execute(); - conn.commit(); + stmt = conn.prepareStatement("DELETE DM_APPLICATION WHERE APP_IDENTIFIER = ? AND TENANT_ID = ?", + Statement.RETURN_GENERATED_KEYS); + for(Application app:apps){ + stmt.setString(1, app.getApplicationIdentifier()); + stmt.setInt(2, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); rs = stmt.getGeneratedKeys(); if (rs.next()) { - applicationId = rs.getInt(1); + applicationIds.add(rs.getInt(1)); } - return applicationId; + return applicationIds; } catch (SQLException e) { try { conn.rollback(); } catch (SQLException e1) { log.warn("Error occurred while roll-backing the transaction", e); } - throw new DeviceManagementDAOException("Error occurred while removing application '" + - applicationName + "'", e); + throw new DeviceManagementDAOException("Error occurred while removing bulk application list", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java index 2a830603f7..4a5ea72e16 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java @@ -102,8 +102,9 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { } @Override - public int removeApplicationMapping(int deviceId, int applicationId, - int tenantId) throws DeviceManagementDAOException { + public void removeApplicationMapping(int deviceId, List appIdList, int tenantId) + throws DeviceManagementDAOException { + Connection conn; ResultSet rs; int mappingId = -1; @@ -112,17 +113,15 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { String sql = "DELETE DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " + "APPLICATION_ID = ? AND TENANT_ID = ?"; PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); - stmt.setInt(1, deviceId); - stmt.setInt(2, applicationId); - stmt.setInt(3, tenantId); - stmt.execute(); - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - mappingId = rs.getInt(1); + for(Integer appId:appIdList){ + stmt.setInt(1, deviceId); + stmt.setInt(2, appId); + stmt.setInt(3, tenantId); + stmt.addBatch(); } - return mappingId; - } catch (SQLException e) { + stmt.executeBatch(); + } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e); } } 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 150287fdbd..a60e626b82 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 @@ -53,21 +53,37 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv private DeviceTypeDAO deviceTypeDAO; private EnrolmentDAO enrolmentDAO; private DeviceManagementPluginRepository pluginRepository; + private boolean isTest = false; private static Log log = LogFactory.getLog(DeviceManagementProviderServiceImpl.class); + private int tenantId; public DeviceManagementProviderServiceImpl() { this.pluginRepository = new DeviceManagementPluginRepository(); - this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); - this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - this.enrolmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO(); - + initDataAccessObjects(); /* Registering a listener to retrieve events when some device management service plugin is installed after * the component is done getting initialized */ 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 public String getProviderType() { return null; @@ -87,8 +103,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override 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); try { if (dms.isClaimable(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()))) { @@ -96,7 +112,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } else { device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE); } - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = getTenantId(); DeviceManagementDAOFactory.beginTransaction(); @@ -687,4 +703,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } + public int getTenantId() { + + ThreadLocal tenantId = new ThreadLocal(); + int tenant = 0; + + if (isTest){ + tenant = org.wso2.carbon.device.mgt.core.common.; + }else{ + tenant = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + } + return tenant; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java index f074adc2e5..89ddc53b13 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java @@ -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.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.core.common.TestDataHolder; import java.util.List; 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 public String getProviderType() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/BaseDeviceManagementDAOTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/BaseDeviceManagementDAOTest.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java index bf4a460534..e57b888f2b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/BaseDeviceManagementDAOTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java @@ -16,7 +16,7 @@ * 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.LogFactory; @@ -28,7 +28,7 @@ import org.testng.annotations.BeforeSuite; import org.w3c.dom.Document; import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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 javax.sql.DataSource; @@ -41,10 +41,10 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; -public abstract class BaseDeviceManagementDAOTest { +public abstract class BaseDeviceManagementTest { private DataSource dataSource; - private static final Log log = LogFactory.getLog(BaseDeviceManagementDAOTest.class); + private static final Log log = LogFactory.getLog(BaseDeviceManagementTest.class); @BeforeSuite public void setupDataSource() throws Exception { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceDAOTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java similarity index 68% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceDAOTests.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java index 427d664a0b..02c649187f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceDAOTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java @@ -24,33 +24,25 @@ import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; 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(ApplicationPersistenceDAOTests.class); + private static final Log log = LogFactory.getLog(ApplicationPersistenceTests.class); private ApplicationDAO applicationDAO = DeviceManagementDAOFactory.getApplicationDAO(); @Test public void testAddApplication() { /* 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 */ + String testAppIdentifier = "test sample1"; try { DeviceManagementDAOFactory.openConnection(); - applicationDAO.addApplication(source, -1234); + applicationDAO.addApplication(TestDataHolder.generateApplicationDummyData(testAppIdentifier), -1234); } catch (DeviceManagementDAOException e) { - log.error("Error occurred while adding application '" + source.getName() + "'", e); + log.error("Error occurred while adding application test sample1", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -61,22 +53,22 @@ public class ApplicationPersistenceDAOTests extends BaseDeviceManagementDAOTest /* Retrieving the application by its name */ Application target = null; try { - target = this.getApplication(source.getApplicationIdentifier(), -1234); + target = this.getApplication(testAppIdentifier, -1234); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving application info"; log.error(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 " + "retrieved"); } - private Application getApplication(String packageName, int tenantId) throws DeviceManagementDAOException { + private Application getApplication(String appIdentifier, int tenantId) throws DeviceManagementDAOException { try { DeviceManagementDAOFactory.openConnection(); - return applicationDAO.getApplication(packageName, tenantId); + return applicationDAO.getApplication(appIdentifier, tenantId); } finally { try { DeviceManagementDAOFactory.closeConnection(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java similarity index 70% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java index 10d77ee3ab..e392b3335f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java @@ -21,27 +21,24 @@ package org.wso2.carbon.device.mgt.core.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.Assert; -import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; 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.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.DeviceManagementException; 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 java.sql.*; -import java.util.Date; -public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { +public class DevicePersistTests extends BaseDeviceManagementTest { DeviceDAO deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - private static final Log log = LogFactory.getLog(DeviceManagementDAOTests.class); + private static final Log log = LogFactory.getLog(DevicePersistTests.class); @BeforeClass @Override @@ -51,7 +48,7 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { @Test public void testAddDeviceTypeTest() { - DeviceType deviceType = this.loadDummyDeviceType(); + DeviceType deviceType = TestDataHolder.generateDeviceTypeData(TestDataHolder.TEST_DEVICE_TYPE); try { DeviceManagementDAOFactory.openConnection(); deviceTypeDAO.addDeviceType(deviceType); @@ -69,7 +66,7 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { int targetTypeId = -1; try { - targetTypeId = this.getDeviceTypeId(); + targetTypeId = this.getDeviceTypeId(TestDataHolder.TEST_DEVICE_TYPE); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving target device type id"; log.error(msg, e); @@ -78,20 +75,21 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { Assert.assertNotNull(targetTypeId, "Device Type Id is null"); deviceType.setId(targetTypeId); + TestDataHolder.initialTestDeviceType = deviceType; } @Test(dependsOnMethods = {"testAddDeviceTypeTest"}) public void testAddDeviceTest() { - DeviceType deviceType = this.loadDummyDeviceType(); - deviceType.setId(1); - int tenantId = -1234; - Device device = this.loadDummyDevice(); + int tenantId = TestDataHolder.SUPER_TENANT_ID; + Device device = TestDataHolder.generateDummyDeviceData("ios"); + try { DeviceManagementDAOFactory.openConnection(); - int deviceId = deviceDAO.addDevice(deviceType.getId(), device, tenantId); + int deviceId = deviceDAO.addDevice(TestDataHolder.initialTestDeviceType.getId(), device, tenantId); device.setId(deviceId); deviceDAO.addEnrollment(device, tenantId); + TestDataHolder.initialTestDevice = device; } catch (DeviceManagementDAOException e) { String msg = "Error occurred while adding '" + device.getType() + "' device with the identifier '" + device.getDeviceIdentifier() + "'"; @@ -107,7 +105,8 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { int targetId = -1; try { - targetId = this.getDeviceId(); + targetId = this.getDeviceId(TestDataHolder.initialTestDevice.getDeviceIdentifier(), + TestDataHolder.SUPER_TENANT_ID); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving device id"; log.error(msg, e); @@ -117,26 +116,7 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { device.getType() + "' carrying the identifier '" + device.getDeviceIdentifier() + "', is null"); } - private void addDeviceEnrolment() { - 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 { + private int getDeviceId(String deviceIdentification, int tenantId) throws DeviceManagementDAOException { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; @@ -147,8 +127,8 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { conn = getDataSource().getConnection(); String sql = "SELECT ID FROM DM_DEVICE WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setString(1, "111"); - stmt.setInt(2, -1234); + stmt.setString(1, deviceIdentification); + stmt.setInt(2, tenantId); rs = stmt.executeQuery(); 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; Connection conn = null; PreparedStatement stmt = null; String sql = "SELECT ID, NAME FROM DM_DEVICE_TYPE WHERE NAME = ?"; - DeviceType deviceType = this.loadDummyDeviceType(); + DeviceType deviceType = TestDataHolder.generateDeviceTypeData("ios"); try { Assert.assertNotNull(getDataSource(), "Data Source is not initialized properly"); conn = getDataSource().getConnection(); @@ -191,11 +171,14 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { @Test(dependsOnMethods = "testAddDeviceTest") public void testSetEnrolmentStatus() { - Device device = this.loadDummyDevice(); + + Device device = TestDataHolder.initialTestDevice; try { DeviceManagementDAOFactory.openConnection(); 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) { String msg = "Error occurred while setting enrolment status"; log.error(msg, e); @@ -209,24 +192,26 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { } Status target = null; try { - target = this.getEnrolmentStatus(); + target = this.getEnrolmentStatus(device.getDeviceIdentifier(), device.getType(), + TestDataHolder.SUPER_TENANT_ID); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving the target enrolment status"; log.error(msg, e); Assert.fail(msg, e); } - Assert.assertNotNull(target, "Enrolment status retrieved for the device carrying its identifier as '" + device.getDeviceIdentifier() + "' is null"); Assert.assertEquals(target, Status.ACTIVE, "Enrolment status retrieved is not as same as what's configured"); } - private Status getEnrolmentStatus() throws DeviceManagementDAOException { - Device device = this.loadDummyDevice(); + private Status getEnrolmentStatus(String identifier, String deviceType, int tenantId) + throws DeviceManagementDAOException { + + Device device = TestDataHolder.generateDummyDeviceData("ios"); try { DeviceManagementDAOFactory.openConnection(); - DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); - return deviceDAO.getEnrolmentStatus(deviceId, device.getEnrolmentInfo().getOwner(), -1234); + DeviceIdentifier deviceId = new DeviceIdentifier(identifier, deviceType); + return deviceDAO.getEnrolmentStatus(deviceId, device.getEnrolmentInfo().getOwner(), tenantId); } catch (DeviceManagementDAOException e) { throw new DeviceManagementDAOException("Error occurred while retrieving the current status of the " + "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"); - } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceDAOTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceTests.java similarity index 86% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceDAOTests.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceTests.java index 91c4bad037..c1a90ce203 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceDAOTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceTests.java @@ -24,18 +24,17 @@ import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; 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(EnrolmentPersistenceDAOTests.class); + private static final Log log = LogFactory.getLog(EnrolmentPersistenceTests.class); private EnrolmentDAO enrolmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO(); @Test public void testAddEnrolment() { - int deviceId = 1234; + int deviceId = TestDataHolder.initialTestDevice.getId(); String owner = "admin"; /* 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 */ try { DeviceManagementDAOFactory.openConnection(); - enrolmentDAO.addEnrollment(deviceId, source, -1234); + enrolmentDAO.addEnrollment(deviceId, source, TestDataHolder.SUPER_TENANT_ID); } catch (DeviceManagementDAOException e) { log.error("Error occurred while adding enrollment", e); } finally { @@ -59,7 +58,7 @@ public class EnrolmentPersistenceDAOTests extends BaseDeviceManagementDAOTest { /* Retrieving the enrolment associated with the given deviceId and owner */ EnrolmentInfo target = null; try { - target = this.getEnrolmentConfig(deviceId, owner, -1234); + target = this.getEnrolmentConfig(deviceId, owner, TestDataHolder.SUPER_TENANT_ID); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving application info"; log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index 1a82c6d651..935fde5089 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -24,11 +24,16 @@ - - + + - + + + + + + \ No newline at end of file