]> git.basschouten.com Git - openhab-addons.git/blob
fec775df890a298e1a7caa236d9d00b2ca8c28a9
[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 import static org.openhab.binding.rfxcom.internal.messages.RFXComSecurity1Message.SubType.*;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.junit.Test;
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
23 import org.openhab.binding.rfxcom.internal.messages.RFXComSecurity1Message.Contact;
24 import org.openhab.binding.rfxcom.internal.messages.RFXComSecurity1Message.Motion;
25 import org.openhab.binding.rfxcom.internal.messages.RFXComSecurity1Message.Status;
26 import org.openhab.binding.rfxcom.internal.messages.RFXComSecurity1Message.SubType;
27 import org.openhab.core.util.HexUtils;
28
29 /**
30  * Test for RFXCom-binding
31  *
32  * @author Martin van Wingerden - Initial contribution
33  */
34 @NonNullByDefault
35 public class RFXComSecurity1MessageTest {
36     private void testSomeMessages(String hexMessage, @Nullable SubType subType, int sequenceNumber,
37             @Nullable String deviceId, int batteryLevel, @Nullable Contact contact, @Nullable Motion motion,
38             @Nullable Status status, int signalLevel) throws RFXComException {
39         byte[] message = HexUtils.hexToBytes(hexMessage);
40         RFXComSecurity1Message msg = (RFXComSecurity1Message) RFXComMessageFactory.createMessage(message);
41         assertEquals("SubType", subType, msg.subType);
42         assertEquals("Seq Number", sequenceNumber, (short) (msg.seqNbr & 0xFF));
43         assertEquals("Sensor Id", deviceId, msg.getDeviceId());
44         assertEquals("Battery level", batteryLevel, msg.batteryLevel);
45         assertEquals("Contact", contact, msg.contact);
46         assertEquals("Motion", motion, msg.motion);
47         assertEquals("Status", status, msg.status);
48         assertEquals("Signal Level", signalLevel, msg.signalLevel);
49
50         byte[] decoded = msg.decodeMessage();
51
52         assertEquals("Message converted back", hexMessage, HexUtils.bytesToHex(decoded));
53     }
54
55     @Test
56     public void testX10SecurityMessage() throws RFXComException {
57         testSomeMessages("0820004DD3DC540089", X10_SECURITY, 77, "13884500", 8, Contact.NORMAL, Motion.UNKNOWN,
58                 Status.NORMAL, 9);
59     }
60
61     @Test
62     public void testRM174RFSecurityMessage() throws RFXComException {
63         testSomeMessages("08200A0E8000200650", RM174RF, 14, "8388640", 5, Contact.UNKNOWN, Motion.UNKNOWN, Status.PANIC,
64                 0);
65         testSomeMessages("08200A081450450650", RM174RF, 8, "1331269", 5, Contact.UNKNOWN, Motion.UNKNOWN, Status.PANIC,
66                 0);
67     }
68
69     @Test(expected = RFXComUnsupportedValueException.class)
70     public void testSomeInvalidSecurityMessage() throws RFXComException {
71         testSomeMessages("08FF0A1F0000000650", null, 0, null, 0, null, null, null, 0);
72     }
73 }