use of org.openhab.core.events.EventFilter in project openhab-core by openhab.
the class GenericItemProviderTest method assertThatItemEventsAreSentOnlyOncePerItemEvenWithMultipleItemFiles.
@Test
public void assertThatItemEventsAreSentOnlyOncePerItemEvenWithMultipleItemFiles() {
List<AbstractItemRegistryEvent> receivedEvents = new ArrayList<>();
EventSubscriber itemEventSubscriber = new EventSubscriber() {
@Override
public void receive(Event event) {
logger.debug("Received event: {}", event);
receivedEvents.add((AbstractItemRegistryEvent) event);
}
@Override
public Set<String> getSubscribedEventTypes() {
return Stream.of(ItemAddedEvent.TYPE, ItemUpdatedEvent.TYPE, ItemRemovedEvent.TYPE).collect(toSet());
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
};
registerService(itemEventSubscriber);
assertThat(itemRegistry.getAll(), hasSize(0));
receivedEvents.clear();
String model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n" + "String test2 \"Test Item [%s]\" { channel=\"test:test:test:test\" }";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
waitForAssert(() -> {
assertThat(itemRegistry.getAll(), hasSize(2));
assertThat(receivedEvents, hasSize(2));
assertThat(receivedEvents.stream().filter(e -> "test1".equals(e.getItem().name)).findFirst().get(), instanceOf(ItemAddedEvent.class));
assertThat(receivedEvents.stream().filter(e -> "test2".equals(e.getItem().name)).findFirst().get(), instanceOf(ItemAddedEvent.class));
});
receivedEvents.clear();
model = "String test3 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n" + "String test4 \"Test Item [%s]\" { channel=\"test:test:test:test\" }";
modelRepository.addOrRefreshModel(TESTMODEL_NAME2, new ByteArrayInputStream(model.getBytes()));
// only ItemAddedEvents for items test3 and test4 should be fired, NOT for test1 and test2 again
waitForAssert(() -> {
assertThat(itemRegistry.getAll(), hasSize(4));
assertThat(receivedEvents, hasSize(2));
assertThat(receivedEvents.stream().filter(e -> "test3".equals(e.getItem().name)).findFirst().get(), instanceOf(ItemAddedEvent.class));
assertThat(receivedEvents.stream().filter(e -> "test4".equals(e.getItem().name)).findFirst().get(), instanceOf(ItemAddedEvent.class));
});
unregisterService(itemEventSubscriber);
}
use of org.openhab.core.events.EventFilter in project openhab-core by openhab.
the class GenericItemProviderTest method assertThatItemEventsAreSentCorrectly.
@Test
public void assertThatItemEventsAreSentCorrectly() {
List<AbstractItemRegistryEvent> receivedEvents = new ArrayList<>();
EventSubscriber itemEventSubscriber = new EventSubscriber() {
@Override
public void receive(Event event) {
logger.debug("Received event: {}", event);
receivedEvents.add((AbstractItemRegistryEvent) event);
}
@Override
public Set<String> getSubscribedEventTypes() {
return Stream.of(ItemAddedEvent.TYPE, ItemUpdatedEvent.TYPE, ItemRemovedEvent.TYPE).collect(toSet());
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
};
registerService(itemEventSubscriber);
assertThat(itemRegistry.getAll(), hasSize(0));
receivedEvents.clear();
String model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n" + "String test2 \"Test Item [%s]\" { channel=\"test:test:test:test\" }";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
waitForAssert(() -> {
assertThat(itemRegistry.getAll(), hasSize(2));
assertThat(receivedEvents, hasSize(2));
assertThat(receivedEvents.stream().filter(e -> "test1".equals(e.getItem().name)).findFirst().get(), instanceOf(ItemAddedEvent.class));
assertThat(receivedEvents.stream().filter(e -> "test2".equals(e.getItem().name)).findFirst().get(), instanceOf(ItemAddedEvent.class));
});
receivedEvents.clear();
model = "String test1 \"Test Item Changed [%s]\" { channel=\"test:test:test:test\" }";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
waitForAssert(() -> {
assertThat(itemRegistry.getAll(), hasSize(1));
assertThat(receivedEvents, hasSize(2));
assertThat(receivedEvents.stream().filter(e -> "test1".equals(e.getItem().name)).findFirst().get(), instanceOf(ItemUpdatedEvent.class));
assertThat(receivedEvents.stream().filter(e -> "test2".equals(e.getItem().name)).findFirst().get(), instanceOf(ItemRemovedEvent.class));
});
receivedEvents.clear();
model = "";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
waitForAssert(() -> {
assertThat(itemRegistry.getAll(), hasSize(0));
assertThat(receivedEvents, hasSize(1));
assertThat(receivedEvents.stream().filter(e -> "test1".equals(e.getItem().name)).findFirst().get(), instanceOf(ItemRemovedEvent.class));
});
unregisterService(itemEventSubscriber);
}
use of org.openhab.core.events.EventFilter in project openhab-core by openhab.
the class AutomationIntegrationJsonTest method assertThatARuleFromJsonFileIsAddedAutomaticallyAndTheRuntimeRuleHasResolvedModuleReferences.
@Test
public void assertThatARuleFromJsonFileIsAddedAutomaticallyAndTheRuntimeRuleHasResolvedModuleReferences() {
logger.info("assert that a rule from json file is added automatically and the runtime rule has resolved module references");
// WAIT until Rule modules types are parsed and the rule becomes IDLE
waitForAssert(() -> {
assertThat(ruleRegistry.getAll().isEmpty(), is(false));
Optional<Rule> rule2 = ruleRegistry.stream().filter(RulePredicates.hasAllTags("jsonTest", "references")).findFirst();
assertThat(rule2.isPresent(), is(true));
RuleStatusInfo ruleStatus2 = ruleManager.getStatusInfo(rule2.get().getUID());
assertThat(ruleStatus2.getStatus(), is(RuleStatus.IDLE));
}, 10000, 200);
Optional<Rule> optionalRule = ruleRegistry.stream().filter(RulePredicates.hasAllTags("jsonTest", "references")).findFirst();
assertThat(optionalRule.isPresent(), is(true));
Rule rule = optionalRule.get();
assertThat(rule.getName(), is("ItemSampleRuleWithReferences"));
assertTrue(rule.getTags().contains("sample"));
assertTrue(rule.getTags().contains("item"));
assertTrue(rule.getTags().contains("rule"));
assertTrue(rule.getTags().contains("references"));
Optional<? extends Trigger> trigger = rule.getTriggers().stream().filter(t -> "ItemStateChangeTriggerID".equals(t.getId())).findFirst();
assertThat(trigger.isPresent(), is(true));
assertThat(trigger.get().getTypeUID(), is("core.GenericEventTrigger"));
assertThat(trigger.get().getConfiguration().get("eventTopic"), is("openhab/items/*"));
assertThat(trigger.get().getConfiguration().get("eventTypes"), is("ItemStateEvent"));
Optional<? extends Action> action = rule.getActions().stream().filter(a -> "ItemPostCommandActionID".equals(a.getId())).findFirst();
assertThat(action.isPresent(), is(true));
assertThat(action.get().getTypeUID(), is("core.ItemCommandAction"));
assertThat(action.get().getConfiguration().get("command"), is("ON"));
RuleStatusInfo ruleStatus = ruleManager.getStatusInfo(rule.getUID());
assertThat(ruleStatus.getStatus(), is(RuleStatus.IDLE));
// run the rule to check if the runtime rule has resolved module references and is executed successfully
EventPublisher eventPublisher = getService(EventPublisher.class);
@NonNullByDefault EventSubscriber itemEventHandler = new EventSubscriber() {
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ItemCommandEvent.TYPE);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
@Override
public void receive(Event e) {
logger.info("Event: {}", e.getTopic());
if (e.getTopic().contains("myLampItem")) {
itemEvent = e;
}
}
};
registerService(itemEventHandler);
eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem", OnOffType.ON));
waitForAssert(() -> {
assertThat(itemEvent, is(notNullValue()));
}, 3000, 100);
assertThat(itemEvent.getTopic(), is(equalTo("openhab/items/myLampItem/command")));
assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.ON));
}
use of org.openhab.core.events.EventFilter in project openhab-core by openhab.
the class RunRuleModuleTest method subscribeToEvents.
private void subscribeToEvents(String eventType, final Queue<Event> events) {
EventSubscriber eventSubscriber = new EventSubscriber() {
@Override
public void receive(final Event event) {
logger.info("Event: {}", event.getTopic());
events.add(event);
}
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(eventType);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
};
ServiceReference<?> subscriberReference = registerService(eventSubscriber).getReference();
assertNotNull(getServices(EventSubscriber.class, (reference) -> reference.equals(subscriberReference)));
}
use of org.openhab.core.events.EventFilter in project openhab-core by openhab.
the class GroupItemOSGiTest method beforeEach.
@BeforeEach
public void beforeEach() {
registerVolatileStorageService();
itemRegistry = getService(ItemRegistry.class);
assertNotNull(itemRegistry);
registerService(new EventSubscriber() {
@Override
public void receive(Event event) {
events.add(event);
}
@Override
public Set<String> getSubscribedEventTypes() {
Set<String> hs = new HashSet<>();
hs.add(ItemUpdatedEvent.TYPE);
return hs;
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
});
when(unitProviderMock.getUnit(Temperature.class)).thenReturn(Units.CELSIUS);
itemStateConverter = new ItemStateConverterImpl(unitProviderMock);
}
Aggregations