]> git.basschouten.com Git - openhab-addons.git/blob
1ee2e1b171e8f11ed792cefc0f230d64a0cb32a6
[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
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
22 import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.thing.ThingUID;
26
27 /**
28  * Helper class for testing the RFXCom-binding
29  *
30  * @author Martin van Wingerden - Initial contribution
31  */
32 @NonNullByDefault
33 class RFXComTestHelper {
34     static void basicBoundaryCheck(PacketType packetType, RFXComMessage message) throws RFXComException {
35         // This is a place where its easy to make mistakes in coding, and can result in errors, normally
36         // array bounds errors
37         byte[] binaryMessage = message.decodeMessage();
38         assertEquals("Wrong packet length", binaryMessage[0], binaryMessage.length - 1);
39         assertEquals("Wrong packet type", packetType.toByte(), binaryMessage[1]);
40     }
41
42     static void checkDiscoveryResult(RFXComDeviceMessage msg, String deviceId, @Nullable Integer pulse, String subType,
43             int offCommand, int onCommand) throws RFXComException {
44         String thingUID = "homeduino:rfxcom:fssfsd:thing";
45         DiscoveryResultBuilder builder = DiscoveryResultBuilder.create(new ThingUID(thingUID));
46
47         // check whether the pulse is stored
48         msg.addDevicePropertiesTo(builder);
49
50         Map<String, Object> properties = builder.build().getProperties();
51         assertEquals("Device Id", deviceId, properties.get("deviceId"));
52         assertEquals("Sub type", subType, properties.get("subType"));
53         if (pulse != null) {
54             assertEquals("Pulse", pulse, properties.get("pulse"));
55         }
56         assertEquals("On command", onCommand, properties.get("onCommandId"));
57         assertEquals("Off command", offCommand, properties.get("offCommandId"));
58     }
59
60     static int getActualIntValue(RFXComDeviceMessage msg, String channelId) throws RFXComException {
61         return ((DecimalType) msg.convertToState(channelId, new MockDeviceState())).intValue();
62     }
63 }