From c1430327104d7bf90e08934c5d8306fe46d5c629 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Thu, 28 Sep 2017 10:08:15 +0530 Subject: [PATCH] Unit tests for DeviceTaskManagerService --- .../org.wso2.carbon.device.mgt.core/pom.xml | 5 + .../device/mgt/core/TestTaskServiceImpl.java | 73 +++-------- .../carbon/device/mgt/core/TestUtils.java | 21 ++- .../task/DeviceTaskManagerServiceTest.java | 124 ++++++++++++++++++ .../mgt/core/task/TestTaskManagerImpl.java | 82 ++++++++++++ .../repository/conf/etc/tasks-config.xml | 51 +++++++ .../src/test/resources/testng.xml | 1 + 7 files changed, 297 insertions(+), 60 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerServiceTest.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/TestTaskManagerImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/carbon-home/repository/conf/etc/tasks-config.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index af555a7b9ca..04949389ac2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -163,6 +163,11 @@ org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.common + + org.powermock + powermock-api-mockito + test + org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.analytics.data.publisher diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestTaskServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestTaskServiceImpl.java index 7210942ca53..882cc266f6d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestTaskServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestTaskServiceImpl.java @@ -17,73 +17,30 @@ */ package org.wso2.carbon.device.mgt.core; +import org.wso2.carbon.device.mgt.core.task.TestTaskManagerImpl; 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.service.TaskService; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Set; public class TestTaskServiceImpl implements TaskService { - @Override - public TaskManager getTaskManager(String s) throws TaskException { - return new TaskManager() { - @Override - public void initStartupTasks() throws TaskException { - - } - - @Override - public void scheduleTask(String s) throws TaskException { - - } - - @Override - public void rescheduleTask(String s) throws TaskException { - - } - - @Override - public boolean deleteTask(String s) throws TaskException { - return false; - } - - @Override - public void pauseTask(String s) throws TaskException { - - } + private Set registeredTaskTypes; + private TaskManager taskManager; - @Override - public void resumeTask(String s) throws TaskException { + public TestTaskServiceImpl() { - } - - @Override - public void registerTask(TaskInfo taskInfo) throws TaskException { - - } - - @Override - public TaskState getTaskState(String s) throws TaskException { - return null; - } - - @Override - public TaskInfo getTask(String s) throws TaskException { - return null; - } - - @Override - public List getAllTasks() throws TaskException { - return null; - } + this.registeredTaskTypes = new HashSet<>(); + this.taskManager = new TestTaskManagerImpl(); + } - @Override - public boolean isTaskScheduled(String s) throws TaskException { - return false; - } - }; + @Override + public TaskManager getTaskManager(String s) throws TaskException { + return this.taskManager; } @Override @@ -93,12 +50,12 @@ public class TestTaskServiceImpl implements TaskService { @Override public void registerTaskType(String s) throws TaskException { - + this.registeredTaskTypes.add(s); } @Override public Set getRegisteredTaskTypes() { - return null; + return this.registeredTaskTypes; } @Override @@ -108,7 +65,7 @@ public class TestTaskServiceImpl implements TaskService { @Override public boolean isServerInit() { - return false; + return true; } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestUtils.java index 1a216461506..29aad743c83 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestUtils.java @@ -22,7 +22,15 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; - +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +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 java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; @@ -58,7 +66,6 @@ public class TestUtils { } } - public static DeviceGroup createDeviceGroup1(){ DeviceGroup group = new DeviceGroup(); group.setName("TEST_GROUP_01"); @@ -108,4 +115,14 @@ public class TestUtils { return list; } + + public static RegistryService getRegistryService(Class clazz) throws RegistryException { + RealmService realmService = new InMemoryRealmService(); + RegistryDataHolder.getInstance().setRealmService(realmService); + DeviceManagementDataHolder.getInstance().setRealmService(realmService); + InputStream is = clazz.getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml"); + RegistryContext context = RegistryContext.getBaseInstance(is, realmService); + context.setSetup(true); + return context.getEmbeddedRegistryService(); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerServiceTest.java new file mode 100644 index 00000000000..2db940a4ec9 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerServiceTest.java @@ -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 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; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/TestTaskManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/TestTaskManagerImpl.java new file mode 100644 index 00000000000..fcebed343c1 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/TestTaskManagerImpl.java @@ -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 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 getAllTasks() throws TaskException { + return this.registeredTasks; + } + + @Override + public boolean isTaskScheduled(String taskName) throws TaskException { + return this.registeredTasks.size() > 0; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/carbon-home/repository/conf/etc/tasks-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/carbon-home/repository/conf/etc/tasks-config.xml new file mode 100644 index 00000000000..0a9289da708 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/carbon-home/repository/conf/etc/tasks-config.xml @@ -0,0 +1,51 @@ + + + + STANDALONE + + + 2 + + + + org.wso2.carbon.ntask.core.impl.RoundRobinTaskLocationResolver + + + + + + + https://localhost:9448 + + + https://localhost:9443 + + + admin + + + admin + + + + + 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 7ab82eaf075..b77715ab91b 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 @@ -44,6 +44,7 @@ +