use of org.openhab.core.model.core.ModelRepositoryChangeListener in project openhab-core by openhab.
the class GenericItemProviderTest method tearDown.
/**
* Make sure the models and items are removed and the removal events have been processed.
*/
@AfterEach
public void tearDown() {
Collection<Item> itemsToRemove = itemRegistry.getAll();
List<String> modelNamesToRemove = TESTMODEL_NAMES.stream().filter(name -> modelRepository.getModel(name) != null).collect(Collectors.toList());
if (!modelNamesToRemove.isEmpty()) {
Set<String> removedModelNames = new HashSet<>();
ModelRepositoryChangeListener modelListener = new ModelRepositoryChangeListener() {
@Override
public void modelChanged(String modelName, EventType type) {
logger.debug("Received event: {} {}", modelName, type);
if (type == EventType.REMOVED) {
removedModelNames.add(modelName);
}
}
};
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;
}
};
modelRepository.addModelRepositoryChangeListener(modelListener);
registerService(itemEventSubscriber);
modelNamesToRemove.forEach(modelRepository::removeModel);
waitForAssert(() -> {
assertThat(removedItemEvents, hasSize(itemsToRemove.size()));
assertThat(removedModelNames, hasSize(modelNamesToRemove.size()));
});
modelRepository.removeModelRepositoryChangeListener(modelListener);
unregisterService(itemEventSubscriber);
}
}
Aggregations