Search in sources :

Example 1 with TranslationProvider

use of org.openhab.core.i18n.TranslationProvider in project openhab-addons by openhab.

the class NetatmoModuleDiscoveryServiceTest method before.

@BeforeEach
public void before() {
    Bridge bridgeMock = mock(Bridge.class);
    when(bridgeMock.getUID()).thenReturn(new ThingUID("netatmo", "bridge"));
    bridgeHandlerSpy = spy(new NetatmoBridgeHandler(bridgeMock, null));
    LocaleProvider localeProviderMock = mock(LocaleProvider.class);
    TranslationProvider translationProvider = mock(TranslationProvider.class);
    service = new NetatmoModuleDiscoveryServiceAccessible(bridgeHandlerSpy, localeProviderMock, translationProvider);
}
Also used : ThingUID(org.openhab.core.thing.ThingUID) LocaleProvider(org.openhab.core.i18n.LocaleProvider) TranslationProvider(org.openhab.core.i18n.TranslationProvider) NetatmoBridgeHandler(org.openhab.binding.netatmo.internal.handler.NetatmoBridgeHandler) Bridge(org.openhab.core.thing.Bridge) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with TranslationProvider

use of org.openhab.core.i18n.TranslationProvider in project openhab-core by openhab.

the class ProgressCallbackTest method setUp.

@BeforeEach
public void setUp() {
    expectedFirmware = FirmwareBuilder.create(THING_TYPE, "1").build();
    postedEvents.clear();
    EventPublisher publisher = new EventPublisher() {

        @Override
        public void post(Event event) throws IllegalArgumentException, IllegalStateException {
            postedEvents.add(event);
        }
    };
    TranslationProvider i18nProvider = new TranslationProvider() {

        @Override
        @Nullable
        public String getText(@Nullable Bundle bundle, @Nullable String key, @Nullable String defaultText, @Nullable Locale locale, @Nullable Object@Nullable ... arguments) {
            usedMessagedKey = key;
            return "Dummy Message";
        }

        @Override
        @Nullable
        public String getText(@Nullable Bundle bundle, @Nullable String key, @Nullable String defaultText, @Nullable Locale locale) {
            usedMessagedKey = key;
            return "Dummy Message";
        }
    };
    Bundle bundle = mock(Bundle.class);
    when(bundle.getSymbolicName()).thenReturn("");
    BundleResolver bundleResolver = mock(BundleResolver.class);
    when(bundleResolver.resolveBundle(any(Class.class))).thenReturn(bundle);
    sut = new ProgressCallbackImpl(new DummyFirmwareHandler(), publisher, i18nProvider, bundleResolver, EXPECTED_THING_UID, expectedFirmware, null);
}
Also used : Locale(java.util.Locale) BundleResolver(org.openhab.core.util.BundleResolver) EventPublisher(org.openhab.core.events.EventPublisher) Bundle(org.osgi.framework.Bundle) TranslationProvider(org.openhab.core.i18n.TranslationProvider) FirmwareUpdateResultInfoEvent(org.openhab.core.thing.firmware.FirmwareUpdateResultInfoEvent) Event(org.openhab.core.events.Event) FirmwareUpdateProgressInfoEvent(org.openhab.core.thing.firmware.FirmwareUpdateProgressInfoEvent) Nullable(org.eclipse.jdt.annotation.Nullable) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with TranslationProvider

use of org.openhab.core.i18n.TranslationProvider 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 4 with TranslationProvider

use of org.openhab.core.i18n.TranslationProvider in project openhab-core by openhab.

the class TranslationProviderOSGiTest method setup.

@BeforeEach
public void setup() {
    translationProvider = getService(TranslationProvider.class);
    LocaleProvider localeProvider = getService(LocaleProvider.class);
    Map<String, Object> localeCfg = new HashMap<>();
    localeCfg.put("language", "de");
    localeCfg.put("region", "DE");
    ((I18nProviderImpl) localeProvider).modified(localeCfg);
}
Also used : HashMap(java.util.HashMap) LocaleProvider(org.openhab.core.i18n.LocaleProvider) TranslationProvider(org.openhab.core.i18n.TranslationProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with TranslationProvider

use of org.openhab.core.i18n.TranslationProvider in project openhab-core by openhab.

the class ConfigStatusServiceTest method getTranslationProvider.

private TranslationProvider getTranslationProvider() {
    TranslationProvider translationProvider = mock(TranslationProvider.class);
    when(translationProvider.getText(any(), anyString(), eq(null), any(), any())).thenAnswer(invocation -> {
        String key = invocation.getArgument(1);
        Locale locale = invocation.getArgument(3);
        if (LOCALE_DE.equals(locale)) {
            if (key.endsWith(MSG_KEY1)) {
                return MSG1_DE;
            } else if (key.endsWith(MSG_KEY2)) {
                return MessageFormat.format(MSG2_DE, ARGS);
            } else if (key.endsWith(MSG_KEY3)) {
                return MessageFormat.format(MSG3_DE, ARGS);
            }
        } else {
            if (key.endsWith(MSG_KEY1)) {
                return MSG1_EN;
            } else if (key.endsWith(MSG_KEY2)) {
                return MessageFormat.format(MSG2_EN, ARGS);
            } else if (key.endsWith(MSG_KEY3)) {
                return MessageFormat.format(MSG3_EN, ARGS);
            }
        }
        return null;
    });
    return translationProvider;
}
Also used : Locale(java.util.Locale) TranslationProvider(org.openhab.core.i18n.TranslationProvider)

Aggregations

TranslationProvider (org.openhab.core.i18n.TranslationProvider)5 Locale (java.util.Locale)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Nullable (org.eclipse.jdt.annotation.Nullable)2 Event (org.openhab.core.events.Event)2 LocaleProvider (org.openhab.core.i18n.LocaleProvider)2 ThingUID (org.openhab.core.thing.ThingUID)2 Bundle (org.osgi.framework.Bundle)2 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 NetatmoBridgeHandler (org.openhab.binding.netatmo.internal.handler.NetatmoBridgeHandler)1 ConfigStatusService (org.openhab.core.config.core.status.ConfigStatusService)1 EventPublisher (org.openhab.core.events.EventPublisher)1 EventSubscriber (org.openhab.core.events.EventSubscriber)1 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)1 Bridge (org.openhab.core.thing.Bridge)1 Thing (org.openhab.core.thing.Thing)1 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)1