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