]> git.basschouten.com Git - openhab-addons.git/blob
f2d2053ffc880f365b0a8b9d8aef74f95e058289
[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.lcn.internal;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
17 import static org.mockito.ArgumentMatchers.anyString;
18 import static org.mockito.Mockito.*;
19
20 import java.nio.ByteBuffer;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Captor;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.openhab.binding.lcn.internal.common.LcnDefs;
30 import org.openhab.binding.lcn.internal.common.LcnException;
31
32 /**
33  * Test class for {@link LcnModuleActions}.
34  *
35  * @author Fabian Wolter - Initial contribution
36  */
37 @ExtendWith(MockitoExtension.class)
38 @NonNullByDefault
39 public class ModuleActionsTest {
40     private LcnModuleActions a = new LcnModuleActions();
41     private final LcnModuleHandler handler = mock(LcnModuleHandler.class);
42     @Captor
43     private @NonNullByDefault({}) ArgumentCaptor<byte[]> byteBufferCaptor;
44
45     @BeforeEach
46     public void setUp() {
47         a = new LcnModuleActions();
48         a.setThingHandler(handler);
49     }
50
51     private byte[] stringToByteBuffer(String string) {
52         return string.getBytes(LcnDefs.LCN_ENCODING);
53     }
54
55     @Test
56     public void testSendDynamicText1CharRow1() throws LcnException {
57         a.sendDynamicText(1, "a");
58
59         verify(handler).sendPck(stringToByteBuffer("GTDT11a\0\0\0\0\0\0\0\0\0\0\0"));
60     }
61
62     @Test
63     public void testSendDynamicText1ChunkRow1() throws LcnException {
64         a.sendDynamicText(1, "abcdfghijklm");
65
66         verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
67
68         assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT11abcdfghijklm"),
69                 stringToByteBuffer("GTDT12 \0\0\0\0\0\0\0\0\0\0\0")));
70     }
71
72     @Test
73     public void testSendDynamicText1Chunk1CharRow1() throws LcnException {
74         a.sendDynamicText(1, "abcdfghijklmn");
75
76         verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
77
78         assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT11abcdfghijklm"),
79                 stringToByteBuffer("GTDT12n\0\0\0\0\0\0\0\0\0\0\0")));
80     }
81
82     @Test
83     public void testSendDynamicText5ChunksRow1() throws LcnException {
84         a.sendDynamicText(1, "abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijk");
85
86         verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
87
88         assertThat(byteBufferCaptor.getAllValues(),
89                 containsInAnyOrder(stringToByteBuffer("GTDT11abcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
90                         stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
91                         stringToByteBuffer("GTDT15yzabcdfghijk")));
92     }
93
94     @Test
95     public void testSendDynamicText5Chunks1CharRow1Truncated() throws LcnException {
96         a.sendDynamicText(1, "abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijkl");
97
98         verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
99
100         assertThat(byteBufferCaptor.getAllValues(),
101                 containsInAnyOrder(stringToByteBuffer("GTDT11abcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
102                         stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
103                         stringToByteBuffer("GTDT15yzabcdfghijk")));
104     }
105
106     @Test
107     public void testSendDynamicText5Chunks1UmlautRow1Truncated() throws LcnException {
108         a.sendDynamicText(1, "äcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijkl");
109
110         verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
111
112         assertThat(byteBufferCaptor.getAllValues(),
113                 containsInAnyOrder(stringToByteBuffer("GTDT11äcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
114                         stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
115                         stringToByteBuffer("GTDT15yzabcdfghijk")));
116     }
117
118     @Test
119     public void testSendDynamicTextRow4() throws LcnException {
120         a.sendDynamicText(4, "abcdfghijklmn");
121
122         verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
123
124         assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT41abcdfghijklm"),
125                 stringToByteBuffer("GTDT42n\0\0\0\0\0\0\0\0\0\0\0")));
126     }
127
128     @Test
129     public void testSendDynamicTextSplitInCharacter() throws LcnException {
130         a.sendDynamicText(4, "Test 123 öäüß");
131
132         verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
133
134         String string1 = "GTDT41Test 123 ö";
135         ByteBuffer chunk1 = ByteBuffer.allocate(stringToByteBuffer(string1).length + 1);
136         chunk1.put(stringToByteBuffer(string1));
137         chunk1.put((byte) -61); // first byte of ä
138
139         ByteBuffer chunk2 = ByteBuffer.allocate(18);
140         chunk2.put(stringToByteBuffer("GTDT42"));
141         chunk2.put((byte) -92); // second byte of ä
142         chunk2.put(stringToByteBuffer("üß\0\0\0\0\0\0"));
143
144         assertThat(byteBufferCaptor.getAllValues(), contains(chunk1.array(), chunk2.array()));
145     }
146
147     @Test
148     public void testSendKeysInvalidTable() throws LcnException {
149         a.hitKey("E", 3, "MAKE");
150         verify(handler, times(0)).sendPck(anyString());
151     }
152
153     @Test
154     public void testSendKeysNullTable() throws LcnException {
155         a.hitKey(null, 3, "MAKE");
156         verify(handler, times(0)).sendPck(anyString());
157     }
158
159     @Test
160     public void testSendKeysNullAction() throws LcnException {
161         a.hitKey("D", 3, null);
162         verify(handler, times(0)).sendPck(anyString());
163     }
164
165     @Test
166     public void testSendKeysInvalidKey0() throws LcnException {
167         a.hitKey("D", 0, "MAKE");
168         verify(handler, times(0)).sendPck(anyString());
169     }
170
171     @Test
172     public void testSendKeysInvalidKey9() throws LcnException {
173         a.hitKey("D", 9, "MAKE");
174         verify(handler, times(0)).sendPck(anyString());
175     }
176
177     @Test
178     public void testSendKeysInvalidAction() throws LcnException {
179         a.hitKey("D", 8, "invalid");
180         verify(handler, times(0)).sendPck(anyString());
181     }
182
183     @Test
184     public void testSendKeysA1Hit() throws LcnException {
185         a.hitKey("a", 1, "HIT");
186
187         verify(handler).sendPck("TSK--10000000");
188     }
189
190     @Test
191     public void testSendKeysC8Hit() throws LcnException {
192         a.hitKey("C", 8, "break");
193
194         verify(handler).sendPck("TS--O00000001");
195     }
196
197     @Test
198     public void testSendKeysD3Make() throws LcnException {
199         a.hitKey("D", 3, "MAKE");
200
201         verify(handler).sendPck("TS---L00100000");
202     }
203
204     @Test
205     public void testBeepNull() throws LcnException {
206         a.beep(null, null, null);
207
208         verify(handler).sendPck("PIN1");
209         verify(handler, times(1)).sendPck(anyString());
210     }
211
212     @Test
213     public void testBeepSpecial() throws LcnException {
214         a.beep(null, "S", 5);
215
216         verify(handler).sendPck("PIS5");
217         verify(handler, times(1)).sendPck(anyString());
218     }
219
220     @Test
221     public void testBeepVolume() throws LcnException {
222         a.beep(50d, "3", 5);
223
224         verify(handler).sendPck("PIV050");
225         verify(handler).sendPck("PI35");
226         verify(handler, times(2)).sendPck(anyString());
227     }
228
229     @Test
230     public void testBeepInvalidVolume() throws LcnException {
231         a.beep(-1d, "3", 5);
232
233         verify(handler, times(0)).sendPck(anyString());
234     }
235
236     @Test
237     public void testBeepInvalidTonality() throws LcnException {
238         a.beep(null, "X", 5);
239
240         verify(handler).sendPck("PIN5");
241         verify(handler, times(1)).sendPck(anyString());
242     }
243 }