]> git.basschouten.com Git - openhab-addons.git/blob
a943d4288e080c57852cbe292fd381909ff7eecc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.knx.internal.channel;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
18
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Objects;
22 import java.util.Set;
23
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;
32
33 import tuwien.auto.calimero.GroupAddress;
34 import tuwien.auto.calimero.KNXFormatException;
35
36 /**
37  *
38  * @author Simon Kaufmann - Initial contribution
39  *
40  */
41 @NonNullByDefault
42 class KNXChannelTest {
43
44     @Test
45     public void invalidFails() {
46         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<1/3/22+0/3/22+<0/8/15");
47         assertNull(res);
48     }
49
50     @Test
51     void testParseWithDptMultipleWithRead() throws KNXFormatException {
52         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<1/3/22+0/3/22+<0/7/15");
53
54         if (res == null) {
55             fail();
56             return;
57         }
58
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());
64     }
65
66     @Test
67     void testParseWithDptMultipleWithoutRead() throws KNXFormatException {
68         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:1/3/22+0/3/22+0/7/15");
69
70         if (res == null) {
71             fail();
72             return;
73         }
74
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());
80     }
81
82     @Test
83     void testParseWithoutDptSingleWithoutRead() throws KNXFormatException {
84         GroupAddressConfiguration res = GroupAddressConfiguration.parse("1/3/22");
85
86         if (res == null) {
87             fail();
88             return;
89         }
90
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());
96     }
97
98     @Test
99     void testParseWithoutDptSingleWithRead() throws KNXFormatException {
100         GroupAddressConfiguration res = GroupAddressConfiguration.parse("<1/3/22");
101
102         if (res == null) {
103             fail();
104             return;
105         }
106
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());
112     }
113
114     @Test
115     void testParseTwoLevel() throws KNXFormatException {
116         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<3/1024+<4/1025");
117
118         if (res == null) {
119             fail();
120             return;
121         }
122
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());
127     }
128
129     @Test
130     void testParseFreeLevel() throws KNXFormatException {
131         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<4610+<4611");
132
133         if (res == null) {
134             fail();
135             return;
136         }
137
138         assertEquals(new GroupAddress("4610"), res.getMainGA());
139         assertEquals(2, res.getListenGAs().size());
140         assertEquals(2, res.getReadGAs().size());
141     }
142
143     @Test
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");
151
152         MyKNXChannel knxChannel = new MyKNXChannel(channel);
153
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")));
161     }
162
163     @Test
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");
173     }
174
175     private static class MyKNXChannel extends KNXChannel {
176         public MyKNXChannel(Channel channel) {
177             super(Set.of("key1", "key2"), List.of(UnDefType.class), channel);
178         }
179
180         @Override
181         protected String getDefaultDPT(String gaConfigKey) {
182             return "";
183         }
184     }
185 }