Search in sources :

Example 1 with ThingHandlerCallback

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"));
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) NonNull(org.eclipse.jdt.annotation.NonNull) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ChannelBuilder(org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 2 with ThingHandlerCallback

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();
}
Also used : TestBridgeHandler(org.eclipse.smarthome.binding.dmx.test.TestBridgeHandler) Configuration(org.eclipse.smarthome.config.core.Configuration) ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 3 with ThingHandlerCallback

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();
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) Lib485BridgeHandler(org.eclipse.smarthome.binding.dmx.handler.Lib485BridgeHandler) ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) Thing(org.eclipse.smarthome.core.thing.Thing) Before(org.junit.Before)

Example 4 with ThingHandlerCallback

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);
}
Also used : ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 5 with ThingHandlerCallback

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();
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) ArtnetBridgeHandler(org.eclipse.smarthome.binding.dmx.handler.ArtnetBridgeHandler) ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) Thing(org.eclipse.smarthome.core.thing.Thing) Before(org.junit.Before)

Aggregations

ThingHandlerCallback (org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback)8 Thing (org.eclipse.smarthome.core.thing.Thing)7 Configuration (org.eclipse.smarthome.config.core.Configuration)6 Before (org.junit.Before)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 TestBridgeHandler (org.eclipse.smarthome.binding.dmx.test.TestBridgeHandler)2 BaseThingHandler (org.eclipse.smarthome.core.thing.binding.BaseThingHandler)2 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)2 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)2 Test (org.junit.Test)2 NonNull (org.eclipse.jdt.annotation.NonNull)1 ArtnetBridgeHandler (org.eclipse.smarthome.binding.dmx.handler.ArtnetBridgeHandler)1 Lib485BridgeHandler (org.eclipse.smarthome.binding.dmx.handler.Lib485BridgeHandler)1 SacnBridgeHandler (org.eclipse.smarthome.binding.dmx.handler.SacnBridgeHandler)1 Channel (org.eclipse.smarthome.core.thing.Channel)1 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)1 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)1 BaseThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory)1 ThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)1 ChannelBuilder (org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder)1