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;
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.thing.Channel;
28 import org.openhab.core.thing.type.ChannelTypeUID;
29 import org.openhab.core.types.UnDefType;
31 import tuwien.auto.calimero.GroupAddress;
32 import tuwien.auto.calimero.KNXFormatException;
36 * @author Simon Kaufmann - Initial contribution
40 class KNXChannelTest {
43 public void invalidFails() {
44 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<1/3/22+0/3/22+<0/8/15");
49 void testParseWithDptMultipleWithRead() throws KNXFormatException {
50 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<1/3/22+0/3/22+<0/7/15");
57 assertEquals("5.001", res.getDPT());
58 assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
59 assertTrue(res.getReadGAs().contains(res.getMainGA()));
60 assertEquals(3, res.getListenGAs().size());
61 assertEquals(2, res.getReadGAs().size());
65 void testParseWithDptMultipleWithoutRead() throws KNXFormatException {
66 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:1/3/22+0/3/22+0/7/15");
73 assertEquals("5.001", res.getDPT());
74 assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
75 assertFalse(res.getReadGAs().contains(res.getMainGA()));
76 assertEquals(3, res.getListenGAs().size());
77 assertEquals(0, res.getReadGAs().size());
81 void testParseWithoutDptSingleWithoutRead() throws KNXFormatException {
82 GroupAddressConfiguration res = GroupAddressConfiguration.parse("1/3/22");
89 assertNull(res.getDPT());
90 assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
91 assertFalse(res.getReadGAs().contains(res.getMainGA()));
92 assertEquals(1, res.getListenGAs().size());
93 assertEquals(0, res.getReadGAs().size());
97 void testParseWithoutDptSingleWithRead() throws KNXFormatException {
98 GroupAddressConfiguration res = GroupAddressConfiguration.parse("<1/3/22");
105 assertNull(res.getDPT());
106 assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
107 assertTrue(res.getReadGAs().contains(res.getMainGA()));
108 assertEquals(1, res.getListenGAs().size());
109 assertEquals(1, res.getReadGAs().size());
113 void testParseTwoLevel() throws KNXFormatException {
114 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<3/1024+<4/1025");
121 assertEquals(new GroupAddress("3/1024"), res.getMainGA());
122 assertTrue(res.getReadGAs().contains(res.getMainGA()));
123 assertEquals(2, res.getListenGAs().size());
124 assertEquals(2, res.getReadGAs().size());
128 void testParseFreeLevel() throws KNXFormatException {
129 GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<4610+<4611");
136 assertEquals(new GroupAddress("4610"), res.getMainGA());
137 assertEquals(2, res.getListenGAs().size());
138 assertEquals(2, res.getReadGAs().size());
142 public void testChannelGaParsing() throws KNXFormatException {
143 Channel channel = Objects.requireNonNull(mock(Channel.class));
144 Configuration configuration = new Configuration(
145 Map.of("key1", "5.001:<1/2/3+4/5/6+1/5/6", "key2", "1.001:7/1/9+1/1/2"));
146 when(channel.getChannelTypeUID()).thenReturn(new ChannelTypeUID("a:b:c"));
147 when(channel.getConfiguration()).thenReturn(configuration);
148 when(channel.getAcceptedItemType()).thenReturn("none");
150 MyKNXChannel knxChannel = new MyKNXChannel(channel);
152 Set<GroupAddress> listenAddresses = knxChannel.getAllGroupAddresses();
153 assertEquals(5, listenAddresses.size());
154 // we don't check the content since parsing has been checked before and the quantity is correct
155 Set<GroupAddress> writeAddresses = knxChannel.getWriteAddresses();
156 assertEquals(2, writeAddresses.size());
157 assertTrue(writeAddresses.contains(new GroupAddress("1/2/3")));
158 assertTrue(writeAddresses.contains(new GroupAddress("7/1/9")));
161 private static class MyKNXChannel extends KNXChannel {
162 public MyKNXChannel(Channel channel) {
163 super(Set.of("key1", "key2"), List.of(UnDefType.class), channel);
167 protected String getDefaultDPT(String gaConfigKey) {