]> git.basschouten.com Git - openhab-addons.git/blob
2867607cc6d90fbb5965b4b7312189e2afcfbe4c
[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.Matchers.*;
16 import static org.junit.Assert.assertThat;
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.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;
30
31 /**
32  * Test class for {@link LcnModuleActions}.
33  *
34  * @author Fabian Wolter - Initial contribution
35  */
36 @NonNullByDefault
37 public class ModuleActionsTest {
38     private LcnModuleActions a = new LcnModuleActions();
39     private final LcnModuleHandler handler = mock(LcnModuleHandler.class);
40     @Captor
41     private @NonNullByDefault({}) ArgumentCaptor<byte[]> byteBufferCaptor;
42
43     @Before
44     public void setUp() {
45         MockitoAnnotations.initMocks(this);
46         a = new LcnModuleActions();
47         a.setThingHandler(handler);
48     }
49
50     private byte[] stringToByteBuffer(String string) {
51         return string.getBytes(LcnDefs.LCN_ENCODING);
52     }
53
54     @Test
55     public void testSendDynamicText1CharRow1() throws LcnException {
56         a.sendDynamicText(1, "a");
57
58         verify(handler).sendPck(stringToByteBuffer("GTDT11a\0\0\0\0\0\0\0\0\0\0\0"));
59     }
60
61     @Test
62     public void testSendDynamicText1ChunkRow1() throws LcnException {
63         a.sendDynamicText(1, "abcdfghijklm");
64
65         verify(handler).sendPck(stringToByteBuffer("GTDT11abcdfghijklm"));
66     }
67
68     @Test
69     public void testSendDynamicText1Chunk1CharRow1() throws LcnException {
70         a.sendDynamicText(1, "abcdfghijklmn");
71
72         verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
73
74         assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT11abcdfghijklm"),
75                 stringToByteBuffer("GTDT12n\0\0\0\0\0\0\0\0\0\0\0")));
76     }
77
78     @Test
79     public void testSendDynamicText5ChunksRow1() throws LcnException {
80         a.sendDynamicText(1, "abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijk");
81
82         verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
83
84         assertThat(byteBufferCaptor.getAllValues(),
85                 containsInAnyOrder(stringToByteBuffer("GTDT11abcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
86                         stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
87                         stringToByteBuffer("GTDT15yzabcdfghijk")));
88     }
89
90     @Test
91     public void testSendDynamicText5Chunks1CharRow1Truncated() throws LcnException {
92         a.sendDynamicText(1, "abcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijkl");
93
94         verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
95
96         assertThat(byteBufferCaptor.getAllValues(),
97                 containsInAnyOrder(stringToByteBuffer("GTDT11abcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
98                         stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
99                         stringToByteBuffer("GTDT15yzabcdfghijk")));
100     }
101
102     @Test
103     public void testSendDynamicText5Chunks1UmlautRow1Truncated() throws LcnException {
104         a.sendDynamicText(1, "äcdfghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdfghijkl");
105
106         verify(handler, times(5)).sendPck(byteBufferCaptor.capture());
107
108         assertThat(byteBufferCaptor.getAllValues(),
109                 containsInAnyOrder(stringToByteBuffer("GTDT11äcdfghijklm"), stringToByteBuffer("GTDT12nopqrstuvwxy"),
110                         stringToByteBuffer("GTDT13zabcdfghijkl"), stringToByteBuffer("GTDT14mnopqrstuvwx"),
111                         stringToByteBuffer("GTDT15yzabcdfghijk")));
112     }
113
114     @Test
115     public void testSendDynamicTextRow4() throws LcnException {
116         a.sendDynamicText(4, "abcdfghijklmn");
117
118         verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
119
120         assertThat(byteBufferCaptor.getAllValues(), contains(stringToByteBuffer("GTDT41abcdfghijklm"),
121                 stringToByteBuffer("GTDT42n\0\0\0\0\0\0\0\0\0\0\0")));
122     }
123
124     @Test
125     public void testSendDynamicTextSplitInCharacter() throws LcnException {
126         a.sendDynamicText(4, "Test 123 öäüß");
127
128         verify(handler, times(2)).sendPck(byteBufferCaptor.capture());
129
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 ä
134
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"));
139
140         assertThat(byteBufferCaptor.getAllValues(), contains(chunk1.array(), chunk2.array()));
141     }
142
143     @Test
144     public void testSendKeysInvalidTable() throws LcnException {
145         a.hitKey("E", 3, "MAKE");
146         verify(handler, times(0)).sendPck(anyString());
147     }
148
149     @Test
150     public void testSendKeysNullTable() throws LcnException {
151         a.hitKey(null, 3, "MAKE");
152         verify(handler, times(0)).sendPck(anyString());
153     }
154
155     @Test
156     public void testSendKeysNullAction() throws LcnException {
157         a.hitKey("D", 3, null);
158         verify(handler, times(0)).sendPck(anyString());
159     }
160
161     @Test
162     public void testSendKeysInvalidKey0() throws LcnException {
163         a.hitKey("D", 0, "MAKE");
164         verify(handler, times(0)).sendPck(anyString());
165     }
166
167     @Test
168     public void testSendKeysInvalidKey9() throws LcnException {
169         a.hitKey("D", 9, "MAKE");
170         verify(handler, times(0)).sendPck(anyString());
171     }
172
173     @Test
174     public void testSendKeysInvalidAction() throws LcnException {
175         a.hitKey("D", 8, "invalid");
176         verify(handler, times(0)).sendPck(anyString());
177     }
178
179     @Test
180     public void testSendKeysA1Hit() throws LcnException {
181         a.hitKey("a", 1, "HIT");
182
183         verify(handler).sendPck("TSK--10000000");
184     }
185
186     @Test
187     public void testSendKeysC8Hit() throws LcnException {
188         a.hitKey("C", 8, "break");
189
190         verify(handler).sendPck("TS--O00000001");
191     }
192
193     @Test
194     public void testSendKeysD3Make() throws LcnException {
195         a.hitKey("D", 3, "MAKE");
196
197         verify(handler).sendPck("TS---L00100000");
198     }
199 }