Search in sources :

Example 6 with ItemChannelLink

use of org.eclipse.smarthome.core.thing.link.ItemChannelLink in project smarthome by eclipse.

the class ChannelItemProviderTest method testDisableBeforeDelayedInitialization.

@Test
public void testDisableBeforeDelayedInitialization() throws Exception {
    provider = createProvider();
    reset(linkRegistry);
    // Set the initialization delay to 40ms so we don't have to wait 2000ms to do the assertion
    Field field = ChannelItemProvider.class.getDeclaredField("INITIALIZATION_DELAY_NANOS");
    field.setAccessible(true);
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(provider, TimeUnit.MILLISECONDS.toNanos(40));
    Map<String, Object> props = new HashMap<>();
    props.put("enabled", "true");
    provider.activate(props);
    provider.linkRegistryListener.added(new ItemChannelLink(ITEM_NAME, CHANNEL_UID));
    verify(listener, never()).added(same(provider), same(ITEM));
    verify(linkRegistry, never()).getAll();
    props = new HashMap<>();
    props.put("enabled", "false");
    provider.modified(props);
    Thread.sleep(100);
    provider.linkRegistryListener.added(new ItemChannelLink(ITEM_NAME, CHANNEL_UID));
    verify(listener, never()).added(same(provider), same(ITEM));
    verify(linkRegistry, never()).getAll();
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) Test(org.junit.Test)

Example 7 with ItemChannelLink

use of org.eclipse.smarthome.core.thing.link.ItemChannelLink in project smarthome by eclipse.

the class ChannelItemProviderTest method testItemRemoval_linkRemoved.

@Test
public void testItemRemoval_linkRemoved() throws Exception {
    provider.linkRegistryListener.added(new ItemChannelLink(ITEM_NAME, CHANNEL_UID));
    resetAndPrepareListener();
    provider.linkRegistryListener.removed(new ItemChannelLink(ITEM_NAME, CHANNEL_UID));
    verify(listener, never()).added(same(provider), same(ITEM));
    verify(listener, only()).removed(same(provider), same(ITEM));
}
Also used : ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) Test(org.junit.Test)

Example 8 with ItemChannelLink

use of org.eclipse.smarthome.core.thing.link.ItemChannelLink in project smarthome by eclipse.

the class ChannelStateDescriptionProviderOSGiTest method presentItemStateDescription.

/**
 * Assert that item's state description is present.
 */
@Test
public void presentItemStateDescription() throws ItemNotFoundException {
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
    Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration());
    assertNotNull(thing);
    managedThingProvider.add(thing);
    ItemChannelLink link = new ItemChannelLink("TestItem", getChannel(thing, "1").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem2", getChannel(thing, "2").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem3", getChannel(thing, "3").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem4", getChannel(thing, "4").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem5", getChannel(thing, "5").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem6", getChannel(thing, "6").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem7_1", getChannel(thing, "7_1").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID());
    linkRegistry.add(link);
    // 
    final Collection<Item> items = itemRegistry.getItems();
    assertEquals(false, items.isEmpty());
    Item item = itemRegistry.getItem("TestItem");
    assertEquals("Number", item.getType());
    StateDescription state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.ZERO, state.getMinimum());
    assertEquals(BigDecimal.valueOf(100), state.getMaximum());
    assertEquals(BigDecimal.TEN, state.getStep());
    assertEquals("%d Peek", state.getPattern());
    assertEquals(true, state.isReadOnly());
    List<StateOption> opts = state.getOptions();
    assertEquals(1, opts.size());
    final StateOption opt = opts.get(0);
    assertEquals("SOUND", opt.getValue());
    assertEquals("My great sound.", opt.getLabel());
    item = itemRegistry.getItem("TestItem2");
    assertEquals("Number", item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.ZERO, state.getMinimum());
    assertEquals(BigDecimal.valueOf(256), state.getMaximum());
    assertEquals(BigDecimal.valueOf(8), state.getStep());
    assertEquals("%.0f", state.getPattern());
    assertEquals(false, state.isReadOnly());
    opts = state.getOptions();
    assertEquals(0, opts.size());
    item = itemRegistry.getItem("TestItem3");
    assertEquals("String", item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertNull(state.getMinimum());
    assertNull(state.getMaximum());
    assertNull(state.getStep());
    assertEquals("%s", state.getPattern());
    assertEquals(false, state.isReadOnly());
    opts = state.getOptions();
    assertEquals(0, opts.size());
    item = itemRegistry.getItem("TestItem4");
    assertEquals("Color", item.getType());
    state = item.getStateDescription();
    assertNull(state);
    item = itemRegistry.getItem("TestItem5");
    assertEquals("Dimmer", item.getType());
    state = item.getStateDescription();
    assertNull(state);
    item = itemRegistry.getItem("TestItem6");
    assertEquals("Switch", item.getType());
    state = item.getStateDescription();
    assertNull(state);
    item = itemRegistry.getItem("TestItem7_1");
    assertEquals("Number", item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.valueOf(10), state.getMinimum());
    assertEquals(BigDecimal.valueOf(100), state.getMaximum());
    assertEquals(BigDecimal.valueOf(5), state.getStep());
    assertEquals("VALUE %d", state.getPattern());
    assertEquals(false, state.isReadOnly());
    opts = state.getOptions();
    assertNotNull(opts);
    assertEquals(2, opts.size());
    final StateOption opt0 = opts.get(0);
    assertNotNull(opt0);
    assertEquals(opt0.getValue(), "value0");
    assertEquals(opt0.getLabel(), "label0");
    final StateOption opt1 = opts.get(1);
    assertNotNull(opt1);
    assertEquals(opt1.getValue(), "value1");
    assertEquals(opt1.getLabel(), "label1");
    item = itemRegistry.getItem("TestItem7_2");
    assertEquals("Number", item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.valueOf(1), state.getMinimum());
    assertEquals(BigDecimal.valueOf(101), state.getMaximum());
    assertEquals(BigDecimal.valueOf(20), state.getStep());
    assertEquals("NEW %d Peek", state.getPattern());
    assertEquals(true, state.isReadOnly());
    opts = state.getOptions();
    assertNotNull(opts);
    assertEquals(1, opts.size());
    final StateOption opt2 = opts.get(0);
    assertEquals("SOUND", opt2.getValue());
    assertEquals("My great sound.", opt2.getLabel());
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) Configuration(org.eclipse.smarthome.config.core.Configuration) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing) StateOption(org.eclipse.smarthome.core.types.StateOption) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) StateDescription(org.eclipse.smarthome.core.types.StateDescription) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 9 with ItemChannelLink

use of org.eclipse.smarthome.core.thing.link.ItemChannelLink in project smarthome by eclipse.

the class CommunicationManagerTest method setup.

@Before
public void setup() {
    initMocks(this);
    safeCaller = getService(SafeCaller.class);
    assertNotNull(safeCaller);
    manager = new CommunicationManager();
    manager.setEventPublisher(eventPublisher);
    manager.setDefaultProfileFactory(new SystemProfileFactory());
    manager.setSafeCaller(safeCaller);
    doAnswer(invocation -> {
        switch(((Channel) invocation.getArguments()[0]).getKind()) {
            case STATE:
                return new ProfileTypeUID("test:state");
            case TRIGGER:
                return new ProfileTypeUID("test:trigger");
        }
        return null;
    }).when(mockProfileAdvisor).getSuggestedProfileTypeUID(isA(Channel.class), isA(String.class));
    doAnswer(invocation -> {
        switch(((ProfileTypeUID) invocation.getArguments()[0]).toString()) {
            case "test:state":
                return stateProfile;
            case "test:trigger":
                return triggerProfile;
        }
        return null;
    }).when(mockProfileFactory).createProfile(isA(ProfileTypeUID.class), isA(ProfileCallback.class), isA(ProfileContext.class));
    when(mockProfileFactory.getSupportedProfileTypeUIDs()).thenReturn(Stream.of(new ProfileTypeUID("test:state"), new ProfileTypeUID("test:trigger")).collect(Collectors.toList()));
    manager.addProfileFactory(mockProfileFactory);
    manager.addProfileAdvisor(mockProfileAdvisor);
    ItemChannelLinkRegistry iclRegistry = new ItemChannelLinkRegistry() {

        @Override
        public Stream<ItemChannelLink> stream() {
            return Arrays.asList(LINK_1_S1, LINK_1_S2, LINK_2_S2, LINK_1_T1, LINK_1_T2, LINK_2_T2).stream();
        }
    };
    manager.setItemChannelLinkRegistry(iclRegistry);
    when(itemRegistry.get(eq(ITEM_NAME_1))).thenReturn(ITEM_1);
    when(itemRegistry.get(eq(ITEM_NAME_2))).thenReturn(ITEM_2);
    manager.setItemRegistry(itemRegistry);
    THING.setHandler(mockHandler);
    when(thingRegistry.get(eq(THING_UID))).thenReturn(THING);
    manager.setThingRegistry(thingRegistry);
    manager.addItemFactory(new CoreItemFactory());
}
Also used : CoreItemFactory(org.eclipse.smarthome.core.library.CoreItemFactory) SafeCaller(org.eclipse.smarthome.core.common.SafeCaller) SystemProfileFactory(org.eclipse.smarthome.core.thing.internal.profiles.SystemProfileFactory) ProfileCallback(org.eclipse.smarthome.core.thing.profiles.ProfileCallback) ProfileContext(org.eclipse.smarthome.core.thing.profiles.ProfileContext) Channel(org.eclipse.smarthome.core.thing.Channel) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) ProfileTypeUID(org.eclipse.smarthome.core.thing.profiles.ProfileTypeUID) Before(org.junit.Before)

Example 10 with ItemChannelLink

use of org.eclipse.smarthome.core.thing.link.ItemChannelLink in project smarthome by eclipse.

the class GenericItemChannelLinkProviderJavaTest method testNoAmnesia.

@SuppressWarnings("unchecked")
@Test
public void testNoAmnesia() throws Exception {
    GenericItemChannelLinkProvider provider = new GenericItemChannelLinkProvider();
    provider.addProviderChangeListener(listener);
    provider.startConfigurationUpdate(ITEMS_TESTMODEL_NAME);
    provider.processBindingConfiguration(ITEMS_TESTMODEL_NAME, "Number", ITEM, CHANNEL, new Configuration());
    provider.stopConfigurationUpdate(ITEMS_TESTMODEL_NAME);
    assertThat(provider.getAll().size(), is(1));
    assertThat(provider.getAll().iterator().next().toString(), is(LINK));
    verify(listener, only()).added(same(provider), eq(new ItemChannelLink(ITEM, new ChannelUID(CHANNEL))));
    reset(listener);
    provider.startConfigurationUpdate(ITEMS_TESTMODEL_NAME);
    provider.processBindingConfiguration(ITEMS_TESTMODEL_NAME, "Number", ITEM, CHANNEL, new Configuration());
    provider.stopConfigurationUpdate(ITEMS_TESTMODEL_NAME);
    assertThat(provider.getAll().size(), is(1));
    assertThat(provider.getAll().iterator().next().toString(), is(LINK));
    verify(listener, only()).updated(same(provider), eq(new ItemChannelLink(ITEM, new ChannelUID(CHANNEL))), eq(new ItemChannelLink(ITEM, new ChannelUID(CHANNEL))));
    reset(listener);
    provider.startConfigurationUpdate(ITEMS_TESTMODEL_NAME);
    provider.stopConfigurationUpdate(ITEMS_TESTMODEL_NAME);
    assertThat(provider.getAll().size(), is(0));
    verify(listener, only()).removed(same(provider), eq(new ItemChannelLink(ITEM, new ChannelUID(CHANNEL))));
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) GenericItemChannelLinkProvider(org.eclipse.smarthome.model.thing.internal.GenericItemChannelLinkProvider) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

ItemChannelLink (org.eclipse.smarthome.core.thing.link.ItemChannelLink)17 Test (org.junit.Test)8 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)4 Channel (org.eclipse.smarthome.core.thing.Channel)3 Thing (org.eclipse.smarthome.core.thing.Thing)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Configuration (org.eclipse.smarthome.config.core.Configuration)2 Item (org.eclipse.smarthome.core.items.Item)2 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)2 StringItem (org.eclipse.smarthome.core.library.items.StringItem)2 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Field (java.lang.reflect.Field)1 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 DELETE (javax.ws.rs.DELETE)1 Path (javax.ws.rs.Path)1