Search in sources :

Example 1 with BundleResolver

use of org.eclipse.smarthome.core.util.BundleResolver in project smarthome by eclipse.

the class ThingManagerOSGiTest method setUp.

@Before
@SuppressWarnings("null")
public void setUp() {
    thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannels(ChannelBuilder.create(CHANNEL_UID, "Switch").withKind(ChannelKind.STATE).withType(CHANNEL_TYPE_UID).build()).build();
    registerVolatileStorageService();
    channelTypeProvider = mock(ChannelTypeProvider.class);
    when(channelTypeProvider.getChannelType(any(ChannelTypeUID.class), nullable(Locale.class))).thenReturn(ChannelTypeBuilder.state(CHANNEL_TYPE_UID, "label", "Switch").build());
    registerService(channelTypeProvider);
    thingLinkManager = getService(ThingLinkManager.class);
    thingLinkManager.deactivate();
    managedItemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
    managedThingProvider = getService(ManagedThingProvider.class);
    eventPublisher = getService(EventPublisher.class);
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    itemChannelLinkRegistry = getService(ItemChannelLinkRegistry.class);
    assertNotNull(itemChannelLinkRegistry);
    readyService = getService(ReadyService.class);
    assertNotNull(readyService);
    waitForAssert(() -> {
        try {
            assertThat(bundleContext.getServiceReferences(ReadyMarker.class, "(" + ThingManagerImpl.XML_THING_TYPE + "=" + bundleContext.getBundle().getSymbolicName() + ")"), is(notNullValue()));
        } catch (InvalidSyntaxException e) {
            fail("Failed to get service reference: " + e.getMessage());
        }
    });
    waitForAssert(() -> {
        try {
            assertThat(bundleContext.getServiceReferences(ChannelItemProvider.class, null), is(notNullValue()));
        } catch (InvalidSyntaxException e) {
            fail("Failed to get service reference: " + e.getMessage());
        }
    });
    Bundle bundle = mock(Bundle.class);
    when(bundle.getSymbolicName()).thenReturn("org.eclipse.smarthome.core.thing");
    BundleResolver bundleResolver = mock(BundleResolver.class);
    when(bundleResolver.resolveBundle(any())).thenReturn(bundle);
    ThingManagerImpl thingManager = (ThingManagerImpl) getService(ThingTypeMigrationService.class);
    thingManager.setBundleResolver(bundleResolver);
}
Also used : Locale(java.util.Locale) ManagedItemChannelLinkProvider(org.eclipse.smarthome.core.thing.link.ManagedItemChannelLinkProvider) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) Bundle(org.osgi.framework.Bundle) ReadyService(org.eclipse.smarthome.core.service.ReadyService) ThingLinkManager(org.eclipse.smarthome.core.thing.link.ThingLinkManager) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) BundleResolver(org.eclipse.smarthome.core.util.BundleResolver) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) ThingTypeMigrationService(org.eclipse.smarthome.core.thing.ThingTypeMigrationService) ChannelTypeProvider(org.eclipse.smarthome.core.thing.type.ChannelTypeProvider) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ReadyMarker(org.eclipse.smarthome.core.service.ReadyMarker) Before(org.junit.Before)

Example 2 with BundleResolver

use of org.eclipse.smarthome.core.util.BundleResolver 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 3 with BundleResolver

use of org.eclipse.smarthome.core.util.BundleResolver in project smarthome by eclipse.

the class ProgressCallbackTest method setUp.

@Before
public void setUp() {
    ThingTypeUID thingType = new ThingTypeUID("thing:type");
    expectedThingUID = new ThingUID(thingType, "thingid");
    expectedFirmware = FirmwareBuilder.create(thingType, "1").build();
    postedEvents = new LinkedList<>();
    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... 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, expectedThingUID, expectedFirmware, null);
}
Also used : Locale(java.util.Locale) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) Bundle(org.osgi.framework.Bundle) BundleResolver(org.eclipse.smarthome.core.util.BundleResolver) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) TranslationProvider(org.eclipse.smarthome.core.i18n.TranslationProvider) FirmwareUpdateProgressInfoEvent(org.eclipse.smarthome.core.thing.firmware.FirmwareUpdateProgressInfoEvent) FirmwareUpdateResultInfoEvent(org.eclipse.smarthome.core.thing.firmware.FirmwareUpdateResultInfoEvent) Event(org.eclipse.smarthome.core.events.Event) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Nullable(org.eclipse.jdt.annotation.Nullable) Before(org.junit.Before)

Example 4 with BundleResolver

use of org.eclipse.smarthome.core.util.BundleResolver in project smarthome by eclipse.

the class GenericThingProviderMultipleBundlesTest method setup.

@Before
public void setup() {
    thingProvider = new GenericThingProvider();
    Bundle bundle = mock(Bundle.class);
    when(bundle.getSymbolicName()).thenReturn(BUNDLE_NAME);
    BundleResolver bundleResolver = mock(BundleResolver.class);
    when(bundleResolver.resolveBundle(any(Class.class))).thenReturn(bundle);
    thingProvider.setBundleResolver(bundleResolver);
    ModelRepository modelRepository = mock(ModelRepository.class);
    ThingModel thingModel = mock(ThingModel.class);
    EList<ModelThing> dslThings = createModelBridge();
    when(thingModel.getThings()).thenReturn(dslThings);
    when(modelRepository.getModel(TEST_MODEL_THINGS)).thenReturn(thingModel);
    thingProvider.setModelRepository(modelRepository);
    // configure bridgeHandlerFactory to accept the bridge type UID and create a bridge:
    bridgeHandlerFactory = mock(ThingHandlerFactory.class);
    when(bridgeHandlerFactory.supportsThingType(BRIDGE_TYPE_UID)).thenReturn(true);
    when(bridgeHandlerFactory.createThing(eq(BRIDGE_TYPE_UID), any(Configuration.class), eq(BRIDGE_UID), eq(null))).thenReturn(BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_ID).build());
    thingProvider.addThingHandlerFactory(bridgeHandlerFactory);
    // configure thingHandlerFactory to accept the thing type UID and create a thing:
    thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(THING_TYPE_UID)).thenReturn(true);
    when(thingHandlerFactory.createThing(eq(THING_TYPE_UID), any(Configuration.class), eq(THING_UID), eq(BRIDGE_UID))).thenReturn(ThingBuilder.create(THING_TYPE_UID, THING_ID).build());
    thingProvider.addThingHandlerFactory(thingHandlerFactory);
}
Also used : BundleResolver(org.eclipse.smarthome.core.util.BundleResolver) ModelRepository(org.eclipse.smarthome.model.core.ModelRepository) Configuration(org.eclipse.smarthome.config.core.Configuration) Bundle(org.osgi.framework.Bundle) ThingModel(org.eclipse.smarthome.model.thing.thing.ThingModel) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) ModelThing(org.eclipse.smarthome.model.thing.thing.ModelThing) Before(org.junit.Before)

Aggregations

BundleResolver (org.eclipse.smarthome.core.util.BundleResolver)4 Locale (java.util.Locale)3 Before (org.junit.Before)3 Bundle (org.osgi.framework.Bundle)3 Event (org.eclipse.smarthome.core.events.Event)2 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)2 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)2 ThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)2 ArrayList (java.util.ArrayList)1 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)1 LocaleProvider (org.eclipse.smarthome.core.i18n.LocaleProvider)1 TranslationProvider (org.eclipse.smarthome.core.i18n.TranslationProvider)1 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)1 ItemStateEvent (org.eclipse.smarthome.core.items.events.ItemStateEvent)1 ReadyMarker (org.eclipse.smarthome.core.service.ReadyMarker)1 ReadyService (org.eclipse.smarthome.core.service.ReadyService)1 ManagedThingProvider (org.eclipse.smarthome.core.thing.ManagedThingProvider)1