2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.rfxcom.internal.messages;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.nio.ByteBuffer;
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;
27 * Test for RFXCom-binding
29 * @author James Hewitt-Thomas - New addition to the PRO RFXCom firmware
32 public class RFXComRawMessageTest {
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");
45 byte[] decoded = msg.decodeMessage();
47 assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Message converted back");
51 public void testSomeMessages() throws RFXComException {
52 testMessage("087F0027051356ECC0", RFXComRawMessage.SubType.RAW_PACKET1, 0x27, 5, "1356ECC0");
56 public void testLongMessage() throws RFXComException {
57 RFXComRawMessage msg = (RFXComRawMessage) RFXComMessageFactory.createMessage(PacketType.RAW);
58 msg.subType = RFXComRawMessage.SubType.RAW_PACKET1;
61 msg.pulses = new short[125];
63 assertThrows(RFXComMessageTooLongException.class, () -> msg.decodeMessage());