revert-70aa11f8
milanperera 10 years ago
commit 854b7dda8d

@ -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.config.identity.IdentityConfigurations;
import org.wso2.carbon.device.mgt.core.dao.*; import org.wso2.carbon.device.mgt.core.dao.*;
import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener; 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.OAuthAdminServiceException;
import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceStub; import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceStub;
import org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO; import org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -61,6 +63,7 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem
private DeviceDAO deviceDAO; private DeviceDAO deviceDAO;
private ApplicationDAO applicationDAO; private ApplicationDAO applicationDAO;
private ApplicationMappingDAO applicationMappingDAO; private ApplicationMappingDAO applicationMappingDAO;
private boolean isTest;
private static final String GET_APP_LIST_URL = "store/apis/assets/mobileapp?domain=carbon.super&page=1"; 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(); 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 @Override
public Application[] getApplications(String domain, int pageNumber, int size) public Application[] getApplications(String domain, int pageNumber, int size)
throws ApplicationManagementException { throws ApplicationManagementException {
@ -170,11 +181,33 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem
public void updateApplicationListInstalledInDevice( public void updateApplicationListInstalledInDevice(
DeviceIdentifier deviceIdentifier, List<Application> applications) throws ApplicationManagementException { DeviceIdentifier deviceIdentifier, List<Application> applications) throws ApplicationManagementException {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = getTenantId();
try { try {
Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId);
List<Integer> applicationIds = applicationDAO.addApplications(applications, tenantId);
List<Application> installedAppList = getApplicationListForDevice(deviceIdentifier);
List<Application> appsToAdd = new ArrayList<Application>();
List<Integer> appIdsToRemove = new ArrayList<Integer>();
for(Application installedApp:installedAppList){
if (!applications.contains(installedApp)){
appIdsToRemove.add(installedApp.getId());
}
}
for(Application application:applications){
if (!installedAppList.contains(application)){
appsToAdd.add(application);
}
}
List<Integer> applicationIds = applicationDAO.addApplications(appsToAdd, tenantId);
applicationMappingDAO.addApplicationMappings(device.getId(), applicationIds, tenantId); applicationMappingDAO.addApplicationMappings(device.getId(), applicationIds, tenantId);
applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove,tenantId);
} catch (DeviceManagementDAOException deviceDaoEx) { } catch (DeviceManagementDAOException deviceDaoEx) {
String errorMsg = "Error occurred saving application list to the device"; String errorMsg = "Error occurred saving application list to the device";
log.error(errorMsg + ":" + deviceIdentifier.toString()); log.error(errorMsg + ":" + deviceIdentifier.toString());
@ -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 @Override
public List<Application> getApplicationListForDevice(DeviceIdentifier deviceIdentifier) public List<Application> getApplicationListForDevice(DeviceIdentifier deviceIdentifier)
throws ApplicationManagementException { throws ApplicationManagementException {
Device device = null; Device device = null;
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = getTenantId();
device = deviceDAO.getDevice(deviceIdentifier, tenantId); device = deviceDAO.getDevice(deviceIdentifier, tenantId);
return applicationDAO.getInstalledApplications(device.getId()); return applicationDAO.getInstalledApplications(device.getId());
}catch (DeviceManagementDAOException deviceDaoEx) { }catch (DeviceManagementDAOException deviceDaoEx) {

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

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

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

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

@ -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.DeviceManagementServiceComponent;
import org.wso2.carbon.device.mgt.core.internal.EmailServiceDataHolder; 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.internal.PluginInitializationListener;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
import java.io.IOException; import java.io.IOException;
@ -52,21 +53,37 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
private DeviceTypeDAO deviceTypeDAO; private DeviceTypeDAO deviceTypeDAO;
private EnrolmentDAO enrolmentDAO; private EnrolmentDAO enrolmentDAO;
private DeviceManagementPluginRepository pluginRepository; private DeviceManagementPluginRepository pluginRepository;
private boolean isTest = false;
private static Log log = LogFactory.getLog(DeviceManagementProviderServiceImpl.class); private static Log log = LogFactory.getLog(DeviceManagementProviderServiceImpl.class);
private int tenantId;
public DeviceManagementProviderServiceImpl() { public DeviceManagementProviderServiceImpl() {
this.pluginRepository = new DeviceManagementPluginRepository(); this.pluginRepository = new DeviceManagementPluginRepository();
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); initDataAccessObjects();
this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
this.enrolmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
/* Registering a listener to retrieve events when some device management service plugin is installed after /* Registering a listener to retrieve events when some device management service plugin is installed after
* the component is done getting initialized */ * the component is done getting initialized */
DeviceManagementServiceComponent.registerPluginInitializationListener(this); DeviceManagementServiceComponent.registerPluginInitializationListener(this);
} }
/**
* This constructor calls from unit tests
* @param pluginRepo
*/
DeviceManagementProviderServiceImpl(DeviceManagementPluginRepository pluginRepo, boolean test){
this.pluginRepository = pluginRepo;
initDataAccessObjects();
isTest = test;
}
private void initDataAccessObjects() {
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
this.enrolmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
}
@Override @Override
public FeatureManager getFeatureManager() { public FeatureManager getFeatureManager() {
return null; return null;
@ -81,6 +98,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public boolean enrollDevice(Device device) throws DeviceManagementException { public boolean enrollDevice(Device device) throws DeviceManagementException {
DeviceManager dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager();
boolean status = dms.enrollDevice(device); boolean status = dms.enrollDevice(device);
@ -90,7 +108,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} else { } else {
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE);
} }
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = getTenantId();
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
@ -575,9 +593,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
String[] users; String[] users;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
users = users = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)
DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm( .getUserStoreManager().getUserListOfRole(role);
tenantId).getUserStoreManager().getUserListOfRole(role);
} catch (UserStoreException e) { } catch (UserStoreException e) {
throw new DeviceManagementException("Error occurred while obtaining the users, who are assigned " + throw new DeviceManagementException("Error occurred while obtaining the users, who are assigned " +
"with the role '" + role + "'", e); "with the role '" + role + "'", e);
@ -699,6 +716,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
} }
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status) throws DeviceManagementException { public List<Device> getDevicesByStatus(EnrolmentInfo.Status status) throws DeviceManagementException {
List<Device> devices = new ArrayList<Device>(); List<Device> devices = new ArrayList<Device>();
List<Device> allDevices; List<Device> allDevices;
@ -735,4 +753,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return devices; return devices;
} }
public int getTenantId() {
ThreadLocal<Integer> tenantId = new ThreadLocal<Integer>();
int tenant = 0;
if (isTest){
tenant = DeviceManagerUtil.currentTenant.get();
}else{
tenant = CarbonContext.getThreadLocalCarbonContext().getTenantId();
}
return tenant;
}
} }

@ -49,6 +49,7 @@ import java.util.*;
public final class DeviceManagerUtil { public final class DeviceManagerUtil {
private static final Log log = LogFactory.getLog(DeviceManagerUtil.class); private static final Log log = LogFactory.getLog(DeviceManagerUtil.class);
public static ThreadLocal<Integer> currentTenant = new ThreadLocal<Integer>();
enum HTTPMethod { enum HTTPMethod {
GET, POST, DELETE, PUT, OPTIONS GET, POST, DELETE, PUT, OPTIONS
@ -215,4 +216,5 @@ public final class DeviceManagerUtil {
return uriTemplates; return uriTemplates;
} }
} }

@ -23,6 +23,7 @@ import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.DeviceManager;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.core.common.TestDataHolder;
public class DeviceManagementRepositoryTests { public class DeviceManagementRepositoryTests {
@ -35,27 +36,27 @@ public class DeviceManagementRepositoryTests {
@Test @Test
public void testAddDeviceManagementService() { public void testAddDeviceManagementService() {
DeviceManagementService sourceProvider = new TestDeviceManagementService(); DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE);
try { try {
this.getRepository().addDeviceManagementProvider(sourceProvider); this.getRepository().addDeviceManagementProvider(sourceProvider);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
Assert.fail("Unexpected error occurred while invoking addDeviceManagementProvider functionality", e); Assert.fail("Unexpected error occurred while invoking addDeviceManagementProvider functionality", e);
} }
DeviceManagementService targetProvider = DeviceManagementService targetProvider =
this.getRepository().getDeviceManagementService(TestDeviceManagementService.DEVICE_TYPE_TEST); this.getRepository().getDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE);
Assert.assertEquals(targetProvider.getType(), sourceProvider.getType()); Assert.assertEquals(targetProvider.getType(), sourceProvider.getType());
} }
@Test(dependsOnMethods = "testAddDeviceManagementService") @Test(dependsOnMethods = "testAddDeviceManagementService")
public void testRemoveDeviceManagementService() { public void testRemoveDeviceManagementService() {
DeviceManagementService sourceProvider = new TestDeviceManagementService(); DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE);
try { try {
this.getRepository().removeDeviceManagementProvider(sourceProvider); this.getRepository().removeDeviceManagementProvider(sourceProvider);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
Assert.fail("Unexpected error occurred while invoking removeDeviceManagementProvider functionality", e); Assert.fail("Unexpected error occurred while invoking removeDeviceManagementProvider functionality", e);
} }
DeviceManagementService targetProvider = DeviceManagementService targetProvider =
this.getRepository().getDeviceManagementService(TestDeviceManagementService.DEVICE_TYPE_TEST); this.getRepository().getDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE);
Assert.assertNull(targetProvider); Assert.assertNull(targetProvider);
} }

@ -21,19 +21,26 @@ 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.Application;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; 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.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.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; import java.util.List;
public class TestDeviceManagementService implements DeviceManagementService { public class TestDeviceManagementService implements DeviceManagementService {
public static final String DEVICE_TYPE_TEST = "Test"; private String providerType;
public TestDeviceManagementService(String deviceType){
providerType = deviceType;
}
@Override @Override
public String getType() { public String getType() {
return TestDeviceManagementService.DEVICE_TYPE_TEST; return providerType;
} }
@Override @Override
@ -43,7 +50,7 @@ public class TestDeviceManagementService implements DeviceManagementService {
@Override @Override
public DeviceManager getDeviceManager() { public DeviceManager getDeviceManager() {
return null; return new TestDeviceManager();
} }
@Override @Override
@ -52,27 +59,26 @@ public class TestDeviceManagementService implements DeviceManagementService {
} }
@Override @Override
public Application[] getApplications(String domain, int pageNumber, public Application[] getApplications(String domain, int pageNumber, int size)
int size) throws ApplicationManagementException { throws ApplicationManagementException {
return new Application[0]; return new Application[0];
} }
@Override @Override
public void updateApplicationStatus(DeviceIdentifier deviceId, Application application, public void updateApplicationStatus(DeviceIdentifier deviceId, Application application, String status)
String status) throws ApplicationManagementException { throws ApplicationManagementException {
} }
@Override @Override
public String getApplicationStatus(DeviceIdentifier deviceId, public String getApplicationStatus(DeviceIdentifier deviceId, Application application)
Application application) throws ApplicationManagementException { throws ApplicationManagementException {
return null; return null;
} }
@Override @Override
public void installApplication(Operation operation, public void installApplication(Operation operation, List<DeviceIdentifier> deviceIdentifiers)
List<DeviceIdentifier> deviceIdentifiers) throws ApplicationManagementException { throws ApplicationManagementException {
} }
} }

@ -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<Device> 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;
}
}

@ -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<Application> applications = new ArrayList<Application>();
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<Application>();
applications.add(application4);
applications.add(application3);
try {
appMgtProvider.updateApplicationListInstalledInDevice(deviceIdentifier, applications);
List<Application> 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);
}
}
}

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

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

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

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

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

@ -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(){
}
}

@ -18,7 +18,7 @@
--> -->
<DataSourceConfig> <DataSourceConfig>
<Url>jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1</Url> <Url>jdbc:h2:mem:cdm-test-db;DB_CLOSE_ON_EXIT=FALSE;MVCC=true</Url>
<DriverClassName>org.h2.Driver</DriverClassName> <DriverClassName>org.h2.Driver</DriverClassName>
<User>wso2carbon</User> <User>wso2carbon</User>
<Password>wso2carbon</Password> <Password>wso2carbon</Password>

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

@ -27,7 +27,6 @@ public class APIMapperContextListener implements LifecycleListener {
@Override @Override
public void lifecycleEvent(LifecycleEvent lifecycleEvent) { public void lifecycleEvent(LifecycleEvent lifecycleEvent) {
if (Lifecycle.AFTER_INIT_EVENT.equals(lifecycleEvent.getType())) { if (Lifecycle.AFTER_INIT_EVENT.equals(lifecycleEvent.getType())) {
System.out.println("Deployeddd");
} }
} }

Loading…
Cancel
Save