]> git.basschouten.com Git - openhab-addons.git/blob
f251f55fd84b5264d84ed28d66f1e5b9d8c76734
[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.thing.Channel;
28 import org.openhab.core.thing.type.ChannelTypeUID;
29 import org.openhab.core.types.UnDefType;
30
31 import tuwien.auto.calimero.GroupAddress;
32 import tuwien.auto.calimero.KNXFormatException;
33
34 /**
35  *
36  * @author Simon Kaufmann - Initial contribution
37  *
38  */
39 @NonNullByDefault
40 class KNXChannelTest {
41
42     @Test
43     public void invalidFails() {
44         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<1/3/22+0/3/22+<0/8/15");
45         assertNull(res);
46     }
47
48     @Test
49     void testParseWithDptMultipleWithRead() throws KNXFormatException {
50         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<1/3/22+0/3/22+<0/7/15");
51
52         if (res == null) {
53             fail();
54             return;
55         }
56
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());
62     }
63
64     @Test
65     void testParseWithDptMultipleWithoutRead() throws KNXFormatException {
66         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:1/3/22+0/3/22+0/7/15");
67
68         if (res == null) {
69             fail();
70             return;
71         }
72
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());
78     }
79
80     @Test
81     void testParseWithoutDptSingleWithoutRead() throws KNXFormatException {
82         GroupAddressConfiguration res = GroupAddressConfiguration.parse("1/3/22");
83
84         if (res == null) {
85             fail();
86             return;
87         }
88
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());
94     }
95
96     @Test
97     void testParseWithoutDptSingleWithRead() throws KNXFormatException {
98         GroupAddressConfiguration res = GroupAddressConfiguration.parse("<1/3/22");
99
100         if (res == null) {
101             fail();
102             return;
103         }
104
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());
110     }
111
112     @Test
113     void testParseTwoLevel() throws KNXFormatException {
114         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<3/1024+<4/1025");
115
116         if (res == null) {
117             fail();
118             return;
119         }
120
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());
125     }
126
127     @Test
128     void testParseFreeLevel() throws KNXFormatException {
129         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<4610+<4611");
130
131         if (res == null) {
132             fail();
133             return;
134         }
135
136         assertEquals(new GroupAddress("4610"), res.getMainGA());
137         assertEquals(2, res.getListenGAs().size());
138         assertEquals(2, res.getReadGAs().size());
139     }
140
141     @Test
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");
149
150         MyKNXChannel knxChannel = new MyKNXChannel(channel);
151
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")));
159     }
160
161     private static class MyKNXChannel extends KNXChannel {
162         public MyKNXChannel(Channel channel) {
163             super(Set.of("key1", "key2"), List.of(UnDefType.class), channel);
164         }
165
166         @Override
167         protected String getDefaultDPT(String gaConfigKey) {
168             return "";
169         }
170     }
171 }