2 * Copyright (c) 2010-2023 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;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.thing.Channel;
27 import org.openhab.core.thing.type.ChannelTypeUID;
28 import org.openhab.core.types.UnDefType;
30 import tuwien.auto.calimero.GroupAddress;
31 import tuwien.auto.calimero.KNXFormatException;
35 * @author Simon Kaufmann - Initial contribution
39 class KNXChannelTest {
42 public void invalidFails() {
43 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<1/3/22+0/3/22+<0/8/15");
48 void testParseWithDptMultipleWithRead() throws KNXFormatException {
49 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<1/3/22+0/3/22+<0/7/15");
56 assertEquals("5.001", res.getDPT());
57 assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
58 assertTrue(res.getReadGAs().contains(res.getMainGA()));
59 assertEquals(3, res.getListenGAs().size());
60 assertEquals(2, res.getReadGAs().size());
64 void testParseWithDptMultipleWithoutRead() throws KNXFormatException {
65 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:1/3/22+0/3/22+0/7/15");
72 assertEquals("5.001", res.getDPT());
73 assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
74 assertFalse(res.getReadGAs().contains(res.getMainGA()));
75 assertEquals(3, res.getListenGAs().size());
76 assertEquals(0, res.getReadGAs().size());
80 void testParseWithoutDptSingleWithoutRead() throws KNXFormatException {
81 GroupAddressConfiguration res = GroupAddressConfiguration.parse("1/3/22");
88 assertNull(res.getDPT());
89 assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
90 assertFalse(res.getReadGAs().contains(res.getMainGA()));
91 assertEquals(1, res.getListenGAs().size());
92 assertEquals(0, res.getReadGAs().size());
96 void testParseWithoutDptSingleWithRead() throws KNXFormatException {
97 GroupAddressConfiguration res = GroupAddressConfiguration.parse("<1/3/22");
104 assertNull(res.getDPT());
105 assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
106 assertTrue(res.getReadGAs().contains(res.getMainGA()));
107 assertEquals(1, res.getListenGAs().size());
108 assertEquals(1, res.getReadGAs().size());
112 void testParseTwoLevel() throws KNXFormatException {
113 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<3/1024+<4/1025");
120 assertEquals(new GroupAddress("3/1024"), res.getMainGA());
121 assertTrue(res.getReadGAs().contains(res.getMainGA()));
122 assertEquals(2, res.getListenGAs().size());
123 assertEquals(2, res.getReadGAs().size());
127 void testParseFreeLevel() throws KNXFormatException {
128 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<4610+<4611");
135 assertEquals(new GroupAddress("4610"), res.getMainGA());
136 assertEquals(2, res.getListenGAs().size());
137 assertEquals(2, res.getReadGAs().size());
141 public void testChannelGaParsing() throws KNXFormatException {
142 Channel channel = mock(Channel.class);
143 Configuration configuration = new Configuration(
144 Map.of("key1", "5.001:<1/2/3+4/5/6+1/5/6", "key2", "1.001:7/1/9+1/1/2"));
145 when(channel.getChannelTypeUID()).thenReturn(new ChannelTypeUID("a:b:c"));
146 when(channel.getConfiguration()).thenReturn(configuration);
147 when(channel.getAcceptedItemType()).thenReturn("none");
149 MyKNXChannel knxChannel = new MyKNXChannel(channel);
151 Set<GroupAddress> listenAddresses = knxChannel.getAllGroupAddresses();
152 assertEquals(5, listenAddresses.size());
153 // we don't check the content since parsing has been checked before and the quantity is correct
154 Set<GroupAddress> writeAddresses = knxChannel.getWriteAddresses();
155 assertEquals(2, writeAddresses.size());
156 assertTrue(writeAddresses.contains(new GroupAddress("1/2/3")));
157 assertTrue(writeAddresses.contains(new GroupAddress("7/1/9")));
160 private static class MyKNXChannel extends KNXChannel {
161 public MyKNXChannel(Channel channel) {
162 super(Set.of("key1", "key2"), List.of(UnDefType.class), channel);
166 protected String getDefaultDPT(String gaConfigKey) {