use of org.osgi.service.component.ComponentContext in project smarthome by eclipse.
the class ThingLinkManagerOSGiTest method setup.
@Before
public void setup() {
context.clear();
registerVolatileStorageService();
thingRegistry = getService(ThingRegistry.class);
managedThingProvider = getService(ManagedThingProvider.class);
itemRegistry = getService(ItemRegistry.class);
assertNotNull(itemRegistry);
itemChannelLinkRegistry = getService(ItemChannelLinkRegistry.class);
assertThat(thingRegistry, is(notNullValue()));
ComponentContext componentContext = mock(ComponentContext.class);
when(componentContext.getBundleContext()).thenReturn(bundleContext);
TestThingHandlerFactory thingHandlerFactory = new TestThingHandlerFactory();
thingHandlerFactory.activate(componentContext);
registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
StateDescription state = new StateDescription(BigDecimal.valueOf(0), BigDecimal.valueOf(100), BigDecimal.valueOf(10), "%d Peek", true, Collections.singletonList(new StateOption("SOUND", "My great sound.")));
ChannelType channelType = ChannelTypeBuilder.state(new ChannelTypeUID("hue:alarm"), "Alarm Channel", "Number").withStateDescription(state).build();
List<ChannelType> channelTypes = singletonList(channelType);
ChannelTypeProvider channelTypeProvider = mock(ChannelTypeProvider.class);
when(channelTypeProvider.getChannelTypes(nullable(Locale.class))).thenReturn(channelTypes);
when(channelTypeProvider.getChannelType(any(ChannelTypeUID.class), nullable(Locale.class))).then(new Answer<@Nullable ChannelType>() {
@Override
@Nullable
public ChannelType answer(InvocationOnMock invocation) throws Throwable {
ChannelTypeUID uid = (ChannelTypeUID) invocation.getArgument(0);
return channelTypes.stream().filter(t -> t.getUID().equals(uid)).findFirst().get();
}
});
registerService(channelTypeProvider);
ThingType thingType = ThingTypeBuilder.instance(new ThingTypeUID("hue:lamp"), "label").withChannelDefinitions(singletonList(new ChannelDefinitionBuilder("1", channelType.getUID()).build())).build();
SimpleThingTypeProvider thingTypeProvider = new SimpleThingTypeProvider(singletonList(thingType));
registerService(thingTypeProvider);
}
use of org.osgi.service.component.ComponentContext in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method registerThingHandlerFactory.
private void registerThingHandlerFactory(ThingTypeUID thingTypeUID, Function<Thing, ThingHandler> thingHandlerProducer) {
ComponentContext context = mock(ComponentContext.class);
when(context.getBundleContext()).thenReturn(bundleContext);
TestThingHandlerFactory mockThingHandlerFactory = new TestThingHandlerFactory(thingTypeUID, thingHandlerProducer);
mockThingHandlerFactory.activate(context);
registerService(mockThingHandlerFactory, ThingHandlerFactory.class.getName());
}
use of org.osgi.service.component.ComponentContext in project smarthome by eclipse.
the class GenericThingProviderTest3 method setUp.
@Before
public void setUp() throws Exception {
thingRegistry = getService(ThingRegistry.class);
assertThat(thingRegistry, is(notNullValue()));
modelRepository = getService(ModelRepository.class);
assertThat(modelRepository, is(notNullValue()));
modelRepository.removeModel(TESTMODEL_NAME);
ComponentContext componentContextMock = mock(ComponentContext.class);
when(componentContextMock.getBundleContext()).thenReturn(bundleContext);
// create a "dumb" thing handler that acts as if the XML config was not yet loaded
dumbThingHandlerFactory = new DumbThingHandlerFactory(componentContextMock, true);
Collection<Thing> things = thingRegistry.getAll();
assertThat(things.size(), is(0));
String model = //
"dumb:DUMB:boo \"Test Label\" @ \"Test Location\" [" + //
" testConf=\"foo\"" + //
"]" + //
"{" + //
" Switch : manual [ duration = \"5\" ]" + "}";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
registerService(dumbThingHandlerFactory, ThingHandlerFactory.class.getName());
ConfigDescription configDescription = new ConfigDescription(new URI("test:test"), Stream.of(ConfigDescriptionParameterBuilder.create("testAdditional", ConfigDescriptionParameter.Type.TEXT).withRequired(false).withDefault("hello world").build(), ConfigDescriptionParameterBuilder.create("testConf", ConfigDescriptionParameter.Type.TEXT).withRequired(false).withDefault("bar").build()).collect(toList()));
ConfigDescriptionProvider configDescriptionProvider = mock(ConfigDescriptionProvider.class);
when(configDescriptionProvider.getConfigDescription(any(), nullable(Locale.class))).thenReturn(configDescription);
registerService(configDescriptionProvider);
}
use of org.osgi.service.component.ComponentContext in project smarthome by eclipse.
the class ChangeThingTypeOSGiTest method setup.
@Before
public void setup() throws URISyntaxException {
registerVolatileStorageService();
managedThingProvider = getService(ManagedThingProvider.class);
assertThat(managedThingProvider, is(notNullValue()));
configDescriptionRegistry = getService(ConfigDescriptionRegistry.class);
assertThat(configDescriptionRegistry, is(notNullValue()));
managedItemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
assertThat(managedItemChannelLinkProvider, is(notNullValue()));
managedItemProvider = getService(ManagedItemProvider.class);
assertThat(managedItemProvider, is(notNullValue()));
ComponentContext componentContext = mock(ComponentContext.class);
when(componentContext.getBundleContext()).thenReturn(bundleContext);
thingHandlerFactory = new SampleThingHandlerFactory();
thingHandlerFactory.activate(componentContext);
registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
Map<String, String> thingTypeGenericProperties = new HashMap<>();
thingTypeGenericProperties.put(PROPERTY_ON_GENERIC_THING_TYPE, GENERIC_VALUE);
thingTypeGenericProperties.put(PROPERTY_ON_GENERIC_AND_SPECIFIC_THING_TYPE, GENERIC_VALUE);
Map<String, String> thingTypeSpecificProperties = new HashMap<>();
thingTypeSpecificProperties.put(PROPERTY_ON_SPECIFIC_THING_TYPE, SPECIFIC_VALUE);
thingTypeSpecificProperties.put(PROPERTY_ON_GENERIC_AND_SPECIFIC_THING_TYPE, SPECIFIC_VALUE);
thingTypeGeneric = registerThingTypeAndConfigDescription(THING_TYPE_GENERIC_UID, thingTypeGenericProperties);
thingTypeSpecific = registerThingTypeAndConfigDescription(THING_TYPE_SPECIFIC_UID, thingTypeSpecificProperties);
ThingTypeProvider thingTypeProvider = mock(ThingTypeProvider.class);
when(thingTypeProvider.getThingType(any(), any())).thenAnswer(invocation -> thingTypes.get(invocation.getArgument(0)));
registerService(thingTypeProvider);
ThingTypeRegistry thingTypeRegistry = mock(ThingTypeRegistry.class);
when(thingTypeRegistry.getThingType(any(), any())).thenAnswer(invocation -> thingTypes.get(invocation.getArgument(0)));
registerService(thingTypeRegistry);
ConfigDescriptionProvider configDescriptionProvider = mock(ConfigDescriptionProvider.class);
when(configDescriptionProvider.getConfigDescription(any(), any())).thenAnswer(invocation -> configDescriptions.get(invocation.getArgument(0)));
registerService(configDescriptionProvider);
ChannelTypeProvider channelTypeProvider = mock(ChannelTypeProvider.class);
when(channelTypeProvider.getChannelTypes(any())).thenReturn(channelTypes.values());
when(channelTypeProvider.getChannelType(any(), any())).thenAnswer(invocation -> channelTypes.get(invocation.getArgument(0)));
registerService(channelTypeProvider);
ChannelGroupTypeProvider channelGroupTypeProvider = mock(ChannelGroupTypeProvider.class);
when(channelGroupTypeProvider.getChannelGroupTypes(any())).thenReturn(channelGroupTypes.values());
when(channelGroupTypeProvider.getChannelGroupType(any(), any())).thenAnswer(invocation -> channelGroupTypes.get(invocation.getArgument(0)));
registerService(channelGroupTypeProvider);
managedItemProvider.add(new StringItem(ITEM_GENERIC));
managedItemProvider.add(new StringItem(ITEM_SPECIFIC));
managedItemChannelLinkProvider.add(ITEM_CHANNEL_LINK_GENERIC);
managedItemChannelLinkProvider.add(ITEM_CHANNEL_LINK_SPECIFIC);
}
use of org.osgi.service.component.ComponentContext in project jackrabbit-oak by apache.
the class OsgiUtilTest method testComponentLookupWithNotFoundValue.
@Test
public void testComponentLookupWithNotFoundValue() {
Dictionary properties = mock(Dictionary.class);
doReturn(null).when(properties).get("name");
ComponentContext context = mock(ComponentContext.class);
doReturn(properties).when(context).getProperties();
assertNull(lookup(context, "name"));
}
Aggregations