]> git.basschouten.com Git - openhab-addons.git/blob
5d571a17ec5459dd99a2042abc3f38ee65d82d55
[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.paradoxalarm.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.util.Arrays;
18
19 import org.junit.jupiter.api.Test;
20 import org.openhab.binding.paradoxalarm.internal.communication.messages.CommandPayload;
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.PartitionCommand;
26 import org.openhab.binding.paradoxalarm.internal.exceptions.ParadoxException;
27 import org.openhab.binding.paradoxalarm.internal.util.ParadoxUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link TestGetBytes} This test tests creation of IP packet and it's getBytes() method
33  *
34  * @author Konstantin Polihronov - Initial contribution
35  */
36 public class TestGetBytes {
37
38     private static final int PARTITION_NUMBER = 1;
39
40     static {
41         System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "TRACE");
42     }
43
44     private static final Logger logger = LoggerFactory.getLogger(ParadoxUtil.class);
45
46     private static final byte[] EXPECTED1 = { (byte) 0xAA, 0x0A, 0x00, 0x03, 0x08, (byte) 0xF0, 0x00, 0x00, 0x01,
47             (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, 0x01, 0x02, 0x03,
48             0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
49
50     private static final byte[] PAYLOAD = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
51
52     @Test
53     public void testParadoxIPPacket() {
54         ParadoxIPPacket paradoxIPPacket = new ParadoxIPPacket(PAYLOAD, false)
55                 .setCommand(HeaderCommand.CONNECT_TO_IP_MODULE);
56         final byte[] packetBytes = paradoxIPPacket.getBytes();
57
58         ParadoxUtil.printByteArray("Expected =", EXPECTED1);
59         ParadoxUtil.printByteArray("Packet   =", packetBytes);
60
61         assertTrue(Arrays.equals(packetBytes, EXPECTED1));
62     }
63
64     private static final byte[] EXPECTED_COMMAND_PAYLOAD = { 0x40, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
65             0x00, 0x00, 0x00, 0x00, 0x00 };
66
67     @Test
68     public void testCommandPayload() {
69         CommandPayload payload = new CommandPayload(PARTITION_NUMBER, PartitionCommand.ARM);
70         final byte[] packetBytes = payload.getBytes();
71
72         ParadoxUtil.printByteArray("Expected =", EXPECTED_COMMAND_PAYLOAD);
73         ParadoxUtil.printByteArray("Result   =", packetBytes);
74
75         assertTrue(Arrays.equals(packetBytes, EXPECTED_COMMAND_PAYLOAD));
76     }
77
78     private static final byte[] EXPECTED_MEMORY_PAYLOAD = { (byte) 0xAA, 0x0A, 0x00, 0x03, 0x08, (byte) 0xF0, 0x00,
79             0x00, 0x01, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, 0x01,
80             0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
81
82     @Test
83     public void testMemoryRequestPayload() throws ParadoxException {
84         int address = 0x3A6B + (PARTITION_NUMBER) * 107;
85         byte labelLength = 16;
86         IPayload payload = new EpromRequestPayload(address, labelLength);
87         byte[] bytes = payload.getBytes();
88         ParadoxUtil.printByteArray("Expected =", EXPECTED_MEMORY_PAYLOAD);
89         ParadoxUtil.printByteArray("Result   =", bytes);
90     }
91 }