Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
convert types before passing them to the handler (#5183)
Browse files Browse the repository at this point in the history
...as so far types which were incompatible with the channel were
forwarded to the binding anyway if thay made it into the system.

Channel can indicate which types they accept indirectly by specifying
an acceptedItemType

Signed-off-by: Simon Kaufmann <simon.kfm@googlemail.com>
  • Loading branch information
sjsf authored and maggu2810 committed Mar 7, 2018
1 parent d47f7d0 commit 0df63ab
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 67 deletions.
Expand Up @@ -35,7 +35,7 @@ import org.eclipse.smarthome.core.items.ItemRegistry
import org.eclipse.smarthome.core.items.events.ItemEventFactory
import org.eclipse.smarthome.core.items.events.ItemStateEvent
import org.eclipse.smarthome.core.library.items.StringItem
import org.eclipse.smarthome.core.library.types.DecimalType
import org.eclipse.smarthome.core.library.types.OnOffType
import org.eclipse.smarthome.core.library.types.StringType
import org.eclipse.smarthome.core.service.ReadyMarker
import org.eclipse.smarthome.core.service.ReadyService
Expand Down Expand Up @@ -709,13 +709,13 @@ class ThingManagerOSGiTest extends OSGiTest {
callback.statusUpdated(THING, ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build())

// event should be delivered
eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, new DecimalType(10)))
eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, OnOffType.ON))
waitForAssert { assertThat handleCommandWasCalled, is(true) }

handleCommandWasCalled = false

// event should not be delivered, because the source is the same
eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, new DecimalType(10), CHANNEL_UID.toString()))
eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, OnOffType.ON, CHANNEL_UID.toString()))
waitFor({handleCommandWasCalled == true}, 1000)
assertThat handleCommandWasCalled, is(false)
}
Expand Down Expand Up @@ -1591,21 +1591,21 @@ class ThingManagerOSGiTest extends OSGiTest {
itemChannelLinkRegistry.add(new ItemChannelLink("testItem", new ChannelUID(THING.getUID(), "channel")))
waitForAssert { assertThat itemRegistry.get("testItem"), is(notNullValue()) }

eventPublisher.post(ItemEventFactory.createCommandEvent("testItem", new StringType("TEST")))
eventPublisher.post(ItemEventFactory.createCommandEvent("testItem", OnOffType.ON))

assertThat handleCommandCalled, is(false)

def statusInfo = ThingStatusInfoBuilder.create(ThingStatus.UNKNOWN, ThingStatusDetail.NONE).build()
callback.statusUpdated(THING, statusInfo)
assertThat THING.statusInfo, is(statusInfo)

eventPublisher.post(ItemEventFactory.createCommandEvent("testItem", new StringType("TEST")))
eventPublisher.post(ItemEventFactory.createCommandEvent("testItem", OnOffType.ON))

waitForAssert {
assertThat handleCommandCalled, is(true)
}
assertThat calledChannelUID, is(equalTo(new ChannelUID(THING.getUID(), "channel")))
assertThat calledCommand, is(equalTo(new StringType("TEST")))
assertThat calledCommand, is(equalTo(OnOffType.ON))
}

private void registerThingTypeProvider() {
Expand Down
Expand Up @@ -12,8 +12,8 @@
*/
package org.eclipse.smarthome.core.thing.internal;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;

Expand All @@ -25,8 +25,11 @@
import org.eclipse.smarthome.core.events.EventPublisher;
import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.core.items.events.ItemEventFactory;
import org.eclipse.smarthome.core.library.CoreItemFactory;
import org.eclipse.smarthome.core.library.items.SwitchItem;
import org.eclipse.smarthome.core.library.types.HSBType;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
Expand All @@ -48,9 +51,12 @@
import org.eclipse.smarthome.core.thing.profiles.StateProfile;
import org.eclipse.smarthome.core.thing.profiles.TriggerProfile;
import org.eclipse.smarthome.core.thing.type.ChannelKind;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.test.java.JavaOSGiTest;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;

/**
Expand Down Expand Up @@ -166,6 +172,8 @@ public Stream<ItemChannelLink> stream() {

when(thingRegistry.get(eq(THING_UID))).thenReturn(THING);
manager.setThingRegistry(thingRegistry);

manager.addItemFactory(new CoreItemFactory());
}

@Test
Expand Down Expand Up @@ -408,4 +416,44 @@ public void testProfileIsNotReusedOnLinkUpdate() {
verifyNoMoreInteractions(mockProfileAdvisor);
}

@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);
}

@Test
public void testItemStateEvent_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.createStateEvent(ITEM_NAME_2, HSBType.fromRGB(128, 128, 128)));
waitForAssert(() -> {
ArgumentCaptor<State> stateCaptor = ArgumentCaptor.forClass(State.class);
verify(stateProfile).onStateUpdateFromItem(stateCaptor.capture());
State state = stateCaptor.getValue();
assertNotNull(state);
assertEquals(PercentType.class, state.getClass());
});
verifyNoMoreInteractions(stateProfile);
verifyNoMoreInteractions(triggerProfile);
}

}

0 comments on commit 0df63ab

Please sign in to comment.