Search in sources :

Example 21 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class CommunicationManagerTest method testItemCommandEvent_typeDowncast.

@Test
public void testItemCommandEvent_typeDowncast() {
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannels(ChannelBuilder.create(STATE_CHANNEL_UID_2, "Dimmer").withKind(ChannelKind.STATE).build()).build();
    thing.setHandler(mockHandler);
    when(thingRegistry.get(eq(THING_UID))).thenReturn(thing);
    manager.receive(ItemEventFactory.createCommandEvent(ITEM_NAME_2, HSBType.fromRGB(128, 128, 128)));
    waitForAssert(() -> {
        ArgumentCaptor<Command> commandCaptor = ArgumentCaptor.forClass(Command.class);
        verify(stateProfile).onCommandFromItem(commandCaptor.capture());
        Command command = commandCaptor.getValue();
        assertNotNull(command);
        assertEquals(PercentType.class, command.getClass());
    });
    verifyNoMoreInteractions(stateProfile);
    verifyNoMoreInteractions(triggerProfile);
}
Also used : Command(org.eclipse.smarthome.core.types.Command) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 22 with Thing

use of org.eclipse.smarthome.core.thing.Thing 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 23 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_replacedInitializedThing.

@Test
public void testChildHandlerInitialized_replacedInitializedThing() {
    Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
    Semaphore thingUpdatedSemapthore = new Semaphore(1);
    registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
            try {
                childHandlerInitializedSemaphore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void thingUpdated(Thing thing) {
            this.thing = thing;
            try {
                thingUpdatedSemapthore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
    managedThingProvider.add(bridge);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, bridge.getStatus());
    });
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
    managedThingProvider.add(thing);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, thing.getStatus());
    });
    assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
    Thing thing2 = ThingBuilder.create(THING_TYPE_UID, THING_UID).withBridge(BRIDGE_UID).build();
    managedThingProvider.update(thing2);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, thing2.getStatus());
    });
    // childHandlerInitialized(...) is not be called - framework calls ThingHandler.thingUpdated(...) instead.
    assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
    // ThingHandler.thingUpdated(...) must be called
    assertEquals(0, thingUpdatedSemapthore.availablePermits());
}
Also used : BaseBridgeHandler(org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler) Command(org.eclipse.smarthome.core.types.Command) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Semaphore(java.util.concurrent.Semaphore) Bridge(org.eclipse.smarthome.core.thing.Bridge) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 24 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingRegistryOSGiTest method assertThatThingRegistryEventSubscribersReceiveEventsAboutThingChanges.

@Test
public void assertThatThingRegistryEventSubscribersReceiveEventsAboutThingChanges() {
    EventSubscriber thingRegistryEventSubscriber = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            Set<String> types = new HashSet<>();
            types.add(ThingAddedEvent.TYPE);
            types.add(ThingRemovedEvent.TYPE);
            types.add(ThingUpdatedEvent.TYPE);
            return types;
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event event) {
            receivedEvent = event;
        }
    };
    registerService(thingRegistryEventSubscriber);
    // add new thing
    managedThingProvider.add(THING);
    waitForAssert(() -> {
        assertThat(receivedEvent, notNullValue());
    });
    assertThat(receivedEvent, is(instanceOf(ThingAddedEvent.class)));
    receivedEvent = null;
    // update thing
    Thing updatedThing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
    managedThingProvider.update(updatedThing);
    waitForAssert(() -> {
        assertThat(receivedEvent, notNullValue());
    });
    assertThat(receivedEvent, is(instanceOf(ThingUpdatedEvent.class)));
    receivedEvent = null;
    // remove thing
    managedThingProvider.remove(THING.getUID());
    waitForAssert(() -> {
        assertThat(receivedEvent, notNullValue());
    });
    assertThat(receivedEvent, is(instanceOf(ThingRemovedEvent.class)));
    receivedEvent = null;
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) ThingUpdatedEvent(org.eclipse.smarthome.core.thing.events.ThingUpdatedEvent) ThingRemovedEvent(org.eclipse.smarthome.core.thing.events.ThingRemovedEvent) ThingAddedEvent(org.eclipse.smarthome.core.thing.events.ThingAddedEvent) Event(org.eclipse.smarthome.core.events.Event) Thing(org.eclipse.smarthome.core.thing.Thing) HashSet(java.util.HashSet) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 25 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingRegistryOSGiTest method assertThatThingRegistryDelegatesConfigUpdateToThingHandler.

@Test
public void assertThatThingRegistryDelegatesConfigUpdateToThingHandler() {
    ThingUID thingUID = new ThingUID("binding:type:thing");
    Thing thing = ThingBuilder.create(THING_TYPE_UID, thingUID).build();
    ThingHandler thingHandler = new BaseThingHandler(thing) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }

        @Override
        public void handleConfigurationUpdate(@NonNull Map<@NonNull String, @NonNull Object> configurationParameters) {
            changedParameters = configurationParameters;
        }
    };
    thing.setHandler(thingHandler);
    ThingProvider thingProvider = new ThingProvider() {

        @Override
        public void addProviderChangeListener(ProviderChangeListener<@NonNull Thing> listener) {
        }

        @Override
        public Collection<@NonNull Thing> getAll() {
            return Collections.singleton(thing);
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<@NonNull Thing> listener) {
        }
    };
    registerService(thingProvider);
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("param1", "value1");
    parameters.put("param2", 1);
    thingRegistry.updateConfiguration(thingUID, parameters);
    assertThat(changedParameters.entrySet(), is(equalTo(parameters.entrySet())));
}
Also used : BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) HashMap(java.util.HashMap) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) ThingProvider(org.eclipse.smarthome.core.thing.ThingProvider) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) Command(org.eclipse.smarthome.core.types.Command) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) NonNull(org.eclipse.jdt.annotation.NonNull) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) HashMap(java.util.HashMap) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

Thing (org.eclipse.smarthome.core.thing.Thing)98 Test (org.junit.Test)43 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)28 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)24 Configuration (org.eclipse.smarthome.config.core.Configuration)19 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)19 JavaTest (org.eclipse.smarthome.test.java.JavaTest)18 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)14 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)13 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 Item (org.eclipse.smarthome.core.items.Item)9 Path (javax.ws.rs.Path)8 Nullable (org.eclipse.jdt.annotation.Nullable)8 Locale (java.util.Locale)7 RolesAllowed (javax.annotation.security.RolesAllowed)7 Channel (org.eclipse.smarthome.core.thing.Channel)7 ThingHandlerCallback (org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback)7 Command (org.eclipse.smarthome.core.types.Command)7 List (java.util.List)6