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