use of org.openhab.core.events.EventSubscriber in project openhab-core by openhab.
the class InboxOSGiTest method assertThatInboxEventSubscribersReceiveEventsAboutDiscoveryResultChanges.
@Test
public void assertThatInboxEventSubscribersReceiveEventsAboutDiscoveryResultChanges() {
ThingTypeUID thingTypeUID = new ThingTypeUID("some", "thing");
ThingUID thingUID = new ThingUID(thingTypeUID, "uid");
final AsyncResultWrapper<Event> receivedEvent = new AsyncResultWrapper<>();
EventSubscriber inboxEventSubscriber = new EventSubscriber() {
@Override
public void receive(Event event) {
receivedEvent.set(event);
}
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(InboxAddedEvent.TYPE, InboxRemovedEvent.TYPE, InboxUpdatedEvent.TYPE);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
};
registerService(inboxEventSubscriber);
// add discovery result
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID).withTTL(DEFAULT_TTL).build();
addDiscoveryResult(discoveryResult);
waitForAssert(() -> assertThat(receivedEvent.getWrappedObject(), not(nullValue())));
assertThat(receivedEvent.getWrappedObject(), is(instanceOf(InboxAddedEvent.class)));
receivedEvent.reset();
// update discovery result
discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID).withTTL(DEFAULT_TTL).build();
addDiscoveryResult(discoveryResult);
waitForAssert(() -> assertThat(receivedEvent.getWrappedObject(), not(nullValue())));
assertThat(receivedEvent.getWrappedObject(), is(instanceOf(InboxUpdatedEvent.class)));
receivedEvent.reset();
// remove discovery result
removeDiscoveryResult(thingUID);
waitForAssert(() -> assertThat(receivedEvent.getWrappedObject(), not(nullValue())));
assertThat(receivedEvent.getWrappedObject(), is(instanceOf(InboxRemovedEvent.class)));
}
use of org.openhab.core.events.EventSubscriber in project openhab-core by openhab.
the class InboxOSGiTest method assertThatRemoveLeavesAssociatedDiscoveryResultsInInboxWhenBridgeIsAddedToThingRegistry.
@Test
public void assertThatRemoveLeavesAssociatedDiscoveryResultsInInboxWhenBridgeIsAddedToThingRegistry() {
List<Event> receivedEvents = new ArrayList<>();
EventSubscriber inboxEventSubscriber = new EventSubscriber() {
@Override
public void receive(Event event) {
receivedEvents.add(event);
}
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(InboxAddedEvent.TYPE, InboxRemovedEvent.TYPE, InboxUpdatedEvent.TYPE);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
};
registerService(inboxEventSubscriber);
inbox.add(BRIDGE);
inbox.add(THING1_WITH_BRIDGE);
inbox.add(THING2_WITH_BRIDGE);
inbox.add(THING_WITHOUT_BRIDGE);
waitForAssert(() -> assertThat(receivedEvents.size(), is(4)));
receivedEvents.clear();
registry.add(BridgeBuilder.create(BRIDGE_THING_TYPE_UID, BRIDGE_THING_UID).build());
assertFalse(inbox.stream().anyMatch(forThingUID(BRIDGE.getThingUID()).and(withFlag(DiscoveryResultFlag.NEW))));
assertThat(inbox.stream().filter(withFlag(DiscoveryResultFlag.NEW)).collect(Collectors.toList()), hasItems(THING1_WITH_BRIDGE, THING2_WITH_BRIDGE, THING_WITHOUT_BRIDGE));
waitForAssert(() -> {
assertThat(receivedEvents.size(), is(1));
for (Event event : receivedEvents) {
assertThat(event, is(instanceOf(InboxRemovedEvent.class)));
}
});
}
use of org.openhab.core.events.EventSubscriber 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.EventSubscriber 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.EventSubscriber in project openhab-core by openhab.
the class GenericItemProviderTest method testMetadataConfigured.
@Test
public void testMetadataConfigured() {
String model = //
"Switch simple { namespace=\"value\" } " + //
"Switch configured { foo=\"bar\" [ answer=42 ] } " + "Switch negative { foo=\"bar\" [ property=-42 ] }";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
Item item = itemRegistry.get("configured");
assertThat(item, is(notNullValue()));
Metadata res = metadataRegistry.get(new MetadataKey("foo", "configured"));
assertThat(res, is(notNullValue()));
assertThat(res.getValue(), is("bar"));
assertThat(res.getConfiguration().get("answer"), is(new BigDecimal(42)));
res = metadataRegistry.get(new MetadataKey("foo", "negative"));
assertThat(res, is(notNullValue()));
assertThat(res.getValue(), is("bar"));
assertThat(res.getConfiguration().get("property"), is(new BigDecimal(-42)));
Collection<Item> itemsToRemove = itemRegistry.getAll();
List<AbstractItemRegistryEvent> removedItemEvents = new ArrayList<>();
EventSubscriber itemEventSubscriber = new EventSubscriber() {
@Override
public void receive(Event event) {
logger.debug("Received event: {}", event);
removedItemEvents.add((AbstractItemRegistryEvent) event);
}
@Override
public Set<String> getSubscribedEventTypes() {
return Stream.of(ItemRemovedEvent.TYPE).collect(toSet());
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
};
registerService(itemEventSubscriber);
modelRepository.removeModel(TESTMODEL_NAME);
waitForAssert(() -> {
assertThat(removedItemEvents, hasSize(itemsToRemove.size()));
});
unregisterService(itemEventSubscriber);
res = metadataRegistry.get(new MetadataKey("foo", "configured"));
assertThat(res, is(nullValue()));
}
Aggregations