use of org.openhab.core.items.events.AbstractItemRegistryEvent in project openhab-core by openhab.
the class ChannelLinkNotifierOSGiTest method afterEach.
@AfterEach
public void afterEach() throws Exception {
Set<String> itemChannelLinkUIDsToRemove = managedItemChannelLinkProvider.getAll().stream().map(ItemChannelLink::getUID).collect(Collectors.toSet());
Set<String> itemNamesToRemove = managedItemProvider.getAll().stream().map(Item::getName).collect(Collectors.toSet());
Set<String> thingUIDsToRemove = managedThingProvider.getAll().stream().map(Thing::getUID).map(ThingUID::toString).collect(Collectors.toSet());
Set<String> removedItemNames = new HashSet<>();
Set<String> removedItemChannelLinkUIDs = new HashSet<>();
Set<String> removedThingUIDs = new HashSet<>();
EventSubscriber itemEventSubscriber = new EventSubscriber() {
@Override
public void receive(Event event) {
logger.debug("Received event: {}", event);
if (event instanceof AbstractItemChannelLinkRegistryEvent) {
ItemChannelLinkDTO link = ((AbstractItemChannelLinkRegistryEvent) event).getLink();
removedItemChannelLinkUIDs.add(AbstractLink.getIDFor(link.itemName, new ChannelUID(link.channelUID)));
} else if (event instanceof AbstractItemRegistryEvent) {
removedItemNames.add(((AbstractItemRegistryEvent) event).getItem().name);
} else if (event instanceof AbstractThingRegistryEvent) {
removedThingUIDs.add(((AbstractThingRegistryEvent) event).getThing().UID);
}
}
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ItemChannelLinkRemovedEvent.TYPE, ItemRemovedEvent.TYPE, ThingRemovedEvent.TYPE);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
};
registerService(itemEventSubscriber);
Thread.sleep(300);
managedItemChannelLinkProvider.getAll().forEach(itemChannelLink -> managedItemChannelLinkProvider.remove(itemChannelLink.getUID()));
managedItemProvider.getAll().forEach(item -> managedItemProvider.remove(item.getUID()));
managedThingProvider.getAll().forEach(thing -> managedThingProvider.remove(thing.getUID()));
// wait for the resulting events to be handled so they do not cause the next test to fail
waitForAssert(() -> {
assertTrue(removedItemChannelLinkUIDs.containsAll(itemChannelLinkUIDsToRemove));
assertTrue(removedItemNames.containsAll(itemNamesToRemove));
assertTrue(removedThingUIDs.containsAll(thingUIDsToRemove));
});
unregisterService(itemEventSubscriber);
}
use of org.openhab.core.items.events.AbstractItemRegistryEvent 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.items.events.AbstractItemRegistryEvent 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.items.events.AbstractItemRegistryEvent 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()));
}
use of org.openhab.core.items.events.AbstractItemRegistryEvent in project openhab-core by openhab.
the class CommunicationManager method receive.
@Override
public void receive(Event event) {
if (event instanceof ItemStateEvent) {
receiveUpdate((ItemStateEvent) event);
} else if (event instanceof ItemCommandEvent) {
receiveCommand((ItemCommandEvent) event);
} else if (event instanceof ChannelTriggeredEvent) {
receiveTrigger((ChannelTriggeredEvent) event);
} else if (event instanceof AbstractItemRegistryEvent) {
String itemName = ((AbstractItemRegistryEvent) event).getItem().name;
profiles.entrySet().removeIf(entry -> {
ItemChannelLink link = itemChannelLinkRegistry.get(entry.getKey());
return link != null && itemName.equals(link.getItemName());
});
} else if (event instanceof AbstractThingRegistryEvent) {
ThingUID thingUid = new ThingUID(((AbstractThingRegistryEvent) event).getThing().UID);
profiles.entrySet().removeIf(entry -> {
ItemChannelLink link = itemChannelLinkRegistry.get(entry.getKey());
return link != null && thingUid.equals(link.getLinkedUID().getThingUID());
});
}
}
Aggregations