]> git.basschouten.com Git - openhab-addons.git/blob
28e6fb2db646599b8b57918b30842d9744859da3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.Assert.assertEquals;
16 import static org.mockito.MockitoAnnotations.initMocks;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.util.Arrays;
21 import java.util.Collection;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.Parameterized;
27 import org.mockito.Mock;
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 @RunWith(Parameterized.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 int fCoilAddress;
50     private Command fCommand;
51     private int fExpected;
52
53     private @Mock SerialPortManager serialPortManager;
54
55     @Parameterized.Parameters(name = "{index}: f({0}, {1})={2}")
56     public static Collection<Object[]> data() {
57         return Arrays.asList(new Object[][] { { 47028, new DecimalType("-1"), (byte) 0xFF },
58                 { 48132, new DecimalType("0"), 0 }, { 48132, new StringType("0"), 0 },
59                 { 43009, new DecimalType("28.7"), 0x011F }, { 40004, new DecimalType("-0.1"), (short) 0xFFFF },
60                 { 47418, new DecimalType("75"), 0x004B }, { 43514, new DecimalType("7"), 0x0007 },
61                 { 47291, new DecimalType("65535"), 0xFFFF }, { 42437, new DecimalType("429496729.5"), 0xFFFFFFFF },
62                 { 42504, new DecimalType("4294967295"), 0xFFFFFFFF }, { 47041, new StringType("1"), 0x1 },
63                 { 47371, OnOffType.from(true), 0x1 }, { 47371, OnOffType.from(false), 0x0 }, });
64     }
65
66     public NibeHeatPumpHandlerCommand2NibeTest(final int coilAddress, final Command command, final int expected) {
67         this.fCoilAddress = coilAddress;
68         this.fCommand = command;
69         this.fExpected = expected;
70     }
71
72     @Before
73     public void setUp() throws Exception {
74         initMocks(this);
75
76         product = new NibeHeatPumpHandler(null, PumpModel.F1X55, serialPortManager);
77         parameterTypes = new Class[2];
78         parameterTypes[0] = VariableInformation.class;
79         parameterTypes[1] = Command.class;
80         m = product.getClass().getDeclaredMethod(METHOD_NAME, parameterTypes);
81         m.setAccessible(true);
82         parameters = new Object[2];
83     }
84
85     @Test
86     public void convertNibeValueToStateTest() throws InvocationTargetException, IllegalAccessException {
87         VariableInformation varInfo = VariableInformation.getVariableInfo(PumpModel.F1X55, fCoilAddress);
88         parameters[0] = varInfo;
89         parameters[1] = fCommand;
90         int value = (int) m.invoke(product, parameters);
91
92         assertEquals(fExpected, value);
93     }
94 }