]> git.basschouten.com Git - openhab-addons.git/blob
f3baebb59bda8799223201a24fd8ce7678e33fa2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 java.nio.ByteBuffer;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComMessageTooLongException;
23 import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType;
24 import org.openhab.core.util.HexUtils;
25
26 /**
27  * Test for RFXCom-binding
28  *
29  * @author James Hewitt-Thomas - New addition to the PRO RFXCom firmware
30  */
31 @NonNullByDefault
32 public class RFXComRawMessageTest {
33
34     private void testMessage(String hexMsg, RFXComRawMessage.SubType subType, int seqNbr, int repeat, String pulses)
35             throws RFXComException {
36         final RFXComRawMessage msg = (RFXComRawMessage) RFXComMessageFactory.createMessage(HexUtils.hexToBytes(hexMsg));
37         assertEquals(subType, msg.subType, "SubType");
38         assertEquals(seqNbr, (short) (msg.seqNbr & 0xFF), "Seq Number");
39         assertEquals("RAW", msg.getDeviceId(), "Device Id");
40         assertEquals(repeat, msg.repeat, "Repeat");
41         byte[] payload = new byte[msg.pulses.length * 2];
42         ByteBuffer.wrap(payload).asShortBuffer().put(msg.pulses);
43         assertEquals(pulses, HexUtils.bytesToHex(payload), "Pulses");
44
45         byte[] decoded = msg.decodeMessage();
46
47         assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Message converted back");
48     }
49
50     @Test
51     public void testSomeMessages() throws RFXComException {
52         testMessage("087F0027051356ECC0", RFXComRawMessage.SubType.RAW_PACKET1, 0x27, 5, "1356ECC0");
53     }
54
55     @Test
56     public void testLongMessage() throws RFXComException {
57         RFXComRawMessage msg = (RFXComRawMessage) RFXComMessageFactory.createMessage(PacketType.RAW);
58         msg.subType = RFXComRawMessage.SubType.RAW_PACKET1;
59         msg.seqNbr = 1;
60         msg.repeat = 5;
61         msg.pulses = new short[125];
62
63         assertThrows(RFXComMessageTooLongException.class, () -> msg.decodeMessage());
64     }
65 }