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.collection.IsArrayContainingInOrder.arrayContaining;
17 import static org.junit.Assert.assertThat;
18 import static org.openhab.io.transport.modbus.ModbusConstants.*;
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.stream.Collectors;
23 import java.util.stream.IntStream;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.hamcrest.Matcher;
27 import org.junit.Test;
28 import org.openhab.io.transport.modbus.ModbusWriteFunctionCode;
29 import org.openhab.io.transport.modbus.ModbusWriteRequestBlueprint;
30 import org.openhab.io.transport.modbus.json.WriteRequestJsonUtilities;
33 * @author Sami Salonen - Initial contribution
35 public class WriteRequestJsonUtilitiesTest {
37 private static List<String> MAX_REGISTERS = IntStream.range(0, MAX_REGISTERS_WRITE_COUNT).mapToObj(i -> "1")
38 .collect(Collectors.toList());
39 private static List<String> OVER_MAX_REGISTERS = IntStream.range(0, MAX_REGISTERS_WRITE_COUNT + 1)
40 .mapToObj(i -> "1").collect(Collectors.toList());
42 private static List<String> MAX_COILS = IntStream.range(0, MAX_BITS_WRITE_COUNT).mapToObj(i -> "1")
43 .collect(Collectors.toList());
44 private static List<String> OVER_MAX_COILS = IntStream.range(0, MAX_BITS_WRITE_COUNT + 1).mapToObj(i -> "1")
45 .collect(Collectors.toList());
48 public void testEmptyArray() {
49 assertThat(WriteRequestJsonUtilities.fromJson(3, "[]").size(), is(equalTo(0)));
52 @Test(expected = IllegalArgumentException.class)
53 public void testFC6NoRegister() {
54 WriteRequestJsonUtilities.fromJson(55, "[{"//
55 + "\"functionCode\": 6,"//
56 + "\"address\": 5412,"//
61 @SuppressWarnings({ "rawtypes", "unchecked" })
63 public void testFC6SingleRegister() {
64 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
65 + "\"functionCode\": 6,"//
66 + "\"address\": 5412,"//
69 arrayContaining((Matcher) new RegisterMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
70 ModbusWriteFunctionCode.WRITE_SINGLE_REGISTER, 3)));
73 @SuppressWarnings({ "rawtypes", "unchecked" })
75 public void testFC6SingleRegisterMaxTries99() {
76 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
77 + "\"functionCode\": 6,"//
78 + "\"address\": 5412,"//
80 + "\"maxTries\": 99"//
83 (Matcher) new RegisterMatcher(55, 5412, 99, ModbusWriteFunctionCode.WRITE_SINGLE_REGISTER, 3)));
86 @Test(expected = IllegalArgumentException.class)
87 public void testFC6MultipleRegisters() {
88 WriteRequestJsonUtilities.fromJson(55, "[{"//
89 + "\"functionCode\": 6,"//
90 + "\"address\": 5412,"//
91 + "\"value\": [3, 4]"//
95 @Test(expected = IllegalArgumentException.class)
96 public void testFC16NoRegister() {
97 WriteRequestJsonUtilities.fromJson(55, "[{"//
98 + "\"functionCode\": 16,"//
99 + "\"address\": 5412,"//
104 @SuppressWarnings({ "rawtypes", "unchecked" })
106 public void testFC16SingleRegister() {
107 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
108 + "\"functionCode\": 16,"//
109 + "\"address\": 5412,"//
112 arrayContaining((Matcher) new RegisterMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
113 ModbusWriteFunctionCode.WRITE_MULTIPLE_REGISTERS, 3)));
116 @SuppressWarnings({ "rawtypes", "unchecked" })
118 public void testFC16MultipleRegisters() {
119 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
120 + "\"functionCode\": 16,"//
121 + "\"address\": 5412,"//
122 + "\"value\": [3, 4, 2]"//
124 arrayContaining((Matcher) new RegisterMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
125 ModbusWriteFunctionCode.WRITE_MULTIPLE_REGISTERS, 3, 4, 2)));
129 public void testFC16MultipleRegistersMaxRegisters() {
130 Collection<@NonNull ModbusWriteRequestBlueprint> writes = WriteRequestJsonUtilities.fromJson(55, "[{"//
131 + "\"functionCode\": 16,"//
132 + "\"address\": 5412,"//
133 + "\"value\": [" + String.join(",", MAX_REGISTERS) + "]"//
135 assertThat(writes.size(), is(equalTo(1)));
138 @Test(expected = IllegalArgumentException.class)
139 public void testFC16MultipleRegistersTooManyRegisters() {
140 WriteRequestJsonUtilities.fromJson(55, "[{"//
141 + "\"functionCode\": 16,"//
142 + "\"address\": 5412,"//
143 + "\"value\": [" + String.join(",", OVER_MAX_REGISTERS) + "]"//
147 @SuppressWarnings({ "rawtypes", "unchecked" })
149 public void testFC5SingeCoil() {
150 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
151 + "\"functionCode\": 5,"//
152 + "\"address\": 5412,"//
153 + "\"value\": [3]" // value 3 (!= 0) is converted to boolean true
155 arrayContaining((Matcher) new CoilMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
156 ModbusWriteFunctionCode.WRITE_COIL, true)));
159 @Test(expected = IllegalArgumentException.class)
160 public void testFC5MultipleCoils() {
161 WriteRequestJsonUtilities.fromJson(55, "[{"//
162 + "\"functionCode\": 5,"//
163 + "\"address\": 5412,"//
164 + "\"value\": [3, 4]"//
168 @SuppressWarnings({ "rawtypes", "unchecked" })
170 public void testFC15SingleCoil() {
171 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
172 + "\"functionCode\": 15,"//
173 + "\"address\": 5412,"//
176 arrayContaining((Matcher) new CoilMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
177 ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS, true)));
180 @SuppressWarnings({ "rawtypes", "unchecked" })
182 public void testFC15MultipleCoils() {
183 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
184 + "\"functionCode\": 15,"//
185 + "\"address\": 5412,"//
186 + "\"value\": [1, 0, 5]"//
188 arrayContaining((Matcher) new CoilMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
189 ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS, true, false, true)));
193 public void testFC15MultipleCoilsMaxCoils() {
194 Collection<@NonNull ModbusWriteRequestBlueprint> writes = WriteRequestJsonUtilities.fromJson(55, "[{"//
195 + "\"functionCode\": 15,"//
196 + "\"address\": 5412,"//
197 + "\"value\": [" + String.join(",", MAX_COILS) + "]"//
199 assertThat(writes.size(), is(equalTo(1)));
202 @Test(expected = IllegalArgumentException.class)
203 public void testFC15MultipleCoilsTooManyCoils() {
204 WriteRequestJsonUtilities.fromJson(55, "[{"//
205 + "\"functionCode\": 15,"//
206 + "\"address\": 5412,"//
207 + "\"value\": [" + String.join(",", OVER_MAX_COILS) + "]"//
211 @Test(expected = IllegalStateException.class)
212 public void testEmptyObject() {
213 // we are expecting list, not object -> error
214 WriteRequestJsonUtilities.fromJson(3, "{}");
217 @Test(expected = IllegalStateException.class)
218 public void testNumber() {
219 // we are expecting list, not primitive (number) -> error
220 WriteRequestJsonUtilities.fromJson(3, "3");
224 public void testEmptyList() {
225 assertThat(WriteRequestJsonUtilities.fromJson(3, "[]").size(), is(equalTo(0)));