2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nibeheatpump.internal.handler;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import java.lang.reflect.InvocationTargetException;
18 import java.lang.reflect.Method;
19 import java.text.DecimalFormatSymbols;
20 import java.util.Arrays;
21 import java.util.Collection;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.junit.jupiter.params.ParameterizedTest;
26 import org.junit.jupiter.params.provider.MethodSource;
27 import org.mockito.Mock;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.openhab.binding.nibeheatpump.internal.models.PumpModel;
30 import org.openhab.binding.nibeheatpump.internal.models.VariableInformation;
31 import org.openhab.core.io.transport.serial.SerialPortManager;
32 import org.openhab.core.types.State;
35 * Tests cases for {@link NibeHeatPumpHandler}.
37 * @author Jevgeni Kiski - Initial contribution
39 @ExtendWith(MockitoExtension.class)
40 public class NibeHeatPumpHandlerNibe2StateTest {
42 // we need to get the decimal separator of the default locale for our tests
43 private static final char SEP = new DecimalFormatSymbols().getDecimalSeparator();
44 private static final String METHOD_NAME = "convertNibeValueToState";
46 private NibeHeatPumpHandler product; // the class under test
48 private Class<?>[] parameterTypes;
49 private Object[] parameters;
51 private @Mock SerialPortManager serialPortManager;
53 public static Collection<Object[]> data() {
54 return Arrays.asList(new Object[][] { //
55 { 47028, 0xFF, "%d", "Number", "-1" }, //
56 { 47028, 0x7F, "%d", "Number", "127" }, //
57 { 47028, 0x80, "%d", "Number", "-128" }, //
58 { 48132, 1966080, "%s", "String", "0" }, //
59 { 47028, 0xFF, "%d", "Number", "-1" }, //
60 { 43009, 0x011F, "%.1f", "Number", "28" + SEP + "7" }, //
61 { 43009, 0x7FFF, "%.1f", "Number", "3276" + SEP + "7" }, //
62 { 43009, 0x8000, "%.1f", "Number", "-3276" + SEP + "8" }, //
63 { 40004, 0xFFFF, "%.1f", "Number", "-0" + SEP + "1" }, //
64 { 40004, 0xFFFF, "%.1f", "Number", "-0" + SEP + "1" }, //
65 { 43416, 0xFFFFFFFF, "%d", "Number", "4294967295" }, //
66 { 47418, 0x004B, "%d", "Number", "75" }, //
67 { 43514, 0x0007, "%d", "Number", "7" }, //
68 { 47291, 0xFFFF, "%d", "Number", "65535" }, //
69 { 42437, 0xFFFFFFFF, "%.1f", "Number", "429496729" + SEP + "5" }, //
70 { 42504, 0xFFFFFFFF, "%d", "Number", "4294967295" }, //
71 { 43081, 196, "%.1f", "Number", "19" + SEP + "6" }, //
72 { 43424, 1685, "%d", "Number", "1685" }, //
73 { 43416, 4857, "%d", "Number", "4857" }, //
74 { 43420, 9487, "%d", "Number", "9487" }, //
75 { 40940, (byte) 0xFF, "%.1f", "Number", "-0" + SEP + "1" }, //
76 { 40940, 0x80000000, "%.1f", "Number", "-214748364" + SEP + "8" }, //
77 { 40940, 0x7FFFFFFF, "%.1f", "Number", "214748364" + SEP + "7" }, //
78 { 40940, (short) 0xFFFF, "%.1f", "Number", "-0" + SEP + "1" }, //
79 { 40940, (byte) 0xFF, "%.1f", "Number", "-0" + SEP + "1" }, //
80 { 40940, 0xFFFF, "%.1f", "Number", "6553" + SEP + "5" } //
85 public void setUp() throws Exception {
86 product = new NibeHeatPumpHandler(null, PumpModel.F1X55, serialPortManager);
87 parameterTypes = new Class[3];
88 parameterTypes[0] = VariableInformation.class;
89 parameterTypes[1] = int.class;
90 parameterTypes[2] = String.class;
91 m = product.getClass().getDeclaredMethod(METHOD_NAME, parameterTypes);
92 m.setAccessible(true);
93 parameters = new Object[3];
98 public void convertNibeValueToStateTest(final int coilAddress, final int value, final String format,
99 final String type, final String expected) throws InvocationTargetException, IllegalAccessException {
100 VariableInformation varInfo = VariableInformation.getVariableInfo(PumpModel.F1X55, coilAddress);
101 parameters[0] = varInfo;
102 parameters[1] = value;
103 parameters[2] = type;
104 State state = (State) m.invoke(product, parameters);
106 assertEquals(expected, state.format(format));