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