From de1721730385750c74f5fb59d3ec85a9c7e227de Mon Sep 17 00:00:00 2001 From: manoj Date: Wed, 15 Jul 2015 07:37:20 +0530 Subject: [PATCH] Unit tests, application update --- .../DeviceManagementPluginRepository.java | 4 -- ...ApplicationManagerProviderServiceImpl.java | 26 +++++++- .../DeviceManagementProviderServiceImpl.java | 2 - .../mgt/core/TestDeviceManagementService.java | 66 ++++++++----------- .../core/common/BaseDeviceManagementTest.java | 27 ++++++++ .../mgt/core/common/TestDataHolder.java | 3 +- ...icationManagementProviderServiceTests.java | 60 ----------------- .../DeviceManagementProviderServiceTest.java | 10 ++- .../config/datasource/data-source-config.xml | 2 +- .../src/test/resources/testng.xml | 1 + .../framework/APIMapperContextListener.java | 1 - 11 files changed, 87 insertions(+), 115 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/ApplicationManagementProviderServiceTests.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java index c52561e7927..4c276405ffe 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java @@ -43,10 +43,7 @@ public class DeviceManagementPluginRepository { throw new DeviceManagementException("Error occurred while adding device management provider '" + deviceType + "'"); } - providers.put(deviceType, provider); - System.out.println("@plugin Repo:"+deviceType); - System.out.println("@plugin Repo:"+providers.size()); } public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { @@ -55,7 +52,6 @@ public class DeviceManagementPluginRepository { } public DeviceManagementService getDeviceManagementService(String type) { - System.out.println("@plugin get:"+providers.get(type).toString()); return providers.get(type); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index 0afadab9af8..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,6 +41,7 @@ import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.identity.IdentityConfigurations; import org.wso2.carbon.device.mgt.core.dao.*; import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceException; import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceStub; import org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO; @@ -62,6 +63,7 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem private DeviceDAO deviceDAO; private ApplicationDAO applicationDAO; private ApplicationMappingDAO applicationMappingDAO; + private boolean isTest; private static final String GET_APP_LIST_URL = "store/apis/assets/mobileapp?domain=carbon.super&page=1"; @@ -88,6 +90,14 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem this.applicationMappingDAO = DeviceManagementDAOFactory.getApplicationMappingDAO(); } + ApplicationManagerProviderServiceImpl(DeviceManagementPluginRepository pluginRepository, boolean testMode) { + this.pluginRepository = pluginRepository; + this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + this.applicationDAO = DeviceManagementDAOFactory.getApplicationDAO(); + this.applicationMappingDAO = DeviceManagementDAOFactory.getApplicationMappingDAO(); + isTest = testMode; + } + @Override public Application[] getApplications(String domain, int pageNumber, int size) throws ApplicationManagementException { @@ -171,7 +181,7 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem public void updateApplicationListInstalledInDevice( DeviceIdentifier deviceIdentifier, List applications) throws ApplicationManagementException { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = getTenantId(); try { Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); @@ -205,12 +215,24 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem } } + private int getTenantId() { + + int tenantId = 0; + if (isTest){ + tenantId = DeviceManagerUtil.currentTenant.get(); + }else{ + tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + } + + return tenantId; + } + @Override public List getApplicationListForDevice(DeviceIdentifier deviceIdentifier) throws ApplicationManagementException { Device device = null; try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = getTenantId(); device = deviceDAO.getDevice(deviceIdentifier, tenantId); return applicationDAO.getInstalledApplications(device.getId()); }catch (DeviceManagementDAOException deviceDaoEx) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 807c76b6496..5ccb5c64107 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -101,8 +101,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManager dms = this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); - - System.out.println("dms________"+device.getType()); boolean status = dms.enrollDevice(device); try { if (dms.isClaimable(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()))) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java index ffd3061ed1c..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,72 +21,64 @@ import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; +import org.wso2.carbon.device.mgt.common.license.mgt.License; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import java.util.List; -public class TestDeviceManagementService implements DeviceManager { +public class TestDeviceManagementService implements DeviceManagementService { private String providerType; public TestDeviceManagementService(String deviceType){ providerType = deviceType; } - - @Override public FeatureManager getFeatureManager() { - return null; + @Override + public String getType() { + return providerType; } - @Override public boolean enrollDevice(Device device) throws DeviceManagementException { - return false; - } + @Override + public void init() throws DeviceManagementException { - @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { - return false; } - @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - return false; + @Override + public DeviceManager getDeviceManager() { + return new TestDeviceManager(); } - @Override public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { - return false; + @Override + public ApplicationManager getApplicationManager() { + return null; } - @Override public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { - return false; + @Override + public Application[] getApplications(String domain, int pageNumber, int size) + throws ApplicationManagementException { + return new Application[0]; } - @Override public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException { - return false; - } + @Override + public void updateApplicationStatus(DeviceIdentifier deviceId, Application application, String status) + throws ApplicationManagementException { - @Override public List getAllDevices() throws DeviceManagementException { - return null; } - @Override public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + @Override + public String getApplicationStatus(DeviceIdentifier deviceId, Application application) + throws ApplicationManagementException { return null; } - @Override public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) - throws DeviceManagementException { - return false; - } - - @Override public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) - throws DeviceManagementException { - return false; - } - - @Override public boolean isClaimable(DeviceIdentifier deviceId) throws DeviceManagementException { - return false; - } + @Override + public void installApplication(Operation operation, List deviceIdentifiers) + throws ApplicationManagementException { - @Override public boolean setStatus(DeviceIdentifier deviceId, String currentOwner, EnrolmentInfo.Status status) - throws DeviceManagementException { - return false; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java index e57b888f2b7..53546ce5b26 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java @@ -101,6 +101,8 @@ public abstract class BaseDeviceManagementTest { conn.setAutoCommit(false); this.cleanupEnrolmentData(conn); + this.cleanApplicationMappingData(conn); + this.cleanApplicationData(conn); this.cleanupDeviceData(conn); this.cleanupDeviceTypeData(conn); @@ -127,6 +129,31 @@ public abstract class BaseDeviceManagementTest { } } + private void cleanApplicationMappingData(Connection conn) throws SQLException{ + PreparedStatement stmt = null; + try { + stmt = conn.prepareStatement("DELETE FROM DM_DEVICE_APPLICATION_MAPPING"); + stmt.execute(); + } finally { + if (stmt != null) { + stmt.close(); + } + } + } + + private void cleanApplicationData(Connection conn) throws SQLException{ + PreparedStatement stmt = null; + try { + stmt = conn.prepareStatement("DELETE FROM DM_APPLICATION"); + stmt.execute(); + } finally { + if (stmt != null) { + stmt.close(); + } + } + } + + private void cleanupEnrolmentData(Connection conn) throws SQLException { PreparedStatement stmt = null; try { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java index 1c110a16bf9..2defb785e8d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java @@ -14,7 +14,7 @@ public class TestDataHolder { public static DeviceType initialTestDeviceType; public static String TEST_DEVICE_TYPE = "Test"; public static Integer SUPER_TENANT_ID = -1234; - public static ThreadLocal tenant = new ThreadLocal(); + public static String initialDeviceIdentifier = "12345"; public static Device generateDummyDeviceData(String deviceType){ @@ -30,7 +30,6 @@ public class TestDataHolder { device.setDeviceIdentifier("12345"); device.setType(deviceType); return device; - } public static DeviceType generateDeviceTypeData(String devTypeName){ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/ApplicationManagementProviderServiceTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/ApplicationManagementProviderServiceTests.java deleted file mode 100644 index dbd71eb7f0c..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/ApplicationManagementProviderServiceTests.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.wso2.carbon.device.mgt.core.service; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; -import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; - -public class ApplicationManagementProviderServiceTests extends BaseDeviceManagementTest{ - - private static final Log log = LogFactory.getLog(ApplicationManagementProviderServiceTests.class); - - @BeforeClass - @Override - public void init() throws Exception { - this.initDatSource(); - } - - @Test - public void testAddApplications() { - -/* ArrayList sourceApplications = new ArrayList(); - sourceApplications.add(TestDataHolder.generateApplicationDummyData("Test App2")); - sourceApplications.add(TestDataHolder.generateApplicationDummyData("Test App3")); - - DeviceManagementTests deviceManagementDAOTests = new DeviceManagementTests(); - deviceManagementDAOTests.testAddDeviceTest(); - - Device device = TestDataHolder.initialTestDevice; - - try { - DeviceManagementDAOFactory.openConnection(); - - - } catch (DeviceManagementDAOException e) { - log.error("Error occurred while adding application", e); - } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } - } - *//* Retrieving the application by its name *//* - Application target = null; - try { - target = this.getApplication(source.getApplicationIdentifier(), -1234); - } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while retrieving application info"; - log.error(msg, e); - Assert.fail(msg, e); - } - - Assert.assertEquals(target.getApplicationIdentifier(), source.getApplicationIdentifier(), "Application added is not as same as " + - "what's " + - "retrieved");*/ - } - - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java index dcc282c1b20..41780b7f5f2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java @@ -26,16 +26,13 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes DeviceManagementProviderService deviceManagementProviderService = null; - @BeforeClass @Override public void init() throws Exception { initDatSource(); } - - - @Test + @Test public void testEnrollment() { try { @@ -46,10 +43,12 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes deviceManagementProviderService = new DeviceManagementProviderServiceImpl(deviceManagementPluginRepository, true); DeviceManagerUtil.registerDeviceType(TestDataHolder.TEST_DEVICE_TYPE); - TestDataHolder.tenant.set(TestDataHolder.SUPER_TENANT_ID); + DeviceManagerUtil.currentTenant.set(TestDataHolder.SUPER_TENANT_ID); + Device device = TestDataHolder.generateDummyDeviceData(TestDataHolder.TEST_DEVICE_TYPE); boolean isEnrolled = deviceManagementProviderService.enrollDevice(device); + Assert.assertEquals(isEnrolled,true,"Enrolment fail"); if (isEnrolled){ TestDataHolder.initialTestDevice = device; } @@ -69,6 +68,5 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes @AfterClass public void cleanResources(){ - // PrivilegedCarbonContext.endTenantFlow(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/data-source-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/data-source-config.xml index 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 935fde5089b..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 @@ -34,6 +34,7 @@ + \ No newline at end of file diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/APIMapperContextListener.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/APIMapperContextListener.java index 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"); } }