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()));
}
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);
}
Aggregations