Search in sources :

Example 16 with EventSubscriber

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

the class BindingBaseClassesOSGiTest method assertConfigStatusIsPropagated.

@Test
public void assertConfigStatusIsPropagated() throws Exception {
    ConfigStatusProviderThingHandlerFactory thingHandlerFactory = new ConfigStatusProviderThingHandlerFactory();
    thingHandlerFactory.activate(componentContextMock);
    registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
    ThingTypeUID thingTypeUID = new ThingTypeUID("bindingId:type");
    ThingUID thingUID = new ThingUID("bindingId:type:thingId");
    Thing thing = ThingBuilder.create(thingTypeUID, thingUID).build();
    managedThingProvider.add(thing);
    ConfigStatusService service = getService(ConfigStatusService.class);
    TranslationProvider translationProvider = mock(TranslationProvider.class);
    when(translationProvider.getText(nullable(Bundle.class), nullable(String.class), nullable(String.class), nullable(Locale.class), nullable(Object[].class))).then(new Answer<String>() {

        @Override
        @Nullable
        public String answer(InvocationOnMock invocation) throws Throwable {
            String key = (String) invocation.getArgument(1);
            return key.endsWith("param.invalid") ? "param invalid" : "param ok";
        }
    });
    Field field = service.getClass().getDeclaredField("translationProvider");
    field.setAccessible(true);
    field.set(service, translationProvider);
    ConfigStatusInfoEventSubscriber eventSubscriber = new ConfigStatusInfoEventSubscriber(thingUID);
    registerService(eventSubscriber, EventSubscriber.class.getName());
    Thread.sleep(2000);
    thing.getHandler().handleConfigurationUpdate(Map.of("param", "invalid"));
    waitForAssert(() -> {
        Event event = eventSubscriber.getReceivedEvent();
        assertThat(event, is(notNullValue()));
        assertThat(event.getPayload(), CoreMatchers.containsString("\"parameterName\":\"param\",\"type\":\"ERROR\",\"message\":\"param invalid\"}"));
        eventSubscriber.resetReceivedEvent();
    }, 2500, DFL_SLEEP_TIME);
    thing.getHandler().handleConfigurationUpdate(Map.of("param", "ok"));
    waitForAssert(() -> {
        Event event = eventSubscriber.getReceivedEvent();
        assertThat(event, is(notNullValue()));
        assertThat(event.getPayload(), CoreMatchers.containsString("\"parameterName\":\"param\",\"type\":\"INFORMATION\",\"message\":\"param ok\"}"));
    }, 2500, DFL_SLEEP_TIME);
}
Also used : Locale(java.util.Locale) EventSubscriber(org.openhab.core.events.EventSubscriber) ConfigStatusService(org.openhab.core.config.core.status.ConfigStatusService) Bundle(org.osgi.framework.Bundle) Field(java.lang.reflect.Field) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ThingUID(org.openhab.core.thing.ThingUID) TranslationProvider(org.openhab.core.i18n.TranslationProvider) Event(org.openhab.core.events.Event) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 17 with EventSubscriber

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

the class ThingRegistryOSGiTest method assertThatThingRegistryEventSubscribersReceiveEventsAboutThingChanges.

@Test
public void assertThatThingRegistryEventSubscribersReceiveEventsAboutThingChanges() {
    EventSubscriber thingRegistryEventSubscriber = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Set.of(ThingAddedEvent.TYPE, ThingRemovedEvent.TYPE, ThingUpdatedEvent.TYPE);
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event event) {
            receivedEvent = event;
        }
    };
    registerService(thingRegistryEventSubscriber);
    // add new thing
    managedThingProvider.add(THING);
    waitForAssert(() -> {
        assertThat(receivedEvent, notNullValue());
    });
    assertThat(receivedEvent, is(instanceOf(ThingAddedEvent.class)));
    receivedEvent = null;
    // update thing
    Thing updatedThing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
    managedThingProvider.update(updatedThing);
    waitForAssert(() -> {
        assertThat(receivedEvent, notNullValue());
    });
    assertThat(receivedEvent, is(instanceOf(ThingUpdatedEvent.class)));
    receivedEvent = null;
    // remove thing
    managedThingProvider.remove(THING.getUID());
    waitForAssert(() -> {
        assertThat(receivedEvent, notNullValue());
    });
    assertThat(receivedEvent, is(instanceOf(ThingRemovedEvent.class)));
    receivedEvent = null;
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) ThingAddedEvent(org.openhab.core.thing.events.ThingAddedEvent) ThingRemovedEvent(org.openhab.core.thing.events.ThingRemovedEvent) Event(org.openhab.core.events.Event) ThingUpdatedEvent(org.openhab.core.thing.events.ThingUpdatedEvent) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 18 with EventSubscriber

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

the class GroupItemOSGiTest method beforeEach.

@BeforeEach
public void beforeEach() {
    registerVolatileStorageService();
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    registerService(new EventSubscriber() {

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

        @Override
        public Set<String> getSubscribedEventTypes() {
            Set<String> hs = new HashSet<>();
            hs.add(ItemUpdatedEvent.TYPE);
            return hs;
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }
    });
    when(unitProviderMock.getUnit(Temperature.class)).thenReturn(Units.CELSIUS);
    itemStateConverter = new ItemStateConverterImpl(unitProviderMock);
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) Set(java.util.Set) HashSet(java.util.HashSet) GroupItemStateChangedEvent(org.openhab.core.items.events.GroupItemStateChangedEvent) ItemUpdatedEvent(org.openhab.core.items.events.ItemUpdatedEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) ItemStateConverterImpl(org.openhab.core.internal.items.ItemStateConverterImpl) EventFilter(org.openhab.core.events.EventFilter) Nullable(org.eclipse.jdt.annotation.Nullable) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 19 with EventSubscriber

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

the class ThingManagerOSGiTest method thingManagerPostsLocalizedThingStatusInfoAndThingStatusInfoChangedEvents.

@Test
@SuppressWarnings("null")
public void thingManagerPostsLocalizedThingStatusInfoAndThingStatusInfoChangedEvents() throws Exception {
    registerThingTypeProvider();
    class ThingHandlerState {

        @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));
    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);
    BundleResolver bundleResolver = mock(BundleResolver.class);
    when(bundleResolver.resolveBundle(any())).thenReturn(bundleContext.getBundle());
    ThingStatusInfoI18nLocalizationService thingStatusInfoI18nLocalizationService = getService(ThingStatusInfoI18nLocalizationService.class);
    thingStatusInfoI18nLocalizationService.setBundleResolver(bundleResolver);
    final List<ThingStatusInfoEvent> infoEvents = new ArrayList<>();
    @NonNullByDefault EventSubscriber thingStatusInfoEventSubscriber = new EventSubscriber() {

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

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event event) {
            infoEvents.add((ThingStatusInfoEvent) event);
        }
    };
    registerService(thingStatusInfoEventSubscriber);
    final List<ThingStatusInfoChangedEvent> infoChangedEvents = new ArrayList<>();
    @NonNullByDefault EventSubscriber thingStatusInfoChangedEventSubscriber = new EventSubscriber() {

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

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event event) {
            infoChangedEvents.add((ThingStatusInfoChangedEvent) event);
        }
    };
    registerService(thingStatusInfoChangedEventSubscriber);
    // add thing (UNINITIALIZED -> INITIALIZING)
    managedThingProvider.add(thing);
    waitForAssert(() -> {
        assertThat(infoEvents.size(), is(1));
        assertThat(infoChangedEvents.size(), is(1));
    });
    assertThat(infoEvents.get(0).getType(), is(ThingStatusInfoEvent.TYPE));
    assertThat(infoEvents.get(0).getTopic(), is("openhab/things/binding:type:id/status"));
    assertThat(infoEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
    assertThat(infoEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoEvents.get(0).getStatusInfo().getDescription(), is(nullValue()));
    assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
    assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getDescription(), is(nullValue()));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.UNINITIALIZED));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getDescription(), is(nullValue()));
    infoEvents.clear();
    infoChangedEvents.clear();
    LocaleProvider localeProvider = getService(LocaleProvider.class);
    assertThat(localeProvider, is(notNullValue()));
    Locale defaultLocale = localeProvider.getLocale();
    // set status to ONLINE (INITIALIZING -> ONLINE)
    new DefaultLocaleSetter(configurationAdmin).setDefaultLocale(Locale.ENGLISH);
    waitForAssert(() -> assertThat(localeProvider.getLocale(), is(Locale.ENGLISH)));
    ThingStatusInfo onlineNone = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).withDescription("@text/online").build();
    state.callback.statusUpdated(thing, onlineNone);
    waitForAssert(() -> {
        assertThat(infoEvents.size(), is(1));
        assertThat(infoChangedEvents.size(), is(1));
    });
    assertThat(infoEvents.get(0).getType(), is(ThingStatusInfoEvent.TYPE));
    assertThat(infoEvents.get(0).getTopic(), is("openhab/things/binding:type:id/status"));
    assertThat(infoEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.ONLINE));
    assertThat(infoEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoEvents.get(0).getStatusInfo().getDescription(), is("Thing is online."));
    assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
    assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.ONLINE));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getDescription(), is("Thing is online."));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getDescription(), is(nullValue()));
    infoEvents.clear();
    infoChangedEvents.clear();
    // set status to OFFLINE (ONLINE -> OFFLINE)
    new DefaultLocaleSetter(configurationAdmin).setDefaultLocale(Locale.GERMAN);
    waitForAssert(() -> assertThat(localeProvider.getLocale(), is(Locale.GERMAN)));
    ThingStatusInfo offlineNone = ThingStatusInfoBuilder.create(ThingStatus.OFFLINE, ThingStatusDetail.NONE).withDescription("@text/offline.without-param").build();
    state.callback.statusUpdated(thing, offlineNone);
    waitForAssert(() -> {
        assertThat(infoEvents.size(), is(1));
        assertThat(infoChangedEvents.size(), is(1));
    });
    assertThat(infoEvents.get(0).getType(), is(ThingStatusInfoEvent.TYPE));
    assertThat(infoEvents.get(0).getTopic(), is("openhab/things/binding:type:id/status"));
    assertThat(infoEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.OFFLINE));
    assertThat(infoEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoEvents.get(0).getStatusInfo().getDescription(), is("Thing ist offline."));
    assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
    assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.OFFLINE));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getDescription(), is("Thing ist offline."));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.ONLINE));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getDescription(), is("Thing ist online."));
    new DefaultLocaleSetter(configurationAdmin).setDefaultLocale(defaultLocale);
    waitForAssert(() -> assertThat(localeProvider.getLocale(), is(defaultLocale)));
}
Also used : Locale(java.util.Locale) ThingStatusInfoEvent(org.openhab.core.thing.events.ThingStatusInfoEvent) ArrayList(java.util.ArrayList) DefaultLocaleSetter(org.openhab.core.thing.testutil.i18n.DefaultLocaleSetter) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingStatusInfoChangedEvent(org.openhab.core.thing.events.ThingStatusInfoChangedEvent) Thing(org.openhab.core.thing.Thing) EventSubscriber(org.openhab.core.events.EventSubscriber) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatusInfoI18nLocalizationService(org.openhab.core.thing.i18n.ThingStatusInfoI18nLocalizationService) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) BundleResolver(org.openhab.core.util.BundleResolver) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LocaleProvider(org.openhab.core.i18n.LocaleProvider) 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) Nullable(org.eclipse.jdt.annotation.Nullable) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 20 with EventSubscriber

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

the class ThingManagerOSGiTest method thingManagerPostsThingStatusChangedEventsIfTheStatusOfAThingIsChanged.

@Test
public void thingManagerPostsThingStatusChangedEventsIfTheStatusOfAThingIsChanged() throws Exception {
    registerThingTypeProvider();
    class ThingHandlerState {

        @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));
    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);
    final List<ThingStatusInfoChangedEvent> infoChangedEvents = new ArrayList<>();
    @NonNullByDefault EventSubscriber thingStatusEventSubscriber = new EventSubscriber() {

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

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event event) {
            infoChangedEvents.add((ThingStatusInfoChangedEvent) event);
        }
    };
    registerService(thingStatusEventSubscriber);
    // add thing (UNINITIALIZED -> INITIALIZING)
    managedThingProvider.add(thing);
    waitForAssert(() -> assertThat(infoChangedEvents.size(), is(1)));
    assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
    assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.UNINITIALIZED));
    infoChangedEvents.clear();
    // set status to ONLINE (INITIALIZING -> ONLINE)
    ThingStatusInfo onlineNone = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).build();
    state.callback.statusUpdated(thing, onlineNone);
    waitForAssert(() -> assertThat(infoChangedEvents.size(), is(1)));
    assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
    assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.ONLINE));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
    infoChangedEvents.clear();
    // set status to ONLINE again
    state.callback.statusUpdated(thing, onlineNone);
    // make sure no event has been sent
    Thread.sleep(500);
    assertThat(infoChangedEvents.size(), is(0));
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ArrayList(java.util.ArrayList) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) ThingStatusInfoChangedEvent(org.openhab.core.thing.events.ThingStatusInfoChangedEvent) 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) 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)

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