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