From 473986965c042cda798923adcae11682cfc1415f Mon Sep 17 00:00:00 2001 From: Nirothipan Date: Wed, 27 Sep 2017 20:38:29 +0530 Subject: [PATCH 1/3] adding second set of test cases for notificationManagementServiceImpl class --- .../mgt/core/common/TestDataHolder.java | 6 +- ...tificationManagementServiceImplTests.java} | 73 +++++++++++++++++-- .../src/test/resources/testng.xml | 2 +- 3 files changed, 70 insertions(+), 11 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/{NotificationManagementTests.java => NotificationManagementServiceImplTests.java} (58%) 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 356efb2426..adb488fad6 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 @@ -53,13 +53,13 @@ public class TestDataHolder { return device; } - public static Notification getNotification(int notificationId, String status, String deviceId, String deviceName, - int operationId, String deviceType){ + public static Notification getNotification(int notificationId, String status, String deviceId, String description, String deviceName, + int operationId, String deviceType) { Notification notification = new Notification(); notification.setNotificationId(notificationId); notification.setStatus(status); notification.setDeviceIdentifier(deviceId); - notification.setDescription("test description"); + notification.setDescription(description); notification.setDeviceName(deviceName); notification.setOperationId(operationId); notification.setDeviceType(deviceType); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImplTests.java similarity index 58% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementTests.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImplTests.java index 8257785fc1..5fcb9a0423 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImplTests.java @@ -24,6 +24,7 @@ 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.EntityDoesNotExistException; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; @@ -49,19 +50,23 @@ import java.util.List; /** * This class is used to test NotificationManagementServiceImpl. */ -public class NotificationManagementTests { +public class NotificationManagementServiceImplTests { - private static final Log log = LogFactory.getLog(NotificationManagementTests.class); + private static final Log log = LogFactory.getLog(NotificationManagementServiceImplTests.class); private static final String DEVICE_TYPE = "NOTIFICATION_TEST_DEVICE"; private static final String DEVICE_ID_PREFIX = "NOTIFICATION-TEST-DEVICE-ID-"; private static final int NO_OF_DEVICES = 10; + private static final int NO_OF_NOTIFICATIONS = 10; private List deviceIds = new ArrayList<>(); + private NotificationManagementServiceImpl notificationManagementService; + private static final String TEST_NOTIFICATION_DESCRIPTION = "test notification"; + private static final int NOTIFICATION_OPERATION_ID = 1; @BeforeClass public void init() throws Exception { DeviceConfigurationManager.getInstance().initConfig(); log.info("Initializing"); - for (int i = 0; i < NO_OF_DEVICES; i++) { + for (int i = 1; i <= NO_OF_DEVICES; i++) { deviceIds.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); } List devices = TestDataHolder.generateDummyDeviceData(this.deviceIds); @@ -84,6 +89,7 @@ public class NotificationManagementTests { throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); } } + notificationManagementService = new NotificationManagementServiceImpl(); } private RegistryService getRegistryService() throws RegistryException { @@ -98,13 +104,66 @@ public class NotificationManagementTests { @Test(description = "Add notifications using addNotification method and check whether it returns true.") public void addNotification() throws Exception { - for (int i = 0; i < NO_OF_DEVICES; i++) { + Notification notification = null; + for (int i = 1; i <= NO_OF_DEVICES; i++) { DeviceIdentifier testDeviceIdentifier = new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE); - Notification notification = TestDataHolder.getNotification(i, "CHECKED", - testDeviceIdentifier.toString(), DEVICE_ID_PREFIX + i, 1, DEVICE_TYPE); - NotificationManagementServiceImpl notificationManagementService = new NotificationManagementServiceImpl(); + notification = TestDataHolder.getNotification(i, Notification.Status.NEW.toString(), + testDeviceIdentifier.toString(), TEST_NOTIFICATION_DESCRIPTION, DEVICE_ID_PREFIX + i, + NOTIFICATION_OPERATION_ID, DEVICE_TYPE); Assert.assertTrue(notificationManagementService.addNotification(testDeviceIdentifier, notification)); } + try { + notificationManagementService.addNotification(new DeviceIdentifier(DEVICE_ID_PREFIX + 123, + DEVICE_TYPE), notification); + Assert.fail(); + } catch (EntityDoesNotExistException ignored) { + } + } + + @Test(dependsOnMethods = "addNotification", description = "This tests the updateNotification Method" + + " and check whether it returns true ( got updated )") + public void updateNotification() throws Exception { + for (int i = 1; i <= NO_OF_DEVICES; i++) { + DeviceIdentifier testDeviceIdentifier = new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE); + Notification notification = TestDataHolder.getNotification(i, Notification.Status.CHECKED.toString(), + testDeviceIdentifier.toString(), TEST_NOTIFICATION_DESCRIPTION, DEVICE_ID_PREFIX + i, NOTIFICATION_OPERATION_ID, + DEVICE_TYPE); + Assert.assertTrue(notificationManagementService.updateNotification(notification)); + } + } + + @Test(dependsOnMethods = "updateNotification", description = "This method update notification status " + + "and check whether it got updated") + public void updateNotificationStatus() throws Exception { + for (int i = 1; i <= NO_OF_DEVICES; i++) { + Assert.assertTrue(notificationManagementService.updateNotificationStatus(i, Notification.Status.CHECKED)); + } } + + @Test(dependsOnMethods = "addNotification", description = "this tests getAllNotifications" + + " method by listing down all the notifications.") + public void getAllNotifications() throws Exception { + List returnedNotifications = notificationManagementService.getAllNotifications(); + Assert.assertEquals(returnedNotifications.size(), NO_OF_DEVICES); + } + + @Test(dependsOnMethods = "updateNotificationStatus", description = "this method retries notification by id" + + " and checks it") + public void getNotification() throws Exception { + for (int i = 1; i <= NO_OF_DEVICES; i++) { + Notification returnedNotification = notificationManagementService.getNotification(i); + Assert.assertEquals(returnedNotification.getNotificationId(), i); + Assert.assertEquals(returnedNotification.getStatus(), Notification.Status.CHECKED); + Assert.assertEquals(returnedNotification.getDescription(), TEST_NOTIFICATION_DESCRIPTION); + Assert.assertEquals(returnedNotification.getOperationId(), NOTIFICATION_OPERATION_ID); + } + } + + @Test(dependsOnMethods = "updateNotificationStatus", description = "this method gets all notification by status checked") + public void getNotificationsByStatus() throws Exception { + List returnedNotifications = notificationManagementService.getNotificationsByStatus(Notification.Status.CHECKED); + Assert.assertEquals(returnedNotifications.size(), NO_OF_NOTIFICATIONS); + } + } 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 331aac6c38..7ab82eaf07 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 @@ -43,7 +43,7 @@ - + From 35c5a06520709a5d4e951a9c9b1bf3421e582d15 Mon Sep 17 00:00:00 2001 From: Nirothipan Date: Thu, 28 Sep 2017 11:06:46 +0530 Subject: [PATCH 2/3] making required changes --- .../mgt/core/common/TestDataHolder.java | 5 +-- ...otificationManagementServiceImplTests.java | 33 +++++++++++-------- 2 files changed, 22 insertions(+), 16 deletions(-) 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 adb488fad6..a6af04fad7 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 @@ -53,8 +53,9 @@ public class TestDataHolder { return device; } - public static Notification getNotification(int notificationId, String status, String deviceId, String description, String deviceName, - int operationId, String deviceType) { + public static Notification getNotification(int notificationId, String status, String deviceId, + String description, String deviceName, int operationId, + String deviceType) { Notification notification = new Notification(); notification.setNotificationId(notificationId); notification.setStatus(status); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImplTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImplTests.java index 5fcb9a0423..a3ac55747b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImplTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImplTests.java @@ -26,6 +26,7 @@ import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EntityDoesNotExistException; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; +import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; @@ -48,7 +49,7 @@ import java.util.ArrayList; import java.util.List; /** - * This class is used to test NotificationManagementServiceImpl. + * This is the test class for {@link NotificationManagementServiceImpl} */ public class NotificationManagementServiceImplTests { @@ -103,21 +104,25 @@ public class NotificationManagementServiceImplTests { } @Test(description = "Add notifications using addNotification method and check whether it returns true.") - public void addNotification() throws Exception { - Notification notification = null; + public void addNotification() throws NotificationManagementException { for (int i = 1; i <= NO_OF_DEVICES; i++) { DeviceIdentifier testDeviceIdentifier = new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE); - notification = TestDataHolder.getNotification(i, Notification.Status.NEW.toString(), + Notification notification = TestDataHolder.getNotification(i, Notification.Status.NEW.toString(), testDeviceIdentifier.toString(), TEST_NOTIFICATION_DESCRIPTION, DEVICE_ID_PREFIX + i, NOTIFICATION_OPERATION_ID, DEVICE_TYPE); Assert.assertTrue(notificationManagementService.addNotification(testDeviceIdentifier, notification)); } - try { - notificationManagementService.addNotification(new DeviceIdentifier(DEVICE_ID_PREFIX + 123, - DEVICE_TYPE), notification); - Assert.fail(); - } catch (EntityDoesNotExistException ignored) { - } + } + + @Test(expectedExceptions = EntityDoesNotExistException.class, description = "AddNotification method is checked" + + " whether it returns EntityDoesNotExistException when the device not registered is added notification") + public void addNotificationExceptions() throws NotificationManagementException { + DeviceIdentifier testDeviceIdentifier = new DeviceIdentifier(DEVICE_ID_PREFIX + 123, DEVICE_TYPE); + Notification notification = TestDataHolder.getNotification(1, Notification.Status.NEW.toString(), + testDeviceIdentifier.toString(), TEST_NOTIFICATION_DESCRIPTION, DEVICE_ID_PREFIX + 123, + NOTIFICATION_OPERATION_ID, DEVICE_TYPE); + notificationManagementService.addNotification(new DeviceIdentifier(DEVICE_ID_PREFIX + 123, + DEVICE_TYPE), notification); } @Test(dependsOnMethods = "addNotification", description = "This tests the updateNotification Method" + @@ -134,7 +139,7 @@ public class NotificationManagementServiceImplTests { @Test(dependsOnMethods = "updateNotification", description = "This method update notification status " + "and check whether it got updated") - public void updateNotificationStatus() throws Exception { + public void updateNotificationStatus() throws NotificationManagementException { for (int i = 1; i <= NO_OF_DEVICES; i++) { Assert.assertTrue(notificationManagementService.updateNotificationStatus(i, Notification.Status.CHECKED)); } @@ -142,14 +147,14 @@ public class NotificationManagementServiceImplTests { @Test(dependsOnMethods = "addNotification", description = "this tests getAllNotifications" + " method by listing down all the notifications.") - public void getAllNotifications() throws Exception { + public void getAllNotifications() throws NotificationManagementException { List returnedNotifications = notificationManagementService.getAllNotifications(); Assert.assertEquals(returnedNotifications.size(), NO_OF_DEVICES); } @Test(dependsOnMethods = "updateNotificationStatus", description = "this method retries notification by id" + " and checks it") - public void getNotification() throws Exception { + public void getNotification() throws NotificationManagementException { for (int i = 1; i <= NO_OF_DEVICES; i++) { Notification returnedNotification = notificationManagementService.getNotification(i); Assert.assertEquals(returnedNotification.getNotificationId(), i); @@ -160,7 +165,7 @@ public class NotificationManagementServiceImplTests { } @Test(dependsOnMethods = "updateNotificationStatus", description = "this method gets all notification by status checked") - public void getNotificationsByStatus() throws Exception { + public void getNotificationsByStatus() throws NotificationManagementException { List returnedNotifications = notificationManagementService.getNotificationsByStatus(Notification.Status.CHECKED); Assert.assertEquals(returnedNotifications.size(), NO_OF_NOTIFICATIONS); } From 80a706117a74ba5f2d2bc3fb415dae17f54c99e5 Mon Sep 17 00:00:00 2001 From: Nirothipan Date: Thu, 28 Sep 2017 11:08:05 +0530 Subject: [PATCH 3/3] making required changes --- .../mgt/NotificationManagementServiceImplTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImplTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImplTests.java index a3ac55747b..87de357830 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImplTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImplTests.java @@ -127,7 +127,7 @@ public class NotificationManagementServiceImplTests { @Test(dependsOnMethods = "addNotification", description = "This tests the updateNotification Method" + " and check whether it returns true ( got updated )") - public void updateNotification() throws Exception { + public void updateNotification() throws NotificationManagementException { for (int i = 1; i <= NO_OF_DEVICES; i++) { DeviceIdentifier testDeviceIdentifier = new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE); Notification notification = TestDataHolder.getNotification(i, Notification.Status.CHECKED.toString(),