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
Basic tests for StrijpS office app
Michal Rutkac0ee82e491a7
diff --git a/agent/src/main/java/org/openremote/agent/rules/RuleEngine.java b/agent/src/main/java/org/openremote/agent/rules/RuleEngine.javaindex 1b0845cfd..cdec24e2c 100644--- a/agent/src/main/java/org/openremote/agent/rules/RuleEngine.java+++ b/agent/src/main/java/org/openremote/agent/rules/RuleEngine.java@@ -29,11 +29,13 @@ import org.kie.api.builder.model.KieBaseModel;import org.kie.api.builder.model.KieModuleModel;import org.kie.api.builder.model.KieSessionModel;import org.kie.api.conf.EqualityBehaviorOption;+import org.kie.api.conf.EventProcessingOption;import org.kie.api.io.Resource;import org.kie.api.io.ResourceType;import org.kie.api.runtime.KieContainer;import org.kie.api.runtime.KieSession;import org.kie.api.runtime.KieSessionConfiguration;+import org.kie.api.runtime.conf.ClockTypeOption;import org.kie.api.runtime.conf.TimedRuleExectionOption;import org.kie.api.runtime.rule.FactHandle;import org.kie.internal.builder.DecisionTableConfiguration;@@ -72,10 +74,12 @@ public class RuleEngine {KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel("OpenRemoteKBase").setDefault(true)- .setEqualsBehavior(EqualityBehaviorOption.EQUALITY);+ .setEqualsBehavior(EqualityBehaviorOption.EQUALITY)+ .setEventProcessingMode(EventProcessingOption.STREAM);KieSessionModel kieSessionModel = kieBaseModel.newKieSessionModel("OpenRemoteKSession").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL);+ // .setClockType(ClockTypeOption.get("pseudo")); // TODO: set in on the fly for testsKieFileSystem kfs = kieServices.newKieFileSystem();kfs.writeKModuleXML(kieModuleModel.toXML());@@ -211,6 +215,10 @@ public class RuleEngine {}}+ public KieSession getKnowledgeSession() {+ return knowledgeSession;+ }+protected void setGlobal(String identifier, Object object) {try {knowledgeSession.setGlobal(identifier, object);diff --git a/agent/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovy b/agent/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovyindex 8010df2e6..65c5f9d9f 100644--- a/agent/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovy+++ b/agent/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovy@@ -21,7 +21,6 @@ class ClimateControlTest extends Specification implements ContainerTrait {def deploymentXml = getClass().getResourceAsStream("/org/openremote/test/rules/climatecontrol/agent.xml")- def conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)and: "some rules"def rulesProvider = new RulesProvider() {@@ -36,7 +35,7 @@ class ClimateControlTest extends Specification implements ContainerTrait {}and: "the started server"- def testCommandBuilder = new TestCommandBuilder();+ def testCommandBuilder = new CustomTimeCommandBuilder();def agentService = new AgentService(deploymentXml,testCommandBuilder,@@ -45,21 +44,22 @@ class ClimateControlTest extends Specification implements ContainerTrait {def serverPort = findEphemeralPort()def container = startContainer(defaultConfig(serverPort), [agentService])+ def sensor = { $sensor -> agentService.getContext().queryValue($sensor) }+ def command = { $name, $arg -> agentService.getContext().getCommands().execute($name, $arg) }+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)+// TODO Write testscleanup: "the server should be stopped"stopContainer(container)}- @Ignore- def "Person sense test"() {+ def "Estimate times initial values check"() {given: "a deployment with commands and sensors"def deploymentXml = getClass().getResourceAsStream("/org/openremote/test/rules/climatecontrol/agent.xml")- // def conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)- def result = new BlockingVariable<String>()and: "some rules"def rulesProvider = new RulesProvider() {@@ -74,7 +74,8 @@ class ClimateControlTest extends Specification implements ContainerTrait {}and: "the started server"- def testCommandBuilder = new TestCommandBuilder();+ def testCommandBuilder = new CustomTimeCommandBuilder();+ testCommandBuilder.currentTime = "07:07:00"def agentService = new AgentService(deploymentXml,testCommandBuilder,@@ -83,19 +84,82 @@ class ClimateControlTest extends Specification implements ContainerTrait {def serverPort = findEphemeralPort()def container = startContainer(defaultConfig(serverPort), [agentService])- // TODO Write tests+ def sensor = { $sensor -> agentService.getContext().queryValue($sensor) }+ def command = { $name, $arg -> agentService.getContext().getCommands().execute($name, $arg) }++ // TODO: wait for answer from Drools forum how it should be used. It does not work now.+ // KieSession ksession = agentService.getContext().getDeployment().getRuleEngine().getKnowledgeSession()+ // PseudoClockScheduler clock = new PseudoClockScheduler(ksession)++ when: "give some time for initialization rules"+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)++ then: "check all defaults"+ conditions.eventually {+ assert sensor("VR1.ET") == "09:00"+ assert sensor("VR1.ET.inc") == "OFF"+ assert sensor("VR1.ET.dec") == "OFF"+ assert sensor("VETA.inc") == "OFF"+ assert sensor("VETA.dec") == "OFF"+ assert sensor("VETD.inc") == "OFF"+ assert sensor("VETD.dec") == "OFF"+ assert sensor("VR1.COMFORT") == "20.0°"+ assert sensor("VR1.COMFORT.inc") == "OFF"+ assert sensor("VR1.COMFORT.dec") == "OFF"+ assert sensor("VR1.TEMPERATURE") == "16.0\u00B0" // ECO set by heating setpoint ECO rule+ assert sensor("VR1.TEMPERATURE.inc") == "OFF"+ assert sensor("VR1.TEMPERATURE.dec") == "OFF"+ assert sensor("VNEXTACTION") == "Arrive"+ assert sensor("VPERSONSENSETIME") == "-"+ assert sensor("VLEAVES") == "3.0"+ assert sensor("VSCORE") == "4"+ assert Double.parseDouble(sensor("VHEATINGSETPOINT")) == 16+ assert sensor("VWINDOW") == "Closed"+ assert sensor("VADVICEDONE") == "OFF"+ assert sensor("VSUMMER") == "No"+ assert sensor("VADVICE") == "You're doing great!"+ assert sensor("VACATION.inc") == "OFF"+ assert sensor("VACATION.dec") == "OFF"+ assert sensor("VACATION") == "0"+ assert sensor("VTOTALSCORE") == "0"+ assert sensor("VLEVEL") == "0"+ assert sensor("VATA") == "-"+ assert sensor("VATD") == "-"+ assert sensor("VPRESENCE") == "No"+ assert sensor("GVconfig") == "OpenRemote"+ assert sensor("VArrivalBackground") == "on"+ assert sensor("VDepartureBackground") == "on"+ assert sensor("FS.Toutside") == "15°"+ assert sensor("FS.Tinside") == "19.5°"+ assert sensor("FS.PIR") == "off"+ assert sensor("FS.Window") == "off"+ assert sensor("VETA.Mon") == "09:00"+ assert sensor("VETD.Mon") == "17:00"+ assert sensor("VETA.Tue") == "09:00"+ assert sensor("VETD.Tue") == "17:00"+ assert sensor("VETA.Wed") == "09:00"+ assert sensor("VETD.Wed") == "17:00"+ assert sensor("VETA.Thu") == "09:00"+ assert sensor("VETD.Thu") == "17:00"+ assert sensor("VETA.Fri") == "09:00"+ assert sensor("VETD.Fri") == "17:00"+ assert sensor("VETA.Sat") == "09:00"+ assert sensor("VETD.Sat") == "17:00"+ assert sensor("VETA.Sun") == "09:00"+ assert sensor("VETD.Sun") == "17:00"+ }cleanup: "the server should be stopped"stopContainer(container)}- def "Estimate times initial values check"() {+ def "Person sense test"() {given: "a deployment with commands and sensors"def deploymentXml = getClass().getResourceAsStream("/org/openremote/test/rules/climatecontrol/agent.xml")- def conditions = new PollingConditions(timeout: 1, initialDelay: 0.01)+ def result = new BlockingVariable<String>()and: "some rules"def rulesProvider = new RulesProvider() {@@ -110,8 +174,9 @@ class ClimateControlTest extends Specification implements ContainerTrait {}and: "the started server"-// def testCommandBuilder = new TestCommandBuilder();def testCommandBuilder = new CustomTimeCommandBuilder();+ def time = { $time -> testCommandBuilder.currentTime = $time }+ time "8:25:00"def agentService = new AgentService(deploymentXml,testCommandBuilder,@@ -120,92 +185,69 @@ class ClimateControlTest extends Specification implements ContainerTrait {def serverPort = findEphemeralPort()def container = startContainer(defaultConfig(serverPort), [agentService])- when: "give some time for initialization rules"-// agentService.getContext().getCommands().execute("FS.Tinside.dec.ON")-// Thread.sleep(10)- testCommandBuilder.currentTime = "7:00:00"- Thread.sleep(1000)- testCommandBuilder.currentTime = "10:27:00"- Thread.sleep(1000)- testCommandBuilder.currentTime = "17:55:00"- Thread.sleep(1000)- testCommandBuilder.currentTime = "23:58:00"- Thread.sleep(1000)- testCommandBuilder.currentTime = "23:59:00"- Thread.sleep(1000)- testCommandBuilder.currentTime = "24:00:00"- Thread.sleep(1000)- testCommandBuilder.currentTime = "24:01:00"- Thread.sleep(1000)- testCommandBuilder.currentTime = "24:02:00"- Thread.sleep(1000)- testCommandBuilder.currentTime = "24:03:00"- Thread.sleep(1000)- testCommandBuilder.currentTime = "48:04:00"- Thread.sleep(1000)- testCommandBuilder.currentTime = "72:05:00"- Thread.sleep(1000)- testCommandBuilder.currentTime = "96:06:00"- Thread.sleep(1000)- testCommandBuilder.currentTime = "120:07:00"+ def sensor = { $sensor -> agentService.getContext().queryValue($sensor) }+ def command = { $name, $arg -> agentService.getContext().getCommands().execute($name, $arg) }- then: "check all defaults"+ when: "Set time before going to office"+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)++ then: "Check if there is no person"+ conditions.eventually {+ assert sensor("VNEXTACTION") == "Arrive"+ assert sensor("FS.PIR") != "on"+ assert sensor("VPERSONSENSE") == "0"+ assert sensor("VPRESENCE") == "No"+ }++ when: "now let's enter the office at 10 AM"+ time "10:00:00"+ command "FS.PIR", "on"+ conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)++ then: "see if the arrival is registered"+ conditions.eventually {+ assert sensor("VNEXTACTION") == "Departure"+ assert sensor("VATA") == "10:00"+ assert sensor("VPERSONSENSE") == "0"+ assert sensor("VPRESENCE") == "Yes"+ assert sensor("FS.PIR") == "off"+ }++ when: "go home at 4PM"+ time "16:00:00"+ command "FS.PIR", "on"+ conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)++ then: "wait for PIR off"+ conditions.eventually {+ assert sensor("VPERSONSENSE") == "1"+ assert sensor("FS.PIR") == "off"+ }++ when: "Go at midnight adjusting"+ time "23:58:00"+ conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)++ then: "check departure"+ conditions.eventually {+ assert sensor("VPRESENCE") == "No"+ assert sensor("VATD") == "16:00"+ assert sensor("VNEXTACTION") == "-"+ }++ when: "Check updated arrival and departure times"+ time "23:59:00"+ conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)++ then: "see if times schifted by half an hour"conditions.eventually {-// assert agentService.getContext().queryValue("VR1.ET") == "--:--"-// assert agentService.getContext().queryValue("VR1.ET.inc") == "OFF"-// assert agentService.getContext().queryValue("VR1.ET.dec") == "OFF"-// assert agentService.getContext().queryValue("VETA.inc") == "OFF"-// assert agentService.getContext().queryValue("VETA.dec") == "OFF"-// assert agentService.getContext().queryValue("VETD.inc") == "OFF"-// assert agentService.getContext().queryValue("VETD.dec") == "OFF"-// assert agentService.getContext().queryValue("VR1.COMFORT") == "20.0°"-// assert agentService.getContext().queryValue("VR1.COMFORT.inc") == "OFF"-// assert agentService.getContext().queryValue("VR1.COMFORT.dec") == "OFF"-// assert agentService.getContext().queryValue("VR1.TEMPERATURE") == "19.5\u00B0"-// assert agentService.getContext().queryValue("VR1.TEMPERATURE.inc") == "OFF"-// assert agentService.getContext().queryValue("VR1.TEMPERATURE.dec") == "OFF"-// assert agentService.getContext().queryValue("VNEXTACTION") == "-"-// assert agentService.getContext().queryValue("VPERSONSENSETIME") == "-"-// assert agentService.getContext().queryValue("VLEAVES") == "1.0"-// assert agentService.getContext().queryValue("VSCORE") == "0"-// assert agentService.getContext().queryValue("VHEATINGSETPOINT") == "16"-// assert agentService.getContext().queryValue("VWINDOW") == "Closed"-// assert agentService.getContext().queryValue("VADVICEDONE") == "OFF"-// assert agentService.getContext().queryValue("VSUMMER") == "No"-// assert agentService.getContext().queryValue("VADVICE") == "You're doing great!"-// assert agentService.getContext().queryValue("VACATION.inc") == "OFF"-// assert agentService.getContext().queryValue("VACATION.dec") == "OFF"-// assert agentService.getContext().queryValue("VACATION") == "0"-// assert agentService.getContext().queryValue("VTOTALSCORE") == "0"-// assert agentService.getContext().queryValue("VLEVEL") == "0"-// assert agentService.getContext().queryValue("VATA") == "-"-// assert agentService.getContext().queryValue("VATD") == "-"-// assert agentService.getContext().queryValue("VPRESENCE") == "No"-// assert agentService.getContext().queryValue("GVconfig") == "OpenRemote"-//// assert agentService.getContext().queryValue("VArrivalBackground") == "on"-//// assert agentService.getContext().queryValue("VDepartureBackground") == "off"-// assert agentService.getContext().queryValue("FS.Toutside") == "15°"-// assert agentService.getContext().queryValue("FS.Tinside") == "19.5°"-// assert agentService.getContext().queryValue("FS.PIR") == "off"-// assert agentService.getContext().queryValue("FS.Window") == "off"-// assert agentService.getContext().queryValue("VETA.Mon") == "09:00"-// assert agentService.getContext().queryValue("VETD.Mon") == "17:00"-// assert agentService.getContext().queryValue("VETA.Tue") == "09:00"-// assert agentService.getContext().queryValue("VETD.Tue") == "17:00"-// assert agentService.getContext().queryValue("VETA.Wed") == "09:00"-// assert agentService.getContext().queryValue("VETD.Wed") == "17:00"-// assert agentService.getContext().queryValue("VETA.Thu") == "09:00"-// assert agentService.getContext().queryValue("VETD.Thu") == "17:00"-// assert agentService.getContext().queryValue("VETA.Fri") == "09:00"-// assert agentService.getContext().queryValue("VETD.Fri") == "17:00"-// assert agentService.getContext().queryValue("VETA.Sat") == "09:00"-// assert agentService.getContext().queryValue("VETD.Sat") == "17:00"-// assert agentService.getContext().queryValue("VETA.Sun") == "09:00"-// assert agentService.getContext().queryValue("VETD.Sun") == "17:00"+ def dow = sensor("TimerEEE")+ assert sensor("VETA.$dow") == "09:30"+ assert sensor("VETD.$dow") == "16:30"}cleanup: "the server should be stopped"stopContainer(container)}-}+}\ No newline at end of filediff --git a/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovy b/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovyindex cd34953f2..aae1c58a3 100644--- a/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovy+++ b/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovy@@ -18,7 +18,6 @@ class PIDTest extends Specification implements ContainerTrait {def deploymentXml = getClass().getResourceAsStream("/org/openremote/test/rules/pid/agent.xml")- def conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)and: "some rules"def rulesProvider = new RulesProvider() {@@ -42,28 +41,33 @@ class PIDTest extends Specification implements ContainerTrait {def serverPort = findEphemeralPort()def container = startContainer(defaultConfig(serverPort), [agentService])+ def sensor = { $sensor -> Double.parseDouble(agentService.getContext().queryValue($sensor)) }+ def command = { $name -> agentService.getContext().getCommands().execute($name) }+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)++when: "we increase Sp"// 2 ways of increasing the set point.// Set the value of sensor directly://def customStateEvent = new CustomSensorState(358537, "PID.sp.inc", "ON");//agentService.getContext().update(customStateEvent);// Send a command linked with the button:- agentService.getContext().getCommands().execute("PID.sp.inc.ON")+ command("PID.sp.inc.ON")then: "set point should be 1.5"conditions.eventually {- assert agentService.getContext().queryValue("GV.PID.Sp") == "1.5"- assert agentService.getContext().queryValue("PID.Output") == "1.5000"+ assert sensor("GV.PID.Sp") == 1.5+ assert sensor("PID.Output") == 1.5}-when: "we decrease Sp"- agentService.getContext().getCommands().execute("PID.sp.dec.ON")+ command("PID.sp.dec.ON")+ conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)then: "the PID output should be 1.0"conditions.eventually {- assert agentService.getContext().queryValue("GV.PID.Sp") == "1.0" // set point- assert agentService.getContext().queryValue("PID.Output") == "1.0000" // PID output+ assert sensor("GV.PID.Sp") == 1 // set point+ assert sensor("PID.Output") == 1 // PID output}// TODO: how can I test if there is no overshot?diff --git a/agent/src/test/resources/org/openremote/test/rules/climatecontrol/ClimateControl.drl b/agent/src/test/resources/org/openremote/test/rules/climatecontrol/ClimateControl.drlindex a8e0b70a1..a544ab3f7 100644--- a/agent/src/test/resources/org/openremote/test/rules/climatecontrol/ClimateControl.drl+++ b/agent/src/test/resources/org/openremote/test/rules/climatecontrol/ClimateControl.drl@@ -70,21 +70,18 @@ thencommands.execute($s, persistence.readData($s, "17:00"));end-rule "Show timers"+rule "--Show timers"salience 10-timer(int:100)+// timer(int:100)when- SensorState(sensorName=="Timerd", value!="N/A")- SensorState(sensorName=="TimerEEEE", value!="N/A")- SensorState(sensorName=="TimerHH", value!="N/A")- SensorState(sensorName=="TimerMMM", value!="N/A")+ SensorState(sensorName=="TimerEEEE", $a:value!="N/A")+ SensorState(sensorName=="Timerd", $b:value!="N/A")+ SensorState(sensorName=="TimerHH", $d:value!="N/A")+ SensorState(sensorName=="TimerMMM", $c:value!="N/A")SensorState(sensorName=="TimerEEE", value!="N/A")then- LOG.info("Drools SessionClock() is: "+- (new java.util.Date(drools.getWorkingMemory()- .getSessionClock()- .getCurrentTime()))- .toString());+ LOG.info("Sensor clock is: "+$a+" "+$b+" "+$c+" "+$d);+// LOG.info("Drools SessionClock() is: "+(new java.util.Date(drools.getWorkingMemory().getSessionClock().getCurrentTime())).toString());endrule "--PSB: store values"@@ -547,7 +544,7 @@ whenSensorState($s:sensorName=="FS.PIR", value=="on")SensorState(sensorName=="TimerHH", $h:value)then- LOG.fine("Move sense on "+$s);+ LOG.info("Move sense on "+$s+" @"+$h);commands.execute("VPERSONSENSETIME", $h.toString());end