From da61c08fac666ad046af0533c6c502dc459ed8c3 Mon Sep 17 00:00:00 2001 From: manoj Date: Tue, 14 Jul 2015 10:59:39 +0530 Subject: [PATCH 1/4] Add application update and unit tests --- ...ApplicationManagerProviderServiceImpl.java | 25 ++++- .../device/mgt/core/dao/ApplicationDAO.java | 2 +- .../mgt/core/dao/ApplicationMappingDAO.java | 7 +- .../mgt/core/dao/impl/ApplicationDAOImpl.java | 28 +++--- .../dao/impl/ApplicationMappingDAOImpl.java | 21 ++-- .../DeviceManagementProviderServiceImpl.java | 42 ++++++-- .../mgt/core/TestDeviceManagementService.java | 3 +- .../BaseDeviceManagementTest.java} | 8 +- ....java => ApplicationPersistenceTests.java} | 30 +++--- ...tDAOTests.java => DevicePersistTests.java} | 99 ++++++------------- ...ts.java => EnrolmentPersistenceTests.java} | 15 ++- .../src/test/resources/testng.xml | 11 ++- 12 files changed, 155 insertions(+), 136 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/{dao/BaseDeviceManagementDAOTest.java => common/BaseDeviceManagementTest.java} (96%) rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/{ApplicationPersistenceDAOTests.java => ApplicationPersistenceTests.java} (68%) rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/{DeviceManagementDAOTests.java => DevicePersistTests.java} (70%) rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/{EnrolmentPersistenceDAOTests.java => EnrolmentPersistenceTests.java} (86%) 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 From 41d1408b14188147c2e74d1c87fac3c1e1498d39 Mon Sep 17 00:00:00 2001 From: manoj Date: Tue, 14 Jul 2015 11:00:16 +0530 Subject: [PATCH 2/4] Add application update and unit tests --- .../mgt/core/common/TestDataHolder.java | 59 +++++++++++++++ ...icationManagementProviderServiceTests.java | 60 +++++++++++++++ .../DeviceManagementProviderServiceTest.java | 73 +++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/ApplicationManagementProviderServiceTests.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java 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 new file mode 100644 index 0000000000..899117fd06 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java @@ -0,0 +1,59 @@ +package org.wso2.carbon.device.mgt.core.common; + +import org.wso2.carbon.device.mgt.common.Device; +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.dto.DeviceType; + +import java.util.Date; +import java.util.Properties; + +public class TestDataHolder { + + public static Device initialTestDevice; + public static DeviceType initialTestDeviceType; + public static String TEST_DEVICE_TYPE = "Test"; + public static Integer SUPER_TENANT_ID = -1234; + public static ThreadLocal tenant = new ThreadLocal(); + + public static Device generateDummyDeviceData(String deviceType){ + + Device device = new Device(); + EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); + enrolmentInfo.setDateOfEnrolment(new Date().getTime()); + enrolmentInfo.setDateOfLastUpdate(new Date().getTime()); + enrolmentInfo.setOwner("admin"); + enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); + enrolmentInfo.setStatus(EnrolmentInfo.Status.CREATED); + device.setEnrolmentInfo(enrolmentInfo); + device.setDescription("Test Description"); + device.setDeviceIdentifier("1234"); + device.setType(deviceType); + return device; + + } + + public static DeviceType generateDeviceTypeData(String devTypeName){ + DeviceType deviceType = new DeviceType(); + deviceType.setName(devTypeName); + return deviceType; + } + + public static Application generateApplicationDummyData(String appIdentifier){ + + Application application = new Application(); + Properties properties = new Properties(); + properties.setProperty("test1","testVal"); + + application.setName("SimpleCalculator"); + application.setCategory("TestCategory"); + application.setApplicationIdentifier(appIdentifier); + application.setType("TestType"); + application.setVersion("1.0.0"); + application.setImageUrl("http://test.org/image/"); + application.setLocationUrl("http://test.org/location/"); + application.setAppProperties(properties); + + return application; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/ApplicationManagementProviderServiceTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/ApplicationManagementProviderServiceTests.java new file mode 100644 index 0000000000..dbd71eb7f0 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/ApplicationManagementProviderServiceTests.java @@ -0,0 +1,60 @@ +package org.wso2.carbon.device.mgt.core.service; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; + +public class ApplicationManagementProviderServiceTests extends BaseDeviceManagementTest{ + + private static final Log log = LogFactory.getLog(ApplicationManagementProviderServiceTests.class); + + @BeforeClass + @Override + public void init() throws Exception { + this.initDatSource(); + } + + @Test + public void testAddApplications() { + +/* ArrayList sourceApplications = new ArrayList(); + sourceApplications.add(TestDataHolder.generateApplicationDummyData("Test App2")); + sourceApplications.add(TestDataHolder.generateApplicationDummyData("Test App3")); + + DeviceManagementTests deviceManagementDAOTests = new DeviceManagementTests(); + deviceManagementDAOTests.testAddDeviceTest(); + + Device device = TestDataHolder.initialTestDevice; + + try { + DeviceManagementDAOFactory.openConnection(); + + + } catch (DeviceManagementDAOException e) { + log.error("Error occurred while adding application", e); + } finally { + try { + DeviceManagementDAOFactory.closeConnection(); + } catch (DeviceManagementDAOException e) { + log.warn("Error occurred while closing the connection", e); + } + } + *//* Retrieving the application by its name *//* + Application target = null; + try { + target = this.getApplication(source.getApplicationIdentifier(), -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 " + + "what's " + + "retrieved");*/ + } + + +} 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 new file mode 100644 index 0000000000..5985d5ece3 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java @@ -0,0 +1,73 @@ +package org.wso2.carbon.device.mgt.core.service; + +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.context.CarbonContext; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.context.internal.CarbonContextDataHolder; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; +import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; +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.dao.DeviceManagementDAOException; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; + +public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest { + + private static final Log log = LogFactory.getLog(DeviceManagementProviderServiceTest.class); + DeviceManagementProviderService deviceManagementProviderService = null; + + + + @BeforeClass + @Override + public void init() throws Exception { + initDatSource(); + DeviceManagementPluginRepository deviceManagementPluginRepository = new DeviceManagementPluginRepository(); + TestDeviceManagementService testDeviceManagementService = new TestDeviceManagementService(); + deviceManagementPluginRepository.addDeviceManagementProvider(testDeviceManagementService); + deviceManagementProviderService = new DeviceManagementProviderServiceImpl(deviceManagementPluginRepository, + true); + } + + + + @Test + public void testEnrollment() { + + try { + DeviceManagerUtil.registerDeviceType(TestDataHolder.TEST_DEVICE_TYPE); + TestDataHolder.tenant.set(TestDataHolder.SUPER_TENANT_ID); + Device device = TestDataHolder.generateDummyDeviceData(TestDataHolder.TEST_DEVICE_TYPE); + boolean isEnrolled = deviceManagementProviderService.enrollDevice(device); + + if (isEnrolled){ + TestDataHolder.initialTestDevice = device; + } + + } catch (DeviceManagementException e) { + String msg = "Error occurred while adding device type '" + TestDataHolder.TEST_DEVICE_TYPE + "'"; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + try { + DeviceManagementDAOFactory.closeConnection(); + } catch (DeviceManagementDAOException e) { + log.warn("Error occurred while closing the connection", e); + } + } + } + + @AfterClass + public void cleanResources(){ + // PrivilegedCarbonContext.endTenantFlow(); + } +} From de1721730385750c74f5fb59d3ec85a9c7e227de Mon Sep 17 00:00:00 2001 From: manoj Date: Wed, 15 Jul 2015 07:37:20 +0530 Subject: [PATCH 3/4] Unit tests, application update --- .../DeviceManagementPluginRepository.java | 4 -- ...ApplicationManagerProviderServiceImpl.java | 26 +++++++- .../DeviceManagementProviderServiceImpl.java | 2 - .../mgt/core/TestDeviceManagementService.java | 66 ++++++++----------- .../core/common/BaseDeviceManagementTest.java | 27 ++++++++ .../mgt/core/common/TestDataHolder.java | 3 +- ...icationManagementProviderServiceTests.java | 60 ----------------- .../DeviceManagementProviderServiceTest.java | 10 ++- .../config/datasource/data-source-config.xml | 2 +- .../src/test/resources/testng.xml | 1 + .../framework/APIMapperContextListener.java | 1 - 11 files changed, 87 insertions(+), 115 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/ApplicationManagementProviderServiceTests.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java index c52561e792..4c276405ff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java @@ -43,10 +43,7 @@ public class DeviceManagementPluginRepository { throw new DeviceManagementException("Error occurred while adding device management provider '" + deviceType + "'"); } - providers.put(deviceType, provider); - System.out.println("@plugin Repo:"+deviceType); - System.out.println("@plugin Repo:"+providers.size()); } public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { @@ -55,7 +52,6 @@ public class DeviceManagementPluginRepository { } public DeviceManagementService getDeviceManagementService(String type) { - System.out.println("@plugin get:"+providers.get(type).toString()); return providers.get(type); } 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 0afadab9af..d9cab6e58a 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 @@ -41,6 +41,7 @@ import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.identity.IdentityConfigurations; import org.wso2.carbon.device.mgt.core.dao.*; import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceException; import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceStub; import org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO; @@ -62,6 +63,7 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem private DeviceDAO deviceDAO; private ApplicationDAO applicationDAO; private ApplicationMappingDAO applicationMappingDAO; + private boolean isTest; private static final String GET_APP_LIST_URL = "store/apis/assets/mobileapp?domain=carbon.super&page=1"; @@ -88,6 +90,14 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem this.applicationMappingDAO = DeviceManagementDAOFactory.getApplicationMappingDAO(); } + ApplicationManagerProviderServiceImpl(DeviceManagementPluginRepository pluginRepository, boolean testMode) { + this.pluginRepository = pluginRepository; + this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + this.applicationDAO = DeviceManagementDAOFactory.getApplicationDAO(); + this.applicationMappingDAO = DeviceManagementDAOFactory.getApplicationMappingDAO(); + isTest = testMode; + } + @Override public Application[] getApplications(String domain, int pageNumber, int size) throws ApplicationManagementException { @@ -171,7 +181,7 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem public void updateApplicationListInstalledInDevice( DeviceIdentifier deviceIdentifier, List applications) throws ApplicationManagementException { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = getTenantId(); try { Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); @@ -205,12 +215,24 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem } } + private int getTenantId() { + + int tenantId = 0; + if (isTest){ + tenantId = DeviceManagerUtil.currentTenant.get(); + }else{ + tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + } + + return tenantId; + } + @Override public List getApplicationListForDevice(DeviceIdentifier deviceIdentifier) throws ApplicationManagementException { Device device = null; try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = getTenantId(); device = deviceDAO.getDevice(deviceIdentifier, tenantId); return applicationDAO.getInstalledApplications(device.getId()); }catch (DeviceManagementDAOException deviceDaoEx) { 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 807c76b649..5ccb5c6410 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 @@ -101,8 +101,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManager dms = this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); - - System.out.println("dms________"+device.getType()); boolean status = dms.enrollDevice(device); try { if (dms.isClaimable(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()))) { 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 ffd3061ed1..0c3386fddf 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 @@ -21,72 +21,64 @@ import org.wso2.carbon.device.mgt.common.*; 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.ApplicationManager; +import org.wso2.carbon.device.mgt.common.license.mgt.License; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import java.util.List; -public class TestDeviceManagementService implements DeviceManager { +public class TestDeviceManagementService implements DeviceManagementService { private String providerType; public TestDeviceManagementService(String deviceType){ providerType = deviceType; } - - @Override public FeatureManager getFeatureManager() { - return null; + @Override + public String getType() { + return providerType; } - @Override public boolean enrollDevice(Device device) throws DeviceManagementException { - return false; - } + @Override + public void init() throws DeviceManagementException { - @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { - return false; } - @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - return false; + @Override + public DeviceManager getDeviceManager() { + return new TestDeviceManager(); } - @Override public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { - return false; + @Override + public ApplicationManager getApplicationManager() { + return null; } - @Override public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { - return false; + @Override + public Application[] getApplications(String domain, int pageNumber, int size) + throws ApplicationManagementException { + return new Application[0]; } - @Override public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException { - return false; - } + @Override + public void updateApplicationStatus(DeviceIdentifier deviceId, Application application, String status) + throws ApplicationManagementException { - @Override public List getAllDevices() throws DeviceManagementException { - return null; } - @Override public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + @Override + public String getApplicationStatus(DeviceIdentifier deviceId, Application application) + throws ApplicationManagementException { return null; } - @Override public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) - throws DeviceManagementException { - return false; - } - - @Override public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) - throws DeviceManagementException { - return false; - } - - @Override public boolean isClaimable(DeviceIdentifier deviceId) throws DeviceManagementException { - return false; - } + @Override + public void installApplication(Operation operation, List deviceIdentifiers) + throws ApplicationManagementException { - @Override public boolean setStatus(DeviceIdentifier deviceId, String currentOwner, EnrolmentInfo.Status status) - throws DeviceManagementException { - return false; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java index e57b888f2b..53546ce5b2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java @@ -101,6 +101,8 @@ public abstract class BaseDeviceManagementTest { conn.setAutoCommit(false); this.cleanupEnrolmentData(conn); + this.cleanApplicationMappingData(conn); + this.cleanApplicationData(conn); this.cleanupDeviceData(conn); this.cleanupDeviceTypeData(conn); @@ -127,6 +129,31 @@ public abstract class BaseDeviceManagementTest { } } + private void cleanApplicationMappingData(Connection conn) throws SQLException{ + PreparedStatement stmt = null; + try { + stmt = conn.prepareStatement("DELETE FROM DM_DEVICE_APPLICATION_MAPPING"); + stmt.execute(); + } finally { + if (stmt != null) { + stmt.close(); + } + } + } + + private void cleanApplicationData(Connection conn) throws SQLException{ + PreparedStatement stmt = null; + try { + stmt = conn.prepareStatement("DELETE FROM DM_APPLICATION"); + stmt.execute(); + } finally { + if (stmt != null) { + stmt.close(); + } + } + } + + private void cleanupEnrolmentData(Connection conn) throws SQLException { PreparedStatement stmt = null; try { 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 1c110a16bf..2defb785e8 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 @@ -14,7 +14,7 @@ public class TestDataHolder { public static DeviceType initialTestDeviceType; public static String TEST_DEVICE_TYPE = "Test"; public static Integer SUPER_TENANT_ID = -1234; - public static ThreadLocal tenant = new ThreadLocal(); + public static String initialDeviceIdentifier = "12345"; public static Device generateDummyDeviceData(String deviceType){ @@ -30,7 +30,6 @@ public class TestDataHolder { device.setDeviceIdentifier("12345"); device.setType(deviceType); return device; - } public static DeviceType generateDeviceTypeData(String devTypeName){ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/ApplicationManagementProviderServiceTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/ApplicationManagementProviderServiceTests.java deleted file mode 100644 index dbd71eb7f0..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/ApplicationManagementProviderServiceTests.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.wso2.carbon.device.mgt.core.service; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; -import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; - -public class ApplicationManagementProviderServiceTests extends BaseDeviceManagementTest{ - - private static final Log log = LogFactory.getLog(ApplicationManagementProviderServiceTests.class); - - @BeforeClass - @Override - public void init() throws Exception { - this.initDatSource(); - } - - @Test - public void testAddApplications() { - -/* ArrayList sourceApplications = new ArrayList(); - sourceApplications.add(TestDataHolder.generateApplicationDummyData("Test App2")); - sourceApplications.add(TestDataHolder.generateApplicationDummyData("Test App3")); - - DeviceManagementTests deviceManagementDAOTests = new DeviceManagementTests(); - deviceManagementDAOTests.testAddDeviceTest(); - - Device device = TestDataHolder.initialTestDevice; - - try { - DeviceManagementDAOFactory.openConnection(); - - - } catch (DeviceManagementDAOException e) { - log.error("Error occurred while adding application", e); - } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } - } - *//* Retrieving the application by its name *//* - Application target = null; - try { - target = this.getApplication(source.getApplicationIdentifier(), -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 " + - "what's " + - "retrieved");*/ - } - - -} 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 dcc282c1b2..41780b7f5f 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 @@ -26,16 +26,13 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes DeviceManagementProviderService deviceManagementProviderService = null; - @BeforeClass @Override public void init() throws Exception { initDatSource(); } - - - @Test + @Test public void testEnrollment() { try { @@ -46,10 +43,12 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes deviceManagementProviderService = new DeviceManagementProviderServiceImpl(deviceManagementPluginRepository, true); DeviceManagerUtil.registerDeviceType(TestDataHolder.TEST_DEVICE_TYPE); - TestDataHolder.tenant.set(TestDataHolder.SUPER_TENANT_ID); + DeviceManagerUtil.currentTenant.set(TestDataHolder.SUPER_TENANT_ID); + Device device = TestDataHolder.generateDummyDeviceData(TestDataHolder.TEST_DEVICE_TYPE); boolean isEnrolled = deviceManagementProviderService.enrollDevice(device); + Assert.assertEquals(isEnrolled,true,"Enrolment fail"); if (isEnrolled){ TestDataHolder.initialTestDevice = device; } @@ -69,6 +68,5 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes @AfterClass public void cleanResources(){ - // PrivilegedCarbonContext.endTenantFlow(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/data-source-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/data-source-config.xml index b07866513a..750b8fffa8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/data-source-config.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/data-source-config.xml @@ -18,7 +18,7 @@ --> - jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1 + jdbc:h2:mem:cdm-test-db;DB_CLOSE_ON_EXIT=FALSE;MVCC=true org.h2.Driver wso2carbon wso2carbon 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 935fde5089..cf107283b8 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 @@ -34,6 +34,7 @@ + \ No newline at end of file diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/APIMapperContextListener.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/APIMapperContextListener.java index a4c45282bb..21069b6710 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/APIMapperContextListener.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/APIMapperContextListener.java @@ -27,7 +27,6 @@ public class APIMapperContextListener implements LifecycleListener { @Override public void lifecycleEvent(LifecycleEvent lifecycleEvent) { if (Lifecycle.AFTER_INIT_EVENT.equals(lifecycleEvent.getType())) { - System.out.println("Deployeddd"); } } From 00819fe4b67b6074f92897fa8cc5d9558dd89d2e Mon Sep 17 00:00:00 2001 From: manoj Date: Wed, 15 Jul 2015 07:38:13 +0530 Subject: [PATCH 4/4] Unit tests, application update --- .../device/mgt/core/TestDeviceManager.java | 76 ++++++++++++++++ ...licationManagementProviderServiceTest.java | 88 +++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java new file mode 100644 index 0000000000..1e83880cb7 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java @@ -0,0 +1,76 @@ +package org.wso2.carbon.device.mgt.core; + +import org.wso2.carbon.device.mgt.common.*; + +import java.util.List; + +public class TestDeviceManager implements DeviceManager { + + @Override + public FeatureManager getFeatureManager() { + return null; + } + + @Override + public boolean enrollDevice(Device device) throws DeviceManagementException { + return true; + } + + @Override + public boolean modifyEnrollment(Device device) throws DeviceManagementException { + return false; + } + + @Override + public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + return false; + } + + @Override + public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { + return false; + } + + @Override + public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { + return false; + } + + @Override + public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException { + return false; + } + + @Override + public List getAllDevices() throws DeviceManagementException { + return null; + } + + @Override + public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + return null; + } + + @Override + public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) + throws DeviceManagementException { + return false; + } + + @Override + public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) + throws DeviceManagementException { + return false; + } + + @Override + public boolean isClaimable(DeviceIdentifier deviceId) throws DeviceManagementException { + return false; + } + + @Override + public boolean setStatus(DeviceIdentifier deviceId, String currentOwner, EnrolmentInfo.Status status) + throws DeviceManagementException { + return false; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java new file mode 100644 index 0000000000..a8142f2d5c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java @@ -0,0 +1,88 @@ +package org.wso2.carbon.device.mgt.core.app.mgt; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.Assert; +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.DeviceManagementException; +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.core.DeviceManagementPluginRepository; +import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; +import org.wso2.carbon.device.mgt.core.api.mgt.ApplicationManagementProviderService; +import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagerProviderServiceImpl; +import org.wso2.carbon.device.mgt.core.app.mgt.config.AppManagementConfig; +import org.wso2.carbon.device.mgt.core.common.TestDataHolder; + +import java.util.ArrayList; +import java.util.List; + +public class ApplicationManagementProviderServiceTest { + + private ApplicationManagementProviderService appMgtProvider; + private static final Log log = LogFactory.getLog(ApplicationManagementProviderServiceTest.class); + private DeviceManagementPluginRepository deviceManagementPluginRepository = null; + + @BeforeClass + public void init() { + deviceManagementPluginRepository = new DeviceManagementPluginRepository(); + TestDeviceManagementService testDeviceManagementService = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); + try { + deviceManagementPluginRepository.addDeviceManagementProvider(testDeviceManagementService); + } catch (DeviceManagementException e) { + String msg = "Error occurred while initiate plugins '" + TestDataHolder.TEST_DEVICE_TYPE + "'"; + log.error(msg, e); + Assert.fail(msg, e); + } + } + + @Test + public void updateApplicationTest(){ + + List applications = new ArrayList(); + + Application application1 = TestDataHolder.generateApplicationDummyData("org.wso2.app1"); + Application application2 = TestDataHolder.generateApplicationDummyData("org.wso2.app2"); + Application application3 = TestDataHolder.generateApplicationDummyData("org.wso2.app3"); + + applications.add(application1); + applications.add(application2); + applications.add(application3); + + Device device = TestDataHolder.initialTestDevice; + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(TestDataHolder.initialDeviceIdentifier); + deviceIdentifier.setType(device.getType()); + + AppManagementConfig appManagementConfig = new AppManagementConfig(); + appMgtProvider = new ApplicationManagerProviderServiceImpl(deviceManagementPluginRepository, true); + + try { + appMgtProvider.updateApplicationListInstalledInDevice(deviceIdentifier, applications); + } catch (ApplicationManagementException appMgtEx){ + String msg = "Error occurred while updating app list '" + TestDataHolder.TEST_DEVICE_TYPE + "'"; + log.error(msg, appMgtEx); + Assert.fail(msg, appMgtEx); + } + + Application application4 = TestDataHolder.generateApplicationDummyData("org.wso2.app3"); + applications = new ArrayList(); + applications.add(application4); + applications.add(application3); + + try { + appMgtProvider.updateApplicationListInstalledInDevice(deviceIdentifier, applications); + List installedApps = appMgtProvider.getApplicationListForDevice(deviceIdentifier); + Assert.assertEquals(installedApps.size(),2,"Num of installed applications should be two"); + } catch (ApplicationManagementException appMgtEx){ + String msg = "Error occurred while updating app list '" + TestDataHolder.TEST_DEVICE_TYPE + "'"; + log.error(msg, appMgtEx); + Assert.fail(msg, appMgtEx); + } + + } + +}