2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.knx.internal.channel;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
19 import java.util.List;
21 import java.util.Objects;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.core.config.core.Configuration;
27 import org.openhab.core.library.items.ColorItem;
28 import org.openhab.core.library.types.HSBType;
29 import org.openhab.core.thing.Channel;
30 import org.openhab.core.thing.type.ChannelTypeUID;
31 import org.openhab.core.types.UnDefType;
33 import tuwien.auto.calimero.GroupAddress;
34 import tuwien.auto.calimero.KNXFormatException;
38 * @author Simon Kaufmann - Initial contribution
42 class KNXChannelTest {
45 public void invalidFails() {
46 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<1/3/22+0/3/22+<0/8/15");
51 void testParseWithDptMultipleWithRead() throws KNXFormatException {
52 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<1/3/22+0/3/22+<0/7/15");
59 assertEquals("5.001", res.getDPT());
60 assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
61 assertTrue(res.getReadGAs().contains(res.getMainGA()));
62 assertEquals(3, res.getListenGAs().size());
63 assertEquals(2, res.getReadGAs().size());
67 void testParseWithDptMultipleWithoutRead() throws KNXFormatException {
68 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:1/3/22+0/3/22+0/7/15");
75 assertEquals("5.001", res.getDPT());
76 assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
77 assertFalse(res.getReadGAs().contains(res.getMainGA()));
78 assertEquals(3, res.getListenGAs().size());
79 assertEquals(0, res.getReadGAs().size());
83 void testParseWithoutDptSingleWithoutRead() throws KNXFormatException {
84 GroupAddressConfiguration res = GroupAddressConfiguration.parse("1/3/22");
91 assertNull(res.getDPT());
92 assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
93 assertFalse(res.getReadGAs().contains(res.getMainGA()));
94 assertEquals(1, res.getListenGAs().size());
95 assertEquals(0, res.getReadGAs().size());
99 void testParseWithoutDptSingleWithRead() throws KNXFormatException {
100 GroupAddressConfiguration res = GroupAddressConfiguration.parse("<1/3/22");
107 assertNull(res.getDPT());
108 assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
109 assertTrue(res.getReadGAs().contains(res.getMainGA()));
110 assertEquals(1, res.getListenGAs().size());
111 assertEquals(1, res.getReadGAs().size());
115 void testParseTwoLevel() throws KNXFormatException {
116 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<3/1024+<4/1025");
123 assertEquals(new GroupAddress("3/1024"), res.getMainGA());
124 assertTrue(res.getReadGAs().contains(res.getMainGA()));
125 assertEquals(2, res.getListenGAs().size());
126 assertEquals(2, res.getReadGAs().size());
130 void testParseFreeLevel() throws KNXFormatException {
131 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<4610+<4611");
138 assertEquals(new GroupAddress("4610"), res.getMainGA());
139 assertEquals(2, res.getListenGAs().size());
140 assertEquals(2, res.getReadGAs().size());
144 public void testChannelGaParsing() throws KNXFormatException {
145 Channel channel = Objects.requireNonNull(mock(Channel.class));
146 Configuration configuration = new Configuration(
147 Map.of("key1", "5.001:<1/2/3+4/5/6+1/5/6", "key2", "1.001:7/1/9+1/1/2"));
148 when(channel.getChannelTypeUID()).thenReturn(new ChannelTypeUID("a:b:c"));
149 when(channel.getConfiguration()).thenReturn(configuration);
150 when(channel.getAcceptedItemType()).thenReturn("none");
152 MyKNXChannel knxChannel = new MyKNXChannel(channel);
154 Set<GroupAddress> listenAddresses = knxChannel.getAllGroupAddresses();
155 assertEquals(5, listenAddresses.size());
156 // we don't check the content since parsing has been checked before and the quantity is correct
157 Set<GroupAddress> writeAddresses = knxChannel.getWriteAddresses();
158 assertEquals(2, writeAddresses.size());
159 assertTrue(writeAddresses.contains(new GroupAddress("1/2/3")));
160 assertTrue(writeAddresses.contains(new GroupAddress("7/1/9")));
164 void testSubTypeMapping() throws KNXFormatException {
165 Channel channel = Objects.requireNonNull(mock(Channel.class));
166 Configuration configuration = new Configuration(Map.of("key1", "1.001:1/2/3"));
167 when(channel.getChannelTypeUID()).thenReturn(new ChannelTypeUID("a:b:c"));
168 when(channel.getConfiguration()).thenReturn(configuration);
169 when(channel.getAcceptedItemType()).thenReturn(ColorItem.class.getName());
170 MyKNXChannel knxChannel = new MyKNXChannel(channel);
171 assertNotNull(knxChannel.getCommandSpec(new HSBType("0,100,100")));
172 assertEquals(knxChannel.getCommandSpec(new HSBType("0,100,100")).getDPT(), "1.001");
175 private static class MyKNXChannel extends KNXChannel {
176 public MyKNXChannel(Channel channel) {
177 super(Set.of("key1", "key2"), List.of(UnDefType.class), channel);
181 protected String getDefaultDPT(String gaConfigKey) {