From 68e3ad90910656c38a6a2676a8922c6af52a7cbb Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Tue, 3 Oct 2017 10:47:11 +0530 Subject: [PATCH] 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); } } - }