Search in sources :

Example 1 with EventSubscriber

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)));
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) DiscoveryResult(org.openhab.core.config.discovery.DiscoveryResult) AsyncResultWrapper(org.openhab.core.test.AsyncResultWrapper) ThingUID(org.openhab.core.thing.ThingUID) InboxUpdatedEvent(org.openhab.core.config.discovery.inbox.events.InboxUpdatedEvent) InboxAddedEvent(org.openhab.core.config.discovery.inbox.events.InboxAddedEvent) Event(org.openhab.core.events.Event) InboxRemovedEvent(org.openhab.core.config.discovery.inbox.events.InboxRemovedEvent) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 2 with EventSubscriber

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)));
        }
    });
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) ArrayList(java.util.ArrayList) InboxUpdatedEvent(org.openhab.core.config.discovery.inbox.events.InboxUpdatedEvent) InboxAddedEvent(org.openhab.core.config.discovery.inbox.events.InboxAddedEvent) Event(org.openhab.core.events.Event) InboxRemovedEvent(org.openhab.core.config.discovery.inbox.events.InboxRemovedEvent) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 3 with EventSubscriber

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);
}
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) EventSubscriber(org.openhab.core.events.EventSubscriber) ByteArrayInputStream(java.io.ByteArrayInputStream) AbstractItemRegistryEvent(org.openhab.core.items.events.AbstractItemRegistryEvent) ArrayList(java.util.ArrayList) 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) ItemAddedEvent(org.openhab.core.items.events.ItemAddedEvent) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 4 with EventSubscriber

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);
}
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) EventSubscriber(org.openhab.core.events.EventSubscriber) ItemRemovedEvent(org.openhab.core.items.events.ItemRemovedEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) AbstractItemRegistryEvent(org.openhab.core.items.events.AbstractItemRegistryEvent) ArrayList(java.util.ArrayList) 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) ItemAddedEvent(org.openhab.core.items.events.ItemAddedEvent) ItemUpdatedEvent(org.openhab.core.items.events.ItemUpdatedEvent) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 5 with EventSubscriber

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()));
}
Also used : 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) EventSubscriber(org.openhab.core.events.EventSubscriber) ByteArrayInputStream(java.io.ByteArrayInputStream) AbstractItemRegistryEvent(org.openhab.core.items.events.AbstractItemRegistryEvent) Metadata(org.openhab.core.items.Metadata) ArrayList(java.util.ArrayList) 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) MetadataKey(org.openhab.core.items.MetadataKey) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

EventSubscriber (org.openhab.core.events.EventSubscriber)39 Event (org.openhab.core.events.Event)37 Test (org.junit.jupiter.api.Test)31 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)31 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)19 Rule (org.openhab.core.automation.Rule)16 SwitchItem (org.openhab.core.library.items.SwitchItem)15 ArrayList (java.util.ArrayList)14 Nullable (org.eclipse.jdt.annotation.Nullable)14 RuleStatusInfoEvent (org.openhab.core.automation.events.RuleStatusInfoEvent)14 Configuration (org.openhab.core.config.core.Configuration)14 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)13 BeforeEach (org.junit.jupiter.api.BeforeEach)13 EventPublisher (org.openhab.core.events.EventPublisher)12 Item (org.openhab.core.items.Item)12 ItemRegistry (org.openhab.core.items.ItemRegistry)11 HashMap (java.util.HashMap)10 Action (org.openhab.core.automation.Action)10 Trigger (org.openhab.core.automation.Trigger)10 RuleAddedEvent (org.openhab.core.automation.events.RuleAddedEvent)10