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 test robustness
Rich Turnere6aa0d77a436
diff --git a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.javaindex 2941aacf4..042af7e98 100644--- a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java+++ b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java@@ -19,10 +19,10 @@*/package org.openremote.manager.notification;-import org.openremote.model.Container;-import org.openremote.model.ContainerService;import org.openremote.manager.asset.AssetStorageService;import org.openremote.manager.security.ManagerIdentityService;+import org.openremote.model.Container;+import org.openremote.model.ContainerService;import org.openremote.model.asset.Asset;import org.openremote.model.notification.AbstractNotificationMessage;import org.openremote.model.notification.EmailNotificationMessage;@@ -41,7 +41,10 @@ import org.simplejavamail.mailer.Mailer;import org.simplejavamail.mailer.MailerBuilder;import org.simplejavamail.mailer.config.TransportStrategy;-import java.util.*;+import java.util.ArrayList;+import java.util.Arrays;+import java.util.Collections;+import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;import java.util.stream.Collectors;@@ -57,20 +60,6 @@ public class EmailNotificationHandler implements NotificationHandler {protected Mailer mailer;protected ManagerIdentityService managerIdentityService;protected AssetStorageService assetStorageService;- // Keep 100 user email addresses in cache for quick lookup- protected LinkedHashMap<String, EmailNotificationMessage.Recipient> userEmails = new LinkedHashMap<String, EmailNotificationMessage.Recipient>(100) {- @Override- protected boolean removeEldestEntry(Map.Entry<String, EmailNotificationMessage.Recipient> eldest) {- return size() > 100;- }- };- // Keep 1000 asset email addresses in cache for quick lookup- protected LinkedHashMap<String, EmailNotificationMessage.Recipient> assetEmails = new LinkedHashMap<String, EmailNotificationMessage.Recipient>(1000) {- @Override- protected boolean removeEldestEntry(Map.Entry<String, EmailNotificationMessage.Recipient> eldest) {- return size() > 1000;- }- };@Overridepublic int getPriority() {@@ -160,28 +149,34 @@ public class EmailNotificationHandler implements NotificationHandler {switch (targetType) {case TENANT:- // Find all users in this tenant- User[] users = managerIdentityService- .getIdentityProvider()- .getUsers(new UserQuery().tenant(new TenantPredicate(targetId)));+ case USER:+ // Find all users in this tenant or by id+ User[] users = targetType == Notification.TargetType.TENANT+ ? managerIdentityService+ .getIdentityProvider()+ .getUsers(new UserQuery().tenant(new TenantPredicate(targetId)))+ : managerIdentityService+ .getIdentityProvider()+ .getUsers(Collections.singletonList(targetId));if (users.length == 0) {- LOG.fine("No users found in target realm: " + targetId);+ if (targetType == Notification.TargetType.USER) {+ LOG.info("User not found: " + targetId);+ } else {+ LOG.info("No users found in target realm: " + targetId);+ }return;}- Arrays.stream(users).forEach(user -> {- EmailNotificationMessage.Recipient recipient =- new EmailNotificationMessage.Recipient(user.getFullName(), user.getEmail());- userEmails.put(user.getId(), recipient);- });-mappedTargets.addAll(Arrays.stream(users)- .map(user -> new Notification.Target(Notification.TargetType.USER, user.getId()))+ .map(user -> {+ Notification.Target userAssetTarget = new Notification.Target(Notification.TargetType.USER, user.getId());+ userAssetTarget.setData(new EmailNotificationMessage.Recipient(user.getFullName(), user.getEmail()));+ return userAssetTarget;+ }).collect(Collectors.toList()));break;- case USER:case CUSTOM:// Nothing to do heremappedTargets.add(new Notification.Target(targetType, targetId));@@ -202,17 +197,15 @@ public class EmailNotificationHandler implements NotificationHandler {return;}- assets.forEach(asset -> {- EmailNotificationMessage.Recipient recipient =- new EmailNotificationMessage.Recipient(+ mappedTargets.addAll(assets.stream()+ .map(asset -> {+ Notification.Target assetTarget =new Notification.Target(Notification.TargetType.ASSET, asset.getId());+ assetTarget.setData(new EmailNotificationMessage.Recipient(asset.getName(),asset.getEmail()- .orElse(null));- assetEmails.put(asset.getId(), recipient);- });-- mappedTargets.addAll(assets.stream()- .map(asset -> new Notification.Target(Notification.TargetType.ASSET, asset.getId()))+ .orElse(null)));+ return assetTarget;+ }).collect(Collectors.toList()));break;}@@ -275,41 +268,35 @@ public class EmailNotificationHandler implements NotificationHandler {switch (targetType) {case USER:- EmailNotificationMessage.Recipient userRecipient = getUserRecipient(targetId);- if (userRecipient == null) {- LOG.warning("Failed to find user: id=" + targetId);- } else {- LOG.finest("Adding to recipient: " + userRecipient);- toRecipients.add(userRecipient);- }- break;case ASSET:- EmailNotificationMessage.Recipient assetRecipient = getAssetRecipient(targetId);- if (assetRecipient == null) {- LOG.warning("Failed to find asset: id=" + targetId);+ // Recipient should be stored from earlier mapping call+ EmailNotificationMessage.Recipient recipient = (EmailNotificationMessage.Recipient)target.getData();++ if (recipient == null) {+ LOG.warning("User or asset recipient missing: id=" + targetId);} else {- LOG.finest("Adding to recipient: " + assetRecipient);- toRecipients.add(assetRecipient);+ LOG.finest("Adding to recipient: " + recipient);+ toRecipients.add(recipient);}break;case CUSTOM:// This recipient list is the target ID- Arrays.stream(targetId.split(";")).forEach(recipient -> {- if (recipient.startsWith("to:")) {- String email = recipient.substring(3);+ Arrays.stream(targetId.split(";")).forEach(customRecipient -> {+ if (customRecipient.startsWith("to:")) {+ String email = customRecipient.substring(3);LOG.finest("Adding to recipient: " + email);toRecipients.add(new EmailNotificationMessage.Recipient(email));- } else if (recipient.startsWith("cc:")) {- String email = recipient.substring(3);+ } else if (customRecipient.startsWith("cc:")) {+ String email = customRecipient.substring(3);LOG.finest("Adding cc recipient: " + email);ccRecipients.add(new EmailNotificationMessage.Recipient(email));- } else if (recipient.startsWith("bcc:")) {- String email = recipient.substring(4);+ } else if (customRecipient.startsWith("bcc:")) {+ String email = customRecipient.substring(4);LOG.finest("Adding bcc recipient: " + email);bccRecipients.add(new EmailNotificationMessage.Recipient(email));} else {- LOG.finest("Adding to recipient: " + recipient);- toRecipients.add(new EmailNotificationMessage.Recipient(recipient));+ LOG.finest("Adding to recipient: " + customRecipient);+ toRecipients.add(new EmailNotificationMessage.Recipient(customRecipient));}});@@ -365,31 +352,6 @@ public class EmailNotificationHandler implements NotificationHandler {}}- protected EmailNotificationMessage.Recipient getUserRecipient(String userId) {- if (userEmails.containsKey(userId)) {- return userEmails.get(userId);- }-- User[] users = managerIdentityService.getIdentityProvider().getUsers(Collections.singletonList(userId));- if (users == null || users.length == 0) {- return null;- }- return new EmailNotificationMessage.Recipient(users[0].getFullName(), users[0].getEmail());- }-- protected EmailNotificationMessage.Recipient getAssetRecipient(String assetId) {- if (assetEmails.containsKey(assetId)) {- return assetEmails.get(assetId);- }-- Asset<?> asset = assetStorageService.find(assetId);- if (asset == null) {- return null;- }-- return new EmailNotificationMessage.Recipient(asset.getName(), asset.getEmail().orElse(null));- }-protected EmailPopulatingBuilder buildEmailBuilder(long id, EmailNotificationMessage emailNotificationMessage) {EmailPopulatingBuilder emailBuilder = EmailBuilder.startingBlank().withReplyTo(convertRecipient(emailNotificationMessage.getReplyTo()))diff --git a/model/src/main/java/org/openremote/model/notification/Notification.java b/model/src/main/java/org/openremote/model/notification/Notification.javaindex 52937a5a2..2be34dc4d 100644--- a/model/src/main/java/org/openremote/model/notification/Notification.java+++ b/model/src/main/java/org/openremote/model/notification/Notification.java@@ -46,6 +46,7 @@ public class Notification {protected TargetType type;protected String id;+ protected Object data; // Handlers can store temporary data here@JsonCreatorpublic Target(@JsonProperty("type") TargetType type, @JsonProperty("id") String id) {@@ -65,6 +66,15 @@ public class Notification {return id;}+ public Target setData(Object data) {+ this.data = data;+ return this;+ }++ public Object getData() {+ return data;+ }+@Overridepublic String toString() {return getClass().getSimpleName()+ "{" +diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovyindex 8a91cff84..c05c22ba8 100644--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy@@ -44,7 +44,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}}- def conditions = new PollingConditions(timeout: 10, delay: 0.2)+ def conditions = new PollingConditions(timeout: 20, delay: 0.2)def services = Lists.newArrayList(defaultServices())services.replaceAll { it instanceof MqttBrokerService ? spyMqttBrokerService : it }def container = startContainer(defaultConfig(), services)@@ -303,10 +303,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetAttributeSubscriptions.containsKey(new AttributeRef(managerTestSetup.apartment1HallwayId, "motionSensor"))}- when: "time advances"- advancePseudoClock(1, TimeUnit.SECONDS, container)-- and: "that attribute changed"+ when: "that attribute changed"attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 30)((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)@@ -366,10 +363,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetAttributeValueSubscriptions.containsKey(new AttributeRef(managerTestSetup.apartment1HallwayId, "motionSensor"))}- when: "time advances"- advancePseudoClock(1, TimeUnit.SECONDS, container)-- and: "that attribute changed"+ when: "that attribute changed"attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 40)((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)diff --git a/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovyindex 9121036ac..a8c4c2f2a 100644--- a/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy@@ -268,13 +268,10 @@ class HttpServerProtocolTest extends Specification implements ManagerContainerTr}def tryPost(TestResource authenticatedTestResource, Asset testAsset) {- def exception = null+ // There's some weird behaviour where sometimes a forbidden exception is thrown and sometimes nottry {authenticatedTestResource.postAsset(testAsset)- } catch (Exception e) {- exception = e- }- return exception+ } catch (Exception ignored) {}}def "Check HTTP server protocol and JAX-RS deployment"() {@@ -410,7 +407,7 @@ class HttpServerProtocolTest extends Specification implements ManagerContainerTrassert asset.getLocation().map{it.coordinates.y}.orElse(-1d) == testAsset.getLocation().map{it.coordinates.y}.orElse(0d)when: "an agent is deleted"- def protocolInstance = (TestHttpServerProtocol)agentService.getProtocolInstance(agent.id)+ def protocolInstance = agentService.getProtocolInstance(agent.id) as TestHttpServerProtocoldef deploymentManager = Servlets.defaultContainer().getDeployment(protocolInstance.deployment.deploymentInfo.getDeploymentName());assetStorageService.delete([agent.id])@@ -420,11 +417,11 @@ class HttpServerProtocolTest extends Specification implements ManagerContainerTrassert deploymentManager.getState() == DeploymentManager.State.UNDEPLOYED}- // There's some timing issue on the undeployment- then: "if an attempt is made to use the removed endpoint"- new PollingConditions(timeout: 10, initialDelay: 1, delay: 1).eventually {- def exception = tryPost(authenticatedTestResource, testAsset)- assert exception instanceof NotAllowedException+ then: "if an attempt is made to use the removed endpoint then no asset should be posted"+ conditions.eventually {+ def postCount = protocolInstance.resource1.postedAssets.size()+ tryPost(authenticatedTestResource, testAsset)+ assert postCount == protocolInstance.resource1.postedAssets.size()}}}