Adding more tests for operation manager.

4.x.x
sinthuja 7 years ago
parent 3463d79ae1
commit cb92f6ef65

@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core.operation;
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.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;
@ -35,22 +36,21 @@ 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.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; 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.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.*;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagementServiceProvider;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.registry.core.config.RegistryContext; import org.wso2.carbon.registry.core.config.RegistryContext;
import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.internal.RegistryDataHolder; import org.wso2.carbon.registry.core.internal.RegistryDataHolder;
import org.wso2.carbon.registry.core.jdbc.InMemoryEmbeddedRegistryService;
import org.wso2.carbon.registry.core.jdbc.realm.InMemoryRealmService; import org.wso2.carbon.registry.core.jdbc.realm.InMemoryRealmService;
import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import java.io.InputStream; import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
public class OperationManagementTests extends BaseDeviceManagementTest { public class OperationManagementTests extends BaseDeviceManagementTest {
@ -59,9 +59,12 @@ public class OperationManagementTests extends BaseDeviceManagementTest {
private static final String DEVICE_TYPE = "OP_TEST_TYPE"; private static final String DEVICE_TYPE = "OP_TEST_TYPE";
private static final String DEVICE_ID_PREFIX = "OP-TEST-DEVICE-ID-"; private static final String DEVICE_ID_PREFIX = "OP-TEST-DEVICE-ID-";
private static final String COMMAND_OPERATON_CODE = "COMMAND-TEST"; private static final String COMMAND_OPERATON_CODE = "COMMAND-TEST";
private static final String POLICY_OPERATION_CODE = "POLICY-TEST";
private static final String CONFIG_OPERATION_CODE = "CONFIG-TEST";
private static final String PROFILE_OPERATION_CODE = "PROFILE-TEST";
private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
private List<DeviceIdentifier> deviceIds = new ArrayList<>(); private List<DeviceIdentifier> deviceIds = new ArrayList<>();
private DeviceManagementProviderService deviceMgtService;
private OperationManager operationMgtService; private OperationManager operationMgtService;
@BeforeClass @BeforeClass
@ -74,23 +77,22 @@ public class OperationManagementTests extends BaseDeviceManagementTest {
deviceIds.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); deviceIds.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE));
} }
List<Device> devices = TestDataHolder.generateDummyDeviceData(this.deviceIds); List<Device> devices = TestDataHolder.generateDummyDeviceData(this.deviceIds);
this.deviceMgtService = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService deviceMgtService = new DeviceManagementProviderServiceImpl();
DeviceManagementServiceComponent.notifyStartupListeners(); DeviceManagementServiceComponent.notifyStartupListeners();
DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService); DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService);
DeviceManagementDataHolder.getInstance().setRegistryService(getRegistryService()); DeviceManagementDataHolder.getInstance().setRegistryService(getRegistryService());
DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl()); DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl());
this.deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE,
MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); MultitenantConstants.SUPER_TENANT_DOMAIN_NAME));
for (Device device : devices) { for (Device device : devices) {
this.deviceMgtService.enrollDevice(device); deviceMgtService.enrollDevice(device);
} }
List<Device> returnedDevices = this.deviceMgtService.getAllDevices(DEVICE_TYPE); List<Device> returnedDevices = deviceMgtService.getAllDevices(DEVICE_TYPE);
for (Device device : returnedDevices) { for (Device device : returnedDevices) {
if (!device.getDeviceIdentifier().startsWith(DEVICE_ID_PREFIX)) { if (!device.getDeviceIdentifier().startsWith(DEVICE_ID_PREFIX)) {
throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!");
} }
} }
this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE); this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE);
} }
@ -105,9 +107,41 @@ public class OperationManagementTests extends BaseDeviceManagementTest {
@Test @Test
public void addCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { public void addCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException {
CommandOperation operation = new CommandOperation(); this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE),
operation.setType(Operation.Type.PROFILE); this.deviceIds);
operation.setCode(COMMAND_OPERATON_CODE); }
this.operationMgtService.addOperation(operation, this.deviceIds);
@Test(dependsOnMethods = "addCommandOperation")
public void addPolicyOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException {
this.operationMgtService.addOperation(getOperation(new PolicyOperation(), Operation.Type.POLICY, PROFILE_OPERATION_CODE),
this.deviceIds);
}
@Test(dependsOnMethods = "addPolicyOperation")
public void addConfigOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException {
this.operationMgtService.addOperation(getOperation(new ConfigOperation(), Operation.Type.CONFIG, CONFIG_OPERATION_CODE),
this.deviceIds);
}
@Test(dependsOnMethods = "addConfigOperation")
public void addProfileOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException {
this.operationMgtService.addOperation(getOperation(new ProfileOperation(), Operation.Type.PROFILE, PROFILE_OPERATION_CODE),
this.deviceIds);
}
private Operation getOperation(Operation operation, Operation.Type type, String code) {
String date = new SimpleDateFormat(DATE_FORMAT_NOW).format(new Date());
operation.setCreatedTimeStamp(date);
operation.setType(type);
operation.setCode(code);
return operation;
}
@Test(dependsOnMethods = "addProfileOperation")
public void verifyCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException {
for (DeviceIdentifier deviceIdentifier : deviceIds) {
List operations = this.operationMgtService.getPendingOperations(deviceIdentifier);
Assert.assertEquals(operations.size(), 4, "The pending operations should be 4, but found only " + operations.size());
}
} }
} }

Loading…
Cancel
Save