]> git.basschouten.com Git - openhab-addons.git/blob
88c2518aa6536b1e7e6c955a8a1be9c937ea1192
[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.miele.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import org.junit.jupiter.api.Test;
18 import org.openhab.core.library.types.QuantityType;
19 import org.openhab.core.library.unit.SIUnits;
20 import org.openhab.core.test.java.JavaTest;
21 import org.openhab.core.types.UnDefType;
22
23 /**
24  * This class provides test cases for {@link
25  * org.openhab.binding.miele.internal.ExtendedDeviceStateUtil}
26  *
27  * @author Jacob Laursen - Added power/water consumption channels
28  */
29
30 public class ExtendedDeviceStateUtilTest extends JavaTest {
31
32     @Test
33     public void bytesToHexWhenTopBitIsUsedReturnsCorrectString() {
34         String actual = ExtendedDeviceStateUtil
35                 .bytesToHex(new byte[] { (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef });
36         assertEquals("DEADBEEF", actual);
37     }
38
39     /**
40      * This test guards that the UTF-16 returned by the RPC-JSON API will be
41      * considered as a sequence of 8-bit characters and converted into bytes
42      * accordingly. Default behaviour of String.getBytes() assumes UTF-8
43      * and adds a 0xc2 byte before any character out of ASCII range.
44      */
45     @Test
46     public void stringToBytesWhenTopBitIsUsedReturnsSingleByte() {
47         byte[] expected = new byte[] { (byte) 0x00, (byte) 0x80, (byte) 0x00 };
48         byte[] actual = ExtendedDeviceStateUtil.stringToBytes("\u0000\u0080\u0000");
49         assertArrayEquals(expected, actual);
50     }
51
52     @Test
53     public void getTemperatureStateWellFormedValueReturnsQuantityType() throws NumberFormatException {
54         assertEquals(new QuantityType<>(42, SIUnits.CELSIUS), ExtendedDeviceStateUtil.getTemperatureState("42"));
55     }
56
57     @Test
58     public void getTemperatureStateMagicValueReturnsUndefined() throws NumberFormatException {
59         assertEquals(UnDefType.UNDEF, ExtendedDeviceStateUtil.getTemperatureState("32768"));
60     }
61
62     @Test
63     public void getTemperatureStateNonNumericValueThrowsNumberFormatException() {
64         assertThrows(NumberFormatException.class, () -> ExtendedDeviceStateUtil.getTemperatureState("A"));
65     }
66
67     @Test
68     public void getTemperatureStateNullValueThrowsNumberFormatException() {
69         assertThrows(NumberFormatException.class, () -> ExtendedDeviceStateUtil.getTemperatureState(null));
70     }
71 }