]> git.basschouten.com Git - openhab-addons.git/blob
53f567e6da4faa74c060a2bb48985e771a479a69
[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.senechome.internal;
14
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
17
18 import org.eclipse.jetty.client.HttpClient;
19 import org.junit.jupiter.api.Assertions;
20 import org.junit.jupiter.api.extension.ExtendWith;
21 import org.junit.jupiter.params.ParameterizedTest;
22 import org.junit.jupiter.params.provider.MethodSource;
23 import org.mockito.Mock;
24 import org.mockito.junit.jupiter.MockitoExtension;
25 import org.openhab.core.thing.Thing;
26
27 /**
28  * Test for senec value parsing. All test data are from "original" senec (using vars.html).
29  *
30  * @author Erwin Guib - Initial Contribution
31  */
32 @ExtendWith(MockitoExtension.class)
33 class SenecHomeHandlerTest {
34
35     protected static Object[][] data() {
36         return new Object[][] {
37                 // unsigned
38                 { "u1_0002", BigDecimal.valueOf(2) }, //
39                 { "u1_07DB", BigDecimal.valueOf(2011) }, //
40                 { "u3_0000194C", BigDecimal.valueOf(6476) }, //
41                 { "u3_817E00E0", BigDecimal.valueOf(2172518624L) }, //
42                 { "u6_0000000000000001", BigDecimal.valueOf(1) }, //
43                 { "u6_00000000000C75D9", BigDecimal.valueOf(816601) }, //
44                 { "u8_64", BigDecimal.valueOf(100) }, //
45                 // int
46                 { "i1_00FA", BigDecimal.valueOf(250) }, //
47                 { "i3_00000078", BigDecimal.valueOf(120) }, //
48                 { "i3_609F8480", BigDecimal.valueOf(1621066880) }, //
49                 { "i3_FFFFFFFF", BigDecimal.valueOf(-1) }, //
50                 { "i8_18", BigDecimal.valueOf(24) }, //
51                 // string (unknown)
52                 { "st_HMI: 3.15.32 PU: 4.1.89", BigDecimal.valueOf(0) } };
53     }
54
55     protected static Object[][] floatData() {
56         return new Object[][] {
57                 // float
58                 { "fl_41C80000", BigDecimal.valueOf(25), 0 }, //
59                 { "fl_4247632F", BigDecimal.valueOf(49.85), 2 }, //
60                 { "fl_C5AB6F0B", BigDecimal.valueOf(-5485.88), 2 }, //
61                 { "fl_4248CCCD", BigDecimal.valueOf(50.2), 1 }, //
62         };
63     }
64
65     @Mock
66     Thing mockThing;
67
68     @Mock
69     HttpClient mockHttpClient;
70
71     SenecHomeHandler cut = new SenecHomeHandler(mockThing, mockHttpClient);
72
73     @ParameterizedTest
74     @MethodSource("data")
75     void getSenecValue(String value, Object expectedResult) {
76         Assertions.assertEquals(expectedResult, cut.getSenecValue(value));
77     }
78
79     @ParameterizedTest
80     @MethodSource("floatData")
81     void getSenecValueFloat(String value, Object expectedResult, int scale) {
82         Assertions.assertEquals(expectedResult, cut.getSenecValue(value).setScale(scale, RoundingMode.HALF_UP));
83     }
84 }