]> git.basschouten.com Git - openhab-addons.git/blob
49804bc1e769ae6f1c2d15f64fa7f5e10e8d47ee
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.broadlink.internal.handler;
14
15 import static org.junit.Assert.fail;
16 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import static org.mockito.Mockito.*;
18
19 import java.io.IOException;
20
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;
30
31 /**
32  * Tests the Remote Model Pro handler.
33  *
34  * @author John Marshall - Initial contribution
35  */
36 @NonNullByDefault
37 public class BroadlinkRemoteModelProHandlerTest extends AbstractBroadlinkThingHandlerTest {
38
39     @Override
40     @BeforeEach
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);
46     }
47
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, };
55
56     @Test
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,
62                 storageService);
63         setMocksForTesting(model2);
64         reset(trafficObserver);
65         try {
66             model2.getStatusFromDevice();
67         } catch (IOException | BroadlinkException e) {
68             fail("Unexpected exception: " + e.getClass().getCanonicalName());
69         }
70
71         verify(trafficObserver).onCommandSent(commandCaptor.capture());
72         assertEquals(0x6a, commandCaptor.getValue().byteValue());
73
74         verify(trafficObserver).onBytesSent(byteArrayCaptor.capture());
75
76         byte[] sentBytes = byteArrayCaptor.getValue();
77         assertEquals(16, sentBytes.length);
78         assertEquals(0x01, sentBytes[0]);
79     }
80
81     @Test
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,
87                 storageService);
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 };
91
92         reset(trafficObserver);
93         model2.sendCode(code);
94
95         verify(trafficObserver).onCommandSent(commandCaptor.capture());
96         assertEquals(0x6a, commandCaptor.getValue().byteValue());
97
98         verify(trafficObserver).onBytesSent(byteCaptor.capture());
99
100         byte[] sentBytes = byteCaptor.getValue();
101         assertEquals(16, sentBytes.length);
102
103         assertEquals(0x02, sentBytes[0]); // 0x00, 0x00, 0x00
104
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]);
115     }
116
117     @Test
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,
123                 storageService);
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,
127                 0x11 };
128         reset(trafficObserver);
129         modelPro.sendCode(code);
130
131         verify(trafficObserver).onCommandSent(commandCaptor.capture());
132         assertEquals(0x6a, commandCaptor.getValue().byteValue());
133
134         verify(trafficObserver).onBytesSent(byteCaptor.capture());
135
136         byte[] sentBytes = byteCaptor.getValue();
137         assertEquals(32, sentBytes.length);
138
139         assertEquals(0x02, sentBytes[0]); // 0x00, 0x00, 0x00
140
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]);
160     }
161 }