]> git.basschouten.com Git - openhab-addons.git/blob
57be019be473090b468c7452f63158a07c3f7a03
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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).sendPck(stringToByteBuffer("GTDT11abcdfghijklm"));
67     }
68
69     @Test
70     public void testSendDynamicText1Chunk1CharRow1() throws LcnException {
71         a.sendDynamicText(1, "abcdfghijklmn");
72
73         verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
74
75         assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT11abcdfghijklm"),
76                 stringToByteBuffer("GTDT12n\0\0\0\0\0\0\0\0\0\0\0")));
77     }
78
79     @Test
80     public void testSendDynamicText5ChunksRow1() throws LcnException {
81         a.sendDynamicText(1, "abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijk");
82
83         verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
84
85         assertThat(byteBufferCaptor.getAllValues(),
86                 containsInAnyOrder(stringToByteBuffer("GTDT11abcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
87                         stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
88                         stringToByteBuffer("GTDT15yzabcdfghijk")));
89     }
90
91     @Test
92     public void testSendDynamicText5Chunks1CharRow1Truncated() throws LcnException {
93         a.sendDynamicText(1, "abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijkl");
94
95         verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
96
97         assertThat(byteBufferCaptor.getAllValues(),
98                 containsInAnyOrder(stringToByteBuffer("GTDT11abcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
99                         stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
100                         stringToByteBuffer("GTDT15yzabcdfghijk")));
101     }
102
103     @Test
104     public void testSendDynamicText5Chunks1UmlautRow1Truncated() throws LcnException {
105         a.sendDynamicText(1, "äcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijkl");
106
107         verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
108
109         assertThat(byteBufferCaptor.getAllValues(),
110                 containsInAnyOrder(stringToByteBuffer("GTDT11äcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
111                         stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
112                         stringToByteBuffer("GTDT15yzabcdfghijk")));
113     }
114
115     @Test
116     public void testSendDynamicTextRow4() throws LcnException {
117         a.sendDynamicText(4, "abcdfghijklmn");
118
119         verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
120
121         assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT41abcdfghijklm"),
122                 stringToByteBuffer("GTDT42n\0\0\0\0\0\0\0\0\0\0\0")));
123     }
124
125     @Test
126     public void testSendDynamicTextSplitInCharacter() throws LcnException {
127         a.sendDynamicText(4, "Test 123 öäüß");
128
129         verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
130
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 ä
135
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"));
140
141         assertThat(byteBufferCaptor.getAllValues(), contains(chunk1.array(), chunk2.array()));
142     }
143
144     @Test
145     public void testSendKeysInvalidTable() throws LcnException {
146         a.hitKey("E", 3, "MAKE");
147         verify(handler, times(0)).sendPck(anyString());
148     }
149
150     @Test
151     public void testSendKeysNullTable() throws LcnException {
152         a.hitKey(null, 3, "MAKE");
153         verify(handler, times(0)).sendPck(anyString());
154     }
155
156     @Test
157     public void testSendKeysNullAction() throws LcnException {
158         a.hitKey("D", 3, null);
159         verify(handler, times(0)).sendPck(anyString());
160     }
161
162     @Test
163     public void testSendKeysInvalidKey0() throws LcnException {
164         a.hitKey("D", 0, "MAKE");
165         verify(handler, times(0)).sendPck(anyString());
166     }
167
168     @Test
169     public void testSendKeysInvalidKey9() throws LcnException {
170         a.hitKey("D", 9, "MAKE");
171         verify(handler, times(0)).sendPck(anyString());
172     }
173
174     @Test
175     public void testSendKeysInvalidAction() throws LcnException {
176         a.hitKey("D", 8, "invalid");
177         verify(handler, times(0)).sendPck(anyString());
178     }
179
180     @Test
181     public void testSendKeysA1Hit() throws LcnException {
182         a.hitKey("a", 1, "HIT");
183
184         verify(handler).sendPck("TSK--10000000");
185     }
186
187     @Test
188     public void testSendKeysC8Hit() throws LcnException {
189         a.hitKey("C", 8, "break");
190
191         verify(handler).sendPck("TS--O00000001");
192     }
193
194     @Test
195     public void testSendKeysD3Make() throws LcnException {
196         a.hitKey("D", 3, "MAKE");
197
198         verify(handler).sendPck("TS---L00100000");
199     }
200 }