]> git.basschouten.com Git - openhab-addons.git/blob
4661d12864da42a516eb7a1dc4bba866a5d0c671
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.rfxcom.internal.messages;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.junit.jupiter.api.Test;
19 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
20 import org.openhab.binding.rfxcom.internal.exceptions.RFXComMessageTooLongException;
21 import org.openhab.core.util.HexUtils;
22
23 /**
24  * Test for RFXCom-binding
25  *
26  * @author Martin van Wingerden - Initial contribution
27  * @author James Hewitt-Thomas - Added first actual tests
28  */
29 @NonNullByDefault
30 public class RFXComUndecodedRFMessageTest {
31
32     private void testMessage(String hexMsg, RFXComUndecodedRFMessage.SubType subType, int seqNbr, String rawPayload)
33             throws RFXComException {
34         final RFXComUndecodedRFMessage msg = (RFXComUndecodedRFMessage) RFXComMessageFactoryImpl.INSTANCE
35                 .createMessage(HexUtils.hexToBytes(hexMsg));
36         assertEquals(subType, msg.subType, "SubType");
37         assertEquals(seqNbr, (short) (msg.seqNbr & 0xFF), "Seq Number");
38         assertEquals("UNDECODED", msg.getDeviceId(), "Device Id");
39         assertEquals(rawPayload, HexUtils.bytesToHex(msg.rawPayload), "Payload");
40
41         byte[] decoded = msg.decodeMessage();
42
43         assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Message converted back");
44     }
45
46     @Test
47     public void testSomeMessages() throws RFXComException {
48         testMessage("070301271356ECC0", RFXComUndecodedRFMessage.SubType.ARC, 0x27, "1356ECC0");
49     }
50
51     @Test
52     public void testLongMessage() throws RFXComException {
53         assertThrows(RFXComMessageTooLongException.class,
54                 () -> testMessage("25030101000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021",
55                         RFXComUndecodedRFMessage.SubType.ARC, 0x01,
56                         "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021"));
57     }
58 }