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 c2ec4f8d7f2..d9cab6e58a6 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,11 +41,13 @@ 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; import java.rmi.RemoteException; +import java.util.ArrayList; import java.util.List; /** @@ -61,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"; @@ -87,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 { @@ -170,11 +181,33 @@ 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); - 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()); @@ -182,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/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 eb729199fcb..5cc8b3c7d6e 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 327e80f3fa1..372ea626ec6 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 465fdb3f982..f73c1038e52 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 2a830603f75..4a5ea72e16e 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 6e9ccdd0509..7108c6ddd9f 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 @@ -36,6 +36,7 @@ import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.EmailServiceDataHolder; import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.user.api.UserStoreException; import java.io.IOException; @@ -52,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 + */ + 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 FeatureManager getFeatureManager() { return null; @@ -81,6 +98,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean enrollDevice(Device device) throws DeviceManagementException { + DeviceManager dms = this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); boolean status = dms.enrollDevice(device); @@ -90,7 +108,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } else { device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE); } - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = getTenantId(); DeviceManagementDAOFactory.beginTransaction(); @@ -575,9 +593,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv String[] users; int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { - users = - DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm( - tenantId).getUserStoreManager().getUserListOfRole(role); + users = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) + .getUserStoreManager().getUserListOfRole(role); } catch (UserStoreException e) { throw new DeviceManagementException("Error occurred while obtaining the users, who are assigned " + "with the role '" + role + "'", e); @@ -699,6 +716,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } + public List getDevicesByStatus(EnrolmentInfo.Status status) throws DeviceManagementException { List devices = new ArrayList(); List allDevices; @@ -735,4 +753,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return devices; } + + public int getTenantId() { + + ThreadLocal tenantId = new ThreadLocal(); + int tenant = 0; + + if (isTest){ + tenant = DeviceManagerUtil.currentTenant.get(); + }else{ + tenant = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + } + return tenant; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 4824537db42..8fc16db4ec9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -49,6 +49,7 @@ import java.util.*; public final class DeviceManagerUtil { private static final Log log = LogFactory.getLog(DeviceManagerUtil.class); + public static ThreadLocal currentTenant = new ThreadLocal(); enum HTTPMethod { GET, POST, DELETE, PUT, OPTIONS @@ -215,4 +216,5 @@ public final class DeviceManagerUtil { return uriTemplates; } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java index d4d54b6a628..127ee0d707e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java @@ -23,6 +23,7 @@ import org.testng.annotations.Test; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.core.common.TestDataHolder; public class DeviceManagementRepositoryTests { @@ -35,27 +36,27 @@ public class DeviceManagementRepositoryTests { @Test public void testAddDeviceManagementService() { - DeviceManagementService sourceProvider = new TestDeviceManagementService(); + DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); try { this.getRepository().addDeviceManagementProvider(sourceProvider); } catch (DeviceManagementException e) { Assert.fail("Unexpected error occurred while invoking addDeviceManagementProvider functionality", e); } DeviceManagementService targetProvider = - this.getRepository().getDeviceManagementService(TestDeviceManagementService.DEVICE_TYPE_TEST); + this.getRepository().getDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); Assert.assertEquals(targetProvider.getType(), sourceProvider.getType()); } @Test(dependsOnMethods = "testAddDeviceManagementService") public void testRemoveDeviceManagementService() { - DeviceManagementService sourceProvider = new TestDeviceManagementService(); + DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); try { this.getRepository().removeDeviceManagementProvider(sourceProvider); } catch (DeviceManagementException e) { Assert.fail("Unexpected error occurred while invoking removeDeviceManagementProvider functionality", e); } DeviceManagementService targetProvider = - this.getRepository().getDeviceManagementService(TestDeviceManagementService.DEVICE_TYPE_TEST); + this.getRepository().getDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); Assert.assertNull(targetProvider); } 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 58f583edee7..0c3386fddf1 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,58 +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 DeviceManagementService { - public static final String DEVICE_TYPE_TEST = "Test"; - + private String providerType; + public TestDeviceManagementService(String deviceType){ + providerType = deviceType; + } @Override public String getType() { - return TestDeviceManagementService.DEVICE_TYPE_TEST; + return providerType; } @Override public void init() throws DeviceManagementException { - + } @Override public DeviceManager getDeviceManager() { - return null; + return new TestDeviceManager(); } @Override public ApplicationManager getApplicationManager() { - return null; + return null; } @Override - public Application[] getApplications(String domain, int pageNumber, - int size) throws ApplicationManagementException { - return new Application[0]; + public Application[] getApplications(String domain, int pageNumber, int size) + throws ApplicationManagementException { + return new Application[0]; } @Override - public void updateApplicationStatus(DeviceIdentifier deviceId, Application application, - String status) throws ApplicationManagementException { - + public void updateApplicationStatus(DeviceIdentifier deviceId, Application application, String status) + throws ApplicationManagementException { + } @Override - public String getApplicationStatus(DeviceIdentifier deviceId, - Application application) throws ApplicationManagementException { - return null; + public String getApplicationStatus(DeviceIdentifier deviceId, Application application) + throws ApplicationManagementException { + return null; } @Override - public void installApplication(Operation operation, - List deviceIdentifiers) throws ApplicationManagementException { - - } + public void installApplication(Operation operation, List deviceIdentifiers) + throws ApplicationManagementException { + } } 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 00000000000..1e83880cb7d --- /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 00000000000..a8142f2d5c9 --- /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); + } + + } + +} 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 85% 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 bf4a4605345..53546ce5b26 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 { @@ -101,6 +101,8 @@ public abstract class BaseDeviceManagementDAOTest { 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 BaseDeviceManagementDAOTest { } } + 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 new file mode 100644 index 00000000000..2defb785e8d --- /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,58 @@ +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 String initialDeviceIdentifier = "12345"; + + 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("12345"); + 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/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 427d664a0bc..02c649187fc 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 10d77ee3ab7..f3f1f7ebc91 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); @@ -67,31 +64,31 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { } } - int targetTypeId = -1; + Integer targetTypeId = null; 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); Assert.fail(msg, e); } - 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(TestDataHolder.TEST_DEVICE_TYPE); + 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 +104,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 +115,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 +126,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,18 +142,17 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { } } - private int getDeviceTypeId() throws DeviceManagementDAOException { + private int getDeviceTypeId(String deviceTypeName) 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(); try { Assert.assertNotNull(getDataSource(), "Data Source is not initialized properly"); conn = getDataSource().getConnection(); stmt = conn.prepareStatement(sql); - stmt.setString(1, deviceType.getName()); + stmt.setString(1, deviceTypeName); ResultSet rs = stmt.executeQuery(); if (rs.next()) { @@ -191,11 +169,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 +190,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 +221,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 91c4bad0374..c1a90ce2034 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/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 00000000000..41780b7f5f2 --- /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,72 @@ +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(); + } + + @Test + public void testEnrollment() { + + try { + DeviceManagementPluginRepository deviceManagementPluginRepository = new DeviceManagementPluginRepository(); + TestDeviceManagementService testDeviceManagementService = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); + deviceManagementPluginRepository.addDeviceManagementProvider(testDeviceManagementService); + + deviceManagementProviderService = new DeviceManagementProviderServiceImpl(deviceManagementPluginRepository, + true); + DeviceManagerUtil.registerDeviceType(TestDataHolder.TEST_DEVICE_TYPE); + 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; + } + + } 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(){ + } +} 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 b07866513ac..750b8fffa87 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 1a82c6d651f..cf107283b8f 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,17 @@ - - + + - + + + + + + + \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 7cbcabeac07..8e2ca941d08 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -98,6 +98,4 @@ public class PolicyManagerUtil { } return tenantId; } - - } 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 a4c45282bbb..21069b67108 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"); } }