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.lcn.internal;
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.*;
20 import java.nio.ByteBuffer;
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;
33 * Test class for {@link LcnModuleActions}.
35 * @author Fabian Wolter - Initial contribution
37 @ExtendWith(MockitoExtension.class)
39 public class ModuleActionsTest {
40 private LcnModuleActions a = new LcnModuleActions();
41 private final LcnModuleHandler handler = mock(LcnModuleHandler.class);
43 private @NonNullByDefault({}) ArgumentCaptor<byte[]> byteBufferCaptor;
47 a = new LcnModuleActions();
48 a.setThingHandler(handler);
51 private byte[] stringToByteBuffer(String string) {
52 return string.getBytes(LcnDefs.LCN_ENCODING);
56 public void testSendDynamicText1CharRow1() throws LcnException {
57 a.sendDynamicText(1, "a");
59 verify(handler).sendPck(stringToByteBuffer("GTDT11a\0\0\0\0\0\0\0\0\0\0\0"));
63 public void testSendDynamicText1ChunkRow1() throws LcnException {
64 a.sendDynamicText(1, "abcdfghijklm");
66 verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
68 assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT11abcdfghijklm"),
69 stringToByteBuffer("GTDT12 \0\0\0\0\0\0\0\0\0\0\0")));
73 public void testSendDynamicText1Chunk1CharRow1() throws LcnException {
74 a.sendDynamicText(1, "abcdfghijklmn");
76 verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
78 assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT11abcdfghijklm"),
79 stringToByteBuffer("GTDT12n\0\0\0\0\0\0\0\0\0\0\0")));
83 public void testSendDynamicText5ChunksRow1() throws LcnException {
84 a.sendDynamicText(1, "abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijk");
86 verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
88 assertThat(byteBufferCaptor.getAllValues(),
89 containsInAnyOrder(stringToByteBuffer("GTDT11abcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
90 stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
91 stringToByteBuffer("GTDT15yzabcdfghijk")));
95 public void testSendDynamicText5Chunks1CharRow1Truncated() throws LcnException {
96 a.sendDynamicText(1, "abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijkl");
98 verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
100 assertThat(byteBufferCaptor.getAllValues(),
101 containsInAnyOrder(stringToByteBuffer("GTDT11abcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
102 stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
103 stringToByteBuffer("GTDT15yzabcdfghijk")));
107 public void testSendDynamicText5Chunks1UmlautRow1Truncated() throws LcnException {
108 a.sendDynamicText(1, "äcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijkl");
110 verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
112 assertThat(byteBufferCaptor.getAllValues(),
113 containsInAnyOrder(stringToByteBuffer("GTDT11äcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
114 stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
115 stringToByteBuffer("GTDT15yzabcdfghijk")));
119 public void testSendDynamicTextRow4() throws LcnException {
120 a.sendDynamicText(4, "abcdfghijklmn");
122 verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
124 assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT41abcdfghijklm"),
125 stringToByteBuffer("GTDT42n\0\0\0\0\0\0\0\0\0\0\0")));
129 public void testSendDynamicTextSplitInCharacter() throws LcnException {
130 a.sendDynamicText(4, "Test 123 öäüß");
132 verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
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 ä
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"));
144 assertThat(byteBufferCaptor.getAllValues(), contains(chunk1.array(), chunk2.array()));
148 public void testSendKeysInvalidTable() throws LcnException {
149 a.hitKey("E", 3, "MAKE");
150 verify(handler, times(0)).sendPck(anyString());
154 public void testSendKeysNullTable() throws LcnException {
155 a.hitKey(null, 3, "MAKE");
156 verify(handler, times(0)).sendPck(anyString());
160 public void testSendKeysNullAction() throws LcnException {
161 a.hitKey("D", 3, null);
162 verify(handler, times(0)).sendPck(anyString());
166 public void testSendKeysInvalidKey0() throws LcnException {
167 a.hitKey("D", 0, "MAKE");
168 verify(handler, times(0)).sendPck(anyString());
172 public void testSendKeysInvalidKey9() throws LcnException {
173 a.hitKey("D", 9, "MAKE");
174 verify(handler, times(0)).sendPck(anyString());
178 public void testSendKeysInvalidAction() throws LcnException {
179 a.hitKey("D", 8, "invalid");
180 verify(handler, times(0)).sendPck(anyString());
184 public void testSendKeysA1Hit() throws LcnException {
185 a.hitKey("a", 1, "HIT");
187 verify(handler).sendPck("TSK--10000000");
191 public void testSendKeysC8Hit() throws LcnException {
192 a.hitKey("C", 8, "break");
194 verify(handler).sendPck("TS--O00000001");
198 public void testSendKeysD3Make() throws LcnException {
199 a.hitKey("D", 3, "MAKE");
201 verify(handler).sendPck("TS---L00100000");
205 public void testBeepNull() throws LcnException {
206 a.beep(null, null, null);
208 verify(handler).sendPck("PIN1");
209 verify(handler, times(1)).sendPck(anyString());
213 public void testBeepSpecial() throws LcnException {
214 a.beep(null, "S", 5);
216 verify(handler).sendPck("PIS5");
217 verify(handler, times(1)).sendPck(anyString());
221 public void testBeepVolume() throws LcnException {
224 verify(handler).sendPck("PIV050");
225 verify(handler).sendPck("PI35");
226 verify(handler, times(2)).sendPck(anyString());
230 public void testBeepInvalidVolume() throws LcnException {
233 verify(handler, times(0)).sendPck(anyString());
237 public void testBeepInvalidTonality() throws LcnException {
238 a.beep(null, "X", 5);
240 verify(handler).sendPck("PIN5");
241 verify(handler, times(1)).sendPck(anyString());