Search in sources :

Example 6 with SwitchItem

use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.

the class GroupItemTest method assertCyclicGroupItemsCalculateState.

@Test
public void assertCyclicGroupItemsCalculateState() {
    GroupFunction countFn = new ArithmeticGroupFunction.Count(new StringType(".*"));
    GroupItem rootGroup = new GroupItem("rootGroup", new SwitchItem("baseItem"), countFn);
    TestItem rootMember = new TestItem("rootMember");
    rootGroup.addMember(rootMember);
    GroupItem group1 = new GroupItem("group1");
    GroupItem group2 = new GroupItem("group2");
    rootGroup.addMember(group1);
    group1.addMember(group2);
    group2.addMember(group1);
    group1.addMember(new TestItem("sub1"));
    group2.addMember(new TestItem("sub2-1"));
    group2.addMember(new TestItem("sub2-2"));
    group2.addMember(new TestItem("sub2-3"));
    // count: rootMember, sub1, sub2-1, sub2-2, sub2-3
    assertThat(rootGroup.getStateAs(DecimalType.class), is(new DecimalType(5)));
}
Also used : ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) StringType(org.eclipse.smarthome.core.library.types.StringType) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 7 with SwitchItem

use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.

the class GroupItemTest method assertThatGroupItemChangesRespectGroupFunctionORwithUNDEF.

@Test
public void assertThatGroupItemChangesRespectGroupFunctionORwithUNDEF() throws InterruptedException {
    events.clear();
    GroupItem groupItem = new GroupItem("root", new SwitchItem("mySwitch"), new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));
    groupItem.setItemStateConverter(itemStateConverter);
    SwitchItem sw1 = new SwitchItem("switch1");
    SwitchItem sw2 = new SwitchItem("switch2");
    groupItem.addMember(sw1);
    groupItem.addMember(sw2);
    groupItem.setEventPublisher(publisher);
    // State changes -> one change event is fired
    sw1.setState(OnOffType.ON);
    waitForAssert(() -> assertThat(events.size(), is(1)));
    List<Event> changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent).collect(Collectors.toList());
    assertThat(changes.size(), is(1));
    GroupItemStateChangedEvent change = (GroupItemStateChangedEvent) changes.get(0);
    assertTrue(change.getItemName().equals(groupItem.getName()));
    assertTrue(change.getOldItemState().equals(UnDefType.NULL));
    assertTrue(change.getItemState().equals(OnOffType.ON));
    events.clear();
    sw2.setState(OnOffType.ON);
    sw2.setState(UnDefType.UNDEF);
    // wait to see that the event doesn't fire
    Thread.sleep(WAIT_EVENT_TO_BE_HANDLED);
    assertThat(events.size(), is(0));
    assertTrue(groupItem.getState().equals(OnOffType.ON));
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) CoreMatchers(org.hamcrest.CoreMatchers) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Mock(org.mockito.Mock) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) UnDefType(org.eclipse.smarthome.core.types.UnDefType) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) GroupFunctionDTO(org.eclipse.smarthome.core.items.dto.GroupFunctionDTO) GroupFunctionHelper(org.eclipse.smarthome.core.internal.items.GroupFunctionHelper) HashSet(java.util.HashSet) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) Temperature(javax.measure.quantity.Temperature) Quantity(javax.measure.Quantity) Pressure(javax.measure.quantity.Pressure) ItemStateConverterImpl(org.eclipse.smarthome.core.internal.items.ItemStateConverterImpl) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) CoreItemFactory(org.eclipse.smarthome.core.library.CoreItemFactory) StringType(org.eclipse.smarthome.core.library.types.StringType) LinkedList(java.util.LinkedList) State(org.eclipse.smarthome.core.types.State) Dimensionless(javax.measure.quantity.Dimensionless) Before(org.junit.Before) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) RefreshType(org.eclipse.smarthome.core.types.RefreshType) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Collectors(java.util.stream.Collectors) RawType(org.eclipse.smarthome.core.library.types.RawType) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) List(java.util.List) HSBType(org.eclipse.smarthome.core.library.types.HSBType) Ignore(org.junit.Ignore) PercentType(org.eclipse.smarthome.core.library.types.PercentType) Units(tec.uom.se.unit.Units) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) UnitProvider(org.eclipse.smarthome.core.i18n.UnitProvider) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Event(org.eclipse.smarthome.core.events.Event) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 8 with SwitchItem

use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.

the class GroupItemTest method assertThatGroupItemChangesRespectGroupFunctionOR.

@Test
public void assertThatGroupItemChangesRespectGroupFunctionOR() {
    events.clear();
    GroupItem groupItem = new GroupItem("root", new SwitchItem("mySwitch"), new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));
    groupItem.setItemStateConverter(itemStateConverter);
    SwitchItem sw1 = new SwitchItem("switch1");
    SwitchItem sw2 = new SwitchItem("switch2");
    groupItem.addMember(sw1);
    groupItem.addMember(sw2);
    groupItem.setEventPublisher(publisher);
    // State changes -> one change event is fired
    sw1.setState(OnOffType.ON);
    waitForAssert(() -> assertThat(events.size(), is(1)));
    List<Event> changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent).collect(Collectors.toList());
    assertThat(changes.size(), is(1));
    GroupItemStateChangedEvent change = (GroupItemStateChangedEvent) changes.get(0);
    assertTrue(change.getItemName().equals(groupItem.getName()));
    assertTrue(change.getOldItemState().equals(UnDefType.NULL));
    assertTrue(change.getItemState().equals(OnOffType.ON));
    assertTrue(groupItem.getState().equals(OnOffType.ON));
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) CoreMatchers(org.hamcrest.CoreMatchers) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Mock(org.mockito.Mock) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) UnDefType(org.eclipse.smarthome.core.types.UnDefType) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) GroupFunctionDTO(org.eclipse.smarthome.core.items.dto.GroupFunctionDTO) GroupFunctionHelper(org.eclipse.smarthome.core.internal.items.GroupFunctionHelper) HashSet(java.util.HashSet) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) Temperature(javax.measure.quantity.Temperature) Quantity(javax.measure.Quantity) Pressure(javax.measure.quantity.Pressure) ItemStateConverterImpl(org.eclipse.smarthome.core.internal.items.ItemStateConverterImpl) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) CoreItemFactory(org.eclipse.smarthome.core.library.CoreItemFactory) StringType(org.eclipse.smarthome.core.library.types.StringType) LinkedList(java.util.LinkedList) State(org.eclipse.smarthome.core.types.State) Dimensionless(javax.measure.quantity.Dimensionless) Before(org.junit.Before) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) RefreshType(org.eclipse.smarthome.core.types.RefreshType) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Collectors(java.util.stream.Collectors) RawType(org.eclipse.smarthome.core.library.types.RawType) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) List(java.util.List) HSBType(org.eclipse.smarthome.core.library.types.HSBType) Ignore(org.junit.Ignore) PercentType(org.eclipse.smarthome.core.library.types.PercentType) Units(tec.uom.se.unit.Units) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) UnitProvider(org.eclipse.smarthome.core.i18n.UnitProvider) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Event(org.eclipse.smarthome.core.events.Event) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 9 with SwitchItem

use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.

the class GroupItemTest method assertThatGroupItemChangesRespectGroupFunctionAND.

@Test
public void assertThatGroupItemChangesRespectGroupFunctionAND() {
    events.clear();
    GroupItem groupItem = new GroupItem("root", new SwitchItem("mySwitch"), new ArithmeticGroupFunction.And(OnOffType.ON, OnOffType.OFF));
    groupItem.setItemStateConverter(itemStateConverter);
    SwitchItem sw1 = new SwitchItem("switch1");
    SwitchItem sw2 = new SwitchItem("switch2");
    groupItem.addMember(sw1);
    groupItem.addMember(sw2);
    groupItem.setEventPublisher(publisher);
    // State changes -> one change event is fired
    sw1.setState(OnOffType.ON);
    waitForAssert(() -> assertThat(events.size(), is(1)));
    List<Event> changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent).collect(Collectors.toList());
    assertThat(changes.size(), is(1));
    GroupItemStateChangedEvent change = (GroupItemStateChangedEvent) changes.get(0);
    assertTrue(change.getItemName().equals(groupItem.getName()));
    // we expect that the group should now have status "OFF"
    assertTrue(change.getOldItemState().equals(UnDefType.NULL));
    assertTrue(change.getItemState().equals(OnOffType.OFF));
    events.clear();
    // State changes -> one change event is fired
    sw2.setState(OnOffType.ON);
    waitForAssert(() -> assertThat(events.size(), is(1)));
    changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent).collect(Collectors.toList());
    assertThat(changes.size(), is(1));
    change = (GroupItemStateChangedEvent) changes.get(0);
    assertTrue(change.getItemName().equals(groupItem.getName()));
    // we expect that the group should now have status "ON"
    assertTrue(change.getOldItemState().equals(OnOffType.OFF));
    assertTrue(change.getItemState().equals(OnOffType.ON));
    assertTrue(groupItem.getState().equals(OnOffType.ON));
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) CoreMatchers(org.hamcrest.CoreMatchers) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Mock(org.mockito.Mock) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) UnDefType(org.eclipse.smarthome.core.types.UnDefType) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) GroupFunctionDTO(org.eclipse.smarthome.core.items.dto.GroupFunctionDTO) GroupFunctionHelper(org.eclipse.smarthome.core.internal.items.GroupFunctionHelper) HashSet(java.util.HashSet) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) Temperature(javax.measure.quantity.Temperature) Quantity(javax.measure.Quantity) Pressure(javax.measure.quantity.Pressure) ItemStateConverterImpl(org.eclipse.smarthome.core.internal.items.ItemStateConverterImpl) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) CoreItemFactory(org.eclipse.smarthome.core.library.CoreItemFactory) StringType(org.eclipse.smarthome.core.library.types.StringType) LinkedList(java.util.LinkedList) State(org.eclipse.smarthome.core.types.State) Dimensionless(javax.measure.quantity.Dimensionless) Before(org.junit.Before) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) RefreshType(org.eclipse.smarthome.core.types.RefreshType) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Collectors(java.util.stream.Collectors) RawType(org.eclipse.smarthome.core.library.types.RawType) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) List(java.util.List) HSBType(org.eclipse.smarthome.core.library.types.HSBType) Ignore(org.junit.Ignore) PercentType(org.eclipse.smarthome.core.library.types.PercentType) Units(tec.uom.se.unit.Units) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) UnitProvider(org.eclipse.smarthome.core.i18n.UnitProvider) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Event(org.eclipse.smarthome.core.events.Event) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 10 with SwitchItem

use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.

the class ScriptEngineOSGiTest method setup.

@Before
public void setup() {
    registerVolatileStorageService();
    EventPublisher eventPublisher = event -> {
    };
    registerService(eventPublisher);
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    itemProvider = new ItemProvider() {

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

        @Override
        public Collection<Item> getAll() {
            return Lists.newArrayList(new SwitchItem(ITEM_NAME), createNumberItem(NUMBER_ITEM_TEMPERATURE, Temperature.class), createNumberItem(NUMBER_ITEM_LENGTH, Length.class), new NumberItem(NUMBER_ITEM_DECIMAL));
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<Item> listener) {
        }
    };
    registerService(itemProvider);
    ScriptServiceUtil scriptServiceUtil = getService(ScriptServiceUtil.class);
    assertNotNull(scriptServiceUtil);
    scriptEngine = ScriptServiceUtil.getScriptEngine();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Collection(java.util.Collection) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) Test(org.junit.Test) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Item(org.eclipse.smarthome.core.items.Item) ScriptServiceUtil(org.eclipse.smarthome.model.script.ScriptServiceUtil) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) Lists(com.google.common.collect.Lists) Temperature(javax.measure.quantity.Temperature) Length(javax.measure.quantity.Length) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) After(org.junit.After) Assert(org.junit.Assert) State(org.eclipse.smarthome.core.types.State) NonNull(org.eclipse.jdt.annotation.NonNull) Before(org.junit.Before) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ScriptServiceUtil(org.eclipse.smarthome.model.script.ScriptServiceUtil) Collection(java.util.Collection) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Before(org.junit.Before)

Aggregations

SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)24 Test (org.junit.Test)17 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)16 ArithmeticGroupFunction (org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction)11 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)9 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)9 Before (org.junit.Before)9 GroupItem (org.eclipse.smarthome.core.items.GroupItem)7 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)7 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)7 State (org.eclipse.smarthome.core.types.State)7 Item (org.eclipse.smarthome.core.items.Item)6 StringType (org.eclipse.smarthome.core.library.types.StringType)6 Temperature (javax.measure.quantity.Temperature)5 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)5 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)5 Collection (java.util.Collection)4 Collections (java.util.Collections)4 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4