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