Search in sources :

Example 1 with GroupFunction

use of org.openhab.core.items.GroupFunction in project openhab-core by openhab.

the class ItemBuilderTest method testCloneGroupItem.

@Test
public void testCloneGroupItem() {
    Item baseItem = mock(Item.class);
    GroupFunction mockFunction = mock(GroupFunction.class);
    GroupItem originalItem = new GroupItem("name", baseItem, mockFunction);
    originalItem.setCategory("category");
    originalItem.setLabel("label");
    originalItem.addGroupNames("a", "b");
    Item resItem = itemBuilderFactory.newItemBuilder(originalItem).build();
    assertEquals(GroupItem.class, resItem.getClass());
    GroupItem res = (GroupItem) resItem;
    verifyNoMoreInteractions(factoryMock);
    assertEquals("category", res.getCategory());
    assertEquals(List.of("a", "b"), res.getGroupNames());
    assertEquals("label", res.getLabel());
    assertSame(mockFunction, res.getFunction());
    assertSame(baseItem, res.getBaseItem());
}
Also used : GroupFunction(org.openhab.core.items.GroupFunction) ActiveItem(org.openhab.core.items.ActiveItem) GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) GroupItem(org.openhab.core.items.GroupItem) Test(org.junit.jupiter.api.Test)

Example 2 with GroupFunction

use of org.openhab.core.items.GroupFunction in project openhab-core by openhab.

the class ItemBuilderTest method testFullGroupItem.

@Test
public void testFullGroupItem() {
    Item baseItem = mock(Item.class);
    GroupFunction mockFunction = mock(GroupFunction.class);
    Item resItem = // 
    itemBuilderFactory.newItemBuilder("Group", "test").withCategory(// 
    "category").withGroups(// 
    List.of("a", "b")).withLabel(// 
    "label").withBaseItem(// 
    baseItem).withGroupFunction(// 
    mockFunction).build();
    assertEquals(GroupItem.class, resItem.getClass());
    GroupItem res = (GroupItem) resItem;
    verifyNoMoreInteractions(factoryMock);
    assertEquals("category", res.getCategory());
    assertEquals(List.of("a", "b"), res.getGroupNames());
    assertEquals("label", res.getLabel());
    assertSame(mockFunction, res.getFunction());
    assertSame(baseItem, res.getBaseItem());
}
Also used : GroupFunction(org.openhab.core.items.GroupFunction) ActiveItem(org.openhab.core.items.ActiveItem) GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) GroupItem(org.openhab.core.items.GroupItem) Test(org.junit.jupiter.api.Test)

Example 3 with GroupFunction

use of org.openhab.core.items.GroupFunction in project openhab-core by openhab.

the class ItemDTOMapper method map.

/**
 * Maps item DTO into item object.
 *
 * @param itemDTO the DTO
 * @param itemBuilderFactory the item registry
 * @return the item object
 */
@Nullable
public static Item map(ItemDTO itemDTO, ItemBuilderFactory itemBuilderFactory) {
    if (itemDTO == null) {
        throw new IllegalArgumentException("The argument 'itemDTO' must no be null.");
    }
    if (itemBuilderFactory == null) {
        throw new IllegalArgumentException("The argument 'itemBuilderFactory' must no be null.");
    }
    if (!ItemUtil.isValidItemName(itemDTO.name)) {
        throw new IllegalArgumentException("The item name '" + itemDTO.name + "' is invalid.");
    }
    if (itemDTO.type != null) {
        ItemBuilder builder = itemBuilderFactory.newItemBuilder(itemDTO.type, itemDTO.name);
        if (itemDTO instanceof GroupItemDTO && GroupItem.TYPE.equals(itemDTO.type)) {
            GroupItemDTO groupItemDTO = (GroupItemDTO) itemDTO;
            Item baseItem = null;
            if (groupItemDTO.groupType != null && !groupItemDTO.groupType.isEmpty()) {
                baseItem = itemBuilderFactory.newItemBuilder(groupItemDTO.groupType, itemDTO.name).build();
                builder.withBaseItem(baseItem);
            }
            GroupFunction function = new GroupFunction.Equality();
            if (groupItemDTO.function != null) {
                function = mapFunction(baseItem, groupItemDTO.function);
            }
            builder.withGroupFunction(function);
        }
        builder.withLabel(itemDTO.label);
        builder.withCategory(itemDTO.category);
        builder.withGroups(itemDTO.groupNames);
        builder.withTags(itemDTO.tags);
        try {
            return builder.build();
        } catch (IllegalStateException e) {
            return null;
        }
    }
    return null;
}
Also used : GroupFunction(org.openhab.core.items.GroupFunction) GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) ItemBuilder(org.openhab.core.items.ItemBuilder) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 4 with GroupFunction

use of org.openhab.core.items.GroupFunction in project openhab-core by openhab.

the class ItemDTOMapperTest method testMapFunctionWithNumberItemAndCountFunction.

@Test
public void testMapFunctionWithNumberItemAndCountFunction() {
    // testing Group:Number:Count(".*hello.*")
    NumberItem item1 = new NumberItem("item1");
    GroupFunctionDTO gFuncDTO = new GroupFunctionDTO();
    gFuncDTO.name = "COUNT";
    gFuncDTO.params = new String[] { ".*hello.*" };
    GroupFunction gFunc = ItemDTOMapper.mapFunction(item1, gFuncDTO);
    assertThat(gFunc, instanceOf(ArithmeticGroupFunction.Count.class));
    assertThat(gFunc.getParameters().length, is(1));
    assertThat(gFunc.getParameters()[0], instanceOf(StringType.class));
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) ArithmeticGroupFunction(org.openhab.core.library.types.ArithmeticGroupFunction) GroupFunction(org.openhab.core.items.GroupFunction) StringType(org.openhab.core.library.types.StringType) Test(org.junit.jupiter.api.Test)

Example 5 with GroupFunction

use of org.openhab.core.items.GroupFunction in project openhab-core by openhab.

the class ArithmeticGroupFunctionTest method testOrFunction.

@Test
public void testOrFunction() {
    Set<Item> items = new HashSet<>();
    items.add(new TestItem("TestItem1", OpenClosedType.CLOSED));
    items.add(new TestItem("TestItem2", UnDefType.UNDEF));
    items.add(new TestItem("TestItem3", OpenClosedType.OPEN));
    items.add(new TestItem("TestItem4", OpenClosedType.CLOSED));
    items.add(new TestItem("TestItem5", UnDefType.UNDEF));
    GroupFunction function = new ArithmeticGroupFunction.Or(OpenClosedType.OPEN, OpenClosedType.CLOSED);
    State state = function.calculate(items);
    assertEquals(OpenClosedType.OPEN, state);
}
Also used : GroupFunction(org.openhab.core.items.GroupFunction) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) Item(org.openhab.core.items.Item) GenericItem(org.openhab.core.items.GenericItem) State(org.openhab.core.types.State) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

GroupFunction (org.openhab.core.items.GroupFunction)39 Item (org.openhab.core.items.Item)35 State (org.openhab.core.types.State)32 Test (org.junit.jupiter.api.Test)22 GroupItem (org.openhab.core.items.GroupItem)19 HashSet (java.util.HashSet)18 GenericItem (org.openhab.core.items.GenericItem)18 DimmerItem (org.openhab.core.library.items.DimmerItem)16 SwitchItem (org.openhab.core.library.items.SwitchItem)16 NumberItem (org.openhab.core.library.items.NumberItem)15 LinkedHashSet (java.util.LinkedHashSet)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 MethodSource (org.junit.jupiter.params.provider.MethodSource)14 Temperature (javax.measure.quantity.Temperature)12 Pressure (javax.measure.quantity.Pressure)4 ZonedDateTime (java.time.ZonedDateTime)2 ActiveItem (org.openhab.core.items.ActiveItem)2 ModelGroupFunction (org.openhab.core.model.items.ModelGroupFunction)2 ModelGroupItem (org.openhab.core.model.items.ModelGroupItem)2 Dimensionless (javax.measure.quantity.Dimensionless)1