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