]> git.basschouten.com Git - openhab-addons.git/blob
f81c8af3270e3ad3ca05d5fea6468b9915b1b023
[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.modbus.helioseasycontrols.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.lang.reflect.InvocationTargetException;
18 import java.lang.reflect.Method;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
24 import org.junit.jupiter.params.ParameterizedTest;
25 import org.junit.jupiter.params.provider.MethodSource;
26 import org.openhab.core.io.transport.modbus.ModbusRegisterArray;
27
28 /**
29  * @author Sami Salonen - Initial contribution
30  */
31 public class PreparePayloadTest {
32
33     private Method preparePayloadMethod;
34
35     public PreparePayloadTest() throws NoSuchMethodException, SecurityException {
36         preparePayloadMethod = HeliosEasyControlsHandler.class.getDeclaredMethod("preparePayload", String.class);
37         preparePayloadMethod.setAccessible(true);
38     }
39
40     private ModbusRegisterArray preparePayload(String payload) {
41         try {
42             return (ModbusRegisterArray) preparePayloadMethod.invoke(null, payload);
43         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
44             fail("Reflection failure:" + e.getMessage());
45             throw new RuntimeException(); // to make compiler happy
46         }
47     }
48
49     public static Collection<Object[]> data() {
50         return Collections.unmodifiableList(Stream
51                 // Due to nul byte, full register full of nul bytes added
52                 .of(new Object[] { "v00020=a", new ModbusRegisterArray(0x7630, 0x3030, 0x3230, 0x3d61, 0x0000) },
53                         new Object[] { "v00020=aa", new ModbusRegisterArray(0x7630, 0x3030, 0x3230, 0x3d61, 0x6100) })
54                 .collect(Collectors.toList()));
55     }
56
57     @ParameterizedTest
58     @MethodSource("data")
59     public void testPreparePayload(String payload, ModbusRegisterArray expectedRegisters) {
60         ModbusRegisterArray actual = preparePayload(payload);
61
62         assertEquals(actual.size(), expectedRegisters.size(), String.format("payload=%s", payload));
63         for (int i = 0; i < expectedRegisters.size(); i++) {
64             int expectedRegisterDataUnsigned = expectedRegisters.getRegister(i);
65             int actualUnsigned = actual.getRegister(i);
66
67             assertEquals(expectedRegisterDataUnsigned, actualUnsigned,
68                     String.format("register index i=%d, payload=%s", i, payload));
69         }
70     }
71 }