]> git.basschouten.com Git - openhab-addons.git/blob
bcea73431e9af12422a288cc9bd19e76d0c84ccc
[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.io.transport.modbus.test;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.junit.Assert.assertThat;
17
18 import java.nio.ByteBuffer;
19 import java.nio.charset.Charset;
20 import java.nio.charset.StandardCharsets;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.stream.Collectors;
24 import java.util.stream.IntStream;
25 import java.util.stream.Stream;
26
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.Parameterized;
32 import org.junit.runners.Parameterized.Parameters;
33 import org.openhab.core.library.types.StringType;
34 import org.openhab.io.transport.modbus.ModbusBitUtilities;
35 import org.openhab.io.transport.modbus.ModbusRegister;
36 import org.openhab.io.transport.modbus.ModbusRegisterArray;
37
38 /**
39  * @author Sami Salonen - Initial contribution
40  */
41 @RunWith(Parameterized.class)
42 public class BitUtilitiesExtractStringFromRegistersTest {
43
44     final ModbusRegisterArray registers;
45     final int index;
46     final int length;
47     final Object expectedResult;
48     final Charset charset;
49
50     @Rule
51     public final ExpectedException shouldThrow = ExpectedException.none();
52
53     public BitUtilitiesExtractStringFromRegistersTest(Object expectedResult, ModbusRegisterArray registers, int index,
54             int length, Charset charset) {
55         this.registers = registers;
56         this.index = index;
57         this.length = length;
58         this.charset = charset;
59         this.expectedResult = expectedResult;
60     }
61
62     private static ModbusRegisterArray shortArrayToRegisterArray(int... arr) {
63         ModbusRegister[] tmp = new ModbusRegister[0];
64         return new ModbusRegisterArray(IntStream.of(arr).mapToObj(val -> {
65             ByteBuffer buffer = ByteBuffer.allocate(2);
66             buffer.putShort((short) val);
67             return new ModbusRegister(buffer.get(0), buffer.get(1));
68         }).collect(Collectors.toList()).toArray(tmp));
69     }
70
71     @Parameters
72     public static Collection<Object[]> data() {
73         return Collections.unmodifiableList(Stream.of(
74                 new Object[] { new StringType(""), shortArrayToRegisterArray(0), 0, 0, StandardCharsets.UTF_8 },
75                 new Object[] { new StringType("hello"), shortArrayToRegisterArray(0x6865, 0x6c6c, 0x6f00), 0, 5,
76                         StandardCharsets.UTF_8 },
77                 new Object[] { new StringType("hello "), shortArrayToRegisterArray(0, 0, 0x6865, 0x6c6c, 0x6f20, 0, 0),
78                         2, 6, StandardCharsets.UTF_8 },
79                 new Object[] { new StringType("hello"),
80                         shortArrayToRegisterArray(0x6865, 0x6c6c, 0x6f00, 0x0000, 0x0000), 0, 10,
81                         StandardCharsets.UTF_8 },
82                 new Object[] { new StringType("árvíztűrő tükörfúrógép"),
83                         shortArrayToRegisterArray(0xc3a1, 0x7276, 0xc3ad, 0x7a74, 0xc5b1, 0x72c5, 0x9120, 0x74c3,
84                                 0xbc6b, 0xc3b6, 0x7266, 0xc3ba, 0x72c3, 0xb367, 0xc3a9, 0x7000),
85                         0, 32, StandardCharsets.UTF_8 },
86                 new Object[] { new StringType("árvíztűrő tükörfúrógép"),
87                         shortArrayToRegisterArray(0xe172, 0x76ed, 0x7a74, 0xfb72, 0xf520, 0x74fc, 0x6bf6, 0x7266,
88                                 0xfa72, 0xf367, 0xe970),
89                         0, 22, Charset.forName("ISO-8859-2") },
90
91                 // Invalid values
92                 new Object[] { IllegalArgumentException.class, shortArrayToRegisterArray(0, 0), 2, 4,
93                         StandardCharsets.UTF_8 },
94                 new Object[] { IllegalArgumentException.class, shortArrayToRegisterArray(0, 0), 0, -1,
95                         StandardCharsets.UTF_8 },
96                 new Object[] { IllegalArgumentException.class, shortArrayToRegisterArray(0, 0), 0, 5,
97                         StandardCharsets.UTF_8 })
98                 .collect(Collectors.toList()));
99     }
100
101     @SuppressWarnings({ "unchecked", "rawtypes" })
102     @Test
103     public void testExtractStringFromRegisters() {
104         if (expectedResult instanceof Class && Exception.class.isAssignableFrom((Class) expectedResult)) {
105             shouldThrow.expect((Class) expectedResult);
106         }
107
108         StringType actualState = ModbusBitUtilities.extractStringFromRegisters(this.registers, this.index, this.length,
109                 this.charset);
110         assertThat(String.format("registers=%s, index=%d, length=%d", registers, index, length), actualState,
111                 is(equalTo(expectedResult)));
112     }
113 }