]> git.basschouten.com Git - openhab-addons.git/blob
4e7769e11c60d2f0ef66c2cde35849bec16fd240
[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.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.assertNotNull;
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         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);
58
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);
65
66         Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
67         addThing(thing);
68         assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
69
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));
75
76         InOrder orderedVerify = Mockito.inOrder(mockedModbusManager);
77         ModbusSlaveEndpoint endpoint = thingHandler.getEndpoint();
78         Objects.requireNonNull(endpoint);
79         orderedVerify.verify(mockedModbusManager).newModbusCommunicationInterface(endpoint, expectedPoolConfiguration);
80     }
81
82     @Test
83     public void testTwoDifferentEndpointWithDifferentParameters() {
84         // thing1
85         {
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);
91
92             final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
93             addThing(thing);
94             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
95
96             ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
97             assertNotNull(thingHandler);
98         }
99         {
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);
105
106             final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
107             addThing(thing);
108             // Different endpoint (port 45), so should be OK even though timeBetweenTransactionsMillis is different
109             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
110
111             ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
112             assertNotNull(thingHandler);
113         }
114     }
115
116     @Test
117     public void testTwoIdenticalEndpointWithDifferentParameters() {
118         // Real implementation needed to validate this behaviour
119         swapModbusManagerToReal();
120         // thing1
121         {
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);
127
128             final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
129             addThing(thing);
130             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
131
132             ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
133             assertNotNull(thingHandler);
134         }
135         {
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", 100);
142
143             final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
144             addThing(thing);
145             assertThat(thing.getStatus(), is(equalTo(ThingStatus.OFFLINE)));
146             assertThat(thing.getStatusInfo().getStatusDetail(), is(equalTo(ThingStatusDetail.CONFIGURATION_ERROR)));
147         }
148     }
149
150     @Test
151     public void testTwoIdenticalEndpointWithSameParameters() {
152         // Real implementation needed to validate this behaviour
153         swapModbusManagerToReal();
154         // thing1
155         {
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);
161
162             final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
163             addThing(thing);
164             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
165
166             ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
167             assertNotNull(thingHandler);
168         }
169         {
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
176
177             final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
178             addThing(thing);
179             // Same endpoint and same parameters -> should not affect this thing
180             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
181         }
182     }
183 }