use of org.openhab.core.thing.link.ItemChannelLink 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.link.ItemChannelLink 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());
}
use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.
the class ThingManagerOSGiTest method thingManagerHandlesThingUpdatesCorrectly.
@Test
@SuppressWarnings({ "null", "unchecked" })
public void thingManagerHandlesThingUpdatesCorrectly() {
String itemName = "name";
managedThingProvider.add(thing);
managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID));
class ThingHandlerState {
@Nullable
ThingHandlerCallback callback;
}
final ThingHandlerState state = new ThingHandlerState();
ThingHandler thingHandler = mock(ThingHandler.class);
doAnswer(new Answer<Void>() {
@Override
@Nullable
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);
ThingRegistry thingRegistry = getService(ThingRegistry.class);
RegistryChangeListener<Thing> registryChangeListener = mock(RegistryChangeListener.class);
try {
thingRegistry.addRegistryChangeListener(registryChangeListener);
state.callback.thingUpdated(thing);
verify(registryChangeListener, times(1)).updated(any(Thing.class), any(Thing.class));
} finally {
thingRegistry.removeRegistryChangeListener(registryChangeListener);
}
}
use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.
the class ThingManagerOSGiTest method thingManagerConsidersUNKNOWNasReadyToUseAndForwardsCommand.
@Test
public void thingManagerConsidersUNKNOWNasReadyToUseAndForwardsCommand() {
class ThingHandlerState {
boolean handleCommandCalled;
@Nullable
ChannelUID calledChannelUID;
@Nullable
Command calledCommand;
@Nullable
ThingHandlerCallback callback;
}
final ThingHandlerState state = new ThingHandlerState();
managedThingProvider.add(thing);
ThingHandler thingHandler = mock(ThingHandler.class);
doAnswer(new Answer<Void>() {
@Override
@Nullable
public Void answer(InvocationOnMock invocation) throws Throwable {
state.callback = (ThingHandlerCallback) invocation.getArgument(0);
return null;
}
}).when(thingHandler).setCallback(any(ThingHandlerCallback.class));
doAnswer(new Answer<Void>() {
@Override
@Nullable
public Void answer(InvocationOnMock invocation) throws Throwable {
state.handleCommandCalled = true;
state.calledChannelUID = (ChannelUID) invocation.getArgument(0);
state.calledCommand = (Command) invocation.getArgument(1);
return null;
}
}).when(thingHandler).handleCommand(any(ChannelUID.class), any(Command.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);
String itemName = "testItem";
Item item = new StringItem(itemName);
itemRegistry.add(item);
itemChannelLinkRegistry.add(new ItemChannelLink(itemName, new ChannelUID(thing.getUID(), "channel")));
waitForAssert(() -> assertThat(itemRegistry.get(itemName), is(notNullValue())));
eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, OnOffType.ON));
assertThat(state.handleCommandCalled, is(false));
ThingStatusInfo unknownNone = ThingStatusInfoBuilder.create(ThingStatus.UNKNOWN, ThingStatusDetail.NONE).build();
state.callback.statusUpdated(thing, unknownNone);
assertThat(thing.getStatusInfo(), is(unknownNone));
eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, OnOffType.ON));
waitForAssert(() -> {
assertThat(state.handleCommandCalled, is(true));
assertThat(state.calledChannelUID, is(equalTo(new ChannelUID(thing.getUID(), "channel"))));
assertThat(state.calledCommand, is(equalTo(OnOffType.ON)));
});
}
use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.
the class ThingManagerOSGiJavaTest method testThatIsLinkedReturnsTrue.
@Test
public void testThatIsLinkedReturnsTrue() throws Exception {
AtomicReference<ThingHandlerCallback> thc = initializeThingHandlerCallback();
ManagedItemChannelLinkProvider managedItemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
assertNotNull(managedItemChannelLinkProvider);
managedItemChannelLinkProvider.add(new ItemChannelLink("item", CHANNEL_UID));
assertTrue(thc.get().isChannelLinked(CHANNEL_UID));
}
Aggregations