Search in sources :

Example 1 with ModelRepositoryChangeListener

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);
    }
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) BeforeEach(org.junit.jupiter.api.BeforeEach) EventType(org.openhab.core.model.core.EventType) EventSubscriber(org.openhab.core.events.EventSubscriber) SwitchItem(org.openhab.core.library.items.SwitchItem) ItemRemovedEvent(org.openhab.core.items.events.ItemRemovedEvent) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.openhab.core.library.types.OnOffType) StateDescription(org.openhab.core.types.StateDescription) ItemUpdatedEvent(org.openhab.core.items.events.ItemUpdatedEvent) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BigDecimal(java.math.BigDecimal) ByteArrayInputStream(java.io.ByteArrayInputStream) Nullable(org.eclipse.jdt.annotation.Nullable) GenericItem(org.openhab.core.items.GenericItem) ModelRepositoryChangeListener(org.openhab.core.model.core.ModelRepositoryChangeListener) EventFilter(org.openhab.core.events.EventFilter) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LinkedList(java.util.LinkedList) NumberItem(org.openhab.core.library.items.NumberItem) Metadata(org.openhab.core.items.Metadata) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) GroupItem(org.openhab.core.items.GroupItem) ItemAddedEvent(org.openhab.core.items.events.ItemAddedEvent) ArithmeticGroupFunction(org.openhab.core.library.types.ArithmeticGroupFunction) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ItemProvider(org.openhab.core.items.ItemProvider) Collection(java.util.Collection) Event(org.openhab.core.events.Event) UnDefType(org.openhab.core.types.UnDefType) Set(java.util.Set) GroupFunction(org.openhab.core.items.GroupFunction) MetadataRegistry(org.openhab.core.items.MetadataRegistry) Collectors(java.util.stream.Collectors) MetadataKey(org.openhab.core.items.MetadataKey) Item(org.openhab.core.items.Item) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) Test(org.junit.jupiter.api.Test) ItemRegistry(org.openhab.core.items.ItemRegistry) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Stream(java.util.stream.Stream) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest) ModelRepository(org.openhab.core.model.core.ModelRepository) AbstractItemRegistryEvent(org.openhab.core.items.events.AbstractItemRegistryEvent) ModelRepositoryChangeListener(org.openhab.core.model.core.ModelRepositoryChangeListener) EventSubscriber(org.openhab.core.events.EventSubscriber) EventType(org.openhab.core.model.core.EventType) AbstractItemRegistryEvent(org.openhab.core.items.events.AbstractItemRegistryEvent) ArrayList(java.util.ArrayList) SwitchItem(org.openhab.core.library.items.SwitchItem) GenericItem(org.openhab.core.items.GenericItem) NumberItem(org.openhab.core.library.items.NumberItem) GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) ItemRemovedEvent(org.openhab.core.items.events.ItemRemovedEvent) ItemUpdatedEvent(org.openhab.core.items.events.ItemUpdatedEvent) ItemAddedEvent(org.openhab.core.items.events.ItemAddedEvent) Event(org.openhab.core.events.Event) AbstractItemRegistryEvent(org.openhab.core.items.events.AbstractItemRegistryEvent) HashSet(java.util.HashSet) AfterEach(org.junit.jupiter.api.AfterEach)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 CoreMatchers (org.hamcrest.CoreMatchers)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 IsCollectionWithSize.hasSize (org.hamcrest.collection.IsCollectionWithSize.hasSize)1 AfterEach (org.junit.jupiter.api.AfterEach)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 Event (org.openhab.core.events.Event)1