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
Notification bug fixes
Rich Turnerbc0216ba39f8
diff --git a/.github/workflows/provision-account.yml b/.github/workflows/provision-account.ymlindex aaaae3641..06f478fc1 100644--- a/.github/workflows/provision-account.yml+++ b/.github/workflows/provision-account.yml@@ -3,7 +3,7 @@# You need to be a member of the CI/CD github team to use this action as it will incur costs for the organisation.# ----------------------------------------------------------------------------------------------------------------------name: Provision Host+name: Provision Accounton:workflow_dispatch:diff --git a/.github/workflows/provision-host.yml b/.github/workflows/provision-host.ymlindex e22abda22..25120abc5 100644--- a/.github/workflows/provision-host.yml+++ b/.github/workflows/provision-host.yml@@ -27,7 +27,7 @@ on:- m6g.largerequired: truePROVISION_S3_BUCKET:- description: 'Create S3 bucket with name of HOST'+ description: 'Create S3 bucket for backups'type: booleandefault: truerequired: falsediff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex 79133363f..633c0043a 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -1225,6 +1225,26 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicif (query.access == null)query.access = PRIVATE;+ // Do some sanity checks on query values and return empty result set if empty query parameters+ if (query.ids != null && query.ids.length == 0) {+ return Collections.emptyList();+ }+ if (query.paths != null && query.paths.length == 0) {+ return Collections.emptyList();+ }+ if (query.types != null && query.types.length == 0) {+ return Collections.emptyList();+ }+ if (query.names != null && query.names.length == 0) {+ return Collections.emptyList();+ }+ if (query.userIds != null && query.userIds.length == 0) {+ return Collections.emptyList();+ }+ if (query.parents != null && query.parents.length == 0) {+ return Collections.emptyList();+ }+// Default to order by creation date if the query may return multiple resultsif (query.orderBy == null && query.ids == null)query.orderBy = new OrderBy(OrderBy.Property.CREATED_ON);@@ -1573,7 +1593,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicsb.append(" from top_level_assets A ");}- if ((!recursive || level == 3) && query.userIds != null && query.userIds.length > 0) {+ if ((!recursive || level == 3) && query.userIds != null) {sb.append("right join USER_ASSET_LINK UA on A.ID = UA.ASSET_ID ");}@@ -1633,7 +1653,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicreturn false;}- if (level == 1 && query.ids != null && query.ids.length > 0) {+ if (level == 1 && query.ids != null) {final int pos = binders.size() + 1;sb.append(" and A.ID = ANY(?").append(pos)@@ -1641,7 +1661,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicbinders.add((em, st) -> st.setParameter(pos, query.ids, StringArrayType.INSTANCE));}- if (level == 1 && query.names != null && query.names.length > 0) {+ if (level == 1 && query.names != null) {sb.append(" and (");boolean isFirst = true;@@ -1659,7 +1679,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicsb.append(")");}- if (query.parents != null && query.parents.length > 0) {+ if (query.parents != null) {sb.append(" and (");boolean isFirst = true;@@ -1688,7 +1708,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicsb.append(")");}- if (level == 1 && query.paths != null && query.paths.length > 0) {+ if (level == 1 && query.paths != null) {sb.append(" and (");Arrays.stream(query.paths).map(p -> String.join(".", p.path))@@ -1708,7 +1728,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicbinders.add((em, st) -> st.setParameter(pos, query.tenant.realm));}- if (query.userIds != null && query.userIds.length > 0) {+ if (query.userIds != null) {final int pos = binders.size() + 1;sb.append(" and UA.USER_ID = ANY(?").append(pos)@@ -1720,7 +1740,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicsb.append(" and A.ACCESS_PUBLIC_READ is true");}- if (query.types != null && query.types.length > 0) {+ if (query.types != null) {String[] resolvedTypes = getResolvedAssetTypes(query.types);final int pos = binders.size() + 1;sb.append(" and A.TYPE = ANY(?")diff --git a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.javaindex 4c27b1735..6f698c7a0 100644--- a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java+++ b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java@@ -145,14 +145,15 @@ public class EmailNotificationHandler implements NotificationHandler {targets.forEach(target -> {Notification.TargetType targetType = target.getType();String targetId = target.getId();- UserQuery userQuery = new UserQuery();+ UserQuery userQuery = null;switch (targetType) {case TENANT:- userQuery.tenant(new TenantPredicate(targetId));+ userQuery = new UserQuery().tenant(new TenantPredicate(targetId));+ break;case USER:- userQuery.ids(targetId);+ userQuery = new UserQuery().ids(targetId);break;case CUSTOM:// Nothing to do here@@ -170,26 +171,32 @@ public class EmailNotificationHandler implements NotificationHandler {).ifPresent(mappedTargets::add);}- userQuery.asset(new UserAssetPredicate(targetId));+ userQuery = new UserQuery().asset(new UserAssetPredicate(targetId));break;}// Filter users that don't have disabled email notifications attribute- List<Notification.Target> userTargets = Arrays.stream(managerIdentityService- .getIdentityProvider()- .queryUsers(userQuery))- .filter(user -> !Boolean.parseBoolean(user.getAttributes().getOrDefault(KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED, Collections.singletonList("false")).get(0)))- .filter(user -> !TextUtil.isNullOrEmpty(user.getEmail()))- .map(user -> {- Notification.Target emailTarget = new Notification.Target(Notification.TargetType.USER, user.getId());- emailTarget.setData(new EmailNotificationMessage.Recipient(user.getFullName(), user.getEmail()));- return emailTarget;- }).toList();-- if (userTargets.isEmpty()) {- LOG.info("No email targets have been mapped");- } else {- mappedTargets.addAll(userTargets);+ if (userQuery != null) {+ List<Notification.Target> userTargets = Arrays.stream(managerIdentityService+ .getIdentityProvider()+ .queryUsers(userQuery))+ .filter(user -> !Boolean.parseBoolean(user.getAttributes().getOrDefault(KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED, Collections.singletonList("false")).get(0)))+ .filter(user -> !TextUtil.isNullOrEmpty(user.getEmail()))+ .map(user -> {+ Notification.Target emailTarget = new Notification.Target(Notification.TargetType.USER, user.getId());+ emailTarget.setData(new EmailNotificationMessage.Recipient(user.getFullName(), user.getEmail()));+ return emailTarget;+ }).toList();++ if (userTargets.isEmpty()) {+ LOG.info("No email targets have been mapped");+ } else {+ mappedTargets.addAll(+ userTargets+ .stream()+ .filter(userTarget -> mappedTargets.stream().noneMatch(t -> t.getId().equals(userTarget.getId())))+ .toList());+ }}});}diff --git a/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.javaindex 466f769b7..b00ebe0ea 100644--- a/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java+++ b/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java@@ -46,7 +46,6 @@ import org.openremote.model.query.AssetQuery;import org.openremote.model.query.UserQuery;import org.openremote.model.query.filter.AttributePredicate;import org.openremote.model.query.filter.NameValuePredicate;-import org.openremote.model.query.filter.PathPredicate;import org.openremote.model.query.filter.TenantPredicate;import org.openremote.model.security.User;import org.openremote.model.util.TextUtil;@@ -200,6 +199,27 @@ public class PushNotificationHandler extends RouteBuilder implements Notificatioif (targets != null) {+ // Filter out console targets+ String[] assetTargets = targets.stream().filter(target -> target.getType() == Notification.TargetType.ASSET)+ .map(Notification.Target::getId).toArray(String[]::new);++ if (assetTargets.length > 0) {+ List<String> consoleAssets = assetStorageService.findAll(new AssetQuery().ids(assetTargets)+ .select(new AssetQuery.Select().excludeAttributes()).types(ConsoleAsset.class))+ .stream().map(Asset::getId).toList();++ if (!consoleAssets.isEmpty()) {+ targets = targets.stream()+ .filter(target -> {+ boolean isConsoleAsset = consoleAssets.contains(target.getId());+ if (isConsoleAsset) {+ mappedTargets.add(new Notification.Target(Notification.TargetType.ASSET, target.getId()));+ }+ return !isConsoleAsset;+ }).collect(Collectors.toList());+ }+ }+targets.forEach(target -> {Notification.TargetType targetType = target.getType();@@ -267,6 +287,7 @@ public class PushNotificationHandler extends RouteBuilder implements NotificatiomappedTargets.addAll(consoleAssetIds.stream()+ .filter(id -> mappedTargets.stream().noneMatch(t -> t.getId().equals(id))).map(id -> new Notification.Target(Notification.TargetType.ASSET, id)).toList());}diff --git a/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy b/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovyindex 82af64d89..94f0b243c 100644--- a/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy+++ b/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy@@ -428,6 +428,8 @@ class ConsoleTest extends Specification implements ManagerContainerTrait {///////////////////////////////////////when: "the geofences of a user linked console are requested by the authenticated user"+ notificationIds.clear()+ messages.clear()def geofences = authenticatedRulesResource.getAssetGeofences(null, testUser3Console1.id)def expectedLocationPredicate = new RadialGeofencePredicate(100, SMART_BUILDING_LOCATION.y, SMART_BUILDING_LOCATION.x)diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovyindex cc35ba256..ee493c9c4 100644--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy@@ -240,6 +240,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {ex.response.status == 400when: "the admin user sends a push notification to the console assets in building realm"+ notificationIds.clear()notification.targets = [new Notification.Target(Notification.TargetType.ASSET, testuser2Console.id),new Notification.Target(Notification.TargetType.ASSET, testuser3Console1.id),new Notification.Target(Notification.TargetType.ASSET, testuser3Console2.id),@@ -247,10 +248,13 @@ class NotificationTest extends Specification implements ManagerContainerTrait {advancePseudoClock(1, TimeUnit.HOURS, container)adminNotificationResource.sendNotification(null, notification)- then: "the notification should have been sent (not to testuser2)"+ then: "the notification should have been sent (inc. testuser2 as message was direct to console)"conditions.eventually {- assert notificationIds.size() == 8- assert !notificationIds.contains(testuser2Console.id)+ assert notificationIds.size() == 4+ assert notificationTargetIds.contains(testuser2Console.id)+ assert notificationTargetIds.contains(testuser3Console1.id)+ assert notificationTargetIds.contains(testuser3Console2.id)+ assert notificationTargetIds.contains(anonymousConsole.id)}when: "a regular user sends a push notification to the console assets in a different realm"@@ -261,22 +265,30 @@ class NotificationTest extends Specification implements ManagerContainerTrait {ex.response.status == 400when: "a regular user sends a push notification to the console assets in the same realm"+ notificationIds.clear()advancePseudoClock(1, TimeUnit.HOURS, container)testuser2NotificationResource.sendNotification(null, notification)- then: "the notification should have been sent (not to testuser2)"+ then: "the notification should have been sent (inc. testuser2 as message was direct to console)"conditions.eventually {- assert notificationIds.size() == 11- assert !notificationIds.contains(testuser2Console.id)+ assert notificationIds.size() == 4+ assert notificationTargetIds.contains(testuser2Console.id)+ assert notificationTargetIds.contains(testuser3Console1.id)+ assert notificationTargetIds.contains(testuser3Console2.id)+ assert notificationTargetIds.contains(anonymousConsole.id)}when: "a notification is sent using the same mechanism as an asset ruleset"+ notificationIds.clear()notificationService.sendNotification(notification, Notification.Source.ASSET_RULESET, consoleResource.getConsoleParentAssetId(realm))- then: "the notification should have been sent (not to testuser2)"+ then: "the notification should have been sent (inc. testuser2 as message was direct to console)"conditions.eventually {- assert notificationIds.size() == 14- assert !notificationIds.contains(testuser2Console.id)+ assert notificationIds.size() == 4+ assert notificationTargetIds.contains(testuser2Console.id)+ assert notificationTargetIds.contains(testuser3Console1.id)+ assert notificationTargetIds.contains(testuser3Console2.id)+ assert notificationTargetIds.contains(anonymousConsole.id)}when: "a restricted user sends a push notification to the console assets in the same realm"@@ -287,6 +299,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {ex.response.status == 400when: "a restricted user sends a push notification to some consoles linked to them and some not linked to them"+ notificationIds.clear()notification.targets = [new Notification.Target(Notification.TargetType.ASSET, testuser2Console.id),new Notification.Target(Notification.TargetType.ASSET, testuser3Console1.id),new Notification.Target(Notification.TargetType.ASSET, testuser3Console2.id),@@ -298,7 +311,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {ex.response.status == 400and: "no new notifications should have been sent"- notificationIds.size() == 14+ notificationIds.size() == 0when: "a restricted user sends a push notification to some consoles linked to them"advancePseudoClock(1, TimeUnit.HOURS, container)@@ -308,7 +321,9 @@ class NotificationTest extends Specification implements ManagerContainerTrait {then: "the notifications should have been sent"conditions.eventually {- assert notificationIds.size() == 16+ assert notificationIds.size() == 2+ assert notificationTargetIds.contains(testuser3Console1.id)+ assert notificationTargetIds.contains(testuser3Console2.id)}// -----------------------------------------------@@ -317,7 +332,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {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+ assert adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser2Console.id).length == 3notifications = adminNotificationResource.getNotifications(null, null, null, null, null, null, null, testuser3Console1.id)assert notifications.length == 6assert notifications.every {n ->@@ -573,7 +588,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {then: "the email should have been sent to the tenant users"conditions.eventually {- assert sentEmails.size() >= 2+ assert sentEmails.size() == 2assert sentEmails.every {it.getPlainText() == "Hello world!"}assert sentEmails.every {it.getSubject() == "Test"}assert sentEmails.any { it.getRecipients().size() == 1 && it.getRecipients().get(0).address == "[email protected]"}@@ -582,30 +597,32 @@ class NotificationTest extends Specification implements ManagerContainerTrait {}when: "an email attribute is added to an asset"+ sentEmails.clear()def kitchen = assetStorageService.find(managerTestSetup.apartment1KitchenId)kitchen.setEmail("[email protected]")kitchen = assetStorageService.merge(kitchen)and: "an email notification is sent to a parent asset"((EmailNotificationMessage)notification.message).subject = "Test 2"- notification.setTargets([new Notification.Target(Notification.TargetType.ASSET, managerTestSetup.apartment1Id)])+ notification.setTargets([new Notification.Target(Notification.TargetType.ASSET, managerTestSetup.apartment1KitchenId)])notificationService.sendNotification(notification)then: "the child asset with the email attribute should have been sent an email"conditions.eventually {- assert sentEmails.size() >= 3+ assert sentEmails.size() == 1assert sentEmails.any { it.getSubject() == "Test 2"}assert sentEmails.any { it.getRecipients().size() == 1 && it.getRecipients().get(0).address == "[email protected]"}}when: "an email is sent to a custom target"+ sentEmails.clear()((EmailNotificationMessage)notification.message).subject = "Test Custom"notification.setTargets([new Notification.Target(Notification.TargetType.CUSTOM, "[email protected];to:[email protected];cc:[email protected];bcc:[email protected]")])notificationService.sendNotification(notification)then: "the email should have been sent to all custom recipients"conditions.eventually {- assert sentEmails.size() >= 4+ assert sentEmails.size() == 1assert sentEmails.any { it.getSubject() == "Test Custom" && it.getRecipients().size() == 4}assert sentEmails.any { it.getSubject() == "Test Custom" && it.getRecipients().any{it.type == javax.mail.Message.RecipientType.TO && it.address == "[email protected]"}}assert sentEmails.any { it.getSubject() == "Test Custom" && it.getRecipients().any{it.type == javax.mail.Message.RecipientType.TO && it.address == "[email protected]"}}diff --git a/test/src/testFixtures/groovy/org/openremote/test/ContainerTrait.groovy b/test/src/testFixtures/groovy/org/openremote/test/ContainerTrait.groovyindex 85525ef3e..9a0a3e784 100644--- a/test/src/testFixtures/groovy/org/openremote/test/ContainerTrait.groovy+++ b/test/src/testFixtures/groovy/org/openremote/test/ContainerTrait.groovy@@ -27,6 +27,7 @@ import org.jboss.resteasy.client.jaxrs.ResteasyWebTargetimport org.keycloak.representations.AccessTokenResponseimport org.openremote.container.Containerimport org.openremote.container.message.MessageBrokerService+import org.openremote.container.persistence.PersistenceServiceimport org.openremote.container.security.IdentityServiceimport org.openremote.container.security.PasswordAuthFormimport org.openremote.container.security.keycloak.KeycloakIdentityProvider@@ -56,6 +57,7 @@ import org.openremote.model.rules.Rulesetimport org.openremote.model.rules.TenantRulesetimport org.openremote.model.security.User+import javax.persistence.TypedQueryimport javax.websocket.ClientEndpointConfigimport javax.websocket.Endpointimport javax.websocket.Session@@ -385,7 +387,10 @@ trait ContainerTrait {if (!container.hasService(AssetStorageService.class)) {return Collections.emptyList()}- container.getService(AssetStorageService.class).findUserAssetLinks(null, null, null)+ return container.getService(PersistenceService.class).doReturningTransaction {+ TypedQuery<UserAssetLink> query = it.createQuery("select ua from UserAssetLink ua", UserAssetLink.class)+ return query.getResultList()+ }}List<GatewayConnection> getGatewayConnections() {