use of org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method testCreateChannelBuilder.
@Test
public void testCreateChannelBuilder() throws Exception {
registerThingTypeProvider();
registerChannelTypeProvider();
AtomicReference<ThingHandlerCallback> thc = new AtomicReference<>();
ThingHandlerFactory thingHandlerFactory = new BaseThingHandlerFactory() {
@Override
public boolean supportsThingType(@NonNull ThingTypeUID thingTypeUID) {
return true;
}
@Override
@Nullable
protected ThingHandler createHandler(@NonNull Thing thing) {
ThingHandler mockHandler = mock(ThingHandler.class);
doAnswer(a -> {
thc.set((ThingHandlerCallback) a.getArguments()[0]);
return null;
}).when(mockHandler).setCallback(any(ThingHandlerCallback.class));
when(mockHandler.getThing()).thenReturn(THING);
return mockHandler;
}
};
registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
new Thread((Runnable) () -> managedThingProvider.add(THING)).start();
waitForAssert(() -> {
assertNotNull(thc.get());
});
ChannelBuilder channelBuilder = thc.get().createChannelBuilder(new ChannelUID(THING_UID, "test"), CHANNEL_TYPE_UID);
Channel channel = channelBuilder.build();
assertThat(channel.getLabel(), is("Test Label"));
assertThat(channel.getDescription(), is("Test Description"));
assertThat(channel.getAcceptedItemType(), is("Switch"));
assertThat(channel.getDefaultTags().size(), is(1));
assertThat(channel.getDefaultTags().iterator().next(), is("Test Tag"));
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback in project smarthome by eclipse.
the class AbstractDmxThingTest method initializeBridge.
private void initializeBridge() {
bridgeProperties = new HashMap<>();
bridge = BridgeBuilder.create(THING_TYPE_TEST_BRIDGE, "testbridge").withLabel("Test Bridge").withConfiguration(new Configuration(bridgeProperties)).build();
dmxBridgeHandler = new TestBridgeHandler(bridge);
bridge.setHandler(dmxBridgeHandler);
ThingHandlerCallback bridgeHandler = mock(ThingHandlerCallback.class);
doAnswer(answer -> {
((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
return null;
}).when(bridgeHandler).statusUpdated(any(), any());
dmxBridgeHandler.setCallback(bridgeHandler);
dmxBridgeHandler.initialize();
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback in project smarthome by eclipse.
the class Lib485BridgeHandlerTest method setUp.
@Before
public void setUp() {
bridgeProperties = new HashMap<>();
bridgeProperties.put(CONFIG_ADDRESS, TEST_ADDRESS);
bridge = BridgeBuilder.create(THING_TYPE_LIB485_BRIDGE, "lib485bridge").withLabel("Lib485 Bridge").withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build()).withConfiguration(new Configuration(bridgeProperties)).build();
ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
doAnswer(answer -> {
((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
return null;
}).when(mockCallback).statusUpdated(any(), any());
bridgeHandler = new Lib485BridgeHandler(bridge) {
@Override
protected void validateConfigurationParameters(Map<String, Object> configurationParameters) {
}
};
bridgeHandler.getThing().setHandler(bridgeHandler);
bridgeHandler.setCallback(mockCallback);
bridgeHandler.initialize();
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method testInitializeCallsThingUpdated.
@Test
public void testInitializeCallsThingUpdated() throws Exception {
registerThingTypeProvider();
AtomicReference<ThingHandlerCallback> thc = new AtomicReference<>();
AtomicReference<Boolean> initializeRunning = new AtomicReference<>(false);
registerThingHandlerFactory(THING_TYPE_UID, thing -> {
ThingHandler mockHandler = mock(ThingHandler.class);
doAnswer(a -> {
thc.set((ThingHandlerCallback) a.getArguments()[0]);
return null;
}).when(mockHandler).setCallback(ArgumentMatchers.isA(ThingHandlerCallback.class));
doAnswer(a -> {
initializeRunning.set(true);
// call thingUpdated() from within initialize()
thc.get().thingUpdated(THING);
// hang on a little to provoke a potential dead-lock
Thread.sleep(1000);
initializeRunning.set(false);
return null;
}).when(mockHandler).initialize();
when(mockHandler.getThing()).thenReturn(THING);
return mockHandler;
});
new Thread((Runnable) () -> managedThingProvider.add(THING)).start();
waitForAssert(() -> {
assertThat(THING.getStatus(), is(ThingStatus.INITIALIZING));
});
// ensure it didn't run into a dead-lock which gets resolved by the SafeCaller.
waitForAssert(() -> {
assertThat(initializeRunning.get(), is(false));
}, SafeCaller.DEFAULT_TIMEOUT - 100, 50);
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback in project smarthome by eclipse.
the class ArtnetBridgeHandlerTest method setUp.
@Before
public void setUp() {
bridgeProperties = new HashMap<>();
bridgeProperties.put(CONFIG_ADDRESS, TEST_ADDRESS);
bridgeProperties.put(CONFIG_UNIVERSE, TEST_UNIVERSE);
bridge = BridgeBuilder.create(THING_TYPE_ARTNET_BRIDGE, "artnetbridge").withLabel("Artnet Bridge").withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build()).withConfiguration(new Configuration(bridgeProperties)).build();
ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
doAnswer(answer -> {
((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
return null;
}).when(mockCallback).statusUpdated(any(), any());
bridgeHandler = new ArtnetBridgeHandler(bridge) {
@Override
protected void validateConfigurationParameters(Map<String, Object> configurationParameters) {
}
};
bridgeHandler.getThing().setHandler(bridgeHandler);
bridgeHandler.setCallback(mockCallback);
bridgeHandler.initialize();
}
Aggregations