2 * Copyright (c) 2010-2020 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).sendPck(stringToByteBuffer("GTDT11abcdfghijklm"));
70 public void testSendDynamicText1Chunk1CharRow1() throws LcnException {
71 a.sendDynamicText(1, "abcdfghijklmn");
73 verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
75 assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT11abcdfghijklm"),
76 stringToByteBuffer("GTDT12n\0\0\0\0\0\0\0\0\0\0\0")));
80 public void testSendDynamicText5ChunksRow1() throws LcnException {
81 a.sendDynamicText(1, "abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijk");
83 verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
85 assertThat(byteBufferCaptor.getAllValues(),
86 containsInAnyOrder(stringToByteBuffer("GTDT11abcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
87 stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
88 stringToByteBuffer("GTDT15yzabcdfghijk")));
92 public void testSendDynamicText5Chunks1CharRow1Truncated() throws LcnException {
93 a.sendDynamicText(1, "abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijkl");
95 verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
97 assertThat(byteBufferCaptor.getAllValues(),
98 containsInAnyOrder(stringToByteBuffer("GTDT11abcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
99 stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
100 stringToByteBuffer("GTDT15yzabcdfghijk")));
104 public void testSendDynamicText5Chunks1UmlautRow1Truncated() throws LcnException {
105 a.sendDynamicText(1, "äcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijkl");
107 verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
109 assertThat(byteBufferCaptor.getAllValues(),
110 containsInAnyOrder(stringToByteBuffer("GTDT11äcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
111 stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
112 stringToByteBuffer("GTDT15yzabcdfghijk")));
116 public void testSendDynamicTextRow4() throws LcnException {
117 a.sendDynamicText(4, "abcdfghijklmn");
119 verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
121 assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT41abcdfghijklm"),
122 stringToByteBuffer("GTDT42n\0\0\0\0\0\0\0\0\0\0\0")));
126 public void testSendDynamicTextSplitInCharacter() throws LcnException {
127 a.sendDynamicText(4, "Test 123 öäüß");
129 verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
131 String string1 = "GTDT41Test 123 ö";
132 ByteBuffer chunk1 = ByteBuffer.allocate(stringToByteBuffer(string1).length + 1);
133 chunk1.put(stringToByteBuffer(string1));
134 chunk1.put((byte) -61); // first byte of ä
136 ByteBuffer chunk2 = ByteBuffer.allocate(18);
137 chunk2.put(stringToByteBuffer("GTDT42"));
138 chunk2.put((byte) -92); // second byte of ä
139 chunk2.put(stringToByteBuffer("üß\0\0\0\0\0\0"));
141 assertThat(byteBufferCaptor.getAllValues(), contains(chunk1.array(), chunk2.array()));
145 public void testSendKeysInvalidTable() throws LcnException {
146 a.hitKey("E", 3, "MAKE");
147 verify(handler, times(0)).sendPck(anyString());
151 public void testSendKeysNullTable() throws LcnException {
152 a.hitKey(null, 3, "MAKE");
153 verify(handler, times(0)).sendPck(anyString());
157 public void testSendKeysNullAction() throws LcnException {
158 a.hitKey("D", 3, null);
159 verify(handler, times(0)).sendPck(anyString());
163 public void testSendKeysInvalidKey0() throws LcnException {
164 a.hitKey("D", 0, "MAKE");
165 verify(handler, times(0)).sendPck(anyString());
169 public void testSendKeysInvalidKey9() throws LcnException {
170 a.hitKey("D", 9, "MAKE");
171 verify(handler, times(0)).sendPck(anyString());
175 public void testSendKeysInvalidAction() throws LcnException {
176 a.hitKey("D", 8, "invalid");
177 verify(handler, times(0)).sendPck(anyString());
181 public void testSendKeysA1Hit() throws LcnException {
182 a.hitKey("a", 1, "HIT");
184 verify(handler).sendPck("TSK--10000000");
188 public void testSendKeysC8Hit() throws LcnException {
189 a.hitKey("C", 8, "break");
191 verify(handler).sendPck("TS--O00000001");
195 public void testSendKeysD3Make() throws LcnException {
196 a.hitKey("D", 3, "MAKE");
198 verify(handler).sendPck("TS---L00100000");