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());
}
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());
}
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;
}
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));
}
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);
}
Aggregations