]> git.basschouten.com Git - openhab-addons.git/blob
64aa2bf72003c882f955d02377116a00cb43f8a0
[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.assertEquals;
16 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
17 import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType.LIGHTING5;
18 import static org.openhab.binding.rfxcom.internal.messages.RFXComLighting5Message.Commands.*;
19 import static org.openhab.binding.rfxcom.internal.messages.RFXComLighting5Message.SubType.IT;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
24 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.StringType;
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 RFXComLighting5MessageTest {
36     private final MockDeviceState deviceState = new MockDeviceState();
37
38     @Test
39     public void convertFromStateItMessage() throws RFXComException {
40         RFXComDeviceMessage itMessageObject = (RFXComDeviceMessage) RFXComMessageFactoryImpl.INSTANCE
41                 .createMessage(LIGHTING5);
42         itMessageObject.setDeviceId("2061.1");
43         itMessageObject.setSubType(IT);
44         itMessageObject.convertFromState(CHANNEL_COMMAND, OnOffType.ON);
45         byte[] message = itMessageObject.decodeMessage();
46         String hexMessage = HexUtils.bytesToHex(message);
47         assertEquals("0A140F0000080D01010000", hexMessage, "Message is not as expected");
48         RFXComLighting5Message msg = (RFXComLighting5Message) RFXComMessageFactoryImpl.INSTANCE.createMessage(message);
49         assertEquals(IT, msg.subType, "SubType");
50         assertEquals("2061.1", msg.getDeviceId(), "Sensor Id");
51         assertEquals(ON, msg.command, "Command");
52     }
53
54     @Test
55     public void basicBoundaryCheck() throws RFXComException {
56         RFXComLighting5Message message = (RFXComLighting5Message) RFXComMessageFactoryImpl.INSTANCE
57                 .createMessage(LIGHTING5);
58
59         message.subType = RFXComLighting5Message.SubType.LIGHTWAVERF;
60         message.command = ON;
61
62         RFXComTestHelper.basicBoundaryCheck(LIGHTING5, message);
63     }
64
65     // TODO please add more tests for different messages
66
67     @Test
68     public void testStringCommandOpenRelay() throws RFXComUnsupportedChannelException {
69         RFXComLighting5Message msg = new RFXComLighting5Message();
70
71         msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("OPEN_RELAY"));
72         assertEquals(StringType.valueOf("OPEN_RELAY"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
73         assertEquals(OPEN_RELAY, msg.command);
74     }
75
76     @Test
77     public void testStringCommandCloseRelay() throws RFXComUnsupportedChannelException {
78         RFXComLighting5Message msg = new RFXComLighting5Message();
79
80         msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("CLOSE_RELAY"));
81         assertEquals(StringType.valueOf("CLOSE_RELAY"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
82         assertEquals(CLOSE_RELAY, msg.command);
83     }
84
85     @Test
86     public void testStringCommandStopRelay() throws RFXComUnsupportedChannelException {
87         RFXComLighting5Message msg = new RFXComLighting5Message();
88
89         msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("STOP_RELAY"));
90         assertEquals(StringType.valueOf("STOP_RELAY"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
91         assertEquals(STOP_RELAY, msg.command);
92     }
93 }