forked from community/device-mgt-core
Merge pull request #994 from madawas/unit-tests
Unit tests for DeviceTaskManagerServicerevert-70aa11f8
commit
0f4e941586
@ -0,0 +1,124 @@
|
||||
package org.wso2.carbon.device.mgt.core.task;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.opensaml.xml.signature.P;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.quartz.impl.StdSchedulerFactory;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.Test;
|
||||
import org.wso2.carbon.device.mgt.common.MonitoringOperation;
|
||||
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
|
||||
import org.wso2.carbon.device.mgt.core.TestTaskServiceImpl;
|
||||
import org.wso2.carbon.device.mgt.core.TestUtils;
|
||||
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.permission.mgt.PermissionUtils;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
|
||||
import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerServiceImpl;
|
||||
import org.wso2.carbon.ntask.common.TaskException;
|
||||
import org.wso2.carbon.ntask.core.TaskInfo;
|
||||
import org.wso2.carbon.ntask.core.TaskManager;
|
||||
import org.wso2.carbon.ntask.core.TaskUtils;
|
||||
import org.wso2.carbon.ntask.core.impl.QuartzCachedThreadPool;
|
||||
import org.wso2.carbon.ntask.core.internal.TasksDSComponent;
|
||||
import org.wso2.carbon.ntask.core.service.TaskService;
|
||||
import org.wso2.carbon.ntask.core.service.impl.TaskServiceImpl;
|
||||
import org.wso2.carbon.registry.core.exceptions.RegistryException;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
public class DeviceTaskManagerServiceTest {
|
||||
private static final Log log = LogFactory.getLog(DeviceTaskManagerService.class);
|
||||
private static final String TASK_TYPE = "DEVICE_MONITORING";
|
||||
private DeviceTaskManagerService deviceTaskManagerService;
|
||||
@Mock private TaskService taskService;
|
||||
|
||||
@BeforeClass public void init() throws Exception {
|
||||
DeviceConfigurationManager.getInstance().initConfig();
|
||||
log.info("Initializing Device Task Manager Service Test Suite");
|
||||
this.taskService = new TestTaskServiceImpl();
|
||||
DeviceManagementDataHolder.getInstance().setTaskService(this.taskService);
|
||||
this.deviceTaskManagerService = new DeviceTaskManagerServiceImpl();
|
||||
DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(this.deviceTaskManagerService);
|
||||
Field taskServiceField = TasksDSComponent.class.getDeclaredField("taskService");
|
||||
taskServiceField.setAccessible(true);
|
||||
taskServiceField.set(null, Mockito.mock(TaskServiceImpl.class, Mockito.RETURNS_MOCKS));
|
||||
}
|
||||
|
||||
@Test(groups = "Device Task Manager")
|
||||
public void testStartTask() {
|
||||
try {
|
||||
log.debug("Attempting to start task from testStartTask");
|
||||
this.deviceTaskManagerService
|
||||
.startTask(TestDataHolder.TEST_DEVICE_TYPE, generateValidMonitoringTaskConfig("DEVICE_INFO"));
|
||||
TaskManager taskManager = this.taskService.getTaskManager(TASK_TYPE);
|
||||
Assert.assertEquals(this.taskService.getRegisteredTaskTypes().size(), 1);
|
||||
Assert.assertNotNull(taskManager
|
||||
.getTask(TestDataHolder.TEST_DEVICE_TYPE + String.valueOf(TestDataHolder.SUPER_TENANT_ID)));
|
||||
log.debug("Task Successfully started");
|
||||
} catch (DeviceMgtTaskException | TaskException e) {
|
||||
Assert.fail("Exception occurred when starting the task", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(groups = "Device Task Manager", dependsOnMethods = "testStartTask")
|
||||
public void testUpdateTask() {
|
||||
try {
|
||||
log.debug("Attempting to update task from testStartTask");
|
||||
this.deviceTaskManagerService
|
||||
.updateTask(TestDataHolder.TEST_DEVICE_TYPE, generateValidMonitoringTaskConfig("DEVICE_LOCATION"));
|
||||
Assert.assertEquals(this.taskService.getRegisteredTaskTypes().size(), 1);
|
||||
TaskManager taskManager = this.taskService.getTaskManager(TASK_TYPE);
|
||||
Assert.assertEquals(taskManager.getAllTasks().size(), 1);
|
||||
log.debug("Task Successfully updated");
|
||||
} catch (DeviceMgtTaskException | TaskException e) {
|
||||
Assert.fail("Exception occurred when updating the task", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(groups = "Device Task Manager", dependsOnMethods = "testUpdateTask")
|
||||
public void testStopTask() {
|
||||
log.debug("Attempting to stop task from testStopTask");
|
||||
try {
|
||||
this.deviceTaskManagerService
|
||||
.stopTask(TestDataHolder.TEST_DEVICE_TYPE, generateValidMonitoringTaskConfig("DEVICE_LOCATION"));
|
||||
TaskManager taskManager = this.taskService.getTaskManager(TASK_TYPE);
|
||||
Assert.assertEquals(taskManager.getAllTasks().size(), 0);
|
||||
} catch (DeviceMgtTaskException | TaskException e) {
|
||||
Assert.fail("Exception occurred when stopping the task", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private OperationMonitoringTaskConfig generateValidMonitoringTaskConfig(String operationConfig) {
|
||||
OperationMonitoringTaskConfig validTaskConfig = new OperationMonitoringTaskConfig();
|
||||
List<MonitoringOperation> operationList = new ArrayList<>();
|
||||
MonitoringOperation operation = new MonitoringOperation();
|
||||
operation.setTaskName(operationConfig);
|
||||
operation.setRecurrentTimes(1);
|
||||
operationList.add(operation);
|
||||
|
||||
validTaskConfig.setEnabled(true);
|
||||
validTaskConfig.setFrequency(60000);
|
||||
validTaskConfig.setMonitoringOperation(operationList);
|
||||
|
||||
return validTaskConfig;
|
||||
}
|
||||
|
||||
private Properties getStandardQuartzProps() {
|
||||
Properties result = new Properties();
|
||||
result.put("org.quartz.scheduler.skipUpdateCheck", "true");
|
||||
result.put("org.quartz.threadPool.class", QuartzCachedThreadPool.class.getName());
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package org.wso2.carbon.device.mgt.core.task;
|
||||
|
||||
import org.wso2.carbon.ntask.common.TaskException;
|
||||
import org.wso2.carbon.ntask.core.TaskInfo;
|
||||
import org.wso2.carbon.ntask.core.TaskManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TestTaskManagerImpl implements TaskManager {
|
||||
private List<TaskInfo> registeredTasks;
|
||||
|
||||
public TestTaskManagerImpl() {
|
||||
this.registeredTasks = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initStartupTasks() throws TaskException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scheduleTask(String taskName) throws TaskException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rescheduleTask(String taskName) throws TaskException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteTask(String taskName) throws TaskException {
|
||||
for (TaskInfo task : this.registeredTasks) {
|
||||
if (task.getName().contains(taskName)) {
|
||||
this.registeredTasks.remove(task);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pauseTask(String taskName) throws TaskException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resumeTask(String taskName) throws TaskException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerTask(TaskInfo taskInfo) throws TaskException {
|
||||
this.registeredTasks.add(taskInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TaskState getTaskState(String taskName) throws TaskException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TaskInfo getTask(String taskName) throws TaskException {
|
||||
for (TaskInfo task : this.registeredTasks) {
|
||||
if (task.getName().contains(taskName)) {
|
||||
return task;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TaskInfo> getAllTasks() throws TaskException {
|
||||
return this.registeredTasks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTaskScheduled(String taskName) throws TaskException {
|
||||
return this.registeredTasks.size() > 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
<tasks-configuration xmlns:svns="http://org.wso2.securevault/configuration">
|
||||
|
||||
<!--
|
||||
The currently running server mode; possible values are:-
|
||||
STANDALONE, CLUSTERED, REMOTE, AUTO.
|
||||
In AUTO mode, the server startup checks whether clustering is enabled in the system,
|
||||
if so, CLUSTERED mode will be used, or else, the the server mode will be STANDALONE.
|
||||
-->
|
||||
<taskServerMode>STANDALONE</taskServerMode>
|
||||
|
||||
<!--
|
||||
To be used in CLUSTERED mode to notify how many servers are there in
|
||||
the task server cluster, the servers wait till this amount of servers
|
||||
are activated before the tasks are scheduled -->
|
||||
<taskServerCount>2</taskServerCount>
|
||||
|
||||
<!-- The default location resolver configuration -->
|
||||
<defaultLocationResolver>
|
||||
<locationResolverClass>org.wso2.carbon.ntask.core.impl.RoundRobinTaskLocationResolver</locationResolverClass>
|
||||
</defaultLocationResolver>
|
||||
|
||||
<!--
|
||||
if task-type-pattern matches and task-name-pattern matches, check existing addresses of address-pattern,
|
||||
and if addresses exist, select address in round-robin fashion, if not move onto next rule in sequence.
|
||||
<property name="rule-[order]">[task-type-pattern],[task-name-pattern],[address-pattern]</property>
|
||||
-->
|
||||
<!--defaultLocationResolver>
|
||||
<locationResolverClass>org.wso2.carbon.ntask.core.impl.RuleBasedLocationResolver</locationResolverClass>
|
||||
<properties>
|
||||
<property name="rule-1">HIVE_TASK,.*,192.168.2.*</property>
|
||||
<property name="rule-5">.*,.*,.*</property>
|
||||
</properties>
|
||||
</defaultLocationResolver-->
|
||||
|
||||
<!-- The address to which the remote task server should dispatch the trigger messages to,
|
||||
usually this would be an endpoint to a load balancer -->
|
||||
<taskClientDispatchAddress>https://localhost:9448</taskClientDispatchAddress>
|
||||
|
||||
<!-- The address of the remote task server -->
|
||||
<remoteServerAddress>https://localhost:9443</remoteServerAddress>
|
||||
|
||||
<!-- The username to authenticate to the remote task server -->
|
||||
<remoteServerUsername>admin</remoteServerUsername>
|
||||
|
||||
<!-- The password to authenticate to the remote task server -->
|
||||
<remoteServerPassword>admin</remoteServerPassword>
|
||||
|
||||
<!-- Below contain a sample to be used when using with secure vault -->
|
||||
<!--remoteServerPassword svns:secretAlias="remote.task.server.password"></remoteServerPassword-->
|
||||
|
||||
</tasks-configuration>
|
Loading…
Reference in new issue