Search in sources :

Example 1 with TopicEventFilter

use of org.openhab.core.events.TopicEventFilter in project openhab-core by openhab.

the class ThingManagerOSGiTest method thingManagerHandlesStateUpdatesCorrectly.

@Test
public void thingManagerHandlesStateUpdatesCorrectly() {
    registerThingTypeProvider();
    class ThingHandlerState {

        boolean thingUpdatedWasCalled;

        @Nullable
        ThingHandlerCallback callback;
    }
    final ThingHandlerState state = new ThingHandlerState();
    ThingHandler thingHandler = mock(ThingHandler.class);
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.callback = (ThingHandlerCallback) invocation.getArgument(0);
            return null;
        }
    }).when(thingHandler).setCallback(any(ThingHandlerCallback.class));
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.thingUpdatedWasCalled = true;
            return null;
        }
    }).when(thingHandler).thingUpdated(any(Thing.class));
    when(thingHandler.getThing()).thenReturn(thing);
    ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
    when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
    registerService(thingHandlerFactory);
    // Create item
    String itemName = "name";
    Item item = new StringItem(itemName);
    itemRegistry.add(item);
    managedThingProvider.add(thing);
    managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID));
    state.callback.statusUpdated(thing, ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build());
    final List<Event> receivedEvents = new ArrayList<>();
    @NonNullByDefault EventSubscriber itemUpdateEventSubscriber = new EventSubscriber() {

        @Override
        public void receive(Event event) {
            receivedEvents.add(event);
        }

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Set.of(ItemStateEvent.TYPE);
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return new TopicEventFilter("openhab/items/.*/state");
        }
    };
    registerService(itemUpdateEventSubscriber);
    // thing manager posts the update to the event bus via EventPublisher
    state.callback.stateUpdated(CHANNEL_UID, new StringType("Value"));
    waitForAssert(() -> assertThat(receivedEvents.size(), is(1)));
    assertThat(receivedEvents.get(0), is(instanceOf(ItemStateEvent.class)));
    ItemStateEvent itemUpdateEvent = (ItemStateEvent) receivedEvents.get(0);
    assertThat(itemUpdateEvent.getTopic(), is("openhab/items/name/state"));
    assertThat(itemUpdateEvent.getItemName(), is(itemName));
    assertThat(itemUpdateEvent.getSource(), is(CHANNEL_UID.toString()));
    assertThat(itemUpdateEvent.getItemState(), is(instanceOf(StringType.class)));
    assertThat(itemUpdateEvent.getItemState(), is("Value"));
    receivedEvents.clear();
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannel(ChannelBuilder.create(CHANNEL_UID, CoreItemFactory.SWITCH).build()).build();
    managedThingProvider.update(thing);
    state.callback.stateUpdated(CHANNEL_UID, new StringType("Value"));
    waitForAssert(() -> assertThat(receivedEvents.size(), is(1)));
    assertThat(receivedEvents.get(0), is(instanceOf(ItemStateEvent.class)));
    itemUpdateEvent = (ItemStateEvent) receivedEvents.get(0);
    assertThat(itemUpdateEvent.getTopic(), is("openhab/items/name/state"));
    assertThat(itemUpdateEvent.getItemName(), is(itemName));
    assertThat(itemUpdateEvent.getSource(), is(CHANNEL_UID.toString()));
    assertThat(itemUpdateEvent.getItemState(), is(instanceOf(StringType.class)));
    assertThat(itemUpdateEvent.getItemState(), is("Value"));
    waitForAssert(() -> assertThat(state.thingUpdatedWasCalled, is(true)));
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) StringType(org.openhab.core.library.types.StringType) ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) ArrayList(java.util.ArrayList) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) StringItem(org.openhab.core.library.items.StringItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent) ThingStatusInfoEvent(org.openhab.core.thing.events.ThingStatusInfoEvent) Event(org.openhab.core.events.Event) ThingStatusInfoChangedEvent(org.openhab.core.thing.events.ThingStatusInfoChangedEvent) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) TopicEventFilter(org.openhab.core.events.TopicEventFilter) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent) Nullable(org.eclipse.jdt.annotation.Nullable) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 2 with TopicEventFilter

use of org.openhab.core.events.TopicEventFilter in project openhab-core by openhab.

the class OSGiEventManagerOSGiTest method beforeEach.

@BeforeEach
public void beforeEach() throws Exception {
    eventPublisher = getService(EventPublisher.class);
    assertNotNull(eventPublisher);
    when(eventTypeFactoryABMock.getSupportedEventTypes()).thenReturn(Set.of(EVENT_TYPE_A, EVENT_TYPE_B));
    when(eventTypeFactoryABMock.createEvent(any(), any(), any(), any())).thenAnswer(answer -> createEvent(answer.getArgument(0), answer.getArgument(2), answer.getArgument(1)));
    internalRegisterService(EVENT_TYPE_FACTORY_A_B, EventFactory.class, eventTypeFactoryABMock);
    when(eventTypeFactoryCMock.getSupportedEventTypes()).thenReturn(Set.of(EVENT_TYPE_C));
    when(eventTypeFactoryCMock.createEvent(any(), any(), any(), any())).thenAnswer(answer -> createEvent(answer.getArgument(0), answer.getArgument(2), answer.getArgument(1)));
    internalRegisterService(EVENT_TYPE_FACTORY_C, EventFactory.class, eventTypeFactoryCMock);
    when(subscriber1Mock.getSubscribedEventTypes()).thenReturn(Set.of(EVENT_TYPE_A));
    internalRegisterService(TYPE_BASED_SUBSCRIBER_1, EventSubscriber.class, subscriber1Mock);
    when(subscriber2Mock.getSubscribedEventTypes()).thenReturn(Set.of(EVENT_TYPE_A));
    internalRegisterService(TYPE_BASED_SUBSCRIBER_2, EventSubscriber.class, subscriber2Mock);
    when(subscriber3Mock.getSubscribedEventTypes()).thenReturn(Set.of(EVENT_TYPE_B, EVENT_TYPE_C));
    when(subscriber3Mock.getEventFilter()).thenReturn(new TopicEventFilter(TOPIC));
    internalRegisterService(TOPIC_BASED_SUBSCRIBER_3, EventSubscriber.class, subscriber3Mock);
    when(subscriber4Mock.getSubscribedEventTypes()).thenReturn(Set.of(EventSubscriber.ALL_EVENT_TYPES));
    when(subscriber4Mock.getEventFilter()).thenReturn(new TopicEventFilter(TOPIC));
    internalRegisterService(ALL_EVENT_TYPES_SUBSCRIBER_4, EventSubscriber.class, subscriber4Mock);
}
Also used : EventPublisher(org.openhab.core.events.EventPublisher) TopicEventFilter(org.openhab.core.events.TopicEventFilter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

TopicEventFilter (org.openhab.core.events.TopicEventFilter)2 ArrayList (java.util.ArrayList)1 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Event (org.openhab.core.events.Event)1 EventPublisher (org.openhab.core.events.EventPublisher)1 EventSubscriber (org.openhab.core.events.EventSubscriber)1 Item (org.openhab.core.items.Item)1 ItemStateEvent (org.openhab.core.items.events.ItemStateEvent)1 StringItem (org.openhab.core.library.items.StringItem)1 StringType (org.openhab.core.library.types.StringType)1 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)1 Thing (org.openhab.core.thing.Thing)1 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)1 ThingHandler (org.openhab.core.thing.binding.ThingHandler)1 ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)1 ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)1