use of org.eclipse.smarthome.automation.events.RuleRemovedEvent in project smarthome by eclipse.
the class RuleEventFactory method createRuleRemovedEvent.
/**
* Creates a rule removed event.
*
* @param rule the rule for which this event is created.
* @param source the source of the event.
* @return {@link RuleRemovedEvent} instance.
*/
public static RuleRemovedEvent createRuleRemovedEvent(Rule rule, String source) {
String topic = buildTopic(RULE_REMOVED_EVENT_TOPIC, rule);
final RuleDTO ruleDto = RuleDTOMapper.map(rule);
String payload = serializePayload(ruleDto);
return new RuleRemovedEvent(topic, payload, source, ruleDto);
}
use of org.eclipse.smarthome.automation.events.RuleRemovedEvent 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.RuleRemovedEvent in project smarthome by eclipse.
the class RuleEventTest method testRuleEvents.
@Test
public void testRuleEvents() throws ItemNotFoundException {
// Registering eventSubscriber
List<Event> ruleEvents = new ArrayList<>();
EventSubscriber ruleEventHandler = new EventSubscriber() {
@Override
public Set<String> getSubscribedEventTypes() {
Set<String> types = new HashSet<>();
types.add(RuleAddedEvent.TYPE);
types.add(RuleRemovedEvent.TYPE);
types.add(RuleStatusInfoEvent.TYPE);
types.add(RuleUpdatedEvent.TYPE);
return types;
}
@Override
public EventFilter getEventFilter() {
return null;
}
@Override
public void receive(Event event) {
logger.info("RuleEvent: {}", event.getTopic());
ruleEvents.add(event);
}
};
registerService(ruleEventHandler);
// Creation of RULE
Map<String, Object> triggerCfgEntries = new HashMap<>();
triggerCfgEntries.put("eventSource", "myMotionItem2");
triggerCfgEntries.put("eventTopic", "smarthome/*");
triggerCfgEntries.put("eventTypes", "ItemStateEvent");
Configuration triggerConfig = new Configuration(triggerCfgEntries);
Map<String, Object> actionCfgEntries = new HashMap<>();
actionCfgEntries.put("itemName", "myLampItem2");
actionCfgEntries.put("command", "ON");
Configuration actionConfig = new Configuration(actionCfgEntries);
List<Trigger> triggers = Collections.singletonList(ModuleBuilder.createTrigger().withId("ItemStateChangeTrigger2").withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
List<Action> actions = Collections.singletonList(ModuleBuilder.createAction().withId("ItemPostCommandAction2").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
Rule rule = RuleBuilder.create("myRule21").withTriggers(triggers).withActions(actions).withName("RuleEventTestingRule").build();
logger.info("Rule created: {}", rule.getUID());
RuleRegistry ruleRegistry = getService(RuleRegistry.class);
RuleManager ruleEngine = getService(RuleManager.class);
ruleRegistry.add(rule);
ruleEngine.setEnabled(rule.getUID(), true);
waitForAssert(() -> {
assertThat(ruleEngine.getStatusInfo(rule.getUID()).getStatus(), is(RuleStatus.IDLE));
});
// TEST RULE
EventPublisher eventPublisher = getService(EventPublisher.class);
ItemRegistry itemRegistry = getService(ItemRegistry.class);
SwitchItem myMotionItem = (SwitchItem) itemRegistry.getItem("myMotionItem2");
assertNotNull(myMotionItem);
eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem2", OnOffType.ON));
EventSubscriber itemEventHandler = new EventSubscriber() {
@Override
public void receive(Event event) {
logger.info("Event: {}", event.getTopic());
if (event instanceof ItemCommandEvent && event.getTopic().contains("myLampItem2")) {
itemEvent = event;
}
}
@Override
public Set<String> getSubscribedEventTypes() {
return Collections.singleton(ItemCommandEvent.TYPE);
}
@Override
public EventFilter getEventFilter() {
return null;
}
};
registerService(itemEventHandler);
eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem2", OnOffType.ON));
waitForAssert(() -> assertThat(itemEvent, is(notNullValue())));
assertThat(itemEvent.getTopic(), is(equalTo("smarthome/items/myLampItem2/command")));
assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.ON));
assertThat(ruleEvents.size(), is(not(0)));
assertThat(ruleEvents.stream().filter(e -> e.getTopic().equals("smarthome/rules/myRule21/added")).findFirst().isPresent(), is(true));
assertThat(ruleEvents.stream().filter(e -> e.getTopic().equals("smarthome/rules/myRule21/state")).findFirst().isPresent(), is(true));
List<Event> stateEvents = ruleEvents.stream().filter(e -> e.getTopic().equals("smarthome/rules/myRule21/state")).collect(Collectors.toList());
assertThat(stateEvents, is(notNullValue()));
Optional<Event> runningEvent = stateEvents.stream().filter(e -> ((RuleStatusInfoEvent) e).getStatusInfo().getStatus() == RuleStatus.RUNNING).findFirst();
assertThat(runningEvent.isPresent(), is(true));
EventSubscriber ruleRemovedEventHandler = new EventSubscriber() {
@Override
public Set<String> getSubscribedEventTypes() {
return Collections.singleton(RuleRemovedEvent.TYPE);
}
@Override
public EventFilter getEventFilter() {
return null;
}
@Override
public void receive(Event event) {
logger.info("RuleRemovedEvent: {}", event.getTopic());
ruleRemovedEvent = event;
}
};
registerService(ruleRemovedEventHandler);
ruleRegistry.remove("myRule21");
waitForAssert(() -> {
assertThat(ruleRemovedEvent, is(notNullValue()));
assertThat(ruleRemovedEvent.getTopic(), is(equalTo("smarthome/rules/myRule21/removed")));
});
}
Aggregations