Skip to content

Commit

Permalink
Added unit test to cover openhab#1226
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
  • Loading branch information
cweitkamp committed Nov 19, 2019
1 parent fc0c61e commit 058b6f7
Showing 1 changed file with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@
import org.eclipse.smarthome.core.items.Item;
import org.eclipse.smarthome.core.items.ItemFactory;
import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.core.library.CoreItemFactory;
import org.eclipse.smarthome.core.library.items.NumberItem;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingRegistry;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder;
import org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder;
import org.eclipse.smarthome.core.thing.link.ItemChannelLink;
import org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry;
import org.eclipse.smarthome.core.thing.type.ChannelTypeRegistry;
Expand All @@ -46,9 +51,15 @@
*/
public class ChannelItemProviderTest {

private static final String ITEM_NAME = "test";
private static final ChannelUID CHANNEL_UID = new ChannelUID("test:test:test:test");
private static final Channel CHANNEL = ChannelBuilder.create(CHANNEL_UID, CoreItemFactory.NUMBER).build();

private static final ThingTypeUID THING_TYPE_UID = new ThingTypeUID("test:test");
private static final Thing THING = ThingBuilder.create(THING_TYPE_UID, "test").withChannel(CHANNEL).build();

private static final String ITEM_NAME = "test";
private static final NumberItem ITEM = new NumberItem(ITEM_NAME);
// private static final ItemChannelLink LINK = new ItemChannelLink(ITEM_NAME, CHANNEL_UID);

@Mock
private ItemRegistry itemRegistry;
Expand Down Expand Up @@ -76,28 +87,56 @@ public void setup() throws Exception {
props.put("initialDelay", "false");
provider.activate(props);

when(thingRegistry.getChannel(same(CHANNEL_UID)))
.thenReturn(ChannelBuilder.create(CHANNEL_UID, "Number").build());
when(itemFactory.createItem("Number", ITEM_NAME)).thenReturn(ITEM);
when(thingRegistry.getChannel(same(CHANNEL_UID))).thenReturn(CHANNEL);
when(itemFactory.createItem(CoreItemFactory.NUMBER, ITEM_NAME)).thenReturn(ITEM);
when(localeProvider.getLocale()).thenReturn(Locale.ENGLISH);
}

@Test
public void testItemCreationNotThere() throws Exception {
public void testItemCreationFromThingNotThere() {
resetAndPrepareListener();

provider.thingRegistryListener.added(THING);
verify(listener, only()).added(same(provider), same(ITEM));
}

@Test
public void testItemCreationFromThingAlreadyExists() {
when(itemRegistry.get(eq(ITEM_NAME))).thenReturn(ITEM);

resetAndPrepareListener();

provider.thingRegistryListener.added(THING);
verify(listener, never()).added(same(provider), same(ITEM));
}

@Test
public void testItemRemovalFromThingLinkRemoved() {
provider.linkRegistryListener.added(new ItemChannelLink(ITEM_NAME, CHANNEL_UID));

resetAndPrepareListener();

provider.thingRegistryListener.removed(THING);
verify(listener, never()).added(same(provider), same(ITEM));
verify(listener, only()).removed(same(provider), same(ITEM));
}

@Test
public void testItemCreationFromLinkNotThere() throws Exception {
provider.linkRegistryListener.added(new ItemChannelLink(ITEM_NAME, CHANNEL_UID));
verify(listener, only()).added(same(provider), same(ITEM));
}

@Test
public void testItemCreationAlreadyExists() throws Exception {
public void testItemCreationFromLinkAlreadyExists() throws Exception {
when(itemRegistry.get(eq(ITEM_NAME))).thenReturn(ITEM);

provider.linkRegistryListener.added(new ItemChannelLink(ITEM_NAME, CHANNEL_UID));
verify(listener, never()).added(same(provider), same(ITEM));
}

@Test
public void testItemRemovalLinkRemoved() throws Exception {
public void testItemRemovalFromLinkLinkRemoved() throws Exception {
provider.linkRegistryListener.added(new ItemChannelLink(ITEM_NAME, CHANNEL_UID));

resetAndPrepareListener();
Expand Down Expand Up @@ -177,7 +216,6 @@ private ChannelItemProvider createProvider() {
provider.setLocaleProvider(localeProvider);
provider.addProviderChangeListener(listener);
provider.setChannelTypeRegistry(mock(ChannelTypeRegistry.class));

return provider;
}

Expand Down

0 comments on commit 058b6f7

Please sign in to comment.