Search in sources :

Example 41 with ComponentContext

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);
}
Also used : Locale(java.util.Locale) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Channel(org.eclipse.smarthome.core.thing.Channel) CoreMatchers(org.hamcrest.CoreMatchers) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) ChannelTypeBuilder(org.eclipse.smarthome.core.thing.type.ChannelTypeBuilder) ComponentContext(org.osgi.service.component.ComponentContext) ThingTypeBuilder(org.eclipse.smarthome.core.thing.type.ThingTypeBuilder) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) Command(org.eclipse.smarthome.core.types.Command) Collections.singletonList(java.util.Collections.singletonList) BigDecimal(java.math.BigDecimal) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) Nullable(org.eclipse.jdt.annotation.Nullable) Locale(java.util.Locale) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) After(org.junit.After) ChannelDefinitionBuilder(org.eclipse.smarthome.core.thing.type.ChannelDefinitionBuilder) Configuration(org.eclipse.smarthome.config.core.Configuration) ThingLinkManager(org.eclipse.smarthome.core.thing.link.ThingLinkManager) Before(org.junit.Before) ChannelTypeProvider(org.eclipse.smarthome.core.thing.type.ChannelTypeProvider) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) StateOption(org.eclipse.smarthome.core.types.StateOption) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Mockito(org.mockito.Mockito) List(java.util.List) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) StateDescription(org.eclipse.smarthome.core.types.StateDescription) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) ThingType(org.eclipse.smarthome.core.thing.type.ThingType) Assert(org.junit.Assert) Collections(java.util.Collections) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ChannelDefinitionBuilder(org.eclipse.smarthome.core.thing.type.ChannelDefinitionBuilder) ComponentContext(org.osgi.service.component.ComponentContext) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) ThingType(org.eclipse.smarthome.core.thing.type.ThingType) StateOption(org.eclipse.smarthome.core.types.StateOption) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ChannelTypeProvider(org.eclipse.smarthome.core.thing.type.ChannelTypeProvider) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) Nullable(org.eclipse.jdt.annotation.Nullable) StateDescription(org.eclipse.smarthome.core.types.StateDescription) Before(org.junit.Before)

Example 42 with ComponentContext

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());
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)

Example 43 with ComponentContext

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);
}
Also used : Locale(java.util.Locale) ComponentContext(org.osgi.service.component.ComponentContext) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) URI(java.net.URI) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) ModelRepository(org.eclipse.smarthome.model.core.ModelRepository) ByteArrayInputStream(java.io.ByteArrayInputStream) Thing(org.eclipse.smarthome.core.thing.Thing) ConfigDescriptionProvider(org.eclipse.smarthome.config.core.ConfigDescriptionProvider) Before(org.junit.Before)

Example 44 with ComponentContext

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);
}
Also used : ManagedItemProvider(org.eclipse.smarthome.core.items.ManagedItemProvider) ManagedItemChannelLinkProvider(org.eclipse.smarthome.core.thing.link.ManagedItemChannelLinkProvider) ComponentContext(org.osgi.service.component.ComponentContext) HashMap(java.util.HashMap) StringItem(org.eclipse.smarthome.core.library.items.StringItem) ChannelGroupTypeProvider(org.eclipse.smarthome.core.thing.type.ChannelGroupTypeProvider) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ThingTypeRegistry(org.eclipse.smarthome.core.thing.type.ThingTypeRegistry) ChannelTypeProvider(org.eclipse.smarthome.core.thing.type.ChannelTypeProvider) ConfigDescriptionRegistry(org.eclipse.smarthome.config.core.ConfigDescriptionRegistry) ConfigDescriptionProvider(org.eclipse.smarthome.config.core.ConfigDescriptionProvider) Before(org.junit.Before)

Example 45 with ComponentContext

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"));
}
Also used : Dictionary(java.util.Dictionary) ComponentContext(org.osgi.service.component.ComponentContext) Test(org.junit.Test)

Aggregations

ComponentContext (org.osgi.service.component.ComponentContext)85 BundleContext (org.osgi.framework.BundleContext)49 Test (org.junit.Test)36 Before (org.junit.Before)18 Dictionary (java.util.Dictionary)17 JaxbUser (org.opencastproject.security.api.JaxbUser)11 SecurityService (org.opencastproject.security.api.SecurityService)11 Hashtable (java.util.Hashtable)10 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)10 JaxbRole (org.opencastproject.security.api.JaxbRole)10 File (java.io.File)9 URI (java.net.URI)9 Bundle (org.osgi.framework.Bundle)9 OrganizationDirectoryService (org.opencastproject.security.api.OrganizationDirectoryService)7 User (org.opencastproject.security.api.User)7 UserDirectoryService (org.opencastproject.security.api.UserDirectoryService)7 HashMap (java.util.HashMap)6 Workspace (org.opencastproject.workspace.api.Workspace)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5