]> git.basschouten.com Git - openhab-addons.git/blob
4db2029c007e9bc0e8d32e3cb0be053a205ea85f
[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.util.Base64;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.max.internal.Utils;
22
23 /**
24  * Tests cases for {@link TCommand}.
25  *
26  * @author Marcel Verpaalen - Initial contribution
27  */
28 @NonNullByDefault
29 public class TCommandTest {
30
31     @Test
32     public void prefixTest() {
33         TCommand scmd = new TCommand("0f1d54", false);
34
35         String commandStr = scmd.getCommandString();
36         String prefix = commandStr.substring(0, 2);
37
38         assertEquals("t:", prefix);
39     }
40
41     @Test
42     public void baseCommandTest() {
43         TCommand scmd = new TCommand("0f1d54", false);
44
45         String commandStr = scmd.getCommandString();
46
47         String base64Data = commandStr.split(",")[2];
48         byte[] bytes = Base64.getDecoder().decode(base64Data.trim().getBytes());
49         int[] data = new int[bytes.length];
50         for (int i = 0; i < bytes.length; i++) {
51             data[i] = bytes[i] & 0xFF;
52         }
53         String decodedString = Utils.toHex(data);
54
55         assertEquals("t:01,0,Dx1U\r\n", commandStr);
56         assertEquals("0F1D54", decodedString);
57     }
58
59     @Test
60     public void addRoomTest() {
61         TCommand scmd = new TCommand("0f1d54", false);
62         scmd.addRoom("0b0da3");
63
64         String commandStr = scmd.getCommandString();
65
66         String base64Data = commandStr.split(",")[2];
67         byte[] bytes = Base64.getDecoder().decode(base64Data.trim().getBytes());
68         int[] data = new int[bytes.length];
69         for (int i = 0; i < bytes.length; i++) {
70             data[i] = bytes[i] & 0xFF;
71         }
72         String decodedString = Utils.toHex(data);
73
74         assertEquals("t:02,0,Cw2jDx1U\r\n", commandStr);
75         assertEquals("0B0DA30F1D54", decodedString);
76     }
77
78     @Test
79     public void forceModeTest() {
80         TCommand scmd = new TCommand("0f1d54", true);
81         String commandStr = scmd.getCommandString();
82
83         assertEquals("t:01,1,Dx1U\r\n", commandStr);
84     }
85 }