Search in sources :

Example 1 with DefaultLocaleSetter

use of org.eclipse.smarthome.core.thing.testutil.i18n.DefaultLocaleSetter in project smarthome by eclipse.

the class ThingManagerOSGiTest method thingManagerPostsLocalizedThingStatusInfoAndThingStatusInfoChangedEvents.

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

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

        @Override
        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 singleton(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 singleton(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("smarthome/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("smarthome/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(getService(ConfigurationAdmin.class)).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("smarthome/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("smarthome/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(getService(ConfigurationAdmin.class)).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("smarthome/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("smarthome/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(getService(ConfigurationAdmin.class)).setDefaultLocale(defaultLocale);
    waitForAssert(() -> assertThat(localeProvider.getLocale(), is(defaultLocale)));
}
Also used : Locale(java.util.Locale) ThingStatusInfoEvent(org.eclipse.smarthome.core.thing.events.ThingStatusInfoEvent) ArrayList(java.util.ArrayList) DefaultLocaleSetter(org.eclipse.smarthome.core.thing.testutil.i18n.DefaultLocaleSetter) ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) ThingStatusInfoChangedEvent(org.eclipse.smarthome.core.thing.events.ThingStatusInfoChangedEvent) Thing(org.eclipse.smarthome.core.thing.Thing) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatusInfoI18nLocalizationService(org.eclipse.smarthome.core.thing.i18n.ThingStatusInfoI18nLocalizationService) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) BundleResolver(org.eclipse.smarthome.core.util.BundleResolver) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LocaleProvider(org.eclipse.smarthome.core.i18n.LocaleProvider) ThingStatusInfoChangedEvent(org.eclipse.smarthome.core.thing.events.ThingStatusInfoChangedEvent) ItemStateEvent(org.eclipse.smarthome.core.items.events.ItemStateEvent) Event(org.eclipse.smarthome.core.events.Event) ThingStatusInfoEvent(org.eclipse.smarthome.core.thing.events.ThingStatusInfoEvent) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 2 with DefaultLocaleSetter

use of org.eclipse.smarthome.core.thing.testutil.i18n.DefaultLocaleSetter in project smarthome by eclipse.

the class FirmwareRegistryOSGiTest method teardown.

@After
public void teardown() throws IOException {
    new DefaultLocaleSetter(getService(ConfigurationAdmin.class)).setDefaultLocale(defaultLocale);
    waitForAssert(() -> assertThat(getService(LocaleProvider.class).getLocale(), is(defaultLocale)));
}
Also used : LocaleProvider(org.eclipse.smarthome.core.i18n.LocaleProvider) DefaultLocaleSetter(org.eclipse.smarthome.core.thing.testutil.i18n.DefaultLocaleSetter) After(org.junit.After)

Example 3 with DefaultLocaleSetter

use of org.eclipse.smarthome.core.thing.testutil.i18n.DefaultLocaleSetter in project smarthome by eclipse.

the class ThingStatusInfoI18nLocalizationServiceOSGiTest method tearDown.

@After
public void tearDown() throws IOException, BundleException {
    SyntheticBundleInstaller.uninstall(bundleContext, TEST_BUNDLE_NAME);
    managedThingProvider.remove(thing.getUID());
    new DefaultLocaleSetter(getService(ConfigurationAdmin.class)).setDefaultLocale(defaultLocale);
    waitForAssert(() -> assertThat(getService(LocaleProvider.class).getLocale(), is(defaultLocale)));
}
Also used : LocaleProvider(org.eclipse.smarthome.core.i18n.LocaleProvider) DefaultLocaleSetter(org.eclipse.smarthome.core.thing.testutil.i18n.DefaultLocaleSetter) After(org.junit.After)

Example 4 with DefaultLocaleSetter

use of org.eclipse.smarthome.core.thing.testutil.i18n.DefaultLocaleSetter in project smarthome by eclipse.

the class ThingStatusInfoI18nLocalizationServiceOSGiTest method setup.

@Before
public void setup() throws Exception {
    LocaleProvider localeProvider = getService(LocaleProvider.class);
    assertThat(localeProvider, is(notNullValue()));
    defaultLocale = localeProvider.getLocale();
    new DefaultLocaleSetter(getService(ConfigurationAdmin.class)).setDefaultLocale(Locale.ENGLISH);
    waitForAssert(() -> assertThat(localeProvider.getLocale(), is(Locale.ENGLISH)));
    registerVolatileStorageService();
    SimpleThingHandlerFactory simpleThingHandlerFactory = new SimpleThingHandlerFactory();
    ComponentContext componentContext = mock(ComponentContext.class);
    when(componentContext.getBundleContext()).thenReturn(bundleContext);
    simpleThingHandlerFactory.activate(componentContext);
    registerService(simpleThingHandlerFactory, ThingHandlerFactory.class.getName());
    thing = ThingBuilder.create(new ThingTypeUID("aaa:bbb"), "ccc").build();
    managedThingProvider = getService(ManagedThingProvider.class);
    assertThat(managedThingProvider, is(notNullValue()));
    managedThingProvider.add(thing);
    waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
    testBundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
    assertThat(testBundle, is(notNullValue()));
    thingStatusInfoI18nLocalizationService = getService(ThingStatusInfoI18nLocalizationService.class);
    assertThat(thingStatusInfoI18nLocalizationService, is(notNullValue()));
    thingStatusInfoI18nLocalizationService.setBundleResolver(new BundleResolverImpl());
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) LocaleProvider(org.eclipse.smarthome.core.i18n.LocaleProvider) DefaultLocaleSetter(org.eclipse.smarthome.core.thing.testutil.i18n.DefaultLocaleSetter) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) Before(org.junit.Before)

Example 5 with DefaultLocaleSetter

use of org.eclipse.smarthome.core.thing.testutil.i18n.DefaultLocaleSetter in project smarthome by eclipse.

the class FirmwareRegistryOSGiTest method setup.

@Before
public void setup() throws IOException {
    LocaleProvider localeProvider = getService(LocaleProvider.class);
    assertThat(localeProvider, is(notNullValue()));
    defaultLocale = localeProvider.getLocale();
    new DefaultLocaleSetter(getService(ConfigurationAdmin.class)).setDefaultLocale(Locale.ENGLISH);
    waitForAssert(() -> assertThat(localeProvider.getLocale(), is(Locale.ENGLISH)));
    firmwareRegistry = getService(FirmwareRegistry.class);
    assertThat(firmwareRegistry, is(notNullValue()));
    registerService(basicFirmwareProviderMock);
    thing1 = ThingBuilder.create(THING_TYPE_UID1, THING1_ID).build();
    thing2 = ThingBuilder.create(THING_TYPE_UID1, THING2_ID).build();
    thing3 = ThingBuilder.create(THING_TYPE_UID2, THING3_ID).build();
}
Also used : LocaleProvider(org.eclipse.smarthome.core.i18n.LocaleProvider) DefaultLocaleSetter(org.eclipse.smarthome.core.thing.testutil.i18n.DefaultLocaleSetter) FirmwareRegistry(org.eclipse.smarthome.core.thing.firmware.FirmwareRegistry) Before(org.junit.Before)

Aggregations

LocaleProvider (org.eclipse.smarthome.core.i18n.LocaleProvider)5 DefaultLocaleSetter (org.eclipse.smarthome.core.thing.testutil.i18n.DefaultLocaleSetter)5 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)2 ThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)2 After (org.junit.After)2 Before (org.junit.Before)2 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)1 Event (org.eclipse.smarthome.core.events.Event)1 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)1 ItemStateEvent (org.eclipse.smarthome.core.items.events.ItemStateEvent)1 ManagedThingProvider (org.eclipse.smarthome.core.thing.ManagedThingProvider)1 Thing (org.eclipse.smarthome.core.thing.Thing)1 ThingStatusInfo (org.eclipse.smarthome.core.thing.ThingStatusInfo)1 BaseThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory)1 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)1 ThingHandlerCallback (org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback)1 ThingStatusInfoChangedEvent (org.eclipse.smarthome.core.thing.events.ThingStatusInfoChangedEvent)1 ThingStatusInfoEvent (org.eclipse.smarthome.core.thing.events.ThingStatusInfoEvent)1