Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Test related fixes

Rich Turnerb12e53dba5a1
diff --git a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
index 984edd32f..2b9a1e87d 100644
--- a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
+++ b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
@@ -400,7 +400,7 @@ public class JsonRulesBuilder extends RulesBuilder {
Map<String, RuleTriggerState> triggerStateMap = new HashMap<>();
ruleConditionStateMap.put(rule.name, triggerStateMap);
- addRuleTriggerState(rule.when, rule.otherwise != null, 0, triggerStateMap, rule.reset);
+ addRuleTriggerStates(rule.when, rule.otherwise != null, 0, triggerStateMap, rule.reset);
Condition condition = buildLhsCondition(rule, triggerStateMap);
Action action = buildRhsAction(rule, triggerStateMap);
@@ -430,7 +430,7 @@ public class JsonRulesBuilder extends RulesBuilder {
return this;
}
- protected void addRuleTriggerState(LogicGroup<RuleCondition> ruleConditionCondition, boolean trackUnmatched, int index, Map<String, RuleTriggerState> triggerStateMap, RuleConditionReset defaultReset) {
+ protected void addRuleTriggerStates(LogicGroup<RuleCondition> ruleConditionCondition, boolean trackUnmatched, int index, Map<String, RuleTriggerState> triggerStateMap, RuleConditionReset defaultReset) {
if (ruleConditionCondition != null) {
if (ruleConditionCondition.items != null && ruleConditionCondition.items.length > 0) {
for (RuleCondition ruleCondition : ruleConditionCondition.items) {
@@ -446,7 +446,7 @@ public class JsonRulesBuilder extends RulesBuilder {
}
if (ruleConditionCondition.groups != null && ruleConditionCondition.groups.length > 0) {
for (LogicGroup<RuleCondition> childRuleTriggerCondition : ruleConditionCondition.groups) {
- addRuleTriggerState(childRuleTriggerCondition, trackUnmatched, index, triggerStateMap, defaultReset);
+ addRuleTriggerStates(childRuleTriggerCondition, trackUnmatched, index, triggerStateMap, defaultReset);
}
}
}
diff --git a/manager/src/main/resources/demo/rules/DemoResidenceAutoVentilation.groovy b/manager/src/main/resources/demo/rules/DemoResidenceAutoVentilation.groovy
index 827644e9c..90acbba29 100644
--- a/manager/src/main/resources/demo/rules/DemoResidenceAutoVentilation.groovy
+++ b/manager/src/main/resources/demo/rules/DemoResidenceAutoVentilation.groovy
@@ -39,11 +39,23 @@ Closure<Stream<AssetState>> residenceWithVentilationAutoMatch =
facts.matchAssetState(
new AssetQuery().type(RESIDENCE).attributeValue("ventilationAuto", true)
).filter { residenceWithVentilationAuto ->
- facts.matchFirstAssetState(
- // and ventilation level is above/below/equal/etc. threshold
- new AssetQuery().id(residenceWithVentilationAuto.id)
- .attributeValue("ventilationLevel", operator, ventilationLevelThreshold)
- ).isPresent()
+
+ // and ventilation level is above/below/equal/etc. threshold
+ if (operator == LESS_THAN) {
+ // Used to have an implicit "OR NULL" which has now been removed
+ def isNull = facts.matchFirstAssetState(
+ new AssetQuery().id(residenceWithVentilationAuto.id).attributeName("ventilationLevel")
+ ).map {!it.value.isPresent()}.orElse(false)
+
+ if (isNull) {
+ return true
+ }
+ }
+
+ return facts.matchFirstAssetState(
+ new AssetQuery().id(residenceWithVentilationAuto.id)
+ .attributeValue("ventilationLevel", operator, ventilationLevelThreshold)
+ ).isPresent()
}
}
diff --git a/manager/src/main/resources/demo/rules/DemoResidencePresenceDetection.groovy b/manager/src/main/resources/demo/rules/DemoResidencePresenceDetection.groovy
index db487e18b..5391be812 100644
--- a/manager/src/main/resources/demo/rules/DemoResidencePresenceDetection.groovy
+++ b/manager/src/main/resources/demo/rules/DemoResidencePresenceDetection.groovy
@@ -69,7 +69,7 @@ rules.add()
.then(
{ facts ->
AssetState room = facts.bound("room")
- LOG.info("Presence detected in residence '" + room.parentName + "', room: " + room.name)
+ LOG.info("Presence detected in residence '" + room.parentName + "', room: " + room.name + " [" + room.id + "]")
facts.updateAssetState(room.id, "presenceDetected", true)
})
@@ -108,7 +108,7 @@ rules.add()
.then(
{ facts ->
AssetState room = facts.bound("room")
- LOG.info("Presence gone in residence '" + room.parentName + "', room: " + room.name)
+ LOG.info("Presence gone in residence '" + room.parentName + "', room: " + room.name + " [" + room.id + "]")
facts.updateAssetState(room.id, "presenceDetected", false)
})
@@ -125,11 +125,13 @@ rules.add()
new AssetQuery().id(roomWithPresence.id)
.attributeValue("motionSensor", GREATER_THAN, 0)
).flatMap { roomWithMotionSensorTriggered ->
- // and the last presence detection timestamp is older than the motion sensor timestamp
+ // and the last presence detection timestamp is not set or is older than the motion sensor timestamp
facts.matchAssetState(
new AssetQuery().id(roomWithMotionSensorTriggered.id)
- .attributeValue("lastPresenceDetected", LESS_THAN, roomWithMotionSensorTriggered.timestamp)
- ).map { outdatedLastPresenceDetected ->
+ .attributeName("lastPresenceDetected")
+ ).filter({
+ !it.value.isPresent() || it.valueAsNumber.get() < roomWithMotionSensorTriggered.timestamp
+ }).map { outdatedLastPresenceDetected ->
// keep the room and the new timestamp
new Pair<AssetState, Double>(outdatedLastPresenceDetected, roomWithMotionSensorTriggered.timestamp)
}
@@ -142,7 +144,7 @@ rules.add()
.then(
{ facts ->
AssetState room = facts.bound("room")
- LOG.info("Motion sensor triggered, updating last presence in residence '" + room.parentName + "', room: " + room.name)
+ LOG.info("Motion sensor triggered, updating last presence in residence '" + room.parentName + "', room: " + room.name + " [" + room.id + "]")
facts.updateAssetState(room.id, "lastPresenceDetected", facts.bound("lastPresenceTimestamp") as Double)
})
@@ -168,7 +170,7 @@ rules.add()
.then(
{ facts ->
AssetState residence = facts.bound("residence")
- LOG.info("Presence detected in residence: " + residence.name)
+ LOG.info("Presence detected in residence: " + residence.name + " [" + residence.id + "]")
facts.updateAssetState(residence.id, "presenceDetected", true)
})
@@ -193,6 +195,6 @@ rules.add()
.then(
{ facts ->
AssetState residence = facts.bound("residence")
- LOG.info("Presence gone in residence: " + residence.name)
+ LOG.info("Presence gone in residence: " + residence.name + " [" + residence.id + "]")
facts.updateAssetState(residence.id, "presenceDetected", false)
})
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy
index d0dcb24dd..7fb900864 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy
@@ -25,7 +25,7 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
def "Presence detection with motion sensor"() {
given: "the container environment is started"
- def conditions = new PollingConditions(timeout: 20, delay: 1)
+ def conditions = new PollingConditions(timeout: 30, delay: 1)
def serverPort = findEphemeralPort()
def container = startContainerWithPseudoClock(defaultConfig(serverPort), defaultServices())
def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
diff --git a/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json b/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json
index bdaec0ad6..f258b118f 100644
--- a/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json
+++ b/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json
@@ -3,7 +3,7 @@
{
"name": "Test Rule",
"when": {
- "predicates": [
+ "items": [
{
"assets": {
"types": [
@@ -14,7 +14,7 @@
}
],
"attributes": {
- "predicates": [
+ "items": [
{
"name": {
"predicateType": "string",