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