]> git.basschouten.com Git - openhab-addons.git/blob
9529841f5a96946001155b259b647f9923991362
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.max.internal.command;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.nio.charset.StandardCharsets;
18 import java.util.Base64;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.max.internal.Utils;
23 import org.openhab.binding.max.internal.device.ThermostatModeType;
24
25 /**
26  * Tests cases for {@link SCommand}.
27  *
28  * @author Marcel Verpaalen - Initial contribution
29  */
30 @NonNullByDefault
31 public class SCommandTest {
32
33     @Test
34     public void prefixTest() {
35         SCommand scmd = new SCommand("0b0da3", 1, ThermostatModeType.MANUAL, 20.0);
36         String commandStr = scmd.getCommandString();
37         String prefix = commandStr.substring(0, 2);
38
39         assertEquals("s:", prefix);
40     }
41
42     @Test
43     public void baseCommandTest() {
44         SCommand scmd = new SCommand("0b0da3", 1, ThermostatModeType.MANUAL, 20.0);
45
46         String commandStr = scmd.getCommandString();
47
48         String base64Data = commandStr.substring(2).trim();
49         byte[] bytes = Base64.getDecoder().decode(base64Data.getBytes(StandardCharsets.UTF_8));
50
51         int[] data = new int[bytes.length];
52
53         for (int i = 0; i < bytes.length; i++) {
54             data[i] = bytes[i] & 0xFF;
55         }
56
57         String decodedString = Utils.toHex(data);
58         assertEquals("s:AARAAAAACw2jAWg=\r\n", commandStr);
59         assertEquals("0004400000000B0DA30168", decodedString);
60     }
61
62     @Test
63     public void boostModeTest() {
64         SCommand scmd = new SCommand("0b0da3", 1, ThermostatModeType.BOOST, 21.0);
65         String commandStr = scmd.getCommandString();
66         assertEquals("s:AARAAAAACw2jAeo=\r\n", commandStr);
67     }
68
69     @Test
70     public void autoModeTest() {
71         SCommand scmd = new SCommand("0b0da3", 1, ThermostatModeType.AUTOMATIC, 0);
72         String commandStr = scmd.getCommandString();
73         assertEquals("s:AARAAAAACw2jAQA=\r\n", commandStr);
74     }
75 }