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.hamcrest.collection.ArrayMatching.arrayContaining;
18 import static org.junit.jupiter.api.Assertions.assertThrows;
19 import static org.openhab.io.transport.modbus.ModbusConstants.*;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import java.util.stream.IntStream;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.hamcrest.Matcher;
28 import org.junit.jupiter.api.Test;
29 import org.openhab.io.transport.modbus.ModbusWriteFunctionCode;
30 import org.openhab.io.transport.modbus.ModbusWriteRequestBlueprint;
31 import org.openhab.io.transport.modbus.json.WriteRequestJsonUtilities;
34 * @author Sami Salonen - Initial contribution
36 public class WriteRequestJsonUtilitiesTest {
38 private static List<String> MAX_REGISTERS = IntStream.range(0, MAX_REGISTERS_WRITE_COUNT).mapToObj(i -> "1")
39 .collect(Collectors.toList());
40 private static List<String> OVER_MAX_REGISTERS = IntStream.range(0, MAX_REGISTERS_WRITE_COUNT + 1)
41 .mapToObj(i -> "1").collect(Collectors.toList());
43 private static List<String> MAX_COILS = IntStream.range(0, MAX_BITS_WRITE_COUNT).mapToObj(i -> "1")
44 .collect(Collectors.toList());
45 private static List<String> OVER_MAX_COILS = IntStream.range(0, MAX_BITS_WRITE_COUNT + 1).mapToObj(i -> "1")
46 .collect(Collectors.toList());
49 public void testEmptyArray() {
50 assertThat(WriteRequestJsonUtilities.fromJson(3, "[]").size(), is(equalTo(0)));
54 public void testFC6NoRegister() {
55 assertThrows(IllegalArgumentException.class, () -> WriteRequestJsonUtilities.fromJson(55, "[{"//
56 + "\"functionCode\": 6,"//
57 + "\"address\": 5412,"//
62 @SuppressWarnings({ "rawtypes", "unchecked" })
64 public void testFC6SingleRegister() {
65 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
66 + "\"functionCode\": 6,"//
67 + "\"address\": 5412,"//
70 arrayContaining((Matcher) new RegisterMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
71 ModbusWriteFunctionCode.WRITE_SINGLE_REGISTER, 3)));
74 @SuppressWarnings({ "rawtypes", "unchecked" })
76 public void testFC6SingleRegisterMaxTries99() {
77 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
78 + "\"functionCode\": 6,"//
79 + "\"address\": 5412,"//
81 + "\"maxTries\": 99"//
84 (Matcher) new RegisterMatcher(55, 5412, 99, ModbusWriteFunctionCode.WRITE_SINGLE_REGISTER, 3)));
88 public void testFC6MultipleRegisters() {
89 assertThrows(IllegalArgumentException.class, () -> WriteRequestJsonUtilities.fromJson(55, "[{"//
90 + "\"functionCode\": 6,"//
91 + "\"address\": 5412,"//
92 + "\"value\": [3, 4]"//
97 public void testFC16NoRegister() {
98 assertThrows(IllegalArgumentException.class, () -> WriteRequestJsonUtilities.fromJson(55, "[{"//
99 + "\"functionCode\": 16,"//
100 + "\"address\": 5412,"//
105 @SuppressWarnings({ "rawtypes", "unchecked" })
107 public void testFC16SingleRegister() {
108 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
109 + "\"functionCode\": 16,"//
110 + "\"address\": 5412,"//
113 arrayContaining((Matcher) new RegisterMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
114 ModbusWriteFunctionCode.WRITE_MULTIPLE_REGISTERS, 3)));
117 @SuppressWarnings({ "rawtypes", "unchecked" })
119 public void testFC16MultipleRegisters() {
120 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
121 + "\"functionCode\": 16,"//
122 + "\"address\": 5412,"//
123 + "\"value\": [3, 4, 2]"//
125 arrayContaining((Matcher) new RegisterMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
126 ModbusWriteFunctionCode.WRITE_MULTIPLE_REGISTERS, 3, 4, 2)));
130 public void testFC16MultipleRegistersMaxRegisters() {
131 Collection<@NonNull ModbusWriteRequestBlueprint> writes = WriteRequestJsonUtilities.fromJson(55, "[{"//
132 + "\"functionCode\": 16,"//
133 + "\"address\": 5412,"//
134 + "\"value\": [" + String.join(",", MAX_REGISTERS) + "]"//
136 assertThat(writes.size(), is(equalTo(1)));
140 public void testFC16MultipleRegistersTooManyRegisters() {
141 assertThrows(IllegalArgumentException.class, () -> WriteRequestJsonUtilities.fromJson(55, "[{"//
142 + "\"functionCode\": 16,"//
143 + "\"address\": 5412,"//
144 + "\"value\": [" + String.join(",", OVER_MAX_REGISTERS) + "]"//
148 @SuppressWarnings({ "rawtypes", "unchecked" })
150 public void testFC5SingeCoil() {
151 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
152 + "\"functionCode\": 5,"//
153 + "\"address\": 5412,"//
154 + "\"value\": [3]" // value 3 (!= 0) is converted to boolean true
156 arrayContaining((Matcher) new CoilMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
157 ModbusWriteFunctionCode.WRITE_COIL, true)));
161 public void testFC5MultipleCoils() {
162 assertThrows(IllegalArgumentException.class, () -> WriteRequestJsonUtilities.fromJson(55, "[{"//
163 + "\"functionCode\": 5,"//
164 + "\"address\": 5412,"//
165 + "\"value\": [3, 4]"//
169 @SuppressWarnings({ "rawtypes", "unchecked" })
171 public void testFC15SingleCoil() {
172 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
173 + "\"functionCode\": 15,"//
174 + "\"address\": 5412,"//
177 arrayContaining((Matcher) new CoilMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
178 ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS, true)));
181 @SuppressWarnings({ "rawtypes", "unchecked" })
183 public void testFC15MultipleCoils() {
184 assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
185 + "\"functionCode\": 15,"//
186 + "\"address\": 5412,"//
187 + "\"value\": [1, 0, 5]"//
189 arrayContaining((Matcher) new CoilMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
190 ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS, true, false, true)));
194 public void testFC15MultipleCoilsMaxCoils() {
195 Collection<@NonNull ModbusWriteRequestBlueprint> writes = WriteRequestJsonUtilities.fromJson(55, "[{"//
196 + "\"functionCode\": 15,"//
197 + "\"address\": 5412,"//
198 + "\"value\": [" + String.join(",", MAX_COILS) + "]"//
200 assertThat(writes.size(), is(equalTo(1)));
204 public void testFC15MultipleCoilsTooManyCoils() {
205 assertThrows(IllegalArgumentException.class, () -> WriteRequestJsonUtilities.fromJson(55, "[{"//
206 + "\"functionCode\": 15,"//
207 + "\"address\": 5412,"//
208 + "\"value\": [" + String.join(",", OVER_MAX_COILS) + "]"//
213 public void testEmptyObject() {
214 // we are expecting list, not object -> error
215 assertThrows(IllegalStateException.class, () -> WriteRequestJsonUtilities.fromJson(3, "{}"));
219 public void testNumber() {
220 // we are expecting list, not primitive (number) -> error
221 assertThrows(IllegalStateException.class, () -> WriteRequestJsonUtilities.fromJson(3, "3"));
225 public void testEmptyList() {
226 assertThat(WriteRequestJsonUtilities.fromJson(3, "[]").size(), is(equalTo(0)));