From e4fd0607e8a3c003bd52cb5f794bd94064634cef Mon Sep 17 00:00:00 2001 From: prabathabey Date: Wed, 22 Jul 2015 23:43:12 +0530 Subject: [PATCH] Removing some hack used to workaround issues that popped up while writing unit tests upon tenant-enabled code --- ...ApplicationManagerProviderServiceImpl.java | 53 +++++++------------ .../DeviceManagementProviderServiceImpl.java | 27 ++-------- .../mgt/core/util/DeviceManagerUtil.java | 1 - .../DeviceManagementProviderServiceTest.java | 1 - 4 files changed, 22 insertions(+), 60 deletions(-) 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 6dbf00e15bb..8a0fb9e5d97 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 @@ -180,10 +180,8 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem @Override public void updateApplicationListInstalledInDevice( DeviceIdentifier deviceIdentifier, List applications) throws ApplicationManagementException { - - int tenantId = getTenantId(); - try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceManagementDAOFactory.beginTransaction(); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); @@ -199,8 +197,8 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem List appsToAdd = new ArrayList(); List appIdsToRemove = new ArrayList(); - for(Application installedApp:installedAppList){ - if (!applications.contains(installedApp)){ + for (Application installedApp : installedAppList) { + if (!applications.contains(installedApp)) { if (log.isDebugEnabled()) { log.debug("Remove app Id:" + installedApp.getId()); } @@ -211,12 +209,12 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem Application installedApp; List applicationIds = new ArrayList<>(); - for(Application application:applications){ + for (Application application : applications) { if (!installedAppList.contains(application)) { installedApp = applicationDAO.getApplication(application.getApplicationIdentifier(), tenantId); - if (installedApp == null){ + if (installedApp == null) { appsToAdd.add(application); - }else{ + } else { applicationIds.add(installedApp.getId()); } } @@ -234,7 +232,7 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem if (log.isDebugEnabled()) { log.debug("num of remove app Ids:" + appIdsToRemove.size()); } - applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove,tenantId); + applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove, tenantId); DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException deviceDaoEx) { String errorMsg = "Error occurred saving application list to the device"; @@ -242,37 +240,24 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem try { DeviceManagementDAOFactory.rollbackTransaction(); } catch (DeviceManagementDAOException e) { - log.error("Error occurred while roll back transaction",e); + log.error("Error occurred while roll back transaction", e); } throw new ApplicationManagementException(errorMsg, deviceDaoEx); } } - 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 = getTenantId(); - device = deviceDAO.getDevice(deviceIdentifier, tenantId); - return applicationDAO.getInstalledApplications(device.getId()); - }catch (DeviceManagementDAOException deviceDaoEx) { - String errorMsg = "Error occured while fetching the Application List of device : " + device.getId(); - log.error(errorMsg, deviceDaoEx); - throw new ApplicationManagementException(errorMsg, deviceDaoEx); - } + public List getApplicationListForDevice( + DeviceIdentifier deviceId) throws ApplicationManagementException { + Device device = null; + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + device = deviceDAO.getDevice(deviceId, tenantId); + return applicationDAO.getInstalledApplications(device.getId()); + } catch (DeviceManagementDAOException e) { + throw new ApplicationManagementException("Error occured while fetching the Application List of '" + + deviceId.getType() + "' device carrying the identifier'" + deviceId.getId(), e); + } } @Override 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 0538923cdfd..ce6713678ef 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -53,7 +53,6 @@ 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; @@ -75,7 +74,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementProviderServiceImpl(DeviceManagementPluginRepository pluginRepo, boolean test){ this.pluginRepository = pluginRepo; initDataAccessObjects(); - isTest = test; } private void initDataAccessObjects() { @@ -108,7 +106,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } else { device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE); } - int tenantId = getTenantId(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceManagementDAOFactory.beginTransaction(); @@ -724,14 +722,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.getConnection(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); allDevices = deviceDAO.getDevicesByStatus(status, tenantId); - } catch (DeviceManagementDAOException e) { - String errorMsg = "Error occurred while fetching the list of devices that matches to status: '" - + status + "'"; - log.error(errorMsg, e); - throw new DeviceManagementException(errorMsg, e); + throw new DeviceManagementException( + "Error occurred while fetching the list of devices that matches to status: '" + status + "'", e); } finally { - try { DeviceManagementDAOFactory.closeConnection(); } catch (DeviceManagementDAOException e) { @@ -753,19 +747,4 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return devices; } - - - private 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 8fc16db4ec9..dc895d8581a 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,7 +49,6 @@ 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 diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java index 8322684d07c..e79930ee04f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java @@ -58,7 +58,6 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes 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);