Search in sources :

Example 21 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-core by openhab.

the class GenericThingProviderTest3 method setUp.

@BeforeEach
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 = ConfigDescriptionBuilder.create(new URI("test:test")).withParameters(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())).build();
    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.openhab.core.config.core.ConfigDescription) DumbThingHandlerFactory(org.openhab.core.model.thing.testsupport.hue.DumbThingHandlerFactory) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) DumbThingHandlerFactory(org.openhab.core.model.thing.testsupport.hue.DumbThingHandlerFactory) URI(java.net.URI) ThingRegistry(org.openhab.core.thing.ThingRegistry) ModelRepository(org.openhab.core.model.core.ModelRepository) ByteArrayInputStream(java.io.ByteArrayInputStream) Thing(org.openhab.core.thing.Thing) ConfigDescriptionProvider(org.openhab.core.config.core.ConfigDescriptionProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 22 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-core by openhab.

the class GenericThingProviderTest4 method setUp.

@BeforeEach
public void setUp() {
    registerVolatileStorageService();
    readyService = getService(ReadyService.class);
    assertThat(readyService, is(notNullValue()));
    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);
    hueThingHandlerFactory = new TestHueThingHandlerFactoryX(componentContextMock) {

        @Override
        @Nullable
        protected ThingHandler createHandler(final Thing thing) {
            if (thing instanceof Bridge) {
                return new TestBridgeHandler((Bridge) thing);
            } else {
                return new BaseThingHandler(thing) {

                    @Override
                    public void handleCommand(ChannelUID arg0, Command arg1) {
                    }

                    @Override
                    public void initialize() {
                        updateStatus(ThingStatus.ONLINE);
                    }
                };
            }
        }
    };
    bundle = FrameworkUtil.getBundle(TestHueThingHandlerFactoryX.class);
    removeReadyMarker();
}
Also used : TestHueThingHandlerFactoryX(org.openhab.core.model.thing.testsupport.hue.TestHueThingHandlerFactoryX) ComponentContext(org.osgi.service.component.ComponentContext) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ReadyService(org.openhab.core.service.ReadyService) ThingHandler(org.openhab.core.thing.binding.ThingHandler) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingRegistry(org.openhab.core.thing.ThingRegistry) ModelRepository(org.openhab.core.model.core.ModelRepository) Command(org.openhab.core.types.Command) ChannelUID(org.openhab.core.thing.ChannelUID) Nullable(org.eclipse.jdt.annotation.Nullable) Thing(org.openhab.core.thing.Thing) Bridge(org.openhab.core.thing.Bridge) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 23 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-core by openhab.

the class BindingBaseClassesOSGiTest method assertConfigurationIsRolledbackOnError.

@Test
public void assertConfigurationIsRolledbackOnError() {
    SimpleThingHandlerFactory thingHandlerFactory = new SimpleThingHandlerFactory();
    thingHandlerFactory.activate(componentContextMock);
    registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
    registerThingTypeAndConfigDescription();
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    ThingTypeUID thingTypeUID = new ThingTypeUID("bindingId:type");
    ThingUID thingUID = new ThingUID("bindingId:type:thingId");
    Thing thing = ThingBuilder.create(thingTypeUID, thingUID).build();
    managedThingProvider.add(thing);
    // set the config to an initial value
    thingRegistry.updateConfiguration(thingUID, Map.of("parameter", "before"));
    assertThat(thing.getConfiguration().get("parameter"), is("before"));
    // let it fail next time...
    ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
    Mockito.doThrow(new IllegalStateException()).when(callback).thingUpdated(thing);
    ((SimpleThingHandler) thing.getHandler()).setCallback(callback);
    try {
        thingRegistry.updateConfiguration(thingUID, Map.of("parameter", "after"));
        fail("There should have been an exception!");
    } catch (IllegalStateException e) {
    // all good, we want that
    }
    // now check if the thing's configuration has been rolled back
    assertThat(thing.getConfiguration().get("parameter"), is("before"));
}
Also used : ThingUID(org.openhab.core.thing.ThingUID) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Thing(org.openhab.core.thing.Thing) ThingRegistry(org.openhab.core.thing.ThingRegistry) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 24 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-core by openhab.

the class ChannelCommandDescriptionProviderOSGiTest method presentItemCommandDescription.

/**
 * Assert that item's command description is present.
 */
@Test
public void presentItemCommandDescription() throws ItemNotFoundException {
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    assertNotNull(thingRegistry);
    ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
    assertNotNull(managedThingProvider);
    registerService(new TestDynamicCommandDescriptionProvider(), DynamicCommandDescriptionProvider.class.getName());
    Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration());
    assertNotNull(thing);
    if (thing == null) {
        throw new IllegalStateException("thing is null");
    }
    managedThingProvider.add(thing);
    ItemChannelLink link = new ItemChannelLink("TestItem1", getChannel(thing, "1").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem7_1", getChannel(thing, "7_1").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID());
    linkRegistry.add(link);
    // 
    final Collection<Item> items = itemRegistry.getItems();
    assertFalse(items.isEmpty());
    Item item = itemRegistry.getItem("TestItem1");
    assertEquals(CoreItemFactory.NUMBER, item.getType());
    CommandDescription command = item.getCommandDescription();
    assertNotNull(command);
    List<CommandOption> opts = command.getCommandOptions();
    assertNotNull(opts);
    assertEquals(1, opts.size());
    final CommandOption opt0 = opts.get(0);
    assertNotNull(opt0);
    assertEquals("SOUND", opt0.getCommand());
    assertEquals("My great sound.", opt0.getLabel());
    item = itemRegistry.getItem("TestItem7_1");
    assertEquals(CoreItemFactory.NUMBER, item.getType());
    command = item.getCommandDescription();
    assertNotNull(command);
    opts = command.getCommandOptions();
    assertNotNull(opts);
    assertEquals(1, opts.size());
    final CommandOption opt1 = opts.get(0);
    assertNotNull(opt1);
    assertEquals("COMMAND", opt1.getCommand());
    assertEquals("My command.", opt1.getLabel());
    item = itemRegistry.getItem("TestItem7_2");
    assertEquals(CoreItemFactory.NUMBER, item.getType());
    command = item.getCommandDescription();
    assertNotNull(command);
    opts = command.getCommandOptions();
    assertNotNull(opts);
    assertEquals(1, opts.size());
    final CommandOption opt2 = opts.get(0);
    assertEquals("NEW COMMAND", opt2.getCommand());
    assertEquals("My new command.", opt2.getLabel());
}
Also used : Configuration(org.openhab.core.config.core.Configuration) CommandOption(org.openhab.core.types.CommandOption) CommandDescription(org.openhab.core.types.CommandDescription) ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) ThingRegistry(org.openhab.core.thing.ThingRegistry) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) DynamicCommandDescriptionProvider(org.openhab.core.thing.type.DynamicCommandDescriptionProvider) BaseDynamicCommandDescriptionProvider(org.openhab.core.thing.binding.BaseDynamicCommandDescriptionProvider) ThingUID(org.openhab.core.thing.ThingUID) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 25 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-core by openhab.

the class ChannelStateDescriptionProviderOSGiTest method wrongItemStateDescription.

@Test
public void wrongItemStateDescription() throws ItemNotFoundException {
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    assertNotNull(thingRegistry);
    ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
    assertNotNull(managedThingProvider);
    registerService(new TestMalfunctioningDynamicStateDescriptionProvider(), DynamicStateDescriptionProvider.class.getName());
    registerService(new TestDynamicStateDescriptionProvider(), DynamicStateDescriptionProvider.class.getName());
    Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration());
    assertNotNull(thing);
    if (thing == null) {
        throw new IllegalStateException("thing is null");
    }
    managedThingProvider.add(thing);
    ItemChannelLink link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID());
    linkRegistry.add(link);
    // 
    final Collection<Item> items = itemRegistry.getItems();
    assertFalse(items.isEmpty());
    Item item = itemRegistry.getItem("TestItem7_2");
    StateDescription state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.valueOf(1), state.getMinimum());
    assertEquals(BigDecimal.valueOf(101), state.getMaximum());
    assertEquals(BigDecimal.valueOf(20), state.getStep());
    assertEquals("NEW %d Peek", state.getPattern());
    assertFalse(state.isReadOnly());
    List<StateOption> opts = state.getOptions();
    assertNotNull(opts);
    assertEquals(1, opts.size());
    final StateOption opt2 = opts.get(0);
    assertEquals("NEW SOUND", opt2.getValue());
    assertEquals("My great new sound.", opt2.getLabel());
}
Also used : Configuration(org.openhab.core.config.core.Configuration) ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) BaseDynamicStateDescriptionProvider(org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider) DynamicStateDescriptionProvider(org.openhab.core.thing.type.DynamicStateDescriptionProvider) StateOption(org.openhab.core.types.StateOption) ThingRegistry(org.openhab.core.thing.ThingRegistry) NumberItem(org.openhab.core.library.items.NumberItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) ColorItem(org.openhab.core.library.items.ColorItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) ThingUID(org.openhab.core.thing.ThingUID) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Thing(org.openhab.core.thing.Thing) StateDescription(org.openhab.core.types.StateDescription) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

ThingRegistry (org.openhab.core.thing.ThingRegistry)29 Thing (org.openhab.core.thing.Thing)18 Test (org.junit.jupiter.api.Test)17 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)14 ThingUID (org.openhab.core.thing.ThingUID)13 ManagedThingProvider (org.openhab.core.thing.ManagedThingProvider)11 Configuration (org.openhab.core.config.core.Configuration)9 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)9 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ThingHandler (org.openhab.core.thing.binding.ThingHandler)7 Nullable (org.eclipse.jdt.annotation.Nullable)6 ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)6 Bridge (org.openhab.core.thing.Bridge)5 ThingProvider (org.openhab.core.thing.ThingProvider)5 ItemChannelLink (org.openhab.core.thing.link.ItemChannelLink)5 Item (org.openhab.core.items.Item)4 NumberItem (org.openhab.core.library.items.NumberItem)4 VolatileStorageService (org.openhab.core.test.storage.VolatileStorageService)4 Locale (java.util.Locale)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3