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.Matchers.*;
16 import static org.junit.Assert.assertThat;
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.Before;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Captor;
27 import org.mockito.MockitoAnnotations;
28 import org.openhab.binding.lcn.internal.common.LcnDefs;
29 import org.openhab.binding.lcn.internal.common.LcnException;
32 * Test class for {@link LcnModuleActions}.
34 * @author Fabian Wolter - Initial contribution
37 public class ModuleActionsTest {
38 private LcnModuleActions a = new LcnModuleActions();
39 private final LcnModuleHandler handler = mock(LcnModuleHandler.class);
41 private @NonNullByDefault({}) ArgumentCaptor<byte[]> byteBufferCaptor;
45 MockitoAnnotations.initMocks(this);
46 a = new LcnModuleActions();
47 a.setThingHandler(handler);
50 private byte[] stringToByteBuffer(String string) {
51 return string.getBytes(LcnDefs.LCN_ENCODING);
55 public void testSendDynamicText1CharRow1() throws LcnException {
56 a.sendDynamicText(1, "a");
58 verify(handler).sendPck(stringToByteBuffer("GTDT11a\0\0\0\0\0\0\0\0\0\0\0"));
62 public void testSendDynamicText1ChunkRow1() throws LcnException {
63 a.sendDynamicText(1, "abcdfghijklm");
65 verify(handler).sendPck(stringToByteBuffer("GTDT11abcdfghijklm"));
69 public void testSendDynamicText1Chunk1CharRow1() throws LcnException {
70 a.sendDynamicText(1, "abcdfghijklmn");
72 verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
74 assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT11abcdfghijklm"),
75 stringToByteBuffer("GTDT12n\0\0\0\0\0\0\0\0\0\0\0")));
79 public void testSendDynamicText5ChunksRow1() throws LcnException {
80 a.sendDynamicText(1, "abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijk");
82 verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
84 assertThat(byteBufferCaptor.getAllValues(),
85 containsInAnyOrder(stringToByteBuffer("GTDT11abcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
86 stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
87 stringToByteBuffer("GTDT15yzabcdfghijk")));
91 public void testSendDynamicText5Chunks1CharRow1Truncated() throws LcnException {
92 a.sendDynamicText(1, "abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijkl");
94 verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
96 assertThat(byteBufferCaptor.getAllValues(),
97 containsInAnyOrder(stringToByteBuffer("GTDT11abcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
98 stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
99 stringToByteBuffer("GTDT15yzabcdfghijk")));
103 public void testSendDynamicText5Chunks1UmlautRow1Truncated() throws LcnException {
104 a.sendDynamicText(1, "äcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijkl");
106 verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
108 assertThat(byteBufferCaptor.getAllValues(),
109 containsInAnyOrder(stringToByteBuffer("GTDT11äcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
110 stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
111 stringToByteBuffer("GTDT15yzabcdfghijk")));
115 public void testSendDynamicTextRow4() throws LcnException {
116 a.sendDynamicText(4, "abcdfghijklmn");
118 verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
120 assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT41abcdfghijklm"),
121 stringToByteBuffer("GTDT42n\0\0\0\0\0\0\0\0\0\0\0")));
125 public void testSendDynamicTextSplitInCharacter() throws LcnException {
126 a.sendDynamicText(4, "Test 123 öäüß");
128 verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
130 String string1 = "GTDT41Test 123 ö";
131 ByteBuffer chunk1 = ByteBuffer.allocate(stringToByteBuffer(string1).length + 1);
132 chunk1.put(stringToByteBuffer(string1));
133 chunk1.put((byte) -61); // first byte of ä
135 ByteBuffer chunk2 = ByteBuffer.allocate(18);
136 chunk2.put(stringToByteBuffer("GTDT42"));
137 chunk2.put((byte) -92); // second byte of ä
138 chunk2.put(stringToByteBuffer("üß\0\0\0\0\0\0"));
140 assertThat(byteBufferCaptor.getAllValues(), contains(chunk1.array(), chunk2.array()));
144 public void testSendKeysInvalidTable() throws LcnException {
145 a.hitKey("E", 3, "MAKE");
146 verify(handler, times(0)).sendPck(anyString());
150 public void testSendKeysNullTable() throws LcnException {
151 a.hitKey(null, 3, "MAKE");
152 verify(handler, times(0)).sendPck(anyString());
156 public void testSendKeysNullAction() throws LcnException {
157 a.hitKey("D", 3, null);
158 verify(handler, times(0)).sendPck(anyString());
162 public void testSendKeysInvalidKey0() throws LcnException {
163 a.hitKey("D", 0, "MAKE");
164 verify(handler, times(0)).sendPck(anyString());
168 public void testSendKeysInvalidKey9() throws LcnException {
169 a.hitKey("D", 9, "MAKE");
170 verify(handler, times(0)).sendPck(anyString());
174 public void testSendKeysInvalidAction() throws LcnException {
175 a.hitKey("D", 8, "invalid");
176 verify(handler, times(0)).sendPck(anyString());
180 public void testSendKeysA1Hit() throws LcnException {
181 a.hitKey("a", 1, "HIT");
183 verify(handler).sendPck("TSK--10000000");
187 public void testSendKeysC8Hit() throws LcnException {
188 a.hitKey("C", 8, "break");
190 verify(handler).sendPck("TS--O00000001");
194 public void testSendKeysD3Make() throws LcnException {
195 a.hitKey("D", 3, "MAKE");
197 verify(handler).sendPck("TS---L00100000");