Unit tests, application update

revert-70aa11f8
manoj 10 years ago
parent 6daa71771c
commit de17217303

@ -43,10 +43,7 @@ public class DeviceManagementPluginRepository {
throw new DeviceManagementException("Error occurred while adding device management provider '" + throw new DeviceManagementException("Error occurred while adding device management provider '" +
deviceType + "'"); deviceType + "'");
} }
providers.put(deviceType, provider); providers.put(deviceType, provider);
System.out.println("@plugin Repo:"+deviceType);
System.out.println("@plugin Repo:"+providers.size());
} }
public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException {
@ -55,7 +52,6 @@ public class DeviceManagementPluginRepository {
} }
public DeviceManagementService getDeviceManagementService(String type) { public DeviceManagementService getDeviceManagementService(String type) {
System.out.println("@plugin get:"+providers.get(type).toString());
return providers.get(type); return providers.get(type);
} }

@ -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.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;
@ -62,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";
@ -88,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 {
@ -171,7 +181,7 @@ 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);
@ -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 @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) {

@ -101,8 +101,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManager dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager();
System.out.println("dms________"+device.getType());
boolean status = dms.enrollDevice(device); boolean status = dms.enrollDevice(device);
try { try {
if (dms.isClaimable(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()))) { if (dms.isClaimable(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()))) {

@ -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.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.common.TestDataHolder;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import java.util.List; import java.util.List;
public class TestDeviceManagementService implements DeviceManager { public class TestDeviceManagementService implements DeviceManagementService {
private String providerType; private String providerType;
public TestDeviceManagementService(String deviceType){ public TestDeviceManagementService(String deviceType){
providerType = deviceType; providerType = deviceType;
} }
@Override
@Override public FeatureManager getFeatureManager() { public String getType() {
return null; return providerType;
} }
@Override public boolean enrollDevice(Device device) throws DeviceManagementException { @Override
return false; public void init() throws DeviceManagementException {
}
@Override public boolean modifyEnrollment(Device device) throws DeviceManagementException {
return false;
} }
@Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { @Override
return false; public DeviceManager getDeviceManager() {
return new TestDeviceManager();
} }
@Override public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { @Override
return false; public ApplicationManager getApplicationManager() {
return null;
} }
@Override public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { @Override
return false; 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 { @Override
return false; public void updateApplicationStatus(DeviceIdentifier deviceId, Application application, String status)
} throws ApplicationManagementException {
@Override public List<Device> 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; return null;
} }
@Override public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) @Override
throws DeviceManagementException { public void installApplication(Operation operation, List<DeviceIdentifier> deviceIdentifiers)
return false; throws ApplicationManagementException {
}
@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;
} }
} }

@ -101,6 +101,8 @@ public abstract class BaseDeviceManagementTest {
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 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 { private void cleanupEnrolmentData(Connection conn) throws SQLException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {

@ -14,7 +14,7 @@ public class TestDataHolder {
public static DeviceType initialTestDeviceType; public static DeviceType initialTestDeviceType;
public static String TEST_DEVICE_TYPE = "Test"; public static String TEST_DEVICE_TYPE = "Test";
public static Integer SUPER_TENANT_ID = -1234; public static Integer SUPER_TENANT_ID = -1234;
public static ThreadLocal<Integer> tenant = new ThreadLocal<Integer>(); public static String initialDeviceIdentifier = "12345";
public static Device generateDummyDeviceData(String deviceType){ public static Device generateDummyDeviceData(String deviceType){
@ -30,7 +30,6 @@ public class TestDataHolder {
device.setDeviceIdentifier("12345"); device.setDeviceIdentifier("12345");
device.setType(deviceType); device.setType(deviceType);
return device; return device;
} }
public static DeviceType generateDeviceTypeData(String devTypeName){ public static DeviceType generateDeviceTypeData(String devTypeName){

@ -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<Application> sourceApplications = new ArrayList<Application>();
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");*/
}
}

@ -26,16 +26,13 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
DeviceManagementProviderService deviceManagementProviderService = null; DeviceManagementProviderService deviceManagementProviderService = null;
@BeforeClass @BeforeClass
@Override @Override
public void init() throws Exception { public void init() throws Exception {
initDatSource(); initDatSource();
} }
@Test
@Test
public void testEnrollment() { public void testEnrollment() {
try { try {
@ -46,10 +43,12 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
deviceManagementProviderService = new DeviceManagementProviderServiceImpl(deviceManagementPluginRepository, deviceManagementProviderService = new DeviceManagementProviderServiceImpl(deviceManagementPluginRepository,
true); true);
DeviceManagerUtil.registerDeviceType(TestDataHolder.TEST_DEVICE_TYPE); 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); Device device = TestDataHolder.generateDummyDeviceData(TestDataHolder.TEST_DEVICE_TYPE);
boolean isEnrolled = deviceManagementProviderService.enrollDevice(device); boolean isEnrolled = deviceManagementProviderService.enrollDevice(device);
Assert.assertEquals(isEnrolled,true,"Enrolment fail");
if (isEnrolled){ if (isEnrolled){
TestDataHolder.initialTestDevice = device; TestDataHolder.initialTestDevice = device;
} }
@ -69,6 +68,5 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
@AfterClass @AfterClass
public void cleanResources(){ public void cleanResources(){
// PrivilegedCarbonContext.endTenantFlow();
} }
} }

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

@ -34,6 +34,7 @@
<test name="Service Unit Tests" preserve-order="true"> <test name="Service Unit Tests" preserve-order="true">
<classes> <classes>
<class name="org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceTest"/> <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