]> git.basschouten.com Git - openhab-addons.git/blob
3ccf9b1c32811c290f968af65bc0b24850fedc97
[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.paradoxalarm.internal;
14
15 import static org.junit.jupiter.api.Assertions.assertTrue;
16
17 import java.util.Arrays;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.paradoxalarm.internal.communication.messages.EpromRequestPayload;
22 import org.openhab.binding.paradoxalarm.internal.communication.messages.HeaderCommand;
23 import org.openhab.binding.paradoxalarm.internal.communication.messages.IPayload;
24 import org.openhab.binding.paradoxalarm.internal.communication.messages.ParadoxIPPacket;
25 import org.openhab.binding.paradoxalarm.internal.communication.messages.partition.PartitionCommand;
26 import org.openhab.binding.paradoxalarm.internal.communication.messages.partition.PartitionCommandPayload;
27 import org.openhab.binding.paradoxalarm.internal.communication.messages.zone.ZoneCommand;
28 import org.openhab.binding.paradoxalarm.internal.communication.messages.zone.ZoneCommandPayload;
29 import org.openhab.binding.paradoxalarm.internal.exceptions.ParadoxException;
30 import org.openhab.binding.paradoxalarm.internal.util.ParadoxUtil;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link TestGetBytes} This test tests creation of IP packet and it's getBytes() method
36  *
37  * @author Konstantin Polihronov - Initial contribution
38  */
39 @NonNullByDefault
40 public class TestGetBytes {
41
42     private static final int PARTITION_NUMBER = 1;
43
44     static {
45         System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "TRACE");
46     }
47
48     private static final Logger logger = LoggerFactory.getLogger(ParadoxUtil.class);
49
50     private static final byte[] EXPECTED1 = { (byte) 0xAA, 0x0A, 0x00, 0x03, 0x08, (byte) 0xF0, 0x00, 0x00, 0x01,
51             (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, 0x01, 0x02, 0x03,
52             0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
53
54     private static final byte[] PAYLOAD = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
55
56     @Test
57     public void testParadoxIPPacket() {
58         ParadoxIPPacket paradoxIPPacket = new ParadoxIPPacket(PAYLOAD, false)
59                 .setCommand(HeaderCommand.CONNECT_TO_IP_MODULE);
60         final byte[] packetBytes = paradoxIPPacket.getBytes();
61
62         ParadoxUtil.printByteArray("Expected =", EXPECTED1);
63         ParadoxUtil.printByteArray("Packet   =", packetBytes);
64
65         assertTrue(Arrays.equals(packetBytes, EXPECTED1));
66     }
67
68     private static final byte[] EXPECTED_PARTITION_COMMAND_PAYLOAD = { 0x40, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00,
69             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
70
71     @Test
72     public void testPartitionCommandPayload() {
73         PartitionCommandPayload payload = new PartitionCommandPayload(PARTITION_NUMBER, PartitionCommand.ARM);
74         final byte[] packetBytes = payload.getBytes();
75
76         ParadoxUtil.printByteArray("Expected =", EXPECTED_PARTITION_COMMAND_PAYLOAD);
77         ParadoxUtil.printByteArray("Result   =", packetBytes);
78
79         assertTrue(Arrays.equals(packetBytes, EXPECTED_PARTITION_COMMAND_PAYLOAD));
80     }
81
82     private static final byte[] EXPECTED_MEMORY_PAYLOAD = { (byte) 0xAA, 0x0A, 0x00, 0x03, 0x08, (byte) 0xF0, 0x00,
83             0x00, 0x01, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, 0x01,
84             0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
85
86     @Test
87     public void testMemoryRequestPayload() throws ParadoxException {
88         int address = 0x3A6B + (PARTITION_NUMBER) * 107;
89         byte labelLength = 16;
90         IPayload payload = new EpromRequestPayload(address, labelLength);
91         byte[] bytes = payload.getBytes();
92         ParadoxUtil.printByteArray("Expected =", EXPECTED_MEMORY_PAYLOAD);
93         ParadoxUtil.printByteArray("Result   =", bytes);
94     }
95
96     private static final byte[] EXPECTED_ZONE_5_BYPASS_COMMAND = { (byte) 0xd0, 0x1f, 0x08, 0x08, 0x00, 0x00, 0x10,
97             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98             0x00, 0x00, 0x00, 0x00, 0x00, 0x0f };
99     private static final byte[] EXPECTED_ZONE_5_CLEAR_BYPASS_COMMAND = { (byte) 0xd0, 0x1f, 0x08, 0x00, 0x00, 0x00,
100             0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07 };
102     private static final byte[] EXPECTED_ZONE_20_BYPASS_COMMAND = { (byte) 0xd0, 0x1f, 0x08, 0x08, 0x00, 0x00, 0x00,
103             0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104             0x00, 0x00, 0x00, 0x00, 0x00, 0x07 };
105     private static final byte[] EXPECTED_ZONE_23_BYPASS_COMMAND = { (byte) 0xd0, 0x1f, 0x08, 0x08, 0x00, 0x00, 0x00,
106             0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
107             0x00, 0x00, 0x00, 0x00, 0x00, 0x3f };
108     private static final byte[] EXPECTED_ZONE_23_CLEAR_BYPASS_COMMAND = { (byte) 0xd0, 0x1f, 0x08, 0x00, 0x00, 0x00,
109             0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
110             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x37 };
111
112     @Test
113     public void testZoneCommandPayload() {
114         testZoneCommandPayload(5, ZoneCommand.BYPASS, EXPECTED_ZONE_5_BYPASS_COMMAND);
115         testZoneCommandPayload(5, ZoneCommand.CLEAR_BYPASS, EXPECTED_ZONE_5_CLEAR_BYPASS_COMMAND);
116         testZoneCommandPayload(20, ZoneCommand.BYPASS, EXPECTED_ZONE_20_BYPASS_COMMAND);
117         testZoneCommandPayload(23, ZoneCommand.BYPASS, EXPECTED_ZONE_23_BYPASS_COMMAND);
118         testZoneCommandPayload(23, ZoneCommand.CLEAR_BYPASS, EXPECTED_ZONE_23_CLEAR_BYPASS_COMMAND);
119     }
120
121     private void testZoneCommandPayload(int zoneNumber, ZoneCommand command, byte[] expected) {
122         ZoneCommandPayload payload = new ZoneCommandPayload(zoneNumber, command);
123         final byte[] packetBytes = payload.getBytes();
124
125         ParadoxUtil.printByteArray("Expected " + zoneNumber + " =", expected);
126         ParadoxUtil.printByteArray("Result   " + zoneNumber + " =", packetBytes);
127
128         assertTrue(Arrays.equals(packetBytes, expected));
129     }
130 }