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 Turner9c0464539485
diff --git a/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.javaindex bc3538cf4..0fcfc9c2d 100644--- a/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java@@ -118,25 +118,22 @@ public class SimulatorProtocol extends AbstractProtocol<SimulatorAgent, Simulato}public void updateSensor(AttributeRef attributeRef, Object value) {- updateSensor(attributeRef, value, 0L);+ updateSensor(attributeRef, value, timerService.getCurrentTimeMillis());}/*** Call this to simulate a sensor update using the specified timestamp*/public void updateSensor(AttributeRef attributeRef, Object value, long timestamp) {- withLock(getProtocolName() + "::updateSensor", () -> {-- AttributeState state = new AttributeState(attributeRef, value);- executorService.submit(() -> {- Attribute<?> attribute = getLinkedAttributes().get(attributeRef);- if (attribute == null) {- LOG.info("Attempt to update unlinked attribute: " + state);- return;- }- updateLinkedAttribute(state, timestamp);- });- });+ Attribute<?> attribute = getLinkedAttributes().get(attributeRef);+ AttributeState state = new AttributeState(attributeRef, value);++ if (attribute == null) {+ LOG.info("Attempt to update unlinked attribute: " + state);+ return;+ }++ updateLinkedAttribute(state, timestamp);}public Map<AttributeRef, ScheduledFuture<?>> getReplayMap() {diff --git a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.javaindex c7f0374e9..2941aacf4 100644--- a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java+++ b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java@@ -275,21 +275,40 @@ public class EmailNotificationHandler implements NotificationHandler {switch (targetType) {case USER:- toRecipients.add(getUserRecipient(targetId));+ 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:- toRecipients.add(getAssetRecipient(targetId));+ EmailNotificationMessage.Recipient assetRecipient = getAssetRecipient(targetId);+ if (assetRecipient == null) {+ LOG.warning("Failed to find asset: id=" + targetId);+ } else {+ LOG.finest("Adding to recipient: " + assetRecipient);+ toRecipients.add(assetRecipient);+ }break;case CUSTOM:// This recipient list is the target IDArrays.stream(targetId.split(";")).forEach(recipient -> {if (recipient.startsWith("to:")) {- toRecipients.add(new EmailNotificationMessage.Recipient(recipient.substring(3)));+ String email = recipient.substring(3);+ LOG.finest("Adding to recipient: " + email);+ toRecipients.add(new EmailNotificationMessage.Recipient(email));} else if (recipient.startsWith("cc:")) {- ccRecipients.add(new EmailNotificationMessage.Recipient(recipient.substring(3)));+ String email = recipient.substring(3);+ LOG.finest("Adding cc recipient: " + email);+ ccRecipients.add(new EmailNotificationMessage.Recipient(email));} else if (recipient.startsWith("bcc:")) {- bccRecipients.add(new EmailNotificationMessage.Recipient(recipient.substring(4)));+ String email = recipient.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));}});diff --git a/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy b/test/src/main/groovy/org/openremote/test/ContainerTrait.groovyindex 8ffe3aa33..10323aeda 100644--- a/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy+++ b/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy@@ -102,133 +102,138 @@ trait ContainerTrait {}if (configsMatch && servicesMatch) {+ try {+ println("Services and config matches already running container so checking state")+ def counter = 0++ // Reset gateway connections+ if (container.hasService(GatewayClientService.class)) {+ def gatewayClientService = container.getService(GatewayClientService.class)+ def gatewayConnections = getGatewayConnections()+ println("Purging ${gatewayConnections.size()} gateway connection(s)")+ gatewayClientService.deleteConnections(gatewayConnections.stream().map { it.localRealm }.collect(Collectors.toList()))+ }- println("Services and config matches already running container so checking state")- def counter = 0-- // Reset gateway connections- if (container.hasService(GatewayClientService.class)) {- def gatewayClientService = container.getService(GatewayClientService.class)- def gatewayConnections = getGatewayConnections()- println("Purging ${gatewayConnections.size()} gateway connection(s)")- gatewayClientService.deleteConnections(gatewayConnections.stream().map{it.localRealm}.collect(Collectors.toList()))- }-- // Reset rulesets- if (container.hasService(RulesetStorageService.class) && container.hasService(RulesService.class)) {- def globalRulesets = getRulesets(GlobalRuleset.class)- def tenantRulesets = getRulesets(TenantRuleset.class)- def assetRulesets = getRulesets(AssetRuleset.class)- def rulesService = container.getService(RulesService.class)- def rulesetStorageService = container.getService(RulesetStorageService.class)-- if (!assetRulesets.isEmpty()) {- println("Purging ${assetRulesets.size()} asset ruleset(s)")- assetRulesets.forEach {- rulesetStorageService.delete(AssetRuleset.class, it.id)- def rulesStopped = false- while (!rulesStopped) {- rulesStopped = rulesService.assetEngines.isEmpty() || !rulesService.assetEngines.containsKey(((AssetRuleset)it).assetId) || !rulesService.assetEngines.get(((AssetRuleset)it).assetId).deployments.containsKey(it.id)- if (counter++ > 100) {- throw new IllegalStateException("Failed to purge ruleset: " + it.name)+ // Reset rulesets+ if (container.hasService(RulesetStorageService.class) && container.hasService(RulesService.class)) {+ def globalRulesets = getRulesets(GlobalRuleset.class)+ def tenantRulesets = getRulesets(TenantRuleset.class)+ def assetRulesets = getRulesets(AssetRuleset.class)+ def rulesService = container.getService(RulesService.class)+ def rulesetStorageService = container.getService(RulesetStorageService.class)++ if (!assetRulesets.isEmpty()) {+ println("Purging ${assetRulesets.size()} asset ruleset(s)")+ assetRulesets.forEach {+ rulesetStorageService.delete(AssetRuleset.class, it.id)+ def rulesStopped = false+ while (!rulesStopped) {+ rulesStopped = rulesService.assetEngines.isEmpty() || !rulesService.assetEngines.containsKey(((AssetRuleset) it).assetId) || !rulesService.assetEngines.get(((AssetRuleset) it).assetId).deployments.containsKey(it.id)+ if (counter++ > 100) {+ throw new IllegalStateException("Failed to purge ruleset: " + it.name)+ }+ Thread.sleep(100)}- Thread.sleep(100)}}- }- if (!tenantRulesets.isEmpty()) {- println("Purging ${tenantRulesets.size()} tenant ruleset(s)")- tenantRulesets.forEach {- rulesetStorageService.delete(TenantRuleset.class, it.id)- def rulesStopped = false- while (!rulesStopped) {- rulesStopped = rulesService.tenantEngines.isEmpty() || !rulesService.tenantEngines.containsKey(((TenantRuleset)it).realm) || !rulesService.tenantEngines.get(((TenantRuleset)it).realm).deployments.containsKey(it.id)- if (counter++ > 100) {- throw new IllegalStateException("Failed to purge ruleset: " + it.name)+ if (!tenantRulesets.isEmpty()) {+ println("Purging ${tenantRulesets.size()} tenant ruleset(s)")+ tenantRulesets.forEach {+ rulesetStorageService.delete(TenantRuleset.class, it.id)+ def rulesStopped = false+ while (!rulesStopped) {+ rulesStopped = rulesService.tenantEngines.isEmpty() || !rulesService.tenantEngines.containsKey(((TenantRuleset) it).realm) || !rulesService.tenantEngines.get(((TenantRuleset) it).realm).deployments.containsKey(it.id)+ if (counter++ > 100) {+ throw new IllegalStateException("Failed to purge ruleset: " + it.name)+ }+ Thread.sleep(100)}- Thread.sleep(100)}}- }- if (!globalRulesets.isEmpty()) {- println("Purging ${globalRulesets.size()} global ruleset(s)")- globalRulesets.forEach {- rulesetStorageService.delete(GlobalRuleset.class, it.id)- def rulesStopped = false- while (!rulesStopped) {- rulesStopped = rulesService.globalEngine == null || !rulesService.globalEngine.deployments.containsKey(it.id)- if (counter++ > 100) {- throw new IllegalStateException("Failed to purge ruleset: " + it.name)+ if (!globalRulesets.isEmpty()) {+ println("Purging ${globalRulesets.size()} global ruleset(s)")+ globalRulesets.forEach {+ rulesetStorageService.delete(GlobalRuleset.class, it.id)+ def rulesStopped = false+ while (!rulesStopped) {+ rulesStopped = rulesService.globalEngine == null || !rulesService.globalEngine.deployments.containsKey(it.id)+ if (counter++ > 100) {+ throw new IllegalStateException("Failed to purge ruleset: " + it.name)+ }+ Thread.sleep(100)}- Thread.sleep(100)}}- }- // Wait for all rule engines to stop and be removed- counter = 0- def enginesStopped = false- while (!enginesStopped) {- enginesStopped = rulesService.globalEngine == null && rulesService.tenantEngines.isEmpty() && rulesService.assetEngines.isEmpty()- if (counter++ > 100) {- throw new IllegalStateException("Rule engines have failed to stop")+ // Wait for all rule engines to stop and be removed+ counter = 0+ def enginesStopped = false+ while (!enginesStopped) {+ enginesStopped = rulesService.globalEngine == null && rulesService.tenantEngines.isEmpty() && rulesService.assetEngines.isEmpty()+ if (counter++ > 100) {+ throw new IllegalStateException("Rule engines have failed to stop")+ }+ Thread.sleep(100)}- Thread.sleep(100)- }- if (!TestFixture.assetRulesets.isEmpty()) {- println("Re-inserting ${TestFixture.assetRulesets.size()} asset ruleset(s)")- TestFixture.assetRulesets.forEach { rulesetStorageService.merge(it) }- }- if (!TestFixture.tenantRulesets.isEmpty()) {- println("Re-inserting ${TestFixture.tenantRulesets.size()} tenant ruleset(s)")- TestFixture.tenantRulesets.forEach { rulesetStorageService.merge(it) }- }- if (!TestFixture.globalRulesets.isEmpty()) {- println("Re-inserting ${TestFixture.globalRulesets.size()} global ruleset(s)")- TestFixture.globalRulesets.forEach { rulesetStorageService.merge(it) }+ if (!TestFixture.assetRulesets.isEmpty()) {+ println("Re-inserting ${TestFixture.assetRulesets.size()} asset ruleset(s)")+ TestFixture.assetRulesets.forEach { rulesetStorageService.merge(it) }+ }+ if (!TestFixture.tenantRulesets.isEmpty()) {+ println("Re-inserting ${TestFixture.tenantRulesets.size()} tenant ruleset(s)")+ TestFixture.tenantRulesets.forEach { rulesetStorageService.merge(it) }+ }+ if (!TestFixture.globalRulesets.isEmpty()) {+ println("Re-inserting ${TestFixture.globalRulesets.size()} global ruleset(s)")+ TestFixture.globalRulesets.forEach { rulesetStorageService.merge(it) }+ }}- }- // Reset assets- if (container.hasService(AgentService.class) && container.hasService(AssetStorageService.class)) {- def currentAssets = getAssets().collect { it.getId() }- def assetStorageService = container.getService(AssetStorageService.class)+ // Reset assets+ if (container.hasService(AgentService.class) && container.hasService(AssetStorageService.class)) {+ def currentAssets = getAssets().collect { it.getId() }+ def assetStorageService = container.getService(AssetStorageService.class)- if (!currentAssets.isEmpty()) {- println("Purging ${currentAssets.size()} asset(s)")- assetStorageService.delete(currentAssets, true)- }+ if (!currentAssets.isEmpty()) {+ println("Purging ${currentAssets.size()} asset(s)")+ assetStorageService.delete(currentAssets, true)+ }- // Wait for all assets to be unlinked from protocols- def agentsRemoved = false- while (!agentsRemoved) {- if (counter++ > 100) {- throw new IllegalStateException("Failed to purge agents")+ // Wait for all assets to be unlinked from protocols+ def agentsRemoved = false+ while (!agentsRemoved) {+ if (counter++ > 100) {+ throw new IllegalStateException("Failed to purge agents")+ }+ agentsRemoved = container.getService(AgentService.class).agentMap.isEmpty()+ Thread.sleep(100)}- agentsRemoved = container.getService(AgentService.class).agentMap.isEmpty()- Thread.sleep(100)- }- if (!TestFixture.assets.isEmpty()) {- println("Re-inserting ${TestFixture.assets.size()} asset(s)")- TestFixture.assets.forEach { a ->- a.version = 0- assetStorageService.merge(a, true)+ if (!TestFixture.assets.isEmpty()) {+ println("Re-inserting ${TestFixture.assets.size()} asset(s)")+ TestFixture.assets.forEach { a ->+ a.version = 0+ assetStorageService.merge(a, true)+ }}- }- if (!TestFixture.userAssets.isEmpty()) {- println("Re-linking ${TestFixture.userAssets.size()} user asset(s)")- TestFixture.userAssets.forEach { ua ->- assetStorageService.storeUserAsset(ua)+ if (!TestFixture.userAssets.isEmpty()) {+ println("Re-linking ${TestFixture.userAssets.size()} user asset(s)")+ TestFixture.userAssets.forEach { ua ->+ assetStorageService.storeUserAsset(ua)+ }}}- }- // Start clock in case previous test stopped it- def timerService = container.getService(TimerService.class)- if (timerService != null) {- timerService.getClock().start();+ // Start clock in case previous test stopped it+ def timerService = container.getService(TimerService.class)+ if (timerService != null) {+ timerService.getClock().start();+ }+ } catch (IllegalStateException e) {+ System.out.println("Failed to clean the existing container so creating a new one")+ stopContainer()+ TestFixture.container = null}} else {System.out.println("Request to start container with different config and/or services as already running container so restarting")diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovyindex 803599e8e..87f718df7 100644--- a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy+++ b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy@@ -49,11 +49,11 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait}when: "an attribute linked to the simulator agent receives some values"- def simulatorProtocol = ((SimulatorProtocol)agentService.protocolInstanceMap.get(managerTestSetup.agentId));+ def simulatorProtocol = ((SimulatorProtocol)agentService.protocolInstanceMap.get(managerTestSetup.agentId))advancePseudoClock(10, SECONDS, container)simulatorProtocol.updateSensor(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"), 13.3d)advancePseudoClock(10, SECONDS, container)- simulatorProtocol.updateSensor(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"), 13.3d)+ simulatorProtocol.updateSensor(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"), null)advancePseudoClock(10, SECONDS, container)simulatorProtocol.updateSensor(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"), 13.3d)@@ -103,7 +103,7 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitexpect: "the datapoints to be stored"conditions.eventually {def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"))- assert datapoints.size() == 3+ assert datapoints.size() == 5// Note that the "No value" sensor update should not have created a datapoint, the first// datapoint is the last sensor update with an actual value@@ -116,6 +116,10 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitassert Values.getValue(datapoints.get(2).value, Double.class).orElse(null) == 13.3dassert datapoints.get(2).timestamp == datapoint1ExpectedTimestamp++ assert Values.getValue(datapoints.get(3).value, Double.class).orElse(null) == 13.3d++ assert Values.getValue(datapoints.get(4).value, Double.class).orElse(null) == 13.3d}and: "the aggregated datapoints should match"@@ -215,7 +219,7 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitdef powerDatapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"))def toggleDatapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))- assert powerDatapoints.size() == 4+ assert powerDatapoints.size() == 6assert Values.getValue(powerDatapoints.get(0).value, Double.class).orElse(null) == 17.5dassert powerDatapoints.get(0).timestamp == datapoint4ExpectedTimestampdiff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovyindex e8538accf..2e17b5072 100644--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy@@ -3,7 +3,6 @@ package org.openremote.test.notificationimport com.fasterxml.jackson.databind.node.ObjectNodeimport com.fasterxml.jackson.databind.node.TextNodeimport com.google.firebase.messaging.Message-import org.openremote.container.timer.TimerServiceimport org.openremote.container.web.WebServiceimport org.openremote.manager.asset.AssetProcessingServiceimport org.openremote.manager.asset.AssetStorageService@@ -12,6 +11,7 @@ import org.openremote.manager.notification.EmailNotificationHandlerimport org.openremote.manager.notification.NotificationServiceimport org.openremote.manager.notification.PushNotificationHandlerimport org.openremote.manager.rules.geofence.ORConsoleGeofenceAssetAdapter+import org.openremote.manager.security.ManagerIdentityServiceimport org.openremote.manager.setup.SetupServiceimport org.openremote.manager.setup.builtin.KeycloakTestSetupimport org.openremote.manager.setup.builtin.ManagerTestSetup@@ -20,7 +20,6 @@ import org.openremote.model.console.ConsoleProviderimport org.openremote.model.console.ConsoleRegistrationimport org.openremote.model.console.ConsoleResourceimport org.openremote.model.notification.*-import org.openremote.model.value.Valuesimport org.openremote.test.ManagerContainerTraitimport org.simplejavamail.email.Emailimport spock.lang.Specification@@ -536,7 +535,9 @@ class NotificationTest extends Specification implements ManagerContainerTrait {def conditions = new PollingConditions(timeout: 10, delay: 0.2)def container = startContainer(defaultConfig(), defaultServices())def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)def notificationService = container.getService(NotificationService.class)+ def identityService = container.getService(ManagerIdentityService.class)def emailNotificationHandler = container.getService(EmailNotificationHandler.class)def assetStorageService = container.getService(AssetStorageService.class)def assetProcessingService = container.getService(AssetProcessingService.class)@@ -553,9 +554,10 @@ class NotificationTest extends Specification implements ManagerContainerTrait {}notificationService.notificationHandlerMap.put(emailNotificationHandler.getTypeName(), mockEmailNotificationHandler)- expect: "the container to settle"+ expect: "the demo users to be created"conditions.eventually {- assert noEventProcessedIn(assetProcessingService, 500)+ def users = identityService.getIdentityProvider().getUsers(keycloakTestSetup.tenantBuilding.realm)+ assert users.size() == 4}when: "an email notification is sent to a tenant through same mechanism as rules"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 d435d1476..9121036ac 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@@ -30,6 +30,7 @@ import org.openremote.container.web.OAuthServerResponseimport org.openremote.manager.agent.AgentServiceimport org.openremote.manager.asset.AssetProcessingServiceimport org.openremote.manager.asset.AssetStorageService+import org.openremote.model.asset.Assetimport org.openremote.model.asset.impl.ThingAssetimport org.openremote.model.attribute.Attributeimport org.openremote.model.auth.OAuthGrant@@ -266,6 +267,16 @@ class HttpServerProtocolTest extends Specification implements ManagerContainerTrmockServer.putRequestWithHeadersCalled = false}+ def tryPost(TestResource authenticatedTestResource, Asset testAsset) {+ def exception = null+ try {+ authenticatedTestResource.postAsset(testAsset)+ } catch (Exception e) {+ exception = e+ }+ return exception+ }+def "Check HTTP server protocol and JAX-RS deployment"() {given: "expected conditions"@@ -409,10 +420,11 @@ class HttpServerProtocolTest extends Specification implements ManagerContainerTrassert deploymentManager.getState() == DeploymentManager.State.UNDEPLOYED}- when: "when the removed endpoint is used"- authenticatedTestResource.postAsset(testAsset)-- then: "then an exception should be thrown"- thrown(NotAllowedException)+ // 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+ }}}