]> git.basschouten.com Git - openhab-addons.git/blob
4cc24f8315b56e51a4a0a81a14e4364271ecc4f5
[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 org.junit.jupiter.api.Test;
18 import org.openhab.binding.paradoxalarm.internal.communication.messages.CommandPayload;
19 import org.openhab.binding.paradoxalarm.internal.communication.messages.PartitionCommand;
20 import org.openhab.binding.paradoxalarm.internal.util.ParadoxUtil;
21
22 /**
23  * The {@link TestCreateCommandPayload} This test tests the proper build of command payload object for partition entity
24  * with all types of commands.
25  *
26  * @author Konstantin Polihronov - Initial contribution
27  */
28 public class TestCreateCommandPayload {
29
30     @Test
31     public void testCreatePayload() {
32         for (PartitionCommand command : PartitionCommand.values()) {
33             for (int partitionNumber = 1; partitionNumber <= 8; partitionNumber++) {
34                 CommandPayload payload = new CommandPayload(partitionNumber, command);
35                 assertNibble(partitionNumber, command, payload);
36             }
37         }
38     }
39
40     private void assertNibble(int partitionNumber, PartitionCommand command, CommandPayload payload) {
41         byte[] bytes = payload.getBytes();
42         int payloadIndexOfByteToCheck = 6 + (partitionNumber - 1) / 2;
43         byte byteValue = bytes[payloadIndexOfByteToCheck];
44         if ((partitionNumber - 1) % 2 == 0) {
45             assertTrue(ParadoxUtil.getHighNibble(byteValue) == command.getCommand());
46         } else {
47             assertTrue(ParadoxUtil.getLowNibble(byteValue) == command.getCommand());
48         }
49     }
50 }