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.binding.modbus.tests;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertNotNull;
19 import java.util.Objects;
21 import org.junit.jupiter.api.Test;
22 import org.mockito.InOrder;
23 import org.mockito.Mockito;
24 import org.openhab.binding.modbus.handler.EndpointNotInitializedException;
25 import org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal;
26 import org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.io.transport.modbus.endpoint.EndpointPoolConfiguration;
29 import org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint;
30 import org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingStatusDetail;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.builder.BridgeBuilder;
38 * @author Sami Salonen - Initial contribution
40 public class ModbusTcpThingHandlerTest extends AbstractModbusOSGiTest {
42 private static BridgeBuilder createTcpThingBuilder(String id) {
43 return BridgeBuilder.create(ModbusBindingConstantsInternal.THING_TYPE_MODBUS_TCP,
44 new ThingUID(ModbusBindingConstantsInternal.THING_TYPE_MODBUS_TCP, id));
48 public void testInitializeAndSlaveEndpoint() throws EndpointNotInitializedException {
49 Configuration thingConfig = new Configuration();
50 thingConfig.put("host", "thisishost");
51 thingConfig.put("port", 44);
52 thingConfig.put("id", 9);
53 thingConfig.put("timeBetweenTransactionsMillis", 1);
54 thingConfig.put("timeBetweenReconnectMillis", 2);
55 thingConfig.put("connectMaxTries", 3);
56 thingConfig.put("reconnectAfterMillis", 4);
57 thingConfig.put("connectTimeoutMillis", 5);
59 EndpointPoolConfiguration expectedPoolConfiguration = new EndpointPoolConfiguration();
60 expectedPoolConfiguration.setConnectMaxTries(3);
61 expectedPoolConfiguration.setConnectTimeoutMillis(5);
62 expectedPoolConfiguration.setInterConnectDelayMillis(2);
63 expectedPoolConfiguration.setInterTransactionDelayMillis(1);
64 expectedPoolConfiguration.setReconnectAfterMillis(4);
66 Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
68 assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
70 ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
71 assertNotNull(thingHandler);
72 ModbusSlaveEndpoint slaveEndpoint = thingHandler.getEndpoint();
73 assertThat(slaveEndpoint, is(equalTo(new ModbusTCPSlaveEndpoint("thisishost", 44, false))));
74 assertThat(thingHandler.getSlaveId(), is(9));
76 InOrder orderedVerify = Mockito.inOrder(mockedModbusManager);
77 ModbusSlaveEndpoint endpoint = thingHandler.getEndpoint();
78 Objects.requireNonNull(endpoint);
79 orderedVerify.verify(mockedModbusManager).newModbusCommunicationInterface(endpoint, expectedPoolConfiguration);
83 public void testTwoDifferentEndpointWithDifferentParameters() {
86 Configuration thingConfig = new Configuration();
87 thingConfig.put("host", "thisishost");
88 thingConfig.put("port", 44);
89 thingConfig.put("connectMaxTries", 1);
90 thingConfig.put("timeBetweenTransactionsMillis", 1);
92 final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
94 assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
96 ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
97 assertNotNull(thingHandler);
100 Configuration thingConfig = new Configuration();
101 thingConfig.put("host", "thisishost");
102 thingConfig.put("port", 45);
103 thingConfig.put("connectMaxTries", 1);
104 thingConfig.put("timeBetweenTransactionsMillis", 100);
106 final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
108 // Different endpoint (port 45), so should be OK even though timeBetweenTransactionsMillis is different
109 assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
111 ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
112 assertNotNull(thingHandler);
117 public void testTwoIdenticalEndpointWithDifferentParameters() {
118 // Real implementation needed to validate this behaviour
119 swapModbusManagerToReal();
122 Configuration thingConfig = new Configuration();
123 thingConfig.put("host", "thisishost");
124 thingConfig.put("port", 44);
125 thingConfig.put("connectMaxTries", 1);
126 thingConfig.put("timeBetweenTransactionsMillis", 1);
128 final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
130 assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
132 ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
133 assertNotNull(thingHandler);
137 Configuration thingConfig = new Configuration();
138 thingConfig.put("host", "thisishost");
139 thingConfig.put("port", 44);
140 thingConfig.put("connectMaxTries", 1);
141 thingConfig.put("timeBetweenTransactionsMillis", 100);
143 final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
145 assertThat(thing.getStatus(), is(equalTo(ThingStatus.OFFLINE)));
146 assertThat(thing.getStatusInfo().getStatusDetail(), is(equalTo(ThingStatusDetail.CONFIGURATION_ERROR)));
151 public void testTwoIdenticalEndpointWithSameParameters() {
152 // Real implementation needed to validate this behaviour
153 swapModbusManagerToReal();
156 Configuration thingConfig = new Configuration();
157 thingConfig.put("host", "thisishost");
158 thingConfig.put("port", 44);
159 thingConfig.put("connectMaxTries", 1);
160 thingConfig.put("timeBetweenTransactionsMillis", 1);
162 final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
164 assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
166 ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
167 assertNotNull(thingHandler);
170 Configuration thingConfig = new Configuration();
171 thingConfig.put("host", "thisishost");
172 thingConfig.put("port", 44);
173 thingConfig.put("connectMaxTries", 1);
174 thingConfig.put("timeBetweenTransactionsMillis", 1);
175 thingConfig.put("connectTimeoutMillis", 10000); // default
177 final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
179 // Same endpoint and same parameters -> should not affect this thing
180 assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));