]> git.basschouten.com Git - openhab-addons.git/blob
4432fb6cc3d6323dac79897be3b15f7aa606b61d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.binding.modbus.tests;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
18
19 import java.util.Objects;
20
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;
36
37 /**
38  * @author Sami Salonen - Initial contribution
39  */
40 public class ModbusTcpThingHandlerTest extends AbstractModbusOSGiTest {
41
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));
45     }
46
47     @Test
48     public void testInitializeAndSlaveEndpoint() throws EndpointNotInitializedException {
49         // Using mocked modbus manager
50         Configuration thingConfig = new Configuration();
51         thingConfig.put("host", "thisishost");
52         thingConfig.put("port", 44);
53         thingConfig.put("id", 9);
54         thingConfig.put("timeBetweenTransactionsMillis", 1);
55         thingConfig.put("timeBetweenReconnectMillis", 2);
56         thingConfig.put("connectMaxTries", 3);
57         thingConfig.put("reconnectAfterMillis", 4);
58         thingConfig.put("connectTimeoutMillis", 5);
59
60         EndpointPoolConfiguration expectedPoolConfiguration = new EndpointPoolConfiguration();
61         expectedPoolConfiguration.setConnectMaxTries(3);
62         expectedPoolConfiguration.setConnectTimeoutMillis(5);
63         expectedPoolConfiguration.setInterConnectDelayMillis(2);
64         expectedPoolConfiguration.setInterTransactionDelayMillis(1);
65         expectedPoolConfiguration.setReconnectAfterMillis(4);
66
67         Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
68         addThing(thing);
69         assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
70
71         ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
72         assertNotNull(thingHandler);
73         ModbusSlaveEndpoint slaveEndpoint = thingHandler.getEndpoint();
74         assertThat(slaveEndpoint, is(equalTo(new ModbusTCPSlaveEndpoint("thisishost", 44, false))));
75         assertThat(thingHandler.getSlaveId(), is(9));
76
77         InOrder orderedVerify = Mockito.inOrder(mockedModbusManager);
78         ModbusSlaveEndpoint endpoint = thingHandler.getEndpoint();
79         Objects.requireNonNull(endpoint);
80         orderedVerify.verify(mockedModbusManager).newModbusCommunicationInterface(endpoint, expectedPoolConfiguration);
81     }
82
83     @Test
84     public void testTwoDifferentEndpointWithDifferentParameters() {
85         // Real implementation needed to validate this behaviour
86         swapModbusManagerToReal();
87         // thing1
88         {
89             Configuration thingConfig = new Configuration();
90             thingConfig.put("host", "thisishost");
91             thingConfig.put("port", 44);
92             thingConfig.put("connectMaxTries", 1);
93             thingConfig.put("timeBetweenTransactionsMillis", 1);
94
95             final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
96             addThing(thing);
97             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
98
99             ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
100             assertNotNull(thingHandler);
101
102             EndpointPoolConfiguration expectedPoolConfiguration = new EndpointPoolConfiguration();
103             expectedPoolConfiguration.setConnectMaxTries(1);
104             expectedPoolConfiguration.setInterTransactionDelayMillis(1);
105
106             // defaults
107             expectedPoolConfiguration.setConnectTimeoutMillis(10_000);
108             expectedPoolConfiguration.setInterConnectDelayMillis(0);
109             expectedPoolConfiguration.setReconnectAfterMillis(0);
110
111             assertEquals(expectedPoolConfiguration, realModbusManager
112                     .getEndpointPoolConfiguration(new ModbusTCPSlaveEndpoint("thisishost", 44, false)));
113         }
114         {
115             Configuration thingConfig = new Configuration();
116             thingConfig.put("host", "thisishost");
117             thingConfig.put("port", 45);
118             thingConfig.put("connectMaxTries", 1);
119             thingConfig.put("timeBetweenTransactionsMillis", 100);
120
121             final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
122             addThing(thing);
123             // Different endpoint (port 45), so should be OK even though timeBetweenTransactionsMillis is different
124             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
125
126             ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
127             assertNotNull(thingHandler);
128         }
129     }
130
131     @Test
132     public void testTwoIdenticalEndpointWithDifferentParameters() {
133         // Real implementation needed to validate this behaviour
134         swapModbusManagerToReal();
135         // thing1
136         {
137             Configuration thingConfig = new Configuration();
138             thingConfig.put("host", "thisishost");
139             thingConfig.put("port", 44);
140             thingConfig.put("connectMaxTries", 1);
141             thingConfig.put("timeBetweenTransactionsMillis", 1);
142
143             final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
144             addThing(thing);
145             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
146
147             ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
148             assertNotNull(thingHandler);
149         }
150         {
151
152             Configuration thingConfig = new Configuration();
153             thingConfig.put("host", "thisishost");
154             thingConfig.put("port", 44);
155             thingConfig.put("connectMaxTries", 1);
156             thingConfig.put("timeBetweenTransactionsMillis", 100);
157
158             final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
159             addThing(thing);
160             assertThat(thing.getStatus(), is(equalTo(ThingStatus.OFFLINE)));
161             assertThat(thing.getStatusInfo().getStatusDetail(), is(equalTo(ThingStatusDetail.CONFIGURATION_ERROR)));
162         }
163         {
164             //
165             // Ensure the right EndpointPoolConfiguration is still in place
166             //
167             EndpointPoolConfiguration expectedPoolConfiguration = new EndpointPoolConfiguration();
168             expectedPoolConfiguration.setConnectMaxTries(1);
169             expectedPoolConfiguration.setInterTransactionDelayMillis(1); // Note: not 100
170
171             // defaults
172             expectedPoolConfiguration.setConnectTimeoutMillis(10_000);
173             expectedPoolConfiguration.setInterConnectDelayMillis(0);
174             expectedPoolConfiguration.setReconnectAfterMillis(0);
175
176             assertEquals(expectedPoolConfiguration, realModbusManager
177                     .getEndpointPoolConfiguration(new ModbusTCPSlaveEndpoint("thisishost", 44, false)));
178         }
179     }
180
181     @Test
182     public void testTwoIdenticalEndpointWithSameParameters() {
183         // Real implementation needed to validate this behaviour
184         swapModbusManagerToReal();
185         // thing1
186         {
187             Configuration thingConfig = new Configuration();
188             thingConfig.put("host", "thisishost");
189             thingConfig.put("port", 44);
190             thingConfig.put("connectMaxTries", 1);
191             thingConfig.put("timeBetweenTransactionsMillis", 1);
192
193             final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
194             addThing(thing);
195             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
196
197             ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
198             assertNotNull(thingHandler);
199         }
200         {
201             Configuration thingConfig = new Configuration();
202             thingConfig.put("host", "thisishost");
203             thingConfig.put("port", 44);
204             thingConfig.put("connectMaxTries", 1);
205             thingConfig.put("timeBetweenTransactionsMillis", 1);
206             thingConfig.put("connectTimeoutMillis", 10000); // default
207
208             final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
209             addThing(thing);
210             // Same endpoint and same parameters -> should not affect this thing
211             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
212         }
213     }
214 }