2 * Copyright (c) 2010-2024 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.modbus.helioseasycontrols.internal;
15 import static org.junit.jupiter.api.Assertions.*;
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;
24 import org.junit.jupiter.params.ParameterizedTest;
25 import org.junit.jupiter.params.provider.MethodSource;
26 import org.openhab.core.io.transport.modbus.ModbusRegisterArray;
29 * @author Sami Salonen - Initial contribution
31 public class PreparePayloadTest {
33 private Method preparePayloadMethod;
35 public PreparePayloadTest() throws NoSuchMethodException, SecurityException {
36 preparePayloadMethod = HeliosEasyControlsHandler.class.getDeclaredMethod("preparePayload", String.class);
37 preparePayloadMethod.setAccessible(true);
40 private ModbusRegisterArray preparePayload(String payload) {
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
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()));
59 public void testPreparePayload(String payload, ModbusRegisterArray expectedRegisters) {
60 ModbusRegisterArray actual = preparePayload(payload);
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);
67 assertEquals(expectedRegisterDataUnsigned, actualUnsigned,
68 String.format("register index i=%d, payload=%s", i, payload));