From 1b9f7977206d9faa08253b5a36664bbecbba9ce6 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Thu, 28 Sep 2017 19:16:42 +0530 Subject: [PATCH 1/9] Unit Tests for DeviceTaskManagerService --- .../device/mgt/core/TestTaskServiceImpl.java | 4 + .../mgt/core/common/TestDataHolder.java | 32 +++- .../task/DeviceTaskManagerServiceTest.java | 166 +++++++++++++----- .../mgt/core/task/TestTaskManagerImpl.java | 3 + 4 files changed, 157 insertions(+), 48 deletions(-) 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 882cc266f6..f44e65ee0d 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 @@ -38,6 +38,10 @@ public class TestTaskServiceImpl implements TaskService { this.taskManager = new TestTaskManagerImpl(); } + public void setTaskManager(TaskManager taskManager) { + this.taskManager = taskManager; + } + @Override public TaskManager getTaskManager(String s) throws TaskException { return this.taskManager; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java index a23fdbc638..f2f6805f77 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java @@ -18,11 +18,11 @@ package org.wso2.carbon.device.mgt.core.common; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; -import org.wso2.carbon.device.mgt.common.PaginationRequest; +import org.wso2.carbon.device.mgt.common.MonitoringOperation; +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; -import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import java.util.ArrayList; @@ -32,11 +32,12 @@ import java.util.Properties; public class TestDataHolder { - public final static String TEST_DEVICE_TYPE = "Test"; + public final static String TEST_DEVICE_TYPE = "TEST-DEVICE-TYPE"; public final static Integer SUPER_TENANT_ID = -1234; public final static String SUPER_TENANT_DOMAIN = "carbon.super"; public final static String initialDeviceIdentifier = "12345"; public final static String OWNER = "admin"; + public static final String OPERATION_CONFIG = "TEST-OPERATION-"; public static Device initialTestDevice; public static DeviceType initialTestDeviceType; @@ -126,4 +127,29 @@ public class TestDataHolder { deviceGroup.setOwner(OWNER); return deviceGroup; } + + public static OperationMonitoringTaskConfig generateMonitoringTaskConfig(boolean enabled, int frequency, + int numberOfOperations) { + OperationMonitoringTaskConfig taskConfig = new OperationMonitoringTaskConfig(); + List operationList = new ArrayList<>(); + + while (--numberOfOperations >= 0) { + operationList.add(generateMonitoringOperation(OPERATION_CONFIG + String.valueOf(numberOfOperations), + 1 + (int) (Math.random() * 4))); + } + + taskConfig.setEnabled(enabled); + taskConfig.setFrequency(frequency); + taskConfig.setMonitoringOperation(operationList); + + return taskConfig; + } + + private static MonitoringOperation generateMonitoringOperation(String name, int recurrentTimes) { + MonitoringOperation operation = new MonitoringOperation(); + operation.setTaskName(name); + operation.setRecurrentTimes(recurrentTimes); + + return operation; + } } 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 index 2db940a4ec..10081dbd5b 100644 --- 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 @@ -2,47 +2,32 @@ 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; + private TaskService taskService; - @BeforeClass public void init() throws Exception { + @BeforeClass + public void init() throws Exception { DeviceConfigurationManager.getInstance().initConfig(); log.info("Initializing Device Task Manager Service Test Suite"); this.taskService = new TestTaskServiceImpl(); @@ -54,30 +39,31 @@ public class DeviceTaskManagerServiceTest { taskServiceField.set(null, Mockito.mock(TaskServiceImpl.class, Mockito.RETURNS_MOCKS)); } - @Test(groups = "Device Task Manager") + @Test(groups = "Device Task Manager Service Test Group") public void testStartTask() { try { log.debug("Attempting to start task from testStartTask"); - this.deviceTaskManagerService - .startTask(TestDataHolder.TEST_DEVICE_TYPE, generateValidMonitoringTaskConfig("DEVICE_INFO")); + this.deviceTaskManagerService.startTask(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 60000, 1)); 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))); + .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") + @Test(groups = "Device Task Manager Service Test Group", 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); + this.deviceTaskManagerService.updateTask(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 30000, 1)); + Assert.assertEquals(this.taskService.getRegisteredTaskTypes().size(), 1); Assert.assertEquals(taskManager.getAllTasks().size(), 1); log.debug("Task Successfully updated"); } catch (DeviceMgtTaskException | TaskException e) { @@ -85,12 +71,12 @@ public class DeviceTaskManagerServiceTest { } } - @Test(groups = "Device Task Manager", dependsOnMethods = "testUpdateTask") + @Test(groups = "Device Task Manager Service Test Group", dependsOnMethods = "testUpdateTask") public void testStopTask() { log.debug("Attempting to stop task from testStopTask"); try { - this.deviceTaskManagerService - .stopTask(TestDataHolder.TEST_DEVICE_TYPE, generateValidMonitoringTaskConfig("DEVICE_LOCATION")); + this.deviceTaskManagerService.stopTask(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 30000, 1)); TaskManager taskManager = this.taskService.getTaskManager(TASK_TYPE); Assert.assertEquals(taskManager.getAllTasks().size(), 0); } catch (DeviceMgtTaskException | TaskException e) { @@ -98,27 +84,117 @@ public class DeviceTaskManagerServiceTest { } } + @Test(groups = "Device Task Manager Service Test Group", dependsOnMethods = "testStopTask", expectedExceptions = { + DeviceMgtTaskException.class }) + public void testUpdateUnscheduledTask() throws DeviceMgtTaskException { + log.debug("Attempting to update unscheduled task"); + this.deviceTaskManagerService.updateTask(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 60000, 1)); + } + + @Test(groups = "Device Task Manager Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", expectedExceptions = { + DeviceMgtTaskException.class }) + public void testStartTaskWhenUnableToRetrieveTaskManager() + throws DeviceMgtTaskException, TaskException { + TaskService taskService = Mockito.mock(TestTaskServiceImpl.class); + Mockito.doThrow(new TaskException("Unable to get TaskManager", TaskException.Code.UNKNOWN)).when(taskService) + .getTaskManager(TASK_TYPE); + DeviceManagementDataHolder.getInstance().setTaskService(taskService); + this.deviceTaskManagerService.startTask(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 60000, 2)); + } + + @Test(groups = "Device Task Manager Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", expectedExceptions = { + DeviceMgtTaskException.class }) + public void testUpdateTaskWhenUnableToRetrieveTaskManager() + throws DeviceMgtTaskException, TaskException { + TaskService taskService = Mockito.mock(TestTaskServiceImpl.class); + Mockito.doThrow(new TaskException("Unable to get TaskManager", TaskException.Code.UNKNOWN)).when(taskService) + .getTaskManager(TASK_TYPE); + DeviceManagementDataHolder.getInstance().setTaskService(taskService); + this.deviceTaskManagerService.updateTask(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 60000, 2)); + } + + @Test(groups = "Device Task Manager Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {DeviceMgtTaskException.class }) + public void testStartTaskWhenFailedToRegisterTaskType() + throws DeviceMgtTaskException, TaskException { + TaskService taskService = Mockito.mock(TestTaskServiceImpl.class); + Mockito.doThrow(new TaskException("Unable to register task type", TaskException.Code.UNKNOWN)).when(taskService) + .registerTaskType(TASK_TYPE); + DeviceManagementDataHolder.getInstance().setTaskService(taskService); + this.deviceTaskManagerService.startTask(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 60000, 2)); + } + @Test(groups = "Device Task Manager Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {DeviceMgtTaskException.class }) + public void testStartTaskWhenFailedToRegisterTask() + throws DeviceMgtTaskException, TaskException { + TestTaskServiceImpl taskService = new TestTaskServiceImpl(); + TaskManager taskManager = Mockito.mock(TestTaskManagerImpl.class); + taskService.setTaskManager(taskManager); + Mockito.doThrow(new TaskException("Unable to register task", TaskException.Code.UNKNOWN)).when(taskManager) + .registerTask(Mockito.any(TaskInfo.class)); + DeviceManagementDataHolder.getInstance().setTaskService(taskService); + this.deviceTaskManagerService.startTask(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 60000, 2)); + } - 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); + @Test(groups = "Device Task Manager Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {DeviceMgtTaskException.class }) + public void testUpdateTaskWhenFailedToRegisterTask() + throws DeviceMgtTaskException, TaskException { + TestTaskServiceImpl taskService = new TestTaskServiceImpl(); + TaskManager taskManager = Mockito.mock(TestTaskManagerImpl.class); + taskService.setTaskManager(taskManager); + Mockito.doThrow(new TaskException("Unable to register task", TaskException.Code.UNKNOWN)).when(taskManager) + .registerTask(Mockito.any(TaskInfo.class)); + DeviceManagementDataHolder.getInstance().setTaskService(taskService); + this.deviceTaskManagerService.updateTask(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 60000, 2)); + } - validTaskConfig.setEnabled(true); - validTaskConfig.setFrequency(60000); - validTaskConfig.setMonitoringOperation(operationList); + @Test(groups = "Device Task Manager Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {DeviceMgtTaskException.class }) + public void testUpdateTaskWhenFailedToRescheduleTask() + throws DeviceMgtTaskException, TaskException { + TestTaskServiceImpl taskService = new TestTaskServiceImpl(); + TaskManager taskManager = Mockito.mock(TestTaskManagerImpl.class); + taskService.setTaskManager(taskManager); + Mockito.doThrow(new TaskException("Unable to reschedule task", TaskException.Code.UNKNOWN)).when(taskManager) + .rescheduleTask(Mockito.any(String.class)); + DeviceManagementDataHolder.getInstance().setTaskService(taskService); + this.deviceTaskManagerService.updateTask(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 60000, 2)); + } - return validTaskConfig; + @Test(groups = "Device Task Manager Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {DeviceMgtTaskException.class }) + public void testUpdateTaskWhenFailedToDeleteTask() + throws DeviceMgtTaskException, TaskException { + TestTaskServiceImpl taskService = new TestTaskServiceImpl(); + TaskManager taskManager = Mockito.mock(TestTaskManagerImpl.class); + taskService.setTaskManager(taskManager); + Mockito.doThrow(new TaskException("Unable to delete task", TaskException.Code.UNKNOWN)).when(taskManager) + .deleteTask(Mockito.any(String.class)); + DeviceManagementDataHolder.getInstance().setTaskService(taskService); + this.deviceTaskManagerService.updateTask(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 60000, 2)); } - 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; + @Test(groups = "Device Task Manager Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {DeviceMgtTaskException.class }) + public void testStopTaskWhenFailedToDeleteTask() + throws DeviceMgtTaskException, TaskException { + TestTaskServiceImpl taskService = new TestTaskServiceImpl(); + TaskManager taskManager = Mockito.mock(TestTaskManagerImpl.class); + taskService.setTaskManager(taskManager); + Mockito.doThrow(new TaskException("Unable to delete task", TaskException.Code.UNKNOWN)).when(taskManager) + .deleteTask(Mockito.any(String.class)); + DeviceManagementDataHolder.getInstance().setTaskService(taskService); + this.deviceTaskManagerService.stopTask(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 60000, 2)); } } 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 index fcebed343c..4d7a76f9a6 100644 --- 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 @@ -31,6 +31,9 @@ public class TestTaskManagerImpl implements TaskManager { @Override public boolean deleteTask(String taskName) throws TaskException { + if(this.registeredTasks.size() <= 0) { + throw new TaskException("Cannot delete task.", TaskException.Code.NO_TASK_EXISTS); + } for (TaskInfo task : this.registeredTasks) { if (task.getName().contains(taskName)) { this.registeredTasks.remove(task); From ca6c9f1ec761925d8b97949e81e65b1bf8faf240 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Thu, 28 Sep 2017 19:17:49 +0530 Subject: [PATCH 2/9] Unit Tests for DeviceTaskManagerImpl and DeviceDetailsRetrieverTask --- .../mgt/core/task/DeviceTaskManagerTest.java | 196 ++++++++++++++++++ .../src/test/resources/testng.xml | 1 + 2 files changed, 197 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java new file mode 100644 index 0000000000..3670627645 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java @@ -0,0 +1,196 @@ +package org.wso2.carbon.device.mgt.core.task; + +import com.google.gson.Gson; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +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.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; +import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; +import org.wso2.carbon.device.mgt.core.TestUtils; +import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; +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.device.mgt.core.operation.TestNotificationStrategy; +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.DeviceManagementProviderServiceImpl; +import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; +import org.wso2.carbon.device.mgt.core.task.impl.DeviceDetailsRetrieverTask; +import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerImpl; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public class DeviceTaskManagerTest { + + private static final Log log = LogFactory.getLog(DeviceTaskManagerTest.class); + private static final String NEW_DEVICE_TYPE = "NEW-DEVICE-TYPE"; + private static final String DEVICE_DETAIL_RETRIEVER_OPPCONFIG = "{\"isEnabled\":true,\"frequency\":60000," + + "\"monitoringOperation\":[{\"taskName\":\"DEVICE_INFO\",\"recurrentTimes\":2}]}"; + private List deviceIds; + private DeviceTaskManager deviceTaskManager; + private DeviceManagementProviderService deviceMgtProviderService; + private OperationManager operationManager; + + @BeforeClass + public void init() throws Exception { + DeviceConfigurationManager.getInstance().initConfig(); + log.info("Initializing Device Task Manager Test Suite"); + this.deviceIds = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + deviceIds.add(new DeviceIdentifier(UUID.randomUUID().toString(), TestDataHolder.TEST_DEVICE_TYPE)); + } + List devices = TestDataHolder.generateDummyDeviceData(this.deviceIds); + this.deviceMgtProviderService = new DeviceManagementProviderServiceImpl(); + DeviceManagementServiceComponent.notifyStartupListeners(); + DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(this.deviceMgtProviderService); + DeviceManagementDataHolder.getInstance() + .setRegistryService(TestUtils.getRegistryService(DeviceTaskManagerTest.class)); + DeviceManagementDataHolder.getInstance() + .setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl()); + DeviceManagementDataHolder.getInstance() + .setGroupManagementProviderService(new GroupManagementProviderServiceImpl()); + DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); + NotificationStrategy notificationStrategy = new TestNotificationStrategy(); + this.operationManager = new OperationManagerImpl(TestDataHolder.TEST_DEVICE_TYPE, notificationStrategy); + this.deviceMgtProviderService.registerDeviceType( + new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE, TestDataHolder.SUPER_TENANT_DOMAIN)); + for (Device device : devices) { + this.deviceMgtProviderService.enrollDevice(device); + } + this.deviceTaskManager = new DeviceTaskManagerImpl(TestDataHolder.TEST_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 60000, 3)); + } + + @Test(groups = "Device Task Manager Test Group") + public void testGetTaskFrequency() { + log.debug("Attempting to retrieve task frequency."); + try { + Assert.assertEquals(this.deviceTaskManager.getTaskFrequency(), 60000); + } catch (DeviceMgtTaskException e) { + Assert.fail("Exception occurred when obtaining task frequency.", e); + } + log.debug("Successfully retrieved task frequency."); + } + + @Test(groups = "Device Task Manager Test Group") + public void testIsTaskEnabled() { + log.debug("Attempting to retrieve task status."); + try { + Assert.assertTrue(this.deviceTaskManager.isTaskEnabled()); + } catch (DeviceMgtTaskException e) { + Assert.fail("Exception occurred when checking whether the task is enabled.", e); + } + log.debug("Successfully retrieved task status."); + } + + @Test(groups = "Device Task Manager Test Group") + public void testAddOperation() { + log.debug("Attempting to add operations for devices."); + try { + this.deviceTaskManager.addOperations(); + for(DeviceIdentifier deviceId : deviceIds) { + List operationList = this.operationManager.getOperations(deviceId); + Assert.assertNotNull(operationList); + Assert.assertEquals(operationList.size(), 3); + } + } catch (DeviceMgtTaskException e) { + Assert.fail("Exception occurred when adding operations to available devices.", e); + } catch (OperationManagementException e) { + Assert.fail("Exception occurred when retrieving operations.", e); + } + log.debug("Successfully added operations for devices."); + } + + @Test(groups = "Device Task Manager Test Group") + public void testAddOperationsWithoutDevices() { + try { + this.deviceMgtProviderService.registerDeviceType( + new TestDeviceManagementService(NEW_DEVICE_TYPE, TestDataHolder.SUPER_TENANT_DOMAIN)); + DeviceTaskManager taskManager = new DeviceTaskManagerImpl(NEW_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 50000, 3)); + taskManager.addOperations(); + } catch (DeviceManagementException e) { + Assert.fail("Unexpected exception occurred", e); + } catch (DeviceMgtTaskException e) { + Assert.fail("Exception occurred when adding operations for the devices", e); + } + } + + @Test(groups = "Device Task Manager Test Group", dependsOnMethods = "testAddOperationsWithoutDevices") + public void testAddOperationsWithoutOperations() { + DeviceTaskManager taskManager = new DeviceTaskManagerImpl(NEW_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 50000, 3)); + try { + taskManager.addOperations(); + } catch (DeviceMgtTaskException e) { + Assert.fail("Exception occurred when adding operations for the devices", e); + } + } + + @Test(groups = "Device Task Manager Test Group") + public void testDeviceDetailRetrieverTaskExecute() { + DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask(); + Map map = new HashMap<>(); + map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); + map.put("OPPCONFIG", DEVICE_DETAIL_RETRIEVER_OPPCONFIG); + try { + deviceDetailsRetrieverTask.setProperties(map); + deviceDetailsRetrieverTask.execute(); + for(DeviceIdentifier deviceId : deviceIds) { + List operationList = this.operationManager.getOperations(deviceId); + Assert.assertNotNull(operationList); + Assert.assertEquals(operationList.size(), 4, "Expected number of operations is 4 " + + "after adding the device detail retriever operation"); + Assert.assertEquals(operationList.get(0).getCode(), "DEVICE_INFO", "Operation code of " + + "the device detail retriever task should be DEVICE_LOCATION"); + } + } catch (Exception e) { + Assert.fail("Exception occurred when adding operations for the devices", e); + } + } + + @Test(groups = "Device Task Manager Test Group") + public void testDeviceDetailRetrieverTaskExecuteForAllTenants() { + DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask(); + System.setProperty("is.cloud", "true"); + Map map = new HashMap<>(); + map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); + map.put("OPPCONFIG", DEVICE_DETAIL_RETRIEVER_OPPCONFIG); + try { + deviceDetailsRetrieverTask.setProperties(map); + deviceDetailsRetrieverTask.execute(); + for(DeviceIdentifier deviceId : deviceIds) { + List operationList = this.operationManager.getOperations(deviceId); + Assert.assertNotNull(operationList); + Assert.assertEquals(operationList.size(), 4); + Assert.assertEquals(operationList.get(0).getCode(), "DEVICE_INFO", "Operation code of " + + "the device detail retriever task should be DEVICE_LOCATION"); + } + } catch (Exception e) { + Assert.fail("Exception occurred when adding operations for the devices", e); + } + } + + @AfterClass + public void cleanup() throws DeviceManagementException { + for (DeviceIdentifier deviceId: deviceIds) { + this.deviceMgtProviderService.disenrollDevice(deviceId); + } + } + +} 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 3e27f03445..32c43fcfc1 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 @@ -46,6 +46,7 @@ + From fcc6dd1564687d44eff2b9792b03cec862d2f7f9 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Thu, 28 Sep 2017 19:25:10 +0530 Subject: [PATCH 3/9] Fixing repository test failure --- .../wso2/carbon/device/mgt/core/task/TestTaskManagerImpl.java | 3 --- 1 file changed, 3 deletions(-) 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 index 4d7a76f9a6..fcebed343c 100644 --- 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 @@ -31,9 +31,6 @@ public class TestTaskManagerImpl implements TaskManager { @Override public boolean deleteTask(String taskName) throws TaskException { - if(this.registeredTasks.size() <= 0) { - throw new TaskException("Cannot delete task.", TaskException.Code.NO_TASK_EXISTS); - } for (TaskInfo task : this.registeredTasks) { if (task.getName().contains(taskName)) { this.registeredTasks.remove(task); From 3588381290bb4c0b59f1a64ea5c1dd30be016d39 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Fri, 29 Sep 2017 15:20:24 +0530 Subject: [PATCH 4/9] Removing unnecessary import --- .../carbon/device/mgt/core/task/DeviceTaskManagerTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java index 3670627645..fa6a6af2be 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java @@ -1,6 +1,5 @@ package org.wso2.carbon.device.mgt.core.task; -import com.google.gson.Gson; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.Assert; @@ -28,6 +27,7 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceIm import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.task.impl.DeviceDetailsRetrieverTask; import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerImpl; +import org.wso2.carbon.registry.core.exceptions.RegistryException; import java.util.ArrayList; import java.util.HashMap; @@ -47,7 +47,7 @@ public class DeviceTaskManagerTest { private OperationManager operationManager; @BeforeClass - public void init() throws Exception { + public void init() throws DeviceManagementException, RegistryException { DeviceConfigurationManager.getInstance().initConfig(); log.info("Initializing Device Task Manager Test Suite"); this.deviceIds = new ArrayList<>(); From 677f2d0de4ac6efe05dace48dcd8afe361661edb Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Fri, 29 Sep 2017 15:21:08 +0530 Subject: [PATCH 5/9] Adding unit tests for PushNotificationSchedulerTask class --- .../org.wso2.carbon.device.mgt.core/pom.xml | 5 -- .../PushNotificationSchedulerTaskTest.java | 72 +++++++++++++++++++ .../src/test/resources/testng.xml | 1 + 3 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationSchedulerTaskTest.java 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 a5e7fde91b..cd0b23f3c4 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,11 +163,6 @@ 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/push/notification/mgt/task/PushNotificationSchedulerTaskTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationSchedulerTaskTest.java new file mode 100644 index 0000000000..4582e3dd8a --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationSchedulerTaskTest.java @@ -0,0 +1,72 @@ +package org.wso2.carbon.device.mgt.core.push.notification.mgt.task; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.mockito.Mockito; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; +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.operation.TestNotificationStrategy; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; +import org.wso2.carbon.registry.core.exceptions.RegistryException; + +import java.sql.SQLException; +import java.util.List; +import java.util.Map; + +public class PushNotificationSchedulerTaskTest extends BaseDeviceManagementTest { + private static final Log log = LogFactory.getLog(PushNotificationSchedulerTask.class); + private DeviceManagementProviderService deviceMgtProviderService; + private PushNotificationSchedulerTask pushNotificationSchedulerTask; + private OperationDAO operationDAO; + + @BeforeClass + public void init() throws DeviceManagementException, RegistryException { + DeviceConfigurationManager.getInstance().initConfig(); + log.info("Initializing Push Notification Scheduler Test Class"); + DeviceManagementServiceComponent.notifyStartupListeners(); + this.deviceMgtProviderService = Mockito.mock(DeviceManagementProviderServiceImpl.class, Mockito.CALLS_REAL_METHODS); + DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(this.deviceMgtProviderService); + this.operationDAO = OperationManagementDAOFactory.getOperationDAO(); + this.pushNotificationSchedulerTask = new PushNotificationSchedulerTask(); + } + + @Test + public void testPushNotificationScheduler() { + try { + log.debug("Attempting to execute push notification task scheduler"); + Mockito.doReturn(new TestNotificationStrategy()).when(this.deviceMgtProviderService) + .getNotificationStrategyByDeviceType(Mockito.anyString()); + Mockito.doReturn(new org.wso2.carbon.device.mgt.common.operation.mgt.Operation()) + .when(this.deviceMgtProviderService).getOperation(Mockito.anyString(), Mockito.anyInt()); + this.pushNotificationSchedulerTask.run(); + OperationManagementDAOFactory.openConnection(); + Map> operationMappingsTenantMap = operationDAO + .getOperationMappingsByStatus(Operation.Status.PENDING, Operation.PushNotificationStatus.SCHEDULED, + DeviceConfigurationManager.getInstance().getDeviceManagementConfig() + .getPushNotificationConfiguration().getSchedulerBatchSize()); + Assert.assertEquals(operationMappingsTenantMap.size(), 0); + log.debug("Push notification task execution complete."); + } catch (DeviceManagementException e) { + Assert.fail("Unexpected exception occurred when getting the push notification strategy.", e); + } catch (SQLException | OperationManagementDAOException e) { + Assert.fail("Unexpected exception occurred retrieving the operation mapping list.", e); + } catch (OperationManagementException e) { + Assert.fail("Unexpected exception occurred when retrieving an operation.", e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } + } +} 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 32c43fcfc1..fc454c3943 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 @@ -45,6 +45,7 @@ + From 165425ca5d324d170259a51c3926d33944a0f2f9 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Fri, 29 Sep 2017 15:35:44 +0530 Subject: [PATCH 6/9] Adding licence header for new files --- .../task/PushNotificationSchedulerTaskTest.java | 17 +++++++++++++++++ .../core/task/DeviceTaskManagerServiceTest.java | 17 +++++++++++++++++ .../mgt/core/task/DeviceTaskManagerTest.java | 17 +++++++++++++++++ .../mgt/core/task/TestTaskManagerImpl.java | 17 +++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationSchedulerTaskTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationSchedulerTaskTest.java index 4582e3dd8a..5b1761c5bf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationSchedulerTaskTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationSchedulerTaskTest.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.carbon.device.mgt.core.push.notification.mgt.task; import org.apache.commons.logging.Log; 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 index 10081dbd5b..b0a452b2a2 100644 --- 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 @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.carbon.device.mgt.core.task; import org.apache.commons.logging.Log; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java index fa6a6af2be..eb13f0898f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.carbon.device.mgt.core.task; import org.apache.commons.logging.Log; 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 index fcebed343c..6eda125c3f 100644 --- 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 @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.carbon.device.mgt.core.task; import org.wso2.carbon.ntask.common.TaskException; From 143ad01bd1085d239b14ce99e3cd06107ddd7426 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Tue, 3 Oct 2017 09:31:04 +0530 Subject: [PATCH 7/9] Policy Task Scheduler unit tests --- .../org.wso2.carbon.policy.mgt.core/pom.xml | 6 +- .../task/TaskSchedulerServiceImplTest.java | 210 ++++++++++++++++++ .../mgt/core/task/TestTaskManagerImpl.java | 100 +++++++++ .../mgt/core/task/TestTaskServiceImpl.java | 81 +++++++ .../src/test/resources/testng.xml | 7 + 5 files changed, 403 insertions(+), 1 deletion(-) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/task/TaskSchedulerServiceImplTest.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/task/TestTaskManagerImpl.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/task/TestTaskServiceImpl.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml index 565b46b58c..9a0f7fc578 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml @@ -204,7 +204,11 @@ h2-database-engine test - + + org.mockito + mockito-core + 2.10.0 + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/task/TaskSchedulerServiceImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/task/TaskSchedulerServiceImplTest.java new file mode 100644 index 0000000000..8186a66f20 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/task/TaskSchedulerServiceImplTest.java @@ -0,0 +1,210 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.policy.mgt.core.task; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.mockito.Mockito; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; +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.internal.TasksDSComponent; +import org.wso2.carbon.ntask.core.service.TaskService; +import org.wso2.carbon.ntask.core.service.impl.TaskServiceImpl; +import org.wso2.carbon.policy.mgt.common.PolicyMonitoringTaskException; +import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; + +import java.lang.reflect.Field; + +public class TaskSchedulerServiceImplTest { + private static final Log log = LogFactory.getLog(TaskSchedulerServiceImplTest.class); + private static final String TEST_DEVICE_TYPE = "TEST-DEVICE-TYPE"; + private TaskScheduleService policyTaskSchedulerService; + 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(); + PolicyManagementDataHolder.getInstance().setTaskService(this.taskService); + Field taskServiceField = TasksDSComponent.class.getDeclaredField("taskService"); + taskServiceField.setAccessible(true); + taskServiceField.set(null, Mockito.mock(TaskServiceImpl.class, Mockito.RETURNS_MOCKS)); + PolicyConfiguration policyConfiguration = new PolicyConfiguration(); + policyConfiguration.setMonitoringEnable(true); + DeviceConfigurationManager.getInstance().getDeviceManagementConfig() + .setPolicyConfiguration(policyConfiguration); + this.policyTaskSchedulerService = new TaskScheduleServiceImpl(); + } + + @Test(groups = "Policy Task Schedule Service Test Group") + public void testStartTask() { + try { + log.debug("Attempting to start task from testStartTask"); + this.policyTaskSchedulerService.startTask(60000); + TaskManager taskManager = this.taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); + Assert.assertEquals(this.taskService.getRegisteredTaskTypes().size(), 1); + Assert.assertNotNull(taskManager.getTask(PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String + .valueOf(MultitenantConstants.SUPER_TENANT_ID))); + log.debug("Task Successfully started"); + } catch (PolicyMonitoringTaskException | TaskException e) { + Assert.fail("Exception occurred when starting the task", e); + } + } + + @Test(groups = "Policy Task Schedule Service Test Group", dependsOnMethods = "testStartTask") + public void testIsTaskScheduled() { + try { + Assert.assertTrue(this.policyTaskSchedulerService.isTaskScheduled()); + } catch (PolicyMonitoringTaskException e) { + Assert.fail("Exception occurred when trying to check if task is scheduled."); + } + } + + @Test(groups = "Policy Task Schedule Service Test Group", dependsOnMethods = "testStartTask") + public void testUpdateTask() { + try { + log.debug("Attempting to update task from testStartTask"); + TaskManager taskManager = this.taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); + this.policyTaskSchedulerService.updateTask(30000); + Assert.assertEquals(this.taskService.getRegisteredTaskTypes().size(), 1); + Assert.assertEquals(taskManager.getAllTasks().size(), 1); + log.debug("Task Successfully updated"); + } catch (PolicyMonitoringTaskException | TaskException e) { + Assert.fail("Exception occurred when updating the task", e); + } + } + + @Test(groups = "Policy Task Schedule Service Test Group", dependsOnMethods = "testUpdateTask") + public void testStopTask() { + log.debug("Attempting to stop task from testStopTask"); + try { + this.policyTaskSchedulerService.stopTask(); + TaskManager taskManager = this.taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); + Assert.assertEquals(taskManager.getAllTasks().size(), 0); + } catch (PolicyMonitoringTaskException | TaskException e) { + Assert.fail("Exception occurred when stopping the task", e); + } + } + + @Test(groups = "Policy Task Schedule Service Test Group", dependsOnMethods = "testStopTask", expectedExceptions = { + PolicyMonitoringTaskException.class }) + public void testUpdateUnscheduledTask() throws PolicyMonitoringTaskException { + log.debug("Attempting to update unscheduled task"); + this.policyTaskSchedulerService.updateTask(50000); + } + + @Test(groups = "Policy Task Schedule Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {PolicyMonitoringTaskException.class }) + public void testStartTaskWhenUnableToRetrieveTaskManager() throws PolicyMonitoringTaskException, TaskException { + TaskService taskService = Mockito.mock(TestTaskServiceImpl.class); + Mockito.doThrow(new TaskException("Unable to get TaskManager", TaskException.Code.UNKNOWN)).when(taskService) + .getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); + PolicyManagementDataHolder.getInstance().setTaskService(taskService); + this.policyTaskSchedulerService.startTask(10000); + } + + @Test(groups = "Policy Task Schedule Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {PolicyMonitoringTaskException.class }) + public void testUpdateTaskWhenUnableToRetrieveTaskManager() throws PolicyMonitoringTaskException, TaskException { + TaskService taskService = Mockito.mock(TestTaskServiceImpl.class); + Mockito.doThrow(new TaskException("Unable to get TaskManager", TaskException.Code.UNKNOWN)).when(taskService) + .getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); + PolicyManagementDataHolder.getInstance().setTaskService(taskService); + this.policyTaskSchedulerService.updateTask(20000); + } + + @Test(groups = "Policy Task Schedule Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {PolicyMonitoringTaskException.class }) + public void testStartTaskWhenFailedToRegisterTaskType() throws PolicyMonitoringTaskException, TaskException { + TaskService taskService = Mockito.mock(TestTaskServiceImpl.class); + Mockito.doThrow(new TaskException("Unable to register task type", TaskException.Code.UNKNOWN)).when(taskService) + .registerTaskType(PolicyManagementConstants.MONITORING_TASK_TYPE); + PolicyManagementDataHolder.getInstance().setTaskService(taskService); + this.policyTaskSchedulerService.startTask(20000); + } + + @Test(groups = "Policy Task Schedule Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {PolicyMonitoringTaskException.class }) + public void testStartTaskWhenFailedToRegisterTask() throws PolicyMonitoringTaskException, TaskException { + TestTaskServiceImpl taskService = new TestTaskServiceImpl(); + TaskManager taskManager = Mockito.mock(TestTaskManagerImpl.class); + taskService.setTaskManager(taskManager); + Mockito.doThrow(new TaskException("Unable to register task", TaskException.Code.UNKNOWN)).when(taskManager) + .registerTask(Mockito.any(TaskInfo.class)); + PolicyManagementDataHolder.getInstance().setTaskService(taskService); + this.policyTaskSchedulerService.startTask(30000); + } + + @Test(groups = "Policy Task Schedule Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {PolicyMonitoringTaskException.class }) + public void testUpdateTaskWhenFailedToRegisterTask() throws PolicyMonitoringTaskException, TaskException { + TestTaskServiceImpl taskService = new TestTaskServiceImpl(); + TaskManager taskManager = Mockito.mock(TestTaskManagerImpl.class); + taskService.setTaskManager(taskManager); + Mockito.doThrow(new TaskException("Unable to register task", TaskException.Code.UNKNOWN)).when(taskManager) + .registerTask(Mockito.any(TaskInfo.class)); + PolicyManagementDataHolder.getInstance().setTaskService(taskService); + this.policyTaskSchedulerService.updateTask(18000); + } + + @Test(groups = "Policy Task Schedule Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {PolicyMonitoringTaskException.class }) + public void testUpdateTaskWhenFailedToRescheduleTask() throws PolicyMonitoringTaskException, TaskException { + TestTaskServiceImpl taskService = new TestTaskServiceImpl(); + TaskManager taskManager = Mockito.mock(TestTaskManagerImpl.class); + taskService.setTaskManager(taskManager); + Mockito.doThrow(new TaskException("Unable to reschedule task", TaskException.Code.UNKNOWN)).when(taskManager) + .rescheduleTask(Mockito.any(String.class)); + PolicyManagementDataHolder.getInstance().setTaskService(taskService); + this.policyTaskSchedulerService.updateTask(40000); + } + + @Test(groups = "Policy Task Schedule Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {PolicyMonitoringTaskException.class }) + public void testUpdateTaskWhenFailedToDeleteTask() throws PolicyMonitoringTaskException, TaskException { + TestTaskServiceImpl taskService = new TestTaskServiceImpl(); + TaskManager taskManager = Mockito.mock(TestTaskManagerImpl.class); + taskService.setTaskManager(taskManager); + Mockito.doThrow(new TaskException("Unable to delete task", TaskException.Code.UNKNOWN)).when(taskManager) + .deleteTask(Mockito.any(String.class)); + PolicyManagementDataHolder.getInstance().setTaskService(taskService); + this.policyTaskSchedulerService.updateTask(12000); + } + + @Test(groups = "Policy Task Schedule Service Test Group", dependsOnMethods = "testUpdateUnscheduledTask", + expectedExceptions = {PolicyMonitoringTaskException.class }) + public void testStopTaskWhenFailedToDeleteTask() throws PolicyMonitoringTaskException, TaskException { + TestTaskServiceImpl taskService = new TestTaskServiceImpl(); + TaskManager taskManager = Mockito.mock(TestTaskManagerImpl.class); + taskService.setTaskManager(taskManager); + Mockito.doThrow(new TaskException("Unable to delete task", TaskException.Code.UNKNOWN)).when(taskManager) + .deleteTask(Mockito.any(String.class)); + PolicyManagementDataHolder.getInstance().setTaskService(taskService); + this.policyTaskSchedulerService.stopTask(); + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/task/TestTaskManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/task/TestTaskManagerImpl.java new file mode 100644 index 0000000000..cf15c76b0a --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/task/TestTaskManagerImpl.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.policy.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 (taskName.equals(task.getName())) { + 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 (taskName.contains(task.getName())) { + 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/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/task/TestTaskServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/task/TestTaskServiceImpl.java new file mode 100644 index 0000000000..5c3ba651e5 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/task/TestTaskServiceImpl.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.policy.mgt.core.task; + +import org.wso2.carbon.ntask.common.TaskException; +import org.wso2.carbon.ntask.core.TaskManager; +import org.wso2.carbon.ntask.core.service.TaskService; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class TestTaskServiceImpl implements TaskService { + private Set registeredTaskTypes; + private TaskManager taskManager; + + public TestTaskServiceImpl() { + + this.registeredTaskTypes = new HashSet<>(); + this.taskManager = new TestTaskManagerImpl(); + } + + @Override + public TaskManager getTaskManager(String s) throws TaskException { + return this.taskManager; + } + + @Override + public List getAllTenantTaskManagersForType(String s) throws TaskException { + return null; + } + + @Override + public void registerTaskType(String s) throws TaskException { + this.registeredTaskTypes.add(s); + } + + @Override + public Set getRegisteredTaskTypes() { + return this.registeredTaskTypes; + } + + @Override + public void serverInitialized() { + + } + + @Override + public boolean isServerInit() { + return true; + } + + @Override + public TaskServiceConfiguration getServerConfiguration() { + return null; + } + + @Override + public void runAfterRegistrationActions() throws TaskException { + + } + + public void setTaskManager(TaskManager taskManager) { + this.taskManager = taskManager; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml index 8d74befa5d..c9ce675640 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml @@ -28,6 +28,13 @@ + + + + + + + \ No newline at end of file From 68e3ad90910656c38a6a2676a8922c6af52a7cbb Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Tue, 3 Oct 2017 10:47:11 +0530 Subject: [PATCH 8/9] Fixing review suggestions --- .../PushNotificationSchedulerTaskTest.java | 19 ++- .../mgt/core/task/DeviceTaskManagerTest.java | 146 +++++++----------- 2 files changed, 68 insertions(+), 97 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationSchedulerTaskTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationSchedulerTaskTest.java index 5b1761c5bf..64a01788bf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationSchedulerTaskTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationSchedulerTaskTest.java @@ -43,6 +43,9 @@ import java.sql.SQLException; import java.util.List; import java.util.Map; +/** + * This class contains unit tests to test {@link PushNotificationSchedulerTask} class. + */ public class PushNotificationSchedulerTaskTest extends BaseDeviceManagementTest { private static final Log log = LogFactory.getLog(PushNotificationSchedulerTask.class); private DeviceManagementProviderService deviceMgtProviderService; @@ -60,10 +63,12 @@ public class PushNotificationSchedulerTaskTest extends BaseDeviceManagementTest this.pushNotificationSchedulerTask = new PushNotificationSchedulerTask(); } - @Test - public void testPushNotificationScheduler() { + @Test(description = "Tests the push notification scheduling for devices") + public void testPushNotificationScheduler() + throws DeviceManagementException, OperationManagementException, SQLException, + OperationManagementDAOException { try { - log.debug("Attempting to execute push notification task scheduler"); + log.info("Attempting to execute push notification task scheduler"); Mockito.doReturn(new TestNotificationStrategy()).when(this.deviceMgtProviderService) .getNotificationStrategyByDeviceType(Mockito.anyString()); Mockito.doReturn(new org.wso2.carbon.device.mgt.common.operation.mgt.Operation()) @@ -75,13 +80,7 @@ public class PushNotificationSchedulerTaskTest extends BaseDeviceManagementTest DeviceConfigurationManager.getInstance().getDeviceManagementConfig() .getPushNotificationConfiguration().getSchedulerBatchSize()); Assert.assertEquals(operationMappingsTenantMap.size(), 0); - log.debug("Push notification task execution complete."); - } catch (DeviceManagementException e) { - Assert.fail("Unexpected exception occurred when getting the push notification strategy.", e); - } catch (SQLException | OperationManagementDAOException e) { - Assert.fail("Unexpected exception occurred retrieving the operation mapping list.", e); - } catch (OperationManagementException e) { - Assert.fail("Unexpected exception occurred when retrieving an operation.", e); + log.info("Push notification task execution complete."); } finally { OperationManagementDAOFactory.closeConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java index eb13f0898f..ff020451cf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java @@ -33,6 +33,7 @@ import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.TestUtils; 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.TestDataHolder; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; @@ -52,7 +53,10 @@ import java.util.List; import java.util.Map; import java.util.UUID; -public class DeviceTaskManagerTest { +/** + * This is a test class to test the functionality in {@link DeviceTaskManager}. + */ +public class DeviceTaskManagerTest extends BaseDeviceManagementTest { private static final Log log = LogFactory.getLog(DeviceTaskManagerTest.class); private static final String NEW_DEVICE_TYPE = "NEW-DEVICE-TYPE"; @@ -65,7 +69,6 @@ public class DeviceTaskManagerTest { @BeforeClass public void init() throws DeviceManagementException, RegistryException { - DeviceConfigurationManager.getInstance().initConfig(); log.info("Initializing Device Task Manager Test Suite"); this.deviceIds = new ArrayList<>(); for (int i = 0; i < 5; i++) { @@ -73,14 +76,13 @@ public class DeviceTaskManagerTest { } List devices = TestDataHolder.generateDummyDeviceData(this.deviceIds); this.deviceMgtProviderService = new DeviceManagementProviderServiceImpl(); + DeviceManagementServiceComponent.notifyStartupListeners(); DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(this.deviceMgtProviderService); DeviceManagementDataHolder.getInstance() .setRegistryService(TestUtils.getRegistryService(DeviceTaskManagerTest.class)); DeviceManagementDataHolder.getInstance() .setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl()); - DeviceManagementDataHolder.getInstance() - .setGroupManagementProviderService(new GroupManagementProviderServiceImpl()); DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); NotificationStrategy notificationStrategy = new TestNotificationStrategy(); this.operationManager = new OperationManagerImpl(TestDataHolder.TEST_DEVICE_TYPE, notificationStrategy); @@ -93,113 +95,84 @@ public class DeviceTaskManagerTest { TestDataHolder.generateMonitoringTaskConfig(true, 60000, 3)); } - @Test(groups = "Device Task Manager Test Group") - public void testGetTaskFrequency() { - log.debug("Attempting to retrieve task frequency."); - try { - Assert.assertEquals(this.deviceTaskManager.getTaskFrequency(), 60000); - } catch (DeviceMgtTaskException e) { - Assert.fail("Exception occurred when obtaining task frequency.", e); - } - log.debug("Successfully retrieved task frequency."); + @Test(groups = "Device Task Manager Test Group", description = "Getting the task frequency from the scheduler") + public void testGetTaskFrequency() throws DeviceMgtTaskException { + log.info("Attempting to retrieve task frequency."); + Assert.assertEquals(this.deviceTaskManager.getTaskFrequency(), 60000); + log.info("Successfully retrieved task frequency."); } - @Test(groups = "Device Task Manager Test Group") - public void testIsTaskEnabled() { - log.debug("Attempting to retrieve task status."); - try { - Assert.assertTrue(this.deviceTaskManager.isTaskEnabled()); - } catch (DeviceMgtTaskException e) { - Assert.fail("Exception occurred when checking whether the task is enabled.", e); - } - log.debug("Successfully retrieved task status."); + @Test(groups = "Device Task Manager Test Group", description = "Testing if the task is enabled") + public void testIsTaskEnabled() throws DeviceMgtTaskException { + log.info("Attempting to retrieve task status."); + Assert.assertTrue(this.deviceTaskManager.isTaskEnabled()); + log.info("Successfully retrieved task status."); } - @Test(groups = "Device Task Manager Test Group") - public void testAddOperation() { - log.debug("Attempting to add operations for devices."); - try { - this.deviceTaskManager.addOperations(); - for(DeviceIdentifier deviceId : deviceIds) { - List operationList = this.operationManager.getOperations(deviceId); - Assert.assertNotNull(operationList); - Assert.assertEquals(operationList.size(), 3); - } - } catch (DeviceMgtTaskException e) { - Assert.fail("Exception occurred when adding operations to available devices.", e); - } catch (OperationManagementException e) { - Assert.fail("Exception occurred when retrieving operations.", e); + @Test(groups = "Device Task Manager Test Group", description = "Testing adding operations to devices.") + public void testAddOperation() throws DeviceMgtTaskException, OperationManagementException { + log.info("Attempting to add operations for devices."); + this.deviceTaskManager.addOperations(); + for (DeviceIdentifier deviceId : deviceIds) { + List operationList = this.operationManager.getOperations(deviceId); + Assert.assertNotNull(operationList); + Assert.assertEquals(operationList.size(), 3); } - log.debug("Successfully added operations for devices."); + log.info("Successfully added operations for devices."); } - @Test(groups = "Device Task Manager Test Group") - public void testAddOperationsWithoutDevices() { - try { - this.deviceMgtProviderService.registerDeviceType( - new TestDeviceManagementService(NEW_DEVICE_TYPE, TestDataHolder.SUPER_TENANT_DOMAIN)); - DeviceTaskManager taskManager = new DeviceTaskManagerImpl(NEW_DEVICE_TYPE, - TestDataHolder.generateMonitoringTaskConfig(true, 50000, 3)); - taskManager.addOperations(); - } catch (DeviceManagementException e) { - Assert.fail("Unexpected exception occurred", e); - } catch (DeviceMgtTaskException e) { - Assert.fail("Exception occurred when adding operations for the devices", e); - } + @Test(groups = "Device Task Manager Test Group", + description = "Testing adding operations when no devices are available") + public void testAddOperationsWithoutDevices() throws DeviceManagementException, DeviceMgtTaskException { + this.deviceMgtProviderService.registerDeviceType( + new TestDeviceManagementService(NEW_DEVICE_TYPE, TestDataHolder.SUPER_TENANT_DOMAIN)); + DeviceTaskManager taskManager = new DeviceTaskManagerImpl(NEW_DEVICE_TYPE, + TestDataHolder.generateMonitoringTaskConfig(true, 50000, 3)); + taskManager.addOperations(); } - @Test(groups = "Device Task Manager Test Group", dependsOnMethods = "testAddOperationsWithoutDevices") - public void testAddOperationsWithoutOperations() { + @Test(groups = "Device Task Manager Test Group", dependsOnMethods = "testAddOperationsWithoutDevices", + description = "Testing adding operations when no operations are scheduled") + public void testAddOperationsWithoutOperations() throws DeviceMgtTaskException { DeviceTaskManager taskManager = new DeviceTaskManagerImpl(NEW_DEVICE_TYPE, TestDataHolder.generateMonitoringTaskConfig(true, 50000, 3)); - try { - taskManager.addOperations(); - } catch (DeviceMgtTaskException e) { - Assert.fail("Exception occurred when adding operations for the devices", e); - } + taskManager.addOperations(); } - @Test(groups = "Device Task Manager Test Group") - public void testDeviceDetailRetrieverTaskExecute() { + @Test(groups = "Device Task Manager Test Group", description = "Testing device detail retriever task execution") + public void testDeviceDetailRetrieverTaskExecute() throws OperationManagementException { DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask(); Map map = new HashMap<>(); map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); map.put("OPPCONFIG", DEVICE_DETAIL_RETRIEVER_OPPCONFIG); - try { - deviceDetailsRetrieverTask.setProperties(map); - deviceDetailsRetrieverTask.execute(); - for(DeviceIdentifier deviceId : deviceIds) { - List operationList = this.operationManager.getOperations(deviceId); - Assert.assertNotNull(operationList); - Assert.assertEquals(operationList.size(), 4, "Expected number of operations is 4 " + - "after adding the device detail retriever operation"); - Assert.assertEquals(operationList.get(0).getCode(), "DEVICE_INFO", "Operation code of " + - "the device detail retriever task should be DEVICE_LOCATION"); - } - } catch (Exception e) { - Assert.fail("Exception occurred when adding operations for the devices", e); + deviceDetailsRetrieverTask.setProperties(map); + deviceDetailsRetrieverTask.execute(); + for (DeviceIdentifier deviceId : deviceIds) { + List operationList = this.operationManager.getOperations(deviceId); + Assert.assertNotNull(operationList); + Assert.assertEquals(operationList.size(), 4, + "Expected number of operations is 4 after adding the device detail retriever operation"); + Assert.assertEquals(operationList.get(0).getCode(), "DEVICE_INFO", + "Operation code of the device detail retriever task should be DEVICE_LOCATION"); } } - @Test(groups = "Device Task Manager Test Group") - public void testDeviceDetailRetrieverTaskExecuteForAllTenants() { + @Test(groups = "Device Task Manager Test Group", + description = "Testing device detail retriever task execution for tenants") + public void testDeviceDetailRetrieverTaskExecuteForAllTenants() throws OperationManagementException { DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask(); System.setProperty("is.cloud", "true"); Map map = new HashMap<>(); map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); map.put("OPPCONFIG", DEVICE_DETAIL_RETRIEVER_OPPCONFIG); - try { - deviceDetailsRetrieverTask.setProperties(map); - deviceDetailsRetrieverTask.execute(); - for(DeviceIdentifier deviceId : deviceIds) { - List operationList = this.operationManager.getOperations(deviceId); - Assert.assertNotNull(operationList); - Assert.assertEquals(operationList.size(), 4); - Assert.assertEquals(operationList.get(0).getCode(), "DEVICE_INFO", "Operation code of " + - "the device detail retriever task should be DEVICE_LOCATION"); - } - } catch (Exception e) { - Assert.fail("Exception occurred when adding operations for the devices", e); + deviceDetailsRetrieverTask.setProperties(map); + deviceDetailsRetrieverTask.execute(); + for (DeviceIdentifier deviceId : deviceIds) { + List operationList = this.operationManager.getOperations(deviceId); + Assert.assertNotNull(operationList); + Assert.assertEquals(operationList.size(), 4); + Assert.assertEquals(operationList.get(0).getCode(), "DEVICE_INFO", + "Operation code of the device detail retriever task should be DEVICE_LOCATION"); } } @@ -209,5 +182,4 @@ public class DeviceTaskManagerTest { this.deviceMgtProviderService.disenrollDevice(deviceId); } } - } From 650d87d6ca7583c59cbfbd51dbf3ce703e39af76 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Tue, 3 Oct 2017 11:19:07 +0530 Subject: [PATCH 9/9] Fixing group management test failure --- .../test/java/org/wso2/carbon/device/mgt/core/TestUtils.java | 3 ++- .../mgt/core/service/GroupManagementProviderServiceTest.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) 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 29aad743c8..4b10c865c2 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,6 +22,7 @@ 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.common.TestDataHolder; 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; @@ -108,7 +109,7 @@ public class TestUtils { public static List getDeviceIdentifiersList(){ DeviceIdentifier identifier = new DeviceIdentifier(); identifier.setId("12345"); - identifier.setType("Test"); + identifier.setType(TestDataHolder.TEST_DEVICE_TYPE); List list = new ArrayList<>(); list.add(identifier); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java index 994ef33e8e..7ac1c9a2d8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java @@ -27,6 +27,7 @@ import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.group.mgt.*; import org.wso2.carbon.device.mgt.core.TestUtils; 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.config.cache.DeviceCacheConfiguration; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; @@ -266,7 +267,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest public void getGroupsByDeviceIdentifier() throws GroupManagementException { DeviceIdentifier identifier = new DeviceIdentifier(); identifier.setId("12345"); - identifier.setType("Test"); + identifier.setType(TestDataHolder.TEST_DEVICE_TYPE); List groups = groupManagementProviderService.getGroups(identifier); Assert.assertNotNull(groups); }