]> git.basschouten.com Git - openhab-addons.git/blob
b3536841d7505ba54e67bb22d809c98191d9280c
[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.text.DecimalFormatSymbols;
21 import java.util.Arrays;
22 import java.util.Collection;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.Parameterized;
28 import org.mockito.Mock;
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;
33
34 /**
35  * Tests cases for {@link NibeHeatPumpHandler}.
36  *
37  * @author Jevgeni Kiski - Initial contribution
38  */
39 @RunWith(Parameterized.class)
40 public class NibeHeatPumpHandlerNibe2StateTest {
41
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";
45
46     private NibeHeatPumpHandler product; // the class under test
47     private Method m;
48     private Class<?>[] parameterTypes;
49     private Object[] parameters;
50     private int fCoilAddress;
51     private int fValue;
52     private String fFormat;
53     private String fType;
54     private String fExpected;
55
56     private @Mock SerialPortManager serialPortManager;
57
58     @Parameterized.Parameters(name = "{index}: f({0}, {1}, {3})={4}")
59     public static Collection<Object[]> data() {
60         return Arrays.asList(new Object[][] { //
61                 { 47028, 0xFF, "%d", "Number", "-1" }, //
62                 { 47028, 0x7F, "%d", "Number", "127" }, //
63                 { 47028, 0x80, "%d", "Number", "-128" }, //
64                 { 48132, 1966080, "%s", "String", "0" }, //
65                 { 47028, 0xFF, "%d", "Number", "-1" }, //
66                 { 43009, 0x011F, "%.1f", "Number", "28" + SEP + "7" }, //
67                 { 43009, 0x7FFF, "%.1f", "Number", "3276" + SEP + "7" }, //
68                 { 43009, 0x8000, "%.1f", "Number", "-3276" + SEP + "8" }, //
69                 { 40004, 0xFFFF, "%.1f", "Number", "-0" + SEP + "1" }, //
70                 { 40004, 0xFFFF, "%.1f", "Number", "-0" + SEP + "1" }, //
71                 { 43416, 0xFFFFFFFF, "%d", "Number", "4294967295" }, //
72                 { 47418, 0x004B, "%d", "Number", "75" }, //
73                 { 43514, 0x0007, "%d", "Number", "7" }, //
74                 { 47291, 0xFFFF, "%d", "Number", "65535" }, //
75                 { 42437, 0xFFFFFFFF, "%.1f", "Number", "429496729" + SEP + "5" }, //
76                 { 42504, 0xFFFFFFFF, "%d", "Number", "4294967295" }, //
77                 { 43081, 196, "%.1f", "Number", "19" + SEP + "6" }, //
78                 { 43424, 1685, "%d", "Number", "1685" }, //
79                 { 43416, 4857, "%d", "Number", "4857" }, //
80                 { 43420, 9487, "%d", "Number", "9487" }, //
81                 { 40940, (byte) 0xFF, "%.1f", "Number", "-0" + SEP + "1" }, //
82                 { 40940, 0x80000000, "%.1f", "Number", "-214748364" + SEP + "8" }, //
83                 { 40940, 0x7FFFFFFF, "%.1f", "Number", "214748364" + SEP + "7" }, //
84                 { 40940, (short) 0xFFFF, "%.1f", "Number", "-0" + SEP + "1" }, //
85                 { 40940, (byte) 0xFF, "%.1f", "Number", "-0" + SEP + "1" }, //
86                 { 40940, 0xFFFF, "%.1f", "Number", "6553" + SEP + "5" } //
87         });
88     }
89
90     public NibeHeatPumpHandlerNibe2StateTest(final int coilAddress, final int value, final String format,
91             final String type, final String expected) {
92         this.fCoilAddress = coilAddress;
93         this.fValue = value;
94         this.fFormat = format;
95         this.fType = type;
96         this.fExpected = expected;
97     }
98
99     @Before
100     public void setUp() throws Exception {
101         initMocks(this);
102
103         product = new NibeHeatPumpHandler(null, PumpModel.F1X55, serialPortManager);
104         parameterTypes = new Class[3];
105         parameterTypes[0] = VariableInformation.class;
106         parameterTypes[1] = int.class;
107         parameterTypes[2] = String.class;
108         m = product.getClass().getDeclaredMethod(METHOD_NAME, parameterTypes);
109         m.setAccessible(true);
110         parameters = new Object[3];
111     }
112
113     @Test
114     public void convertNibeValueToStateTest() throws InvocationTargetException, IllegalAccessException {
115         VariableInformation varInfo = VariableInformation.getVariableInfo(PumpModel.F1X55, fCoilAddress);
116         parameters[0] = varInfo;
117         parameters[1] = fValue;
118         parameters[2] = fType;
119         State state = (State) m.invoke(product, parameters);
120
121         assertEquals(fExpected, state.format(fFormat));
122     }
123 }