100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Improve NotificationTest robustness
Rich Turnerea2cde5ea785
diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovyindex 2faead62a..b9e060176 100644--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy@@ -50,6 +50,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {List<AbstractNotificationMessage> notificationMessages = []given: "the container environment is started with the mock handler"+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.1, delay: 0.2)def container = startContainer(defaultConfig(), defaultServices())def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)@@ -132,6 +133,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {def testuser3ConsoleResource = getClientApiTarget(serverUri(serverPort), realm, testuser3AccessToken).proxy(ConsoleResource.class)def adminConsoleResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, adminAccessToken).proxy(ConsoleResource.class)def anonymousConsoleResource = getClientApiTarget(serverUri(serverPort), realm).proxy(ConsoleResource.class)+ SentNotification[] notifications = []when: "various consoles are registered"def consoleRegistration = new ConsoleRegistration(null,@@ -179,14 +181,16 @@ class NotificationTest extends Specification implements ManagerContainerTrait {notification.targets = [new Notification.Target(Notification.TargetType.TENANT, keycloakTestSetup.tenantBuilding.realm)]adminNotificationResource.sendNotification(null, notification)- then: "all consoles in that realm should have been sent a notification"- assert notificationIds.size() == 3- assert notificationTargetTypes.count {t -> t == Notification.TargetType.ASSET} == 3- assert !notificationTargetIds.contains(testuser2Console.id)- assert notificationTargetIds.contains(testuser3Console1.id)- assert notificationTargetIds.contains(testuser3Console2.id)- assert notificationTargetIds.contains(anonymousConsole.id)- assert notificationMessages.count {m -> m instanceof PushNotificationMessage && m.title == "Test Action" && m.body == "Click to cancel" && m.action != null} == 3+ then: "all consoles in that realm should have been sent a notification (excluding testuser2 as they have disabled push notifications)"+ conditions.eventually {+ assert notificationIds.size() == 3+ assert notificationTargetTypes.count { t -> t == Notification.TargetType.ASSET } == 3+ assert !notificationTargetIds.contains(testuser2Console.id)+ assert notificationTargetIds.contains(testuser3Console1.id)+ assert notificationTargetIds.contains(testuser3Console2.id)+ assert notificationTargetIds.contains(anonymousConsole.id)+ assert notificationMessages.count { m -> m instanceof PushNotificationMessage && m.title == "Test Action" && m.body == "Click to cancel" && m.action != null } == 3+ }when: "a regular user sends a push notification to an entire realm"testuser2NotificationResource.sendNotification(null, notification)@@ -211,7 +215,9 @@ class NotificationTest extends Specification implements ManagerContainerTrait {adminNotificationResource.sendNotification(null, notification)then: "the notification should have been sent"- notificationIds.size() == 5+ conditions.eventually {+ assert notificationIds.size() == 5+ }when: "a regular user sends a push notification to a user in a different realm"notification.targets = [new Notification.Target(Notification.TargetType.USER, keycloakTestSetup.testuser2Id)]@@ -244,8 +250,11 @@ class NotificationTest extends Specification implements ManagerContainerTrait {advancePseudoClock(1, TimeUnit.HOURS, container)adminNotificationResource.sendNotification(null, notification)- then: "the notification should have been sent"- notificationIds.size() == 8+ then: "the notification should have been sent (not to testuser2)"+ conditions.eventually {+ assert notificationIds.size() == 8+ assert !notificationIds.contains(testuser2Console.id)+ }when: "a regular user sends a push notification to the console assets in a different realm"testuser1NotificationResource.sendNotification(null, notification)@@ -258,15 +267,19 @@ class NotificationTest extends Specification implements ManagerContainerTrait {advancePseudoClock(1, TimeUnit.HOURS, container)testuser2NotificationResource.sendNotification(null, notification)- then: "the notification should have been sent"- notificationIds.size() == 11+ then: "the notification should have been sent (not to testuser2)"+ conditions.eventually {+ assert notificationIds.size() == 11+ assert !notificationIds.contains(testuser2Console.id)+ }when: "a notification is sent using the same mechanism as an asset ruleset"notificationService.sendNotification(notification, Notification.Source.ASSET_RULESET, consoleResource.getConsoleParentAssetId(realm))- then: "the notification should have been sent"- new PollingConditions(timeout: 10, delay: 0.2).eventually {+ then: "the notification should have been sent (not to testuser2)"+ conditions.eventually {assert notificationIds.size() == 14+ assert !notificationIds.contains(testuser2Console.id)}when: "a restricted user sends a push notification to the console assets in the same realm"@@ -297,53 +310,50 @@ class NotificationTest extends Specification implements ManagerContainerTrait {testuser3NotificationResource.sendNotification(null, notification)then: "the notifications should have been sent"- notificationIds.size() == 16+ conditions.eventually {+ assert notificationIds.size() == 16+ }// -----------------------------------------------// Check notification resource// ------------------------------------------------ when: "the admin user requests the notifications for Building consoles"- List<SentNotification> notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser2Console.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))-- then: "all notifications sent to these consoles should be returned"- assert notifications.size() == 16- assert notifications.count {n ->- PushNotificationMessage pushMessage = n.message as PushNotificationMessage- pushMessage.getTitle() == "Test Action" &&- pushMessage.getBody() == "Click to cancel" &&- pushMessage.getAction() != null &&- n.deliveredOn == null &&- n.acknowledgedOn == null- } == 16+ and: "all notifications sent to consoles in the building realm should be available via the REST API"+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser2Console.id).length == 0+ notifications = adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id)+ assert notifications.length == 6+ assert notifications.every {n ->+ PushNotificationMessage pushMessage = n.message as PushNotificationMessage+ pushMessage.getTitle() == "Test Action" &&+ pushMessage.getBody() == "Click to cancel" &&+ pushMessage.getAction() != null &&+ n.deliveredOn == null &&+ n.acknowledgedOn == null+ }+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id).length == 6+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id).length == 4+ }when: "the admin user marks a Building console notification as delivered and requests the notifications for Building consoles"adminNotificationResource.notificationDelivered(null, testuser3Console1.id, notifications.find {n -> n.targetId == testuser3Console1.id}.id)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser2Console.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))then: "the notification should have been updated"- assert notifications.size() == 16- assert notifications.count {n -> n.deliveredOn != null} == 1+ conditions.eventually {+ notifications = adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id)+ assert notifications.length == 6+ assert notifications.count {n -> n.deliveredOn != null} == 1+ }when: "the admin user marks a Building console notification as delivered and requests the notifications for Building consoles"adminNotificationResource.notificationAcknowledged(null, testuser3Console1.id, notifications.find {n -> n.targetId == testuser3Console1.id && n.deliveredOn != null}.id, new TextNode("dismissed"))- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser2Console.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))then: "the notification should have been updated"- assert notifications.size() == 16- assert notifications.count {n -> n.deliveredOn != null && n.acknowledgedOn != null && n.acknowledgement == "\"dismissed\""} == 1+ conditions.eventually {+ notifications = adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id)+ assert notifications.length == 6+ assert notifications.count {n -> n.deliveredOn != null && n.acknowledgedOn != null && n.acknowledgement == "\"dismissed\""} == 1+ }when: "a regular user marks a console notification from another realm as delivered"testuser1NotificationResource.notificationDelivered(null, testuser3Console1.id, notifications.find {n -> n.targetId == testuser3Console1.id}.id)@@ -354,15 +364,11 @@ class NotificationTest extends Specification implements ManagerContainerTrait {when: "a regular user marks a console notification for their own console as delivered"testuser3NotificationResource.notificationDelivered(null, testuser3Console1.id, notifications.find {n -> n.targetId == testuser3Console1.id}.id)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser2Console.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))then: "the notification should have been updated"- assert notifications.size() == 16- assert notifications.count {n -> n.targetId == testuser3Console1.id && n.deliveredOn != null} == 1+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id).count {it.deliveredOn != null} == 1+ }// TODO: Update once console permissions model finalised// when: "an anonymous user marks a console notification from another console as delivered"@@ -373,16 +379,13 @@ class NotificationTest extends Specification implements ManagerContainerTrait {// ex.response.status == 403when: "an anonymous user marks a console notification for their own console as delivered"+ notifications = adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id)anonymousNotificationResource.notificationDelivered(null, anonymousConsole.id, notifications.find {n -> n.targetId == anonymousConsole.id}.id)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser2Console.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))then: "the notification should have been updated"- assert notifications.size() == 16- assert notifications.count {n -> n.targetId == anonymousConsole.id && n.deliveredOn != null} == 1+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id).count {it.deliveredOn != null} == 1+ }when: "a regular user tries to remove notifications"testuser1NotificationResource.removeNotifications(null, null, PushNotificationMessage.TYPE, null, null, MASTER_REALM, null, null)@@ -399,40 +402,34 @@ class NotificationTest extends Specification implements ManagerContainerTrait {ex.response.status == 403when: "the admin user removes notifications by timestamp and the notifications are retrieved again"- notifications = notifications.sort {n -> n.sentOn}.reverse(true)- adminNotificationResource.removeNotifications(null, null, PushNotificationMessage.TYPE, notifications[0].sentOn.getTime(), null, null, null, null)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser2Console.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))+ notifications = adminNotificationResource.getNotifications(null, null, null, null, null, null, null, null).reverse(true)+ def sentNotification = notifications[0]+ def removeCount = notifications.count {it.sentOn == sentNotification.sentOn}+ adminNotificationResource.removeNotifications(null, null, PushNotificationMessage.TYPE, sentNotification.sentOn.getTime(), null, null, null, null)then: "notifications sent after or at that time should have been removed"- assert notifications.size() == 14+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, null).length == (notifications.length - removeCount)+ }when: "the admin user removes notifications sent to specific console assets without other constraints and the notifications are retrieved again"adminNotificationResource.removeNotifications(null, null, null, null, null, null, null, testuser3Console1.id)adminNotificationResource.removeNotifications(null, null, null, null, null, null, null, testuser3Console2.id)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser2Console.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))then: "all notifications sent to those consoles should have been removed"- assert notifications.size() == 4- assert notifications.count {n -> n.targetId == testuser3Console1.id || n.targetId == testuser3Console2.id} == 0+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id).length == 0+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id).length == 0+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id).length == 4+ }when: "the admin user removes notifications by type and the notifications are retrieved again"adminNotificationResource.removeNotifications(null, null, PushNotificationMessage.TYPE, null, null, null, null, null)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser2Console.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))then: "the notifications should have been removed"- assert notifications.size() == 0+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id).length == 0+ }when: "the admin user removes notifications without sufficient constraints"adminNotificationResource.removeNotifications(null, null, null, null, null, null, null, null)@@ -454,87 +451,83 @@ class NotificationTest extends Specification implements ManagerContainerTrait {advancePseudoClock(ChronoUnit.MILLIS.between(Instant.ofEpochMilli(getClockTimeOf(container)), advancement), TimeUnit.MILLISECONDS, container)notification.setRepeatFrequency(RepeatFrequency.HOURLY)testuser3NotificationResource.sendNotification(null, notification)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))- then: "the notifications should have been sent"- assert notifications.size() == 2+ then: "the notifications should have been sent (to testuser3 consoles)"+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id).length == 1+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id).length == 1+ }when: "a repeat frequency is set and a notification with the same name and scope as a previous notification is sent within the repeat window"testuser3NotificationResource.sendNotification(null, notification)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))then: "no new notifications should have been sent"- assert notifications.size() == 2+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id).length == 1+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id).length == 1+ }when: "a repeat frequency is set and a notification with the same name but different scope is sent within the repeat window"adminNotificationResource.sendNotification(null, notification)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))then: "new notifications should have been sent"- assert notifications.size() == 4+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id).length == 2+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id).length == 2+ }when: "time advances less than the repeat frequency and the notification is sent again"advancePseudoClock(30, TimeUnit.SECONDS, container)adminNotificationResource.sendNotification(null, notification)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))then: "no new notifications should have been sent"- assert notifications.size() == 4+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id).length == 2+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id).length == 2+ }when: "time advances more than the repeat frequency and the notification is sent again"advancePseudoClock(2, TimeUnit.MINUTES, container)adminNotificationResource.sendNotification(null, notification)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))then: "new notifications should have been sent"- assert notifications.size() == 6+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id).length == 3+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id).length == 3+ }when: "a repeat interval is used and a notification with the same name and scope is sent within the repeat window"notification.setRepeatInterval("P1M")advancePseudoClock(10, TimeUnit.DAYS, container)adminNotificationResource.sendNotification(null, notification)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))then: "no new notifications should have been sent"- assert notifications.size() == 6+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id).length == 3+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id).length == 3+ }when: "time advances more than the repeat interval and the notification is sent again"advancePseudoClock(25, TimeUnit.DAYS, container)adminNotificationResource.sendNotification(null, notification)- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id))then: "new notifications should have been sent"- assert notifications.size() == 8-- when: "notifications are retrieved only for the past day"- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, getClockTimeOf(container)-(3600000*24), null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, getClockTimeOf(container)-(3600000*24), null, null, null, testuser3Console2.id))-- then: "only the relevant notifications should have been returned"- assert notifications.size() == 2+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id).length == 4+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console2.id).length == 4+ }- when: "notifications are retrieved for the past 40 days"- notifications = []- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, getClockTimeOf(container)-(3600000L*24*40), null, null, null, testuser3Console1.id))- notifications.addAll(adminNotificationResource.getNotifications(null, null, null, getClockTimeOf(container)-(3600000L*24*40), null, null, null, testuser3Console2.id))+ and: "notifications are retrieved only for the past day only the relevant notifications should have been returned"+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, getClockTimeOf(container)-(3600000*24), null, null, null, testuser3Console1.id).length == 1+ assert adminNotificationResource.getNotifications(null, null, null, getClockTimeOf(container)-(3600000*24), null, null, null, testuser3Console2.id).length == 1+ }- then: "only the relevant notifications should have been returned"- assert notifications.size() == 8+ and: "notifications are retrieved only for the past 40 days only the relevant notifications should have been returned"+ conditions.eventually {+ assert adminNotificationResource.getNotifications(null, null, null, getClockTimeOf(container)-(3600000L*24*40), null, null, null, testuser3Console1.id).length == 4+ assert adminNotificationResource.getNotifications(null, null, null, getClockTimeOf(container)-(3600000L*24*40), null, null, null, testuser3Console2.id).length == 4+ }cleanup: "the mock is removed"notificationService.notificationHandlerMap.put(pushNotificationHandler.getTypeName(), pushNotificationHandler)