]> git.basschouten.com Git - openhab-addons.git/blob
22fa4023097ad92fa44a3beef9d9a4a300d8f993
[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.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.*;
20
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import java.util.stream.IntStream;
25
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;
32
33 /**
34  * @author Sami Salonen - Initial contribution
35  */
36 public class WriteRequestJsonUtilitiesTest {
37
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());
42
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());
47
48     @Test
49     public void testEmptyArray() {
50         assertThat(WriteRequestJsonUtilities.fromJson(3, "[]").size(), is(equalTo(0)));
51     }
52
53     @Test
54     public void testFC6NoRegister() {
55         assertThrows(IllegalArgumentException.class, () -> WriteRequestJsonUtilities.fromJson(55, "[{"//
56                 + "\"functionCode\": 6,"//
57                 + "\"address\": 5412,"//
58                 + "\"value\": []"//
59                 + "}]"));
60     }
61
62     @SuppressWarnings({ "rawtypes", "unchecked" })
63     @Test
64     public void testFC6SingleRegister() {
65         assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
66                 + "\"functionCode\": 6,"//
67                 + "\"address\": 5412,"//
68                 + "\"value\": [3]"//
69                 + "}]").toArray(),
70                 arrayContaining((Matcher) new RegisterMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
71                         ModbusWriteFunctionCode.WRITE_SINGLE_REGISTER, 3)));
72     }
73
74     @SuppressWarnings({ "rawtypes", "unchecked" })
75     @Test
76     public void testFC6SingleRegisterMaxTries99() {
77         assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
78                 + "\"functionCode\": 6,"//
79                 + "\"address\": 5412,"//
80                 + "\"value\": [3],"//
81                 + "\"maxTries\": 99"//
82                 + "}]").toArray(),
83                 arrayContaining(
84                         (Matcher) new RegisterMatcher(55, 5412, 99, ModbusWriteFunctionCode.WRITE_SINGLE_REGISTER, 3)));
85     }
86
87     @Test
88     public void testFC6MultipleRegisters() {
89         assertThrows(IllegalArgumentException.class, () -> WriteRequestJsonUtilities.fromJson(55, "[{"//
90                 + "\"functionCode\": 6,"//
91                 + "\"address\": 5412,"//
92                 + "\"value\": [3, 4]"//
93                 + "}]"));
94     }
95
96     @Test
97     public void testFC16NoRegister() {
98         assertThrows(IllegalArgumentException.class, () -> WriteRequestJsonUtilities.fromJson(55, "[{"//
99                 + "\"functionCode\": 16,"//
100                 + "\"address\": 5412,"//
101                 + "\"value\": []"//
102                 + "}]"));
103     }
104
105     @SuppressWarnings({ "rawtypes", "unchecked" })
106     @Test
107     public void testFC16SingleRegister() {
108         assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
109                 + "\"functionCode\": 16,"//
110                 + "\"address\": 5412,"//
111                 + "\"value\": [3]"//
112                 + "}]").toArray(),
113                 arrayContaining((Matcher) new RegisterMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
114                         ModbusWriteFunctionCode.WRITE_MULTIPLE_REGISTERS, 3)));
115     }
116
117     @SuppressWarnings({ "rawtypes", "unchecked" })
118     @Test
119     public void testFC16MultipleRegisters() {
120         assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
121                 + "\"functionCode\": 16,"//
122                 + "\"address\": 5412,"//
123                 + "\"value\": [3, 4, 2]"//
124                 + "}]").toArray(),
125                 arrayContaining((Matcher) new RegisterMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
126                         ModbusWriteFunctionCode.WRITE_MULTIPLE_REGISTERS, 3, 4, 2)));
127     }
128
129     @Test
130     public void testFC16MultipleRegistersMaxRegisters() {
131         Collection<@NonNull ModbusWriteRequestBlueprint> writes = WriteRequestJsonUtilities.fromJson(55, "[{"//
132                 + "\"functionCode\": 16,"//
133                 + "\"address\": 5412,"//
134                 + "\"value\": [" + String.join(",", MAX_REGISTERS) + "]"//
135                 + "}]");
136         assertThat(writes.size(), is(equalTo(1)));
137     }
138
139     @Test
140     public void testFC16MultipleRegistersTooManyRegisters() {
141         assertThrows(IllegalArgumentException.class, () -> WriteRequestJsonUtilities.fromJson(55, "[{"//
142                 + "\"functionCode\": 16,"//
143                 + "\"address\": 5412,"//
144                 + "\"value\": [" + String.join(",", OVER_MAX_REGISTERS) + "]"//
145                 + "}]"));
146     }
147
148     @SuppressWarnings({ "rawtypes", "unchecked" })
149     @Test
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
155                 + "}]").toArray(),
156                 arrayContaining((Matcher) new CoilMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
157                         ModbusWriteFunctionCode.WRITE_COIL, true)));
158     }
159
160     @Test
161     public void testFC5MultipleCoils() {
162         assertThrows(IllegalArgumentException.class, () -> WriteRequestJsonUtilities.fromJson(55, "[{"//
163                 + "\"functionCode\": 5,"//
164                 + "\"address\": 5412,"//
165                 + "\"value\": [3, 4]"//
166                 + "}]"));
167     }
168
169     @SuppressWarnings({ "rawtypes", "unchecked" })
170     @Test
171     public void testFC15SingleCoil() {
172         assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
173                 + "\"functionCode\": 15,"//
174                 + "\"address\": 5412,"//
175                 + "\"value\": [3]"//
176                 + "}]").toArray(),
177                 arrayContaining((Matcher) new CoilMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
178                         ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS, true)));
179     }
180
181     @SuppressWarnings({ "rawtypes", "unchecked" })
182     @Test
183     public void testFC15MultipleCoils() {
184         assertThat(WriteRequestJsonUtilities.fromJson(55, "[{"//
185                 + "\"functionCode\": 15,"//
186                 + "\"address\": 5412,"//
187                 + "\"value\": [1, 0, 5]"//
188                 + "}]").toArray(),
189                 arrayContaining((Matcher) new CoilMatcher(55, 5412, WriteRequestJsonUtilities.DEFAULT_MAX_TRIES,
190                         ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS, true, false, true)));
191     }
192
193     @Test
194     public void testFC15MultipleCoilsMaxCoils() {
195         Collection<@NonNull ModbusWriteRequestBlueprint> writes = WriteRequestJsonUtilities.fromJson(55, "[{"//
196                 + "\"functionCode\": 15,"//
197                 + "\"address\": 5412,"//
198                 + "\"value\": [" + String.join(",", MAX_COILS) + "]"//
199                 + "}]");
200         assertThat(writes.size(), is(equalTo(1)));
201     }
202
203     @Test
204     public void testFC15MultipleCoilsTooManyCoils() {
205         assertThrows(IllegalArgumentException.class, () -> WriteRequestJsonUtilities.fromJson(55, "[{"//
206                 + "\"functionCode\": 15,"//
207                 + "\"address\": 5412,"//
208                 + "\"value\": [" + String.join(",", OVER_MAX_COILS) + "]"//
209                 + "}]"));
210     }
211
212     @Test
213     public void testEmptyObject() {
214         // we are expecting list, not object -> error
215         assertThrows(IllegalStateException.class, () -> WriteRequestJsonUtilities.fromJson(3, "{}"));
216     }
217
218     @Test
219     public void testNumber() {
220         // we are expecting list, not primitive (number) -> error
221         assertThrows(IllegalStateException.class, () -> WriteRequestJsonUtilities.fromJson(3, "3"));
222     }
223
224     @Test
225     public void testEmptyList() {
226         assertThat(WriteRequestJsonUtilities.fromJson(3, "[]").size(), is(equalTo(0)));
227     }
228 }