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