Adding unit test coverage for devicemgt-core DeviceManagement module

4.x.x
Ace 7 years ago
parent 3e9f62824e
commit e472810955

@ -50,22 +50,22 @@ public class TestDeviceManager implements DeviceManager {
@Override @Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException { public boolean modifyEnrollment(Device device) throws DeviceManagementException {
return false; return true;
} }
@Override @Override
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
return false; return true;
} }
@Override @Override
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
return false; return true;
} }
@Override @Override
public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException {
return false; return true;
} }
@Override @Override

@ -52,6 +52,16 @@ public class TestDataHolder {
return device; return device;
} }
public static Device generateDummyDeviceData(String deviceIdentifier, String deviceType,
EnrolmentInfo enrolmentInfo) {
Device device = new Device();
device.setEnrolmentInfo(enrolmentInfo);
device.setDescription("Test Description");
device.setDeviceIdentifier(deviceIdentifier);
device.setType(deviceType);
return device;
}
public static List<Device> generateDummyDeviceData(List<DeviceIdentifier> deviceIds) { public static List<Device> generateDummyDeviceData(List<DeviceIdentifier> deviceIds) {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
for (DeviceIdentifier deviceId : deviceIds) { for (DeviceIdentifier deviceId : deviceIds) {

@ -18,64 +18,163 @@ package org.wso2.carbon.device.mgt.core.service;
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.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.FeatureManager; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.core.TestDeviceManagementService;
import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl;
import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; 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.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
import org.wso2.carbon.registry.core.config.RegistryContext;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.internal.RegistryDataHolder;
import org.wso2.carbon.registry.core.jdbc.realm.InMemoryRealmService;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
public class DeviceManagementProviderServiceTest { import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Date;
public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest {
private static final Log log = LogFactory.getLog(DeviceManagementProviderServiceTest.class); private static final Log log = LogFactory.getLog(DeviceManagementProviderServiceTest.class);
private DeviceManagementProviderService providerService; private DeviceManagementProviderService providerService;
private static final String DEVICE_TYPE = "RANDOM_DEVICE_TYPE";
private static final String NON_EXISTENT_DEVICE_TYPE = "Test"; DeviceManagementProviderService deviceMgtService;
private Method setProvisioningConfig;
private Method setOperationMonitoringConfig;
private Method setDeviceStatusTaskPluginConfig;
@BeforeClass @BeforeClass
public void init() throws Exception { public void init() throws Exception {
this.providerService = new DeviceManagementProviderServiceImpl(); DeviceConfigurationManager.getInstance().initConfig();
log.info("Initializing");
deviceMgtService = new DeviceManagementProviderServiceImpl();
DeviceManagementServiceComponent.notifyStartupListeners();
DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService);
DeviceManagementDataHolder.getInstance().setRegistryService(getRegistryService());
DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl());
DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(new GroupManagementProviderServiceImpl());
DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null);
deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE,
MultitenantConstants.SUPER_TENANT_DOMAIN_NAME));
}
private RegistryService getRegistryService() throws RegistryException {
RealmService realmService = new InMemoryRealmService();
RegistryDataHolder.getInstance().setRealmService(realmService);
DeviceManagementDataHolder.getInstance().setRealmService(realmService);
InputStream is = this.getClass().getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml");
RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
context.setSetup(true);
return context.getEmbeddedRegistryService();
}
@Test
public void testNullDeviceEnrollment() {
try {
boolean enrollmentStatus = deviceMgtService.enrollDevice(null);
} catch (DeviceManagementException e) {
Assert.assertTrue(true);
}
} }
// @Test @Test
// public void testEnrollment() { public void testSuccessfullDeviceEnrollment() {
// try { Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE);
// DeviceManagementPluginRepository deviceManagementPluginRepository = new DeviceManagementPluginRepository(); try {
// TestDeviceManagementService testDeviceManagementService = boolean enrollmentStatus = deviceMgtService.enrollDevice(device);
// new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); Assert.assertTrue(enrollmentStatus);
// deviceManagementPluginRepository.addDeviceManagementProvider(testDeviceManagementService); } catch (DeviceManagementException e) {
// String msg = "Error Occured while enrolling device";
// deviceManagementProviderService = new DeviceManagementProviderServiceImpl(); Assert.fail(msg, e);
// DeviceManagerUtil.registerDeviceType(TestDataHolder.TEST_DEVICE_TYPE); }
// }
// 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 {
// DeviceManagementDAOFactory.closeConnection();
// }
// }
@Test @Test
public void testGetFeatureManager() { public void testNonExistentDeviceType() {
Device device = TestDataHolder.generateDummyDeviceData("abc");
try {
boolean enrollmentStatus = deviceMgtService.enrollDevice(device);
Assert.assertFalse(enrollmentStatus);
} catch (DeviceManagementException e) {
String msg = "Error Occured while enrolling device";
Assert.fail(msg, e);
}
}
@Test(dependsOnMethods = {"testSuccessfullDeviceEnrollment"})
public void testReEnrollmentofSameDeviceUnderSameUser() {
Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE);
try {
boolean enrollment = deviceMgtService.enrollDevice(device);
Assert.assertTrue(enrollment);
} catch (DeviceManagementException e) {
String msg = "Error Occured while enrolling device";
Assert.fail(msg, e);
}
}
@Test(dependsOnMethods = {"testReEnrollmentofSameDeviceUnderSameUser"})
public void testReEnrollmentofSameDeviceWithOtherUser() {
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setDateOfEnrolment(new Date().getTime());
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
enrolmentInfo.setOwner("user1");
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
enrolmentInfo.setStatus(EnrolmentInfo.Status.CREATED);
Device alternateDevice = TestDataHolder.generateDummyDeviceData("12345", DEVICE_TYPE,
enrolmentInfo);
try { try {
FeatureManager featureManager = providerService.getFeatureManager(NON_EXISTENT_DEVICE_TYPE); Device retrievedDevice1 = deviceMgtService.getDevice(new DeviceIdentifier("12345", DEVICE_TYPE));
Assert.assertNull(featureManager, "Feature manager retrieved is null, which is expected as the " +
"input device type provided is non existent"); deviceMgtService.enrollDevice(alternateDevice);
Device retrievedDevice2 = deviceMgtService.getDevice(new DeviceIdentifier(alternateDevice
.getDeviceIdentifier(), alternateDevice.getType()));
log.info(retrievedDevice1.getEnrolmentInfo().getOwner());
log.info(retrievedDevice2.getEnrolmentInfo().getOwner());
Assert.assertFalse(retrievedDevice1.getEnrolmentInfo().getOwner().equalsIgnoreCase
(retrievedDevice2.getEnrolmentInfo().getOwner()));
} catch (DeviceManagementException e) {
String msg = "Error Occured while enrolling device";
Assert.fail(msg, e);
}
}
@Test(dependsOnMethods = {"testReEnrollmentofSameDeviceWithOtherUser"})
public void testDisenrollment() {
Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE);
try {
boolean disenrollmentStatus = deviceMgtService.disenrollDevice(new DeviceIdentifier
(device
.getDeviceIdentifier(),
device.getType()));
log.info(disenrollmentStatus);
Assert.assertTrue(disenrollmentStatus);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving feature manager associated with device type '" + String msg = "Error Occured while enrolling device";
NON_EXISTENT_DEVICE_TYPE + "'";
log.error(msg, e);
Assert.fail(msg, e); Assert.fail(msg, e);
} }
} }

@ -76,7 +76,7 @@
device caching for upto configured expiry-time in seconds. In clustered setup all worker nodes can enable the device caching for upto configured expiry-time in seconds. In clustered setup all worker nodes can enable the
device-cache to improve performance. --> device-cache to improve performance. -->
<DeviceCacheConfiguration> <DeviceCacheConfiguration>
<Enable>true</Enable> <Enable>false</Enable>
<ExpiryTime>600</ExpiryTime> <ExpiryTime>600</ExpiryTime>
<!--This configuration specifies the number of cache entries in device cache. default capacity is 10000 entries. <!--This configuration specifies the number of cache entries in device cache. default capacity is 10000 entries.
This can be configured to higher number if cache eviction happens due to large number of devices in the This can be configured to higher number if cache eviction happens due to large number of devices in the
@ -84,7 +84,7 @@
<Capacity>10000</Capacity> <Capacity>10000</Capacity>
</DeviceCacheConfiguration> </DeviceCacheConfiguration>
<CertificateCacheConfiguration> <CertificateCacheConfiguration>
<Enable>true</Enable> <Enable>false</Enable>
<ExpiryTime>86400</ExpiryTime> <ExpiryTime>86400</ExpiryTime>
</CertificateCacheConfiguration> </CertificateCacheConfiguration>
<GeoLocationConfiguration> <GeoLocationConfiguration>

Loading…
Cancel
Save