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.junit.Assert.*;
18 import java.util.Objects;
20 import org.openhab.core.config.core.Configuration;
21 import org.openhab.core.thing.Bridge;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.thing.ThingStatusDetail;
24 import org.openhab.core.thing.ThingUID;
25 import org.openhab.core.thing.binding.builder.BridgeBuilder;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.InOrder;
29 import org.mockito.Mockito;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.openhab.binding.modbus.handler.EndpointNotInitializedException;
32 import org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal;
33 import org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler;
34 import org.openhab.io.transport.modbus.endpoint.EndpointPoolConfiguration;
35 import org.openhab.io.transport.modbus.endpoint.ModbusSlaveEndpoint;
36 import org.openhab.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint;
39 * @author Sami Salonen - Initial contribution
41 @RunWith(MockitoJUnitRunner.class)
42 public class ModbusTcpThingHandlerTest extends AbstractModbusOSGiTest {
44 private static BridgeBuilder createTcpThingBuilder(String id) {
45 return BridgeBuilder.create(ModbusBindingConstantsInternal.THING_TYPE_MODBUS_TCP,
46 new ThingUID(ModbusBindingConstantsInternal.THING_TYPE_MODBUS_TCP, id));
50 public void testInitializeAndSlaveEndpoint() throws EndpointNotInitializedException {
51 Configuration thingConfig = new Configuration();
52 thingConfig.put("host", "thisishost");
53 thingConfig.put("port", 44);
54 thingConfig.put("id", 9);
55 thingConfig.put("timeBetweenTransactionsMillis", 1);
56 thingConfig.put("timeBetweenReconnectMillis", 2);
57 thingConfig.put("connectMaxTries", 3);
58 thingConfig.put("reconnectAfterMillis", 4);
59 thingConfig.put("connectTimeoutMillis", 5);
61 EndpointPoolConfiguration expectedPoolConfiguration = new EndpointPoolConfiguration();
62 expectedPoolConfiguration.setConnectMaxTries(3);
63 expectedPoolConfiguration.setConnectTimeoutMillis(5);
64 expectedPoolConfiguration.setInterConnectDelayMillis(2);
65 expectedPoolConfiguration.setInterTransactionDelayMillis(1);
66 expectedPoolConfiguration.setReconnectAfterMillis(4);
68 Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
70 assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
72 ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
73 assertNotNull(thingHandler);
74 ModbusSlaveEndpoint slaveEndpoint = thingHandler.getEndpoint();
75 assertThat(slaveEndpoint, is(equalTo(new ModbusTCPSlaveEndpoint("thisishost", 44))));
76 assertThat(thingHandler.getSlaveId(), is(9));
78 InOrder orderedVerify = Mockito.inOrder(mockedModbusManager);
79 ModbusSlaveEndpoint endpoint = thingHandler.getEndpoint();
80 Objects.requireNonNull(endpoint);
81 orderedVerify.verify(mockedModbusManager).newModbusCommunicationInterface(endpoint, expectedPoolConfiguration);
85 public void testTwoDifferentEndpointWithDifferentParameters() {
88 Configuration thingConfig = new Configuration();
89 thingConfig.put("host", "thisishost");
90 thingConfig.put("port", 44);
91 thingConfig.put("connectMaxTries", 1);
92 thingConfig.put("timeBetweenTransactionsMillis", 1);
94 final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
96 assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
98 ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
99 assertNotNull(thingHandler);
102 Configuration thingConfig = new Configuration();
103 thingConfig.put("host", "thisishost");
104 thingConfig.put("port", 45);
105 thingConfig.put("connectMaxTries", 1);
106 thingConfig.put("timeBetweenTransactionsMillis", 100);
108 final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
110 // Different endpoint (port 45), so should be OK even though timeBetweenTransactionsMillis is different
111 assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
113 ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
114 assertNotNull(thingHandler);
119 public void testTwoIdenticalEndpointWithDifferentParameters() {
120 // Real implementation needed to validate this behaviour
121 swapModbusManagerToReal();
124 Configuration thingConfig = new Configuration();
125 thingConfig.put("host", "thisishost");
126 thingConfig.put("port", 44);
127 thingConfig.put("connectMaxTries", 1);
128 thingConfig.put("timeBetweenTransactionsMillis", 1);
130 final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
132 assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
134 ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
135 assertNotNull(thingHandler);
139 Configuration thingConfig = new Configuration();
140 thingConfig.put("host", "thisishost");
141 thingConfig.put("port", 44);
142 thingConfig.put("connectMaxTries", 1);
143 thingConfig.put("timeBetweenTransactionsMillis", 100);
145 final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
147 assertThat(thing.getStatus(), is(equalTo(ThingStatus.OFFLINE)));
148 assertThat(thing.getStatusInfo().getStatusDetail(), is(equalTo(ThingStatusDetail.CONFIGURATION_ERROR)));
153 public void testTwoIdenticalEndpointWithSameParameters() {
154 // Real implementation needed to validate this behaviour
155 swapModbusManagerToReal();
158 Configuration thingConfig = new Configuration();
159 thingConfig.put("host", "thisishost");
160 thingConfig.put("port", 44);
161 thingConfig.put("connectMaxTries", 1);
162 thingConfig.put("timeBetweenTransactionsMillis", 1);
164 final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
166 assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
168 ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
169 assertNotNull(thingHandler);
172 Configuration thingConfig = new Configuration();
173 thingConfig.put("host", "thisishost");
174 thingConfig.put("port", 44);
175 thingConfig.put("connectMaxTries", 1);
176 thingConfig.put("timeBetweenTransactionsMillis", 1);
177 thingConfig.put("connectTimeoutMillis", 10000); // default
179 final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
181 // Same endpoint and same parameters -> should not affect this thing
182 assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));