2 * Copyright (c) 2010-2020 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.io.transport.modbus.test;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertThrows;
19 import java.nio.ByteBuffer;
20 import java.nio.charset.Charset;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.stream.Collectors;
25 import java.util.stream.IntStream;
26 import java.util.stream.Stream;
28 import org.junit.jupiter.params.ParameterizedTest;
29 import org.junit.jupiter.params.provider.MethodSource;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.io.transport.modbus.ModbusBitUtilities;
32 import org.openhab.io.transport.modbus.ModbusRegister;
33 import org.openhab.io.transport.modbus.ModbusRegisterArray;
35 import io.swagger.v3.oas.annotations.Parameters;
38 * @author Sami Salonen - Initial contribution
40 public class BitUtilitiesExtractStringFromRegistersTest {
42 private static ModbusRegisterArray shortArrayToRegisterArray(int... arr) {
43 ModbusRegister[] tmp = new ModbusRegister[0];
44 return new ModbusRegisterArray(IntStream.of(arr).mapToObj(val -> {
45 ByteBuffer buffer = ByteBuffer.allocate(2);
46 buffer.putShort((short) val);
47 return new ModbusRegister(buffer.get(0), buffer.get(1));
48 }).collect(Collectors.toList()).toArray(tmp));
52 public static Collection<Object[]> data() {
53 return Collections.unmodifiableList(Stream.of(
54 new Object[] { new StringType(""), shortArrayToRegisterArray(0), 0, 0, StandardCharsets.UTF_8 },
55 new Object[] { new StringType("hello"), shortArrayToRegisterArray(0x6865, 0x6c6c, 0x6f00), 0, 5,
56 StandardCharsets.UTF_8 },
57 new Object[] { new StringType("hello "), shortArrayToRegisterArray(0, 0, 0x6865, 0x6c6c, 0x6f20, 0, 0),
58 2, 6, StandardCharsets.UTF_8 },
59 new Object[] { new StringType("hello"),
60 shortArrayToRegisterArray(0x6865, 0x6c6c, 0x6f00, 0x0000, 0x0000), 0, 10,
61 StandardCharsets.UTF_8 },
62 new Object[] { new StringType("árvíztűrő tükörfúrógép"),
63 shortArrayToRegisterArray(0xc3a1, 0x7276, 0xc3ad, 0x7a74, 0xc5b1, 0x72c5, 0x9120, 0x74c3,
64 0xbc6b, 0xc3b6, 0x7266, 0xc3ba, 0x72c3, 0xb367, 0xc3a9, 0x7000),
65 0, 32, StandardCharsets.UTF_8 },
66 new Object[] { new StringType("árvíztűrő tükörfúrógép"),
67 shortArrayToRegisterArray(0xe172, 0x76ed, 0x7a74, 0xfb72, 0xf520, 0x74fc, 0x6bf6, 0x7266,
68 0xfa72, 0xf367, 0xe970),
69 0, 22, Charset.forName("ISO-8859-2") },
72 new Object[] { IllegalArgumentException.class, shortArrayToRegisterArray(0, 0), 2, 4,
73 StandardCharsets.UTF_8 },
74 new Object[] { IllegalArgumentException.class, shortArrayToRegisterArray(0, 0), 0, -1,
75 StandardCharsets.UTF_8 },
76 new Object[] { IllegalArgumentException.class, shortArrayToRegisterArray(0, 0), 0, 5,
77 StandardCharsets.UTF_8 })
78 .collect(Collectors.toList()));
81 @SuppressWarnings({ "unchecked", "rawtypes" })
84 public void testExtractStringFromRegisters(Object expectedResult, ModbusRegisterArray registers, int index,
85 int length, Charset charset) {
86 if (expectedResult instanceof Class && Exception.class.isAssignableFrom((Class) expectedResult)) {
87 assertThrows((Class) expectedResult,
88 () -> ModbusBitUtilities.extractStringFromRegisters(registers, index, length, charset));
92 StringType actualState = ModbusBitUtilities.extractStringFromRegisters(registers, index, length, charset);
93 assertThat(String.format("registers=%s, index=%d, length=%d", registers, index, length), actualState,
94 is(equalTo(expectedResult)));