Search in sources :

Example 1 with RuleUpdatedEvent

use of org.eclipse.smarthome.automation.events.RuleUpdatedEvent 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)

Example 2 with RuleUpdatedEvent

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

the class RuleEventFactory method createRuleUpdatedEvent.

/**
 * Creates a rule updated event.
 *
 * @param rule    the new rule.
 * @param oldRule the rule that has been updated.
 * @param source  the source of the event.
 * @return {@link RuleUpdatedEvent} instance.
 */
public static RuleUpdatedEvent createRuleUpdatedEvent(Rule rule, Rule oldRule, String source) {
    String topic = buildTopic(RULE_UPDATED_EVENT_TOPIC, rule);
    final RuleDTO ruleDto = RuleDTOMapper.map(rule);
    final RuleDTO oldRuleDto = RuleDTOMapper.map(oldRule);
    List<RuleDTO> rules = new LinkedList<>();
    rules.add(ruleDto);
    rules.add(oldRuleDto);
    String payload = serializePayload(rules);
    return new RuleUpdatedEvent(topic, payload, source, ruleDto, oldRuleDto);
}
Also used : RuleDTO(org.eclipse.smarthome.automation.dto.RuleDTO) RuleUpdatedEvent(org.eclipse.smarthome.automation.events.RuleUpdatedEvent) LinkedList(java.util.LinkedList)

Aggregations

RuleUpdatedEvent (org.eclipse.smarthome.automation.events.RuleUpdatedEvent)2 LinkedList (java.util.LinkedList)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 Rule (org.eclipse.smarthome.automation.Rule)1 RuleDTO (org.eclipse.smarthome.automation.dto.RuleDTO)1 RuleAddedEvent (org.eclipse.smarthome.automation.events.RuleAddedEvent)1 RuleRemovedEvent (org.eclipse.smarthome.automation.events.RuleRemovedEvent)1 RuleStatusInfoEvent (org.eclipse.smarthome.automation.events.RuleStatusInfoEvent)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