Search in sources :

Example 1 with RuleAddedEvent

use of org.openhab.core.automation.events.RuleAddedEvent in project openhab-core by openhab.

the class AutomationIntegrationTest method assertThatARuleCanBeAddedUpdatedAndRemovedByTheApi.

@Test
public void assertThatARuleCanBeAddedUpdatedAndRemovedByTheApi() {
    logger.info("assert that a rule can be added, updated and removed by the api");
    EventSubscriber ruleEventHandler = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Stream.of(RuleAddedEvent.TYPE, RuleRemovedEvent.TYPE, RuleUpdatedEvent.TYPE).collect(toSet());
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event e) {
            logger.info("RuleEvent: {}", e.getTopic());
            ruleEvent = e;
        }
    };
    registerService(ruleEventHandler);
    // ADD
    Rule rule = createSimpleRule();
    ruleRegistry.add(rule);
    waitForAssert(() -> {
        assertThat(ruleEvent, is(notNullValue()));
        assertThat(ruleEvent, is(instanceOf(RuleAddedEvent.class)));
        RuleAddedEvent ruleAddedEvent = (RuleAddedEvent) ruleEvent;
        assertThat(ruleAddedEvent.getRule().uid, is(rule.getUID()));
    }, 5000, 500);
    Rule ruleAdded = ruleRegistry.get(rule.getUID());
    assertThat(ruleAdded, is(notNullValue()));
    assertThat(ruleEngine.getStatusInfo(rule.getUID()).getStatus(), is(RuleStatus.IDLE));
    // UPDATE
    ruleEvent = null;
    if (ruleAdded == null) {
        throw new AssertionError("ruleAdded is null");
    }
    Rule updatedRule = RuleBuilder.create(ruleAdded).withDescription("TestDescription").build();
    Rule oldRule = ruleRegistry.update(updatedRule);
    waitForAssert(() -> {
        assertThat(ruleEvent, is(notNullValue()));
        assertThat(ruleEvent, is(instanceOf(RuleUpdatedEvent.class)));
        RuleUpdatedEvent ruEvent = (RuleUpdatedEvent) ruleEvent;
        assertThat(ruEvent.getRule().uid, is(rule.getUID()));
        assertThat(ruEvent.getOldRule().uid, is(rule.getUID()));
        assertThat(ruEvent.getRule().description, is("TestDescription"));
        assertThat(ruEvent.getOldRule().description, is(nullValue()));
    });
    assertThat(oldRule, is(notNullValue()));
    assertThat(oldRule, is(rule));
    // REMOVE
    ruleEvent = null;
    Rule removed = ruleRegistry.remove(rule.getUID());
    waitForAssert(() -> {
        assertThat(ruleEvent, is(notNullValue()));
        assertThat(ruleEvent, is(instanceOf(RuleRemovedEvent.class)));
        RuleRemovedEvent reEvent = (RuleRemovedEvent) ruleEvent;
        assertThat(reEvent.getRule().uid, is(removed.getUID()));
    });
    assertThat(removed, is(notNullValue()));
    assertThat(removed, is(ruleAdded));
    assertThat(ruleRegistry.get(removed.getUID()), is(nullValue()));
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) RuleRemovedEvent(org.openhab.core.automation.events.RuleRemovedEvent) 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) RuleAddedEvent(org.openhab.core.automation.events.RuleAddedEvent) 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 2 with RuleAddedEvent

use of org.openhab.core.automation.events.RuleAddedEvent in project openhab-core by openhab.

the class RuleEventFactory method createRuleAddedEvent.

/**
 * Creates a rule added event.
 *
 * @param rule the rule for which this event is created.
 * @param source the source of the event.
 * @return {@link RuleAddedEvent} instance.
 */
public static RuleAddedEvent createRuleAddedEvent(Rule rule, String source) {
    String topic = buildTopic(RULE_ADDED_EVENT_TOPIC, rule);
    final RuleDTO ruleDto = RuleDTOMapper.map(rule);
    String payload = serializePayload(ruleDto);
    return new RuleAddedEvent(topic, payload, source, ruleDto);
}
Also used : RuleDTO(org.openhab.core.automation.dto.RuleDTO) RuleAddedEvent(org.openhab.core.automation.events.RuleAddedEvent)

Aggregations

RuleAddedEvent (org.openhab.core.automation.events.RuleAddedEvent)2 Test (org.junit.jupiter.api.Test)1 Rule (org.openhab.core.automation.Rule)1 RuleDTO (org.openhab.core.automation.dto.RuleDTO)1 RuleRemovedEvent (org.openhab.core.automation.events.RuleRemovedEvent)1 RuleStatusInfoEvent (org.openhab.core.automation.events.RuleStatusInfoEvent)1 RuleUpdatedEvent (org.openhab.core.automation.events.RuleUpdatedEvent)1 Event (org.openhab.core.events.Event)1 EventSubscriber (org.openhab.core.events.EventSubscriber)1 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)1 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)1