Search in sources :

Example 1 with RuleAddedEvent

use of org.eclipse.smarthome.automation.events.RuleAddedEvent in project smarthome by eclipse.

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.eclipse.smarthome.automation.dto.RuleDTO) RuleAddedEvent(org.eclipse.smarthome.automation.events.RuleAddedEvent)

Example 2 with RuleAddedEvent

use of org.eclipse.smarthome.automation.events.RuleAddedEvent in project smarthome by eclipse.

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
        @NonNull
        public Set<@NonNull String> getSubscribedEventTypes() {
            return Stream.of(RuleAddedEvent.TYPE, RuleRemovedEvent.TYPE, RuleUpdatedEvent.TYPE).collect(toSet());
        }

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

        @Override
        public void receive(@NonNull 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 NullPointerException();
    }
    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.eclipse.smarthome.core.events.EventSubscriber) NonNull(org.eclipse.jdt.annotation.NonNull) RuleRemovedEvent(org.eclipse.smarthome.automation.events.RuleRemovedEvent) RuleRemovedEvent(org.eclipse.smarthome.automation.events.RuleRemovedEvent) RuleAddedEvent(org.eclipse.smarthome.automation.events.RuleAddedEvent) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) RuleUpdatedEvent(org.eclipse.smarthome.automation.events.RuleUpdatedEvent) Event(org.eclipse.smarthome.core.events.Event) RuleAddedEvent(org.eclipse.smarthome.automation.events.RuleAddedEvent) RuleUpdatedEvent(org.eclipse.smarthome.automation.events.RuleUpdatedEvent) Rule(org.eclipse.smarthome.automation.Rule) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

RuleAddedEvent (org.eclipse.smarthome.automation.events.RuleAddedEvent)2 NonNull (org.eclipse.jdt.annotation.NonNull)1 Rule (org.eclipse.smarthome.automation.Rule)1 RuleDTO (org.eclipse.smarthome.automation.dto.RuleDTO)1 RuleRemovedEvent (org.eclipse.smarthome.automation.events.RuleRemovedEvent)1 RuleStatusInfoEvent (org.eclipse.smarthome.automation.events.RuleStatusInfoEvent)1 RuleUpdatedEvent (org.eclipse.smarthome.automation.events.RuleUpdatedEvent)1 Event (org.eclipse.smarthome.core.events.Event)1 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)1 ItemCommandEvent (org.eclipse.smarthome.core.items.events.ItemCommandEvent)1 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)1 Test (org.junit.Test)1