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
Added timer protocol test
Rich Turner06ddb96bf20b
diff --git a/agent/src/main/java/org/openremote/agent/protocol/timer/TimerValue.java b/agent/src/main/java/org/openremote/agent/protocol/timer/TimerValue.javaindex e528e13ca..4a17cab9e 100644--- a/agent/src/main/java/org/openremote/agent/protocol/timer/TimerValue.java+++ b/agent/src/main/java/org/openremote/agent/protocol/timer/TimerValue.java@@ -19,6 +19,9 @@*/package org.openremote.agent.protocol.timer;+import org.openremote.model.attribute.MetaItem;+import org.openremote.model.value.Values;+import java.util.Locale;public enum TimerValue {@@ -48,4 +51,8 @@ public enum TimerValue {public static TimerValue fromString(String value) {return value == null ? null : valueOf(value.toUpperCase(Locale.ROOT));}++ public MetaItem asMetaItem() {+ return new MetaItem(TimerProtocol.META_TIMER_VALUE_LINK, Values.create(this.toString()));+ }}diff --git a/manager/test/src/test/groovy/org/openremote/test/protocol/TimerProtocolTest.groovy b/manager/test/src/test/groovy/org/openremote/test/protocol/TimerProtocolTest.groovyindex e3b9ca1f4..c8b4324ca 100644--- a/manager/test/src/test/groovy/org/openremote/test/protocol/TimerProtocolTest.groovy+++ b/manager/test/src/test/groovy/org/openremote/test/protocol/TimerProtocolTest.groovy@@ -1,112 +1,177 @@package org.openremote.test.protocol+import org.openremote.agent.protocol.timer.TimerProtocol+import org.openremote.agent.protocol.timer.TimerValueimport org.openremote.manager.server.asset.AssetProcessingServiceimport org.openremote.manager.server.asset.AssetStorageService+import org.openremote.manager.server.asset.ServerAssetimport org.openremote.manager.server.setup.SetupServiceimport org.openremote.manager.server.setup.builtin.ManagerDemoSetup+import org.openremote.model.asset.AssetAttribute+import org.openremote.model.asset.AssetQuery+import org.openremote.model.asset.AssetType+import org.openremote.model.asset.agent.AgentLinkimport org.openremote.model.attribute.AttributeEvent-import org.openremote.model.attribute.AttributeExecuteStatus+import org.openremote.model.attribute.AttributeRef+import org.openremote.model.attribute.AttributeTypeimport org.openremote.model.value.Valuesimport org.openremote.test.ManagerContainerTrait-import spock.lang.Ignore+import org.quartz.CronTrigger+import org.quartz.JobKey+import org.quartz.impl.matchers.GroupMatcherimport spock.lang.Specificationimport spock.util.concurrent.PollingConditions+import static org.openremote.manager.server.setup.builtin.BuiltinSetupTasks.SETUP_IMPORT_DEMO_SCENES+class TimerProtocolTest extends Specification implements ManagerContainerTrait {- // TODO: Test the timer protocol- @Ignore- def "Check time trigger protocol"() {+ def "Check timer protocol agent and device asset deployment"() {given: "expected conditions"def conditions = new PollingConditions(timeout: 15, initialDelay: 0)when: "the container starts"def serverPort = findEphemeralPort()- def container = startContainer(defaultConfig(serverPort), defaultServices())+ def container = startContainerWithDemoScenesAndRules(defaultConfig(serverPort) << [(SETUP_IMPORT_DEMO_SCENES): "true"], defaultServices())def assetStorageService = container.getService(AssetStorageService.class)def assetProcessingService = container.getService(AssetProcessingService.class)def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)+ def timerProtocol = container.getService(TimerProtocol.class)+ ServerAsset sceneAgent+ ServerAsset apartment1then: "the container should be running and attributes linked"conditions.eventually {assertNothingProcessedFor(assetProcessingService, 500)- def apartment1 = assetStorageService.find(managerDemoSetup.apartment1Id, true)- assert apartment1.getAttribute("home").get().getValueAsString().orElse("") == AttributeExecuteStatus.READY.toString()- assert apartment1.getAttribute("away").get().getValueAsString().orElse("") == AttributeExecuteStatus.READY.toString()- assert apartment1.getAttribute("evening").get().getValueAsString().orElse("") == AttributeExecuteStatus.READY.toString()- assert apartment1.getAttribute("night").get().getValueAsString().orElse("") == AttributeExecuteStatus.READY.toString()- assert !apartment1.getAttribute("homeAlarmEnabled").get().getValueAsBoolean().orElse(true)- assert apartment1.getAttribute("homeTargetTemperature").get().getValueAsNumber().orElse(0d) == 21d+ apartment1 = assetStorageService.find(managerDemoSetup.apartment1Id, true)+ sceneAgent = assetStorageService.find(new AssetQuery().name("Scene Agent").type(AssetType.AGENT).parent(managerDemoSetup.apartment1Id))+ assert apartment1.getAttribute("awaySceneTimeFRIDAY").get().getValueAsString().orElse("") == "08:30:00"+ assert sceneAgent != null}- when: "Apartment 1 home scene is executed"- def macroExecute = new AttributeEvent(managerDemoSetup.apartment1Id, "home", AttributeExecuteStatus.REQUEST_START.asValue())- assetProcessingService.sendAttributeEvent(macroExecute)-- then: "Apartment 1 alarm enabled, last scene and living room target temp attribute values should be updated to match the scene"+ and: "the quartz scheduler is running and contains all the demo triggers with the correct cron expression"conditions.eventually {- def apartment1 = assetStorageService.find(managerDemoSetup.apartment1Id, true)- def thermostat1 = assetStorageService.find(managerDemoSetup.apartment1LivingroomThermostatId, true)- assert !apartment1.getAttribute("alarmEnabled").get().getValueAsBoolean().orElse(true)- assert apartment1.getAttribute("lastExecutedScene").get().getValueAsString().orElse("") == "HOME"- assert thermostat1.getAttribute("targetTemperature").get().getValueAsNumber().orElse(0d) == 21d+ assert timerProtocol.cronScheduler != null+ assert timerProtocol.cronScheduler.scheduler != null+ assert timerProtocol.cronScheduler.scheduler.isStarted()+ assert timerProtocol.cronScheduler.scheduler.getJobKeys(GroupMatcher.anyJobGroup()).size() == 28}- then: "Apartment 1 home scene attribute status should show as COMPLETED"+ and: "the quartz job has the correct time"conditions.eventually {- def apartment1 = assetStorageService.find(managerDemoSetup.apartment1Id, true)- assert apartment1.getAttribute("home").get().getValueAsString().orElse("") == AttributeExecuteStatus.COMPLETED.toString()+ def awaySceneFridayRef = new AttributeRef(sceneAgent.id, "awaySceneFriday")+ def timerId = timerProtocol.getTimerId(awaySceneFridayRef)+ def jobKey = JobKey.jobKey("cronJob1", timerId)+ def jobDetail = timerProtocol.cronScheduler.scheduler.getJobDetail(jobKey)+ def triggers = timerProtocol.cronScheduler.scheduler.getTriggersOfJob(jobKey)+ assert jobDetail != null+ assert triggers.size() == 1+ assert triggers[0] instanceof CronTrigger+ def cronTrigger = (CronTrigger)triggers[0]+ assert cronTrigger.cronExpression == "0 30 8 ? * FRI *"}- when: "Apartment 1 away scene is executed"- macroExecute = new AttributeEvent(managerDemoSetup.apartment1Id, "away", AttributeExecuteStatus.REQUEST_START.asValue())- assetProcessingService.sendAttributeEvent(macroExecute)+ when: "a trigger is disabled"+ def disableScene = new AttributeEvent(managerDemoSetup.apartment1Id, "awaySceneEnabledFRIDAY", Values.create(false))+ assetProcessingService.sendAttributeEvent(disableScene)- then: "Apartment 1 alarm enabled, last scene and living room target temp attribute values should be update to match the scene"+ then: "the corresponding cron job should be removed from the cron scheduler"conditions.eventually {- def apartment1 = assetStorageService.find(managerDemoSetup.apartment1Id, true)- def thermostat1 = assetStorageService.find(managerDemoSetup.apartment1LivingroomThermostatId, true)- assert apartment1.getAttribute("alarmEnabled").get().getValueAsBoolean().orElse(false)- assert apartment1.getAttribute("lastExecutedScene").get().getValueAsString().orElse("") == "AWAY"- assert thermostat1.getAttribute("targetTemperature").get().getValueAsNumber().orElse(0d) == 15d+ def awaySceneFridayRef = new AttributeRef(sceneAgent.id, "awaySceneFriday")+ def timerId = timerProtocol.getTimerId(awaySceneFridayRef)+ def jobKey = JobKey.jobKey("cronJob1", timerId)+ def jobDetail = timerProtocol.cronScheduler.scheduler.getJobDetail(jobKey)+ def triggers = timerProtocol.cronScheduler.scheduler.getTriggersOfJob(jobKey)+ assert jobDetail == null+ assert triggers.isEmpty()}- then: "Apartment 1 away scene attribute status should show as COMPLETED"+ when: "the trigger is re-enabled"+ def enableScene = new AttributeEvent(managerDemoSetup.apartment1Id, "awaySceneEnabledFRIDAY", Values.create(true))+ assetProcessingService.sendAttributeEvent(enableScene)++ then: "the quartz job should be recreated and have the correct time"conditions.eventually {- def apartment1 = assetStorageService.find(managerDemoSetup.apartment1Id, true)- assert apartment1.getAttribute("away").get().getValueAsString().orElse("") == AttributeExecuteStatus.COMPLETED.toString()+ def awaySceneFridayRef = new AttributeRef(sceneAgent.id, "awaySceneFriday")+ def timerId = timerProtocol.getTimerId(awaySceneFridayRef)+ def jobKey = JobKey.jobKey("cronJob1", timerId)+ def jobDetail = timerProtocol.cronScheduler.scheduler.getJobDetail(jobKey)+ def triggers = timerProtocol.cronScheduler.scheduler.getTriggersOfJob(jobKey)+ assert jobDetail != null+ assert triggers.size() == 1+ assert triggers[0] instanceof CronTrigger+ def cronTrigger = (CronTrigger)triggers[0]+ assert cronTrigger.cronExpression == "0 30 8 ? * FRI *"}- when: "The target temperature of the home scene is modified via the apartment attribute"- def updateTargetTemp = new AttributeEvent(managerDemoSetup.apartment1Id, "homeTargetTemperature", Values.create(10d))- assetProcessingService.sendAttributeEvent(updateTargetTemp)+ when: "a trigger time is modified"+ def modifyTime = new AttributeEvent(managerDemoSetup.apartment1Id, "awaySceneTimeFRIDAY", Values.create("04:00:00"))+ assetProcessingService.sendAttributeEvent(modifyTime)- then: "Apartment 1 home scene attribute status should reset to show as READY and home target temp should show new value"+ then: "the quartz job should have the new trigger time"conditions.eventually {- def apartment1 = assetStorageService.find(managerDemoSetup.apartment1Id, true)- assert apartment1.getAttribute("home").get().getValueAsString().orElse("") == AttributeExecuteStatus.READY.toString()- assert apartment1.getAttribute("homeTargetTemperature").get().getValueAsNumber().orElse(0d) == 10d- assert !apartment1.getAttribute("homeAlarmEnabled").get().getValueAsBoolean().orElse(true)+ def awaySceneFridayRef = new AttributeRef(sceneAgent.id, "awaySceneFriday")+ def timerId = timerProtocol.getTimerId(awaySceneFridayRef)+ def jobKey = JobKey.jobKey("cronJob1", timerId)+ def jobDetail = timerProtocol.cronScheduler.scheduler.getJobDetail(jobKey)+ def triggers = timerProtocol.cronScheduler.scheduler.getTriggersOfJob(jobKey)+ assert jobDetail != null+ assert triggers.size() == 1+ assert triggers[0] instanceof CronTrigger+ def cronTrigger = (CronTrigger)triggers[0]+ assert cronTrigger.cronExpression == "0 0 4 ? * FRI *"}- when: "Apartment 1 home scene is executed"- macroExecute = new AttributeEvent(managerDemoSetup.apartment1Id, "home", AttributeExecuteStatus.REQUEST_START.asValue())- assetProcessingService.sendAttributeEvent(macroExecute)+ when: "an attribute is added that links to a timers cron expression"+ apartment1.addAttributes(+ new AssetAttribute("awaySceneCronFRIDAY", AttributeType.STRING)+ .addMeta(+ AgentLink.asAgentLinkMetaItem(new AttributeRef(sceneAgent.id, "awaySceneFriday")),+ TimerValue.CRON_EXPRESSION.asMetaItem()+ )+ )+ apartment1 = assetStorageService.merge(apartment1)++ then: "the attributes value should contain the timers cron expression"+ conditions.eventually {+ apartment1 = assetStorageService.find(apartment1.id, true)+ apartment1.getAttribute("awaySceneCronFRIDAY").get().getValueAsString() == "0 0 4 ? * FRI *"+ }++ when: "the trigger cron expression is modified"+ def modifyCron = new AttributeEvent(managerDemoSetup.apartment1Id, "awaySceneCronFRIDAY", Values.create("0 0 4 ? * MON,FRI *"))+ assetProcessingService.sendAttributeEvent(modifyCron)- then: "Apartment 1 alarm enabled, last scene and living room target temp attribute values should be updated to match the scene"+ then: "the quartz job should have the new cron expression"conditions.eventually {- def apartment1 = assetStorageService.find(managerDemoSetup.apartment1Id, true)- def thermostat1 = assetStorageService.find(managerDemoSetup.apartment1LivingroomThermostatId, true)- assert !apartment1.getAttribute("alarmEnabled").get().getValueAsBoolean().orElse(true)- assert apartment1.getAttribute("lastExecutedScene").get().getValueAsString().orElse("") == "HOME"- assert thermostat1.getAttribute("targetTemperature").get().getValueAsNumber().orElse(0d) == 10d+ def awaySceneFridayRef = new AttributeRef(sceneAgent.id, "awaySceneFriday")+ def timerId = timerProtocol.getTimerId(awaySceneFridayRef)+ def jobKey = JobKey.jobKey("cronJob1", timerId)+ def jobDetail = timerProtocol.cronScheduler.scheduler.getJobDetail(jobKey)+ def triggers = timerProtocol.cronScheduler.scheduler.getTriggersOfJob(jobKey)+ assert jobDetail != null+ assert triggers.size() == 1+ assert triggers[0] instanceof CronTrigger+ def cronTrigger = (CronTrigger)triggers[0]+ assert cronTrigger.cronExpression == "0 0 4 ? * MON,FRI *"}- then: "Apartment 1 home scene attribute status should show as COMPLETED"+ when: "a trigger is deleted"+ sceneAgent = assetStorageService.find(sceneAgent.id)+ sceneAgent.removeAttribute("awaySceneFriday")+ sceneAgent = assetStorageService.merge(sceneAgent)++ then: "the corresponding cron job should be removed from the cron scheduler"conditions.eventually {- def apartment1 = assetStorageService.find(managerDemoSetup.apartment1Id, true)- assert apartment1.getAttribute("home").get().getValueAsString().orElse("") == AttributeExecuteStatus.COMPLETED.toString()+ assert !sceneAgent.getAttribute("awaySceneFriday").isPresent()+ def awaySceneFridayRef = new AttributeRef(sceneAgent.id, "awaySceneFriday")+ def timerId = timerProtocol.getTimerId(awaySceneFridayRef)+ def jobKey = JobKey.jobKey("cronJob1", timerId)+ def jobDetail = timerProtocol.cronScheduler.scheduler.getJobDetail(jobKey)+ def triggers = timerProtocol.cronScheduler.scheduler.getTriggersOfJob(jobKey)+ assert jobDetail == null+ assert triggers.isEmpty()}cleanup: "the server should be stopped"