2 * Copyright (c) 2010-2024 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.broadlink.internal.handler;
15 import static org.junit.Assert.fail;
16 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import static org.mockito.Mockito.*;
19 import java.io.IOException;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.ArgumentMatchers;
26 import org.mockito.Mockito;
27 import org.mockito.MockitoAnnotations;
28 import org.openhab.binding.broadlink.internal.BroadlinkBindingConstants;
29 import org.openhab.core.test.storage.VolatileStorageService;
32 * Tests the Remote Model Pro handler.
34 * @author John Marshall - Initial contribution
37 public class BroadlinkRemoteModelProHandlerTest extends AbstractBroadlinkThingHandlerTest {
41 public void setUp() throws Exception {
42 configureUnderlyingThing(BroadlinkBindingConstants.THING_TYPE_RM_PRO, "rm_pro-test");
43 MockitoAnnotations.openMocks(this).close();
44 Mockito.when(mockSocket.sendAndReceive(ArgumentMatchers.any(byte[].class), ArgumentMatchers.anyString()))
45 .thenReturn(response);
48 private byte[] response = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
57 public void sendsExpectedBytesWhenGettingDeviceStatus() {
58 VolatileStorageService storageService = new VolatileStorageService();
59 ArgumentCaptor<Byte> commandCaptor = ArgumentCaptor.forClass(Byte.class);
60 ArgumentCaptor<byte[]> byteArrayCaptor = ArgumentCaptor.forClass(byte[].class);
61 BroadlinkRemoteHandler model2 = new BroadlinkRemoteModelProHandler(thing, commandDescriptionProvider,
63 setMocksForTesting(model2);
64 reset(trafficObserver);
66 model2.getStatusFromDevice();
67 } catch (IOException | BroadlinkException e) {
68 fail("Unexpected exception: " + e.getClass().getCanonicalName());
71 verify(trafficObserver).onCommandSent(commandCaptor.capture());
72 assertEquals(0x6a, commandCaptor.getValue().byteValue());
74 verify(trafficObserver).onBytesSent(byteArrayCaptor.capture());
76 byte[] sentBytes = byteArrayCaptor.getValue();
77 assertEquals(16, sentBytes.length);
78 assertEquals(0x01, sentBytes[0]);
82 public void sendsExpectedBytesWhenSendingCode() throws IOException {
83 VolatileStorageService storageService = new VolatileStorageService();
84 ArgumentCaptor<Byte> commandCaptor = ArgumentCaptor.forClass(Byte.class);
85 ArgumentCaptor<byte[]> byteCaptor = ArgumentCaptor.forClass(byte[].class);
86 BroadlinkRemoteHandler model2 = new BroadlinkRemoteModelProHandler(thing, commandDescriptionProvider,
88 setMocksForTesting(model2);
89 // Note the length is 12 so as to not require padding
90 byte[] code = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a };
92 reset(trafficObserver);
93 model2.sendCode(code);
95 verify(trafficObserver).onCommandSent(commandCaptor.capture());
96 assertEquals(0x6a, commandCaptor.getValue().byteValue());
98 verify(trafficObserver).onBytesSent(byteCaptor.capture());
100 byte[] sentBytes = byteCaptor.getValue();
101 assertEquals(16, sentBytes.length);
103 assertEquals(0x02, sentBytes[0]); // 0x00, 0x00, 0x00
105 assertEquals(0x01, sentBytes[4]);
106 assertEquals(0x02, sentBytes[5]);
107 assertEquals(0x03, sentBytes[6]);
108 assertEquals(0x04, sentBytes[7]);
109 assertEquals(0x05, sentBytes[8]);
110 assertEquals(0x06, sentBytes[9]);
111 assertEquals(0x07, sentBytes[10]);
112 assertEquals(0x08, sentBytes[11]);
113 assertEquals(0x09, sentBytes[12]);
114 assertEquals(0x0a, sentBytes[13]);
118 public void sendsExpectedBytesWhenSendingCodeIncludingPadding() throws IOException {
119 VolatileStorageService storageService = new VolatileStorageService();
120 ArgumentCaptor<Byte> commandCaptor = ArgumentCaptor.forClass(Byte.class);
121 ArgumentCaptor<byte[]> byteCaptor = ArgumentCaptor.forClass(byte[].class);
122 BroadlinkRemoteHandler modelPro = new BroadlinkRemoteModelProHandler(thing, commandDescriptionProvider,
124 setMocksForTesting(modelPro);
125 // Note the length is such that padding up to the next multiple of 16 will be needed
126 byte[] code = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
128 reset(trafficObserver);
129 modelPro.sendCode(code);
131 verify(trafficObserver).onCommandSent(commandCaptor.capture());
132 assertEquals(0x6a, commandCaptor.getValue().byteValue());
134 verify(trafficObserver).onBytesSent(byteCaptor.capture());
136 byte[] sentBytes = byteCaptor.getValue();
137 assertEquals(32, sentBytes.length);
139 assertEquals(0x02, sentBytes[0]); // 0x00, 0x00, 0x00
141 assertEquals(0x01, sentBytes[4]);
142 assertEquals(0x02, sentBytes[5]);
143 assertEquals(0x03, sentBytes[6]);
144 assertEquals(0x04, sentBytes[7]);
145 assertEquals(0x05, sentBytes[8]);
146 assertEquals(0x06, sentBytes[9]);
147 assertEquals(0x07, sentBytes[10]);
148 assertEquals(0x08, sentBytes[11]);
149 assertEquals(0x09, sentBytes[12]);
150 assertEquals(0x0a, sentBytes[13]);
151 assertEquals(0x0b, sentBytes[14]);
152 assertEquals(0x0c, sentBytes[15]);
153 assertEquals(0x0d, sentBytes[16]);
154 assertEquals(0x0e, sentBytes[17]);
155 assertEquals(0x0f, sentBytes[18]);
156 assertEquals(0x10, sentBytes[19]);
157 assertEquals(0x11, sentBytes[20]);
158 assertEquals(0x00, sentBytes[21]);
159 assertEquals(0x00, sentBytes[31]);