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)));
}
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);
}
Aggregations