use of org.openhab.core.automation.Rule in project openhab-core by openhab.
the class AutomationIntegrationTest method assertThatRuleNowMethodExecutesActionsOfTheRule.
@Test
public void assertThatRuleNowMethodExecutesActionsOfTheRule() throws ItemNotFoundException {
Configuration triggerConfig = new Configuration(Map.of("eventTopic", "runNowEventTopic/*"));
Map<String, Object> params = new HashMap<>();
params.put("itemName", "myLampItem3");
params.put("command", "TOGGLE");
Configuration actionConfig = new Configuration(params);
params = new HashMap<>();
params.put("itemName", "myLampItem3");
params.put("command", "ON");
Configuration actionConfig2 = new Configuration(params);
params = new HashMap<>();
params.put("itemName", "myLampItem3");
params.put("command", "OFFF");
Configuration actionConfig3 = new Configuration(params);
List<Trigger> triggers = List.of(ModuleBuilder.createTrigger().withId("GenericEventTriggerId").withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
List<Action> actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandActionId").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build(), ModuleBuilder.createAction().withId("ItemPostCommandActionId2").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig2).build(), ModuleBuilder.createAction().withId("ItemPostCommandActionId3").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig3).build());
Rule rule = RuleBuilder.create("runNowRule" + new Random().nextInt()).withTriggers(triggers).withActions(actions).build();
logger.info("Rule created: {}", rule.getUID());
ruleRegistry.add(rule);
// TEST RULE
waitForAssert(() -> {
assertThat(ruleEngine.getStatusInfo(rule.getUID()).getStatus(), is(RuleStatus.IDLE));
}, 3000, 100);
EventSubscriber itemEventHandler = new EventSubscriber() {
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ItemCommandEvent.TYPE);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
@Override
public void receive(Event e) {
logger.info("Event: {}", e.getTopic());
if (e.getTopic().contains("myLampItem3")) {
itemEvent = e;
}
}
};
registerService(itemEventHandler);
ruleEngine.runNow(rule.getUID());
waitForAssert(() -> {
assertThat(itemEvent, is(notNullValue()));
}, 3000, 100);
waitForAssert(() -> {
assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.ON));
}, 3000, 100);
ruleRegistry.remove(rule.getUID());
}
use of org.openhab.core.automation.Rule in project openhab-core by openhab.
the class AutomationIntegrationTest method assertThatARuleCreatedFromATemplateIsExecutedAsExpected.
@Test
public void assertThatARuleCreatedFromATemplateIsExecutedAsExpected() {
logger.info("assert that a rule created from a template is executed as expected");
TemplateRegistry<?> templateRegistry = getService(TemplateRegistry.class);
assertThat(templateRegistry, is(notNullValue()));
waitForAssert(() -> {
Template template = null;
template = templateRegistry.get("SimpleTestTemplate");
assertThat(template, is(notNullValue()));
assertThat(template.getTags(), is(notNullValue()));
assertThat(template.getTags().size(), is(not(0)));
});
Map<String, Object> configs = new HashMap<>();
configs.put("onItem", "templ_MotionItem");
configs.put("ifState", "ON");
configs.put("updateItem", "templ_LampItem");
configs.put("updateCommand", "ON");
Rule templateRule = RuleBuilder.create("templateRuleUID").withTemplateUID("SimpleTestTemplate").withConfiguration(new Configuration(configs)).build();
ruleRegistry.add(templateRule);
assertThat(ruleRegistry.get(templateRule.getUID()), is(notNullValue()));
EventSubscriber itemEventHandler = new EventSubscriber() {
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ItemCommandEvent.TYPE);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
@Override
public void receive(Event e) {
logger.info("Event: {}", e.getTopic());
if (e.getTopic().contains("templ_LampItem")) {
itemEvent = e;
}
}
};
registerService(itemEventHandler);
// causing the event to trigger the rule
eventPublisher.post(ItemEventFactory.createStateEvent("templ_MotionItem", OnOffType.ON));
waitForAssert(() -> {
assertThat(itemEvent, is(notNullValue()));
});
assertThat(itemEvent.getTopic(), is(equalTo("openhab/items/templ_LampItem/command")));
assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.ON));
}
use of org.openhab.core.automation.Rule in project openhab-core by openhab.
the class RuntimeRuleTest method itemStateUpdatedBySimpleRule.
@Test
public void itemStateUpdatedBySimpleRule() throws ItemNotFoundException, InterruptedException {
final Configuration triggerConfig = new Configuration(Map.ofEntries(entry("eventSource", "myMotionItem2"), entry("eventTopic", "openhab/*"), entry("eventTypes", "ItemStateEvent")));
final Configuration actionConfig = new Configuration(Map.ofEntries(entry("itemName", "myLampItem2"), entry("command", "ON")));
final Rule rule = RuleBuilder.create("myRule21" + new Random().nextInt()).withTriggers(ModuleBuilder.createTrigger().withId("ItemStateChangeTrigger2").withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build()).withActions(ModuleBuilder.createAction().withId("ItemPostCommandAction2").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build()).withName("RuleByJAVA_API").build();
logger.info("RuleImpl created: {}", rule.getUID());
final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
final RuleManager ruleEngine = getService(RuleManager.class);
ruleRegistry.add(rule);
ruleEngine.setEnabled(rule.getUID(), true);
waitForAssert(() -> {
assertEquals(RuleStatus.IDLE, ruleEngine.getStatusInfo(rule.getUID()).getStatus());
});
// Test rule
final EventPublisher eventPublisher = getService(EventPublisher.class);
assertNotNull(eventPublisher);
eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem2", OnOffType.ON));
final Queue<Event> events = new LinkedList<>();
subscribeToEvents(ItemCommandEvent.TYPE, events);
eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem2", OnOffType.ON));
waitForAssert(() -> {
assertFalse(events.isEmpty());
ItemCommandEvent event = (ItemCommandEvent) events.remove();
assertEquals("openhab/items/myLampItem2/command", event.getTopic());
assertEquals(OnOffType.ON, event.getItemCommand());
});
}
use of org.openhab.core.automation.Rule in project openhab-core by openhab.
the class RuleEngineTest method testRuleActions.
/**
* test rule actions
*/
@Test
public void testRuleActions() {
RuleImpl rule1 = createRule();
List<Action> actions = new ArrayList<>(rule1.getActions());
ruleRegistry.add(rule1);
Rule rule1Get = ruleEngine.getRule("rule1");
List<Action> actionsGet = rule1Get.getActions();
assertNotNull(actionsGet, "Null actions list");
assertEquals(1, actionsGet.size(), "Empty actions list");
assertEquals(actionsGet, rule1Get.getActions(), "Returned actions list should not be a copy");
actions.add(ModuleBuilder.createAction().withId("actionId2").withTypeUID("typeUID2").build());
rule1.setActions(actions);
ruleEngine.addRule(rule1);
rule1Get = ruleEngine.getRule("rule1");
List<Action> actionsGet2 = rule1Get.getActions();
assertNotNull(actionsGet2, "Null actions list");
assertEquals(2, actionsGet2.size(), "Action was not added to the rule's list of actions");
assertNotNull(rule1Get.getModule("actionId2"), "RuleImpl action with wrong id is returned");
actions.add(ModuleBuilder.createAction().withId("actionId3").withTypeUID("typeUID3").build());
// ruleEngine.update will update the RuleImpl2.moduleMap with the new module
ruleEngine.addRule(rule1);
rule1Get = ruleEngine.getRule("rule1");
List<Action> actionsGet3 = rule1Get.getActions();
assertNotNull(actionsGet3, "Null actions list");
assertEquals(3, actionsGet3.size(), "Action was not added to the rule's list of actions");
assertNotNull(ruleEngine.getRule("rule1").getModule("actionId3"), "RuleImpl modules map was not updated");
}
use of org.openhab.core.automation.Rule in project openhab-core by openhab.
the class RuleEngineTest method testRuleConfigNull.
/**
* test rule configurations with null
*/
@Test
public void testRuleConfigNull() {
Rule rule3 = RuleBuilder.create("rule3").withTriggers(createTriggers("typeUID")).withConditions(createConditions("typeUID")).withActions(createActions("typeUID")).build();
ruleRegistry.add(rule3);
Rule rule3Get = ruleEngine.getRule("rule3");
assertNotNull(rule3Get.getConfiguration(), "RuleImpl configuration is null");
}
Aggregations