]> git.basschouten.com Git - openhab-addons.git/blob
dee72dda3351ba9415d76d83975f8958f0b90026
[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 java.util.Arrays;
16 import java.util.Objects;
17 import java.util.stream.StreamSupport;
18
19 import org.hamcrest.Description;
20 import org.openhab.io.transport.modbus.ModbusWriteFunctionCode;
21 import org.openhab.io.transport.modbus.ModbusWriteRegisterRequestBlueprint;
22
23 /**
24  * @author Sami Salonen - Initial contribution
25  */
26 class RegisterMatcher extends AbstractRequestComparer<ModbusWriteRegisterRequestBlueprint> {
27
28     private Integer[] expectedRegisterValues;
29
30     public RegisterMatcher(int expectedUnitId, int expectedAddress, int expectedMaxTries,
31             ModbusWriteFunctionCode expectedFunctionCode, Integer... expectedRegisterValues) {
32         super(expectedUnitId, expectedAddress, expectedFunctionCode, expectedMaxTries);
33         this.expectedRegisterValues = expectedRegisterValues;
34     }
35
36     @Override
37     public void describeTo(Description description) {
38         super.describeTo(description);
39         description.appendText(" registers=");
40         description.appendValue(Arrays.toString(expectedRegisterValues));
41     }
42
43     @Override
44     protected boolean doMatchData(ModbusWriteRegisterRequestBlueprint item) {
45         Object[] actual = StreamSupport.stream(item.getRegisters().spliterator(), false).map(r -> r.getValue())
46                 .toArray();
47         return Objects.deepEquals(actual, expectedRegisterValues);
48     }
49 }