Search in sources :

Example 26 with Rule

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());
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) Action(org.openhab.core.automation.Action) Configuration(org.openhab.core.config.core.Configuration) HashMap(java.util.HashMap) Trigger(org.openhab.core.automation.Trigger) Random(java.util.Random) RuleRemovedEvent(org.openhab.core.automation.events.RuleRemovedEvent) RuleAddedEvent(org.openhab.core.automation.events.RuleAddedEvent) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) RuleUpdatedEvent(org.openhab.core.automation.events.RuleUpdatedEvent) Rule(org.openhab.core.automation.Rule) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 27 with Rule

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));
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) Configuration(org.openhab.core.config.core.Configuration) HashMap(java.util.HashMap) RuleRemovedEvent(org.openhab.core.automation.events.RuleRemovedEvent) RuleAddedEvent(org.openhab.core.automation.events.RuleAddedEvent) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) RuleUpdatedEvent(org.openhab.core.automation.events.RuleUpdatedEvent) Rule(org.openhab.core.automation.Rule) Template(org.openhab.core.automation.template.Template) RuleTemplate(org.openhab.core.automation.template.RuleTemplate) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 28 with Rule

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());
    });
}
Also used : Configuration(org.openhab.core.config.core.Configuration) Random(java.util.Random) EventPublisher(org.openhab.core.events.EventPublisher) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) RuleRegistry(org.openhab.core.automation.RuleRegistry) RuleManager(org.openhab.core.automation.RuleManager) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) Rule(org.openhab.core.automation.Rule) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 29 with Rule

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");
}
Also used : Action(org.openhab.core.automation.Action) ArrayList(java.util.ArrayList) Rule(org.openhab.core.automation.Rule) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 30 with Rule

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");
}
Also used : Rule(org.openhab.core.automation.Rule) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

Rule (org.openhab.core.automation.Rule)76 Test (org.junit.jupiter.api.Test)47 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)36 Configuration (org.openhab.core.config.core.Configuration)27 Trigger (org.openhab.core.automation.Trigger)23 Action (org.openhab.core.automation.Action)21 Event (org.openhab.core.events.Event)19 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)19 EventSubscriber (org.openhab.core.events.EventSubscriber)16 RuleStatusInfoEvent (org.openhab.core.automation.events.RuleStatusInfoEvent)15 RuleRegistry (org.openhab.core.automation.RuleRegistry)14 HashMap (java.util.HashMap)13 Random (java.util.Random)13 EventPublisher (org.openhab.core.events.EventPublisher)13 ArrayList (java.util.ArrayList)12 Response (javax.ws.rs.core.Response)12 Operation (io.swagger.v3.oas.annotations.Operation)10 Path (javax.ws.rs.Path)10 RuleManager (org.openhab.core.automation.RuleManager)10 RuleAddedEvent (org.openhab.core.automation.events.RuleAddedEvent)10