]> git.basschouten.com Git - openhab-addons.git/blob
a009ed38a71ad0eff0e5251037cb691e2bf9bc90
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
18
19 import org.junit.jupiter.api.Test;
20 import org.openhab.core.library.types.QuantityType;
21 import org.openhab.core.library.unit.SIUnits;
22 import org.openhab.core.test.java.JavaTest;
23 import org.openhab.core.types.UnDefType;
24
25 /**
26  * This class provides test cases for {@link
27  * org.openhab.binding.miele.internal.DeviceUtil}
28  *
29  * @author Jacob Laursen - Initial contribution
30  */
31
32 public class DeviceUtilTest extends JavaTest {
33
34     @Test
35     public void bytesToHexWhenTopBitIsUsedReturnsCorrectString() {
36         String actual = DeviceUtil.bytesToHex(new byte[] { (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef });
37         assertEquals("DEADBEEF", actual);
38     }
39
40     /**
41      * This test guards that the UTF-16 returned by the RPC-JSON API will be
42      * considered as a sequence of 8-bit characters and converted into bytes
43      * accordingly. Default behaviour of String.getBytes() assumes UTF-8
44      * and adds a 0xc2 byte before any character out of ASCII range.
45      */
46     @Test
47     public void stringToBytesWhenTopBitIsUsedReturnsSingleByte() {
48         byte[] expected = new byte[] { (byte) 0x00, (byte) 0x80, (byte) 0x00 };
49         byte[] actual = DeviceUtil.stringToBytes("\u0000\u0080\u0000");
50         assertArrayEquals(expected, actual);
51     }
52
53     @Test
54     public void getTemperatureStateWellFormedValueReturnsQuantityType() throws NumberFormatException {
55         assertEquals(new QuantityType<>(42, SIUnits.CELSIUS), DeviceUtil.getTemperatureState("42"));
56     }
57
58     @Test
59     public void getTemperatureStateMagicValueReturnsUndefined() throws NumberFormatException {
60         assertEquals(UnDefType.UNDEF, DeviceUtil.getTemperatureState("32768"));
61     }
62
63     @Test
64     public void getTemperatureStateColdValueReturns10Degrees() throws NumberFormatException {
65         assertEquals(new QuantityType<>(10, SIUnits.CELSIUS), DeviceUtil.getTemperatureState("-32760"));
66     }
67
68     @Test
69     public void getTemperatureStateNonNumericValueThrowsNumberFormatException() {
70         assertThrows(NumberFormatException.class, () -> DeviceUtil.getTemperatureState("A"));
71     }
72
73     @Test
74     public void getTemperatureStateNullValueThrowsNumberFormatException() {
75         assertThrows(NumberFormatException.class, () -> DeviceUtil.getTemperatureState(null));
76     }
77
78     @Test
79     public void getStateTextStateProviderHasPrecedence() {
80         assertEquals("I brug", this.getStateTextState("5", "Running", "miele.state.running", "I brug"));
81     }
82
83     @Test
84     public void getStateTextStateGatewayTextIsReturnedWhenKeyIsUnknown() {
85         assertEquals("Running", this.getStateTextState("-1", "Running", "key.unknown", "I brug"));
86     }
87
88     @Test
89     public void getStateTextStateKeyIsReturnedWhenUnknownByGatewayAndProvider() {
90         assertEquals("state.99", this.getStateTextState("99", null, "key.unknown", "I brug"));
91     }
92
93     private String getStateTextState(String value, String localizedValue, String mockedKey, String mockedValue) {
94         var metaData = new DeviceMetaData();
95         metaData.LocalizedValue = localizedValue;
96         var translationProvider = mock(MieleTranslationProvider.class);
97         when(translationProvider.getText(mockedKey, metaData.LocalizedValue)).thenReturn(mockedValue);
98
99         return DeviceUtil.getStateTextState(value, metaData, translationProvider).toString();
100     }
101 }