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