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