]> git.basschouten.com Git - openhab-addons.git/blob
0ce561f61051b81a330ac62ae7c8cae3e8a6a96e
[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.nibeheatpump.internal.handler;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16
17 import java.lang.reflect.InvocationTargetException;
18 import java.lang.reflect.Method;
19 import java.util.Arrays;
20 import java.util.Collection;
21
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.extension.ExtendWith;
24 import org.junit.jupiter.params.ParameterizedTest;
25 import org.junit.jupiter.params.provider.MethodSource;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.openhab.binding.nibeheatpump.internal.models.PumpModel;
29 import org.openhab.binding.nibeheatpump.internal.models.VariableInformation;
30 import org.openhab.core.io.transport.serial.SerialPortManager;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.StringType;
34 import org.openhab.core.types.Command;
35
36 /**
37  * Tests cases for {@link NibeHeatPumpHandler}.
38  *
39  * @author Jevgeni Kiski - Initial contribution
40  */
41 @ExtendWith(MockitoExtension.class)
42 public class NibeHeatPumpHandlerCommand2NibeTest {
43     private NibeHeatPumpHandler product; // the class under test
44     private Method m;
45     private static String METHOD_NAME = "convertCommandToNibeValue";
46     private Class<?>[] parameterTypes;
47     private Object[] parameters;
48
49     private @Mock SerialPortManager serialPortManager;
50
51     public static Collection<Object[]> data() {
52         return Arrays.asList(new Object[][] { { 47028, new DecimalType("-1"), (byte) 0xFF },
53                 { 48132, new DecimalType("0"), 0 }, { 48132, new StringType("0"), 0 },
54                 { 43009, new DecimalType("28.7"), 0x011F }, { 40004, new DecimalType("-0.1"), (short) 0xFFFF },
55                 { 47418, new DecimalType("75"), 0x004B }, { 43514, new DecimalType("7"), 0x0007 },
56                 { 47291, new DecimalType("65535"), 0xFFFF }, { 42437, new DecimalType("429496729.5"), 0xFFFFFFFF },
57                 { 42504, new DecimalType("4294967295"), 0xFFFFFFFF }, { 47041, new StringType("1"), 0x1 },
58                 { 47371, OnOffType.from(true), 0x1 }, { 47371, OnOffType.from(false), 0x0 }, });
59     }
60
61     @BeforeEach
62     public void setUp() throws Exception {
63         product = new NibeHeatPumpHandler(null, PumpModel.F1X55, serialPortManager);
64         parameterTypes = new Class[2];
65         parameterTypes[0] = VariableInformation.class;
66         parameterTypes[1] = Command.class;
67         m = product.getClass().getDeclaredMethod(METHOD_NAME, parameterTypes);
68         m.setAccessible(true);
69         parameters = new Object[2];
70     }
71
72     @ParameterizedTest
73     @MethodSource("data")
74     public void convertNibeValueToStateTest(final int coilAddress, final Command command, final int expected)
75             throws InvocationTargetException, IllegalAccessException {
76         VariableInformation varInfo = VariableInformation.getVariableInfo(PumpModel.F1X55, coilAddress);
77         parameters[0] = varInfo;
78         parameters[1] = command;
79         int value = (int) m.invoke(product, parameters);
80
81         assertEquals(expected, value);
82     }
83 }