diff --git a/bundles/org.openhab.core.model.rule/src/org/openhab/core/model/rule/Rules.xtext b/bundles/org.openhab.core.model.rule/src/org/openhab/core/model/rule/Rules.xtext index 4eabd1ea84e..db9107f007b 100644 --- a/bundles/org.openhab.core.model.rule/src/org/openhab/core/model/rule/Rules.xtext +++ b/bundles/org.openhab.core.model.rule/src/org/openhab/core/model/rule/Rules.xtext @@ -13,7 +13,7 @@ VariableDeclaration: Rule: 'rule' name=(STRING|ID) ('uid' '=' uid=(STRING|ID))? ('[' tags+=(ID|STRING) (',' tags+=(ID|STRING))* ']')? - 'when' eventtrigger+=EventTrigger ('or' eventtrigger+=EventTrigger)* + ('when' eventtrigger+=EventTrigger ('or' eventtrigger+=EventTrigger)*)? ('but' 'only' 'if' conditions+=Condition ('and' conditions+=Condition)*)? 'then' script=XExpressionInClosure 'end' diff --git a/itests/org.openhab.core.model.rule.tests/src/main/java/org/openhab/core/model/rule/runtime/DSLRuleProviderTest.java b/itests/org.openhab.core.model.rule.tests/src/main/java/org/openhab/core/model/rule/runtime/DSLRuleProviderTest.java index c93e4b03e7c..4eeb1842977 100644 --- a/itests/org.openhab.core.model.rule.tests/src/main/java/org/openhab/core/model/rule/runtime/DSLRuleProviderTest.java +++ b/itests/org.openhab.core.model.rule.tests/src/main/java/org/openhab/core/model/rule/runtime/DSLRuleProviderTest.java @@ -116,7 +116,8 @@ public void testSimpleRules() { " logInfo('Test', 'Test')\n" + // "end\n\n"; - modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); + modelRepository.addOrRefreshModel(TESTMODEL_NAME, + new ByteArrayInputStream(model.getBytes(StandardCharsets.UTF_8))); Collection actualRules = dslRuleProvider.getAll(); assertThat(actualRules.size(), is(2)); @@ -131,7 +132,9 @@ public void testSimpleRules() { assertThat(firstRule.getTags(), hasItem("Test")); assertThat(firstRule.getTags(), hasItem("my test")); assertThat(firstRule.getConditions(), hasSize(0)); + assertThat(firstRule.getTriggers(), hasSize(1)); assertThat(firstRule.getTriggers().getFirst().getTypeUID(), is(SystemTriggerHandler.STARTLEVEL_MODULE_TYPE_ID)); + assertThat(firstRule.getActions(), hasSize(1)); assertThat(firstRule.getActions().getFirst().getTypeUID(), is(ScriptActionHandler.TYPE_ID)); assertThat(firstRule.getActions().getFirst().getConfiguration() .get(AbstractScriptModuleHandler.CONFIG_SCRIPT_TYPE), is(DSLScriptEngine.MIMETYPE_OPENHAB_DSL_RULE)); @@ -141,15 +144,46 @@ public void testSimpleRules() { assertThat(secondRule.getUID(), is("dslruletest-2")); assertThat(secondRule.getName(), is("Rule Number Two")); assertThat(secondRule.getTags(), hasSize(0)); + assertThat(secondRule.getTriggers(), hasSize(1)); assertThat(secondRule.getTriggers().getFirst().getTypeUID(), is(ItemStateTriggerHandler.CHANGE_MODULE_TYPE_ID)); assertThat(secondRule.getTriggers().getFirst().getConfiguration().get(ItemStateTriggerHandler.CFG_ITEMNAME), is("X")); assertThat(secondRule.getConditions(), hasSize(0)); + assertThat(secondRule.getActions(), hasSize(1)); assertThat(secondRule.getActions().getFirst().getTypeUID(), is(ScriptActionHandler.TYPE_ID)); assertThat(secondRule.getActions().getFirst().getConfiguration() .get(AbstractScriptModuleHandler.CONFIG_SCRIPT_TYPE), is(DSLScriptEngine.MIMETYPE_OPENHAB_DSL_RULE)); } + @Test + public void testNoTrigger() { + Collection rules = dslRuleProvider.getAll(); + assertThat(rules.size(), is(0)); + + String model = "rule RuleNoTrigger\n" + // + "then\n" + // + " logInfo('Test', 'Test')\n" + // + "end\n\n"; + + modelRepository.addOrRefreshModel(TESTMODEL_NAME, + new ByteArrayInputStream(model.getBytes(StandardCharsets.UTF_8))); + Collection actualRules = dslRuleProvider.getAll(); + + assertThat(actualRules.size(), is(1)); + + Rule rule = actualRules.iterator().next(); + + assertThat(rule.getUID(), is("dslruletest-1")); + assertThat(rule.getName(), is("RuleNoTrigger")); + assertThat(rule.getTags(), hasSize(0)); + assertThat(rule.getConditions(), hasSize(0)); + assertThat(rule.getTriggers(), hasSize(0)); + assertThat(rule.getActions(), hasSize(1)); + assertThat(rule.getActions().getFirst().getTypeUID(), is(ScriptActionHandler.TYPE_ID)); + assertThat(rule.getActions().getFirst().getConfiguration().get(AbstractScriptModuleHandler.CONFIG_SCRIPT_TYPE), + is(DSLScriptEngine.MIMETYPE_OPENHAB_DSL_RULE)); + } + @Test public void testAllTriggers() { Collection rules = dslRuleProvider.getAll(); @@ -378,7 +412,8 @@ public void testVars() { " logInfo('Test', 'Test')\n" + // "end\n\n"; - modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); + modelRepository.addOrRefreshModel(TESTMODEL_NAME, + new ByteArrayInputStream(model.getBytes(StandardCharsets.UTF_8))); Collection actualRules = dslRuleProvider.getAll(); assertThat(actualRules.size(), is(1));