Search in sources :

Example 1 with BaseThingHandlerFactory

use of org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory 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 BaseThingHandlerFactory

use of org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory in project smarthome by eclipse.

the class ThingRegistryOSGiTest method assertThatCreateThingDelegatesToRegisteredThingHandlerFactory.

@Test
public void assertThatCreateThingDelegatesToRegisteredThingHandlerFactory() {
    ThingTypeUID expectedThingTypeUID = THING_TYPE_UID;
    ThingUID expectedThingUID = new ThingUID(THING_TYPE_UID, THING1_ID);
    Configuration expectedConfiguration = new Configuration();
    ThingUID expectedBridgeUID = new ThingUID(THING_TYPE_UID, THING2_ID);
    String expectedLabel = "Test Thing";
    AsyncResultWrapper<Thing> thingResultWrapper = new AsyncResultWrapper<Thing>();
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    ThingHandlerFactory thingHandlerFactory = new BaseThingHandlerFactory() {

        @Override
        public boolean supportsThingType(@NonNull ThingTypeUID thingTypeUID) {
            return true;
        }

        @Override
        @Nullable
        protected ThingHandler createHandler(@NonNull Thing thing) {
            return null;
        }

        @Override
        @Nullable
        public Thing createThing(@NonNull ThingTypeUID thingTypeUID, @NonNull Configuration configuration, @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
            assertThat(thingTypeUID, is(expectedThingTypeUID));
            assertThat(configuration, is(expectedConfiguration));
            assertThat(thingUID, is(expectedThingUID));
            assertThat(bridgeUID, is(expectedBridgeUID));
            Thing thing = ThingBuilder.create(thingTypeUID, thingUID.getId()).withBridge(bridgeUID).build();
            thingResultWrapper.set(thing);
            return thing;
        }
    };
    registerThingHandlerFactory(thingHandlerFactory);
    Thing thing = thingRegistry.createThingOfType(expectedThingTypeUID, expectedThingUID, expectedBridgeUID, expectedLabel, expectedConfiguration);
    waitForAssert(() -> {
        assertTrue(thingResultWrapper.isSet());
    });
    assertThat(thing, is(thingResultWrapper.getWrappedObject()));
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) AsyncResultWrapper(org.eclipse.smarthome.test.AsyncResultWrapper) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) NonNull(org.eclipse.jdt.annotation.NonNull) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) Thing(org.eclipse.smarthome.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

NonNull (org.eclipse.jdt.annotation.NonNull)2 Thing (org.eclipse.smarthome.core.thing.Thing)2 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)2 BaseThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory)2 ThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)2 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)2 Test (org.junit.Test)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 Channel (org.eclipse.smarthome.core.thing.Channel)1 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)1 ThingRegistry (org.eclipse.smarthome.core.thing.ThingRegistry)1 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)1 BaseThingHandler (org.eclipse.smarthome.core.thing.binding.BaseThingHandler)1 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)1 ThingHandlerCallback (org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback)1 ChannelBuilder (org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder)1 AsyncResultWrapper (org.eclipse.smarthome.test.AsyncResultWrapper)1