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
AssetStorageService isDescendantAssets bug fix
Rich Turner6197686bd840
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex d02eb6074..1780f7790 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -67,6 +67,7 @@ import java.sql.*;import java.util.*;import java.util.Date;import java.util.function.Consumer;+import java.util.logging.Level;import java.util.logging.Logger;import java.util.stream.Stream;@@ -480,18 +481,20 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic}public boolean isDescendantAssets(String parentAssetId, List<String> assetIds) {- return persistenceService.doReturningTransaction(entityManager -> {- try {- return entityManager.createQuery(- "select count(a) from Asset a where :parentAssetId = a.PATH AND a.id IN :assetIds",- Long.class)- .setParameter("parentAssetId", parentAssetId)- .setParameter("assetIds", assetIds)- .getSingleResult() == assetIds.size();- } catch (NoResultException ex) {- return false;+ return persistenceService.doReturningTransaction(entityManager -> entityManager.unwrap(Session.class).doReturningWork(new AbstractReturningWork<Boolean>() {+ @Override+ public Boolean execute(Connection connection) throws SQLException {+ try (PreparedStatement st = connection.prepareStatement("select count(*) from Asset a where ? = ANY(get_asset_tree_path(a.ID)) AND a.id = ANY(?)")) {+ st.setString(1, parentAssetId);+ st.setArray(2, st.getConnection().createArrayOf("text", assetIds.toArray()));+ ResultSet rs = st.executeQuery();+ return rs.next() && rs.getInt(1) == assetIds.size();+ } catch (SQLException ex) {+ LOG.log(Level.SEVERE, "Failed to execute isDescendantAssets query", ex);+ return false;+ }}- });+ }));}public List<UserAsset> findUserAssets(String realmId, String userId, String assetId) {diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovyindex 4e1ad2231..81a2dabfc 100644--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy@@ -1,7 +1,9 @@package org.openremote.test.notificationimport com.google.firebase.messaging.Message+import org.openremote.container.web.WebServiceimport org.openremote.manager.asset.AssetStorageService+import org.openremote.manager.asset.console.ConsoleResourceImplimport org.openremote.manager.notification.EmailNotificationHandlerimport org.openremote.manager.notification.NotificationServiceimport org.openremote.manager.notification.PushNotificationHandler@@ -71,6 +73,8 @@ class NotificationTest extends Specification implements ManagerContainerTrait {def container = startContainerWithPseudoClock(defaultConfig(serverPort), services)def keycloakDemoSetup = container.getService(SetupService.class).getTaskOfType(KeycloakDemoSetup.class)def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)+ def notificationService = container.getService(NotificationService.class)+ def consoleResource = (ConsoleResourceImpl)container.getService(WebService.class).getApiSingletons().find {it instanceof ConsoleResourceImpl}and: "an authenticated test user"def realm = keycloakDemoSetup.customerATenant.realm@@ -240,6 +244,14 @@ class NotificationTest extends Specification implements ManagerContainerTrait {then: "the notification should have been sent"notificationIds.size() == 13+ 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).eventually {+ assert notificationIds.size() == 17+ }+when: "a restricted user sends a push notification to the console assets in the same realm"testuser3NotificationResource.sendNotification(null, notification)@@ -260,7 +272,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {ex.response.status == 403and: "no new notifications should have been sent"- notificationIds.size() == 13+ notificationIds.size() == 17when: "a restricted user sends a push notification to some consoles linked to them"advancePseudoClock(1, TimeUnit.HOURS, container)@@ -270,7 +282,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {testuser3NotificationResource.sendNotification(null, notification)then: "the notifications should have been sent"- notificationIds.size() == 15+ notificationIds.size() == 19// -----------------------------------------------// Check notification resource@@ -284,14 +296,14 @@ class NotificationTest extends Specification implements ManagerContainerTrait {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() == 15+ assert notifications.size() == 19assert notifications.count {n ->n.message.getString("title").orElse(null) == "Test Action" &&n.message.getString("body").orElse(null) == "Click to cancel" &&n.message.getObject("action").isPresent() &&n.deliveredOn == null &&n.acknowledgedOn == null- } == 15+ } == 19when: "the admin user marks a customer A console notification as delivered and requests the notifications for customer A consoles"adminNotificationResource.notificationDelivered(null, testuser2Console.id, notifications.find {n -> n.targetId == testuser2Console.id}.id)@@ -302,7 +314,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))then: "the notification should have been updated"- assert notifications.size() == 15+ assert notifications.size() == 19assert notifications.count {n -> n.deliveredOn != null} == 1when: "the admin user marks a customer A console notification as delivered and requests the notifications for customer A consoles"@@ -314,7 +326,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))then: "the notification should have been updated"- assert notifications.size() == 15+ assert notifications.size() == 19assert notifications.count {n -> n.deliveredOn != null && n.acknowledgedOn != null && n.acknowledgement == "dismissed"} == 1when: "a regular user marks a console notification from another realm as delivered"@@ -333,7 +345,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))then: "the notification should have been updated"- assert notifications.size() == 15+ assert notifications.size() == 19assert notifications.count {n -> n.targetId == testuser3Console1.id && n.deliveredOn != null} == 1// TODO: Update once console permissions model finalised@@ -353,7 +365,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))then: "the notification should have been updated"- assert notifications.size() == 15+ assert notifications.size() == 19assert notifications.count {n -> n.targetId == anonymousConsole.id && n.deliveredOn != null} == 1when: "a regular user tries to remove notifications"@@ -380,7 +392,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {notifications.addAll(adminNotificationResource.getNotifications(null, null, null, null, null, null, null, anonymousConsole.id))then: "notifications sent after or at that time should have been removed"- assert notifications.size() == 13+ assert notifications.size() == 17when: "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)@@ -392,7 +404,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {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() == 7+ assert notifications.size() == 9assert notifications.count {n -> n.targetId == testuser3Console1.id || n.targetId == testuser3Console2.id} == 0when: "the admin user removes notifications by type and the notifications are retrieved again"@@ -403,7 +415,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {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: "one notification should have been removed"+ then: "the notifications should have been removed"assert notifications.size() == 0when: "the admin user removes notifications without sufficient constraints"