]> git.basschouten.com Git - openhab-addons.git/blob
e9a854efd30d4f1d5000572ce2d2e53bf34c85b6
[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.junit.Assert.*;
17
18 import java.util.Objects;
19
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;
37
38 /**
39  * @author Sami Salonen - Initial contribution
40  */
41 @RunWith(MockitoJUnitRunner.class)
42 public class ModbusTcpThingHandlerTest extends AbstractModbusOSGiTest {
43
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));
47     }
48
49     @Test
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);
60
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);
67
68         Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
69         addThing(thing);
70         assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
71
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));
77
78         InOrder orderedVerify = Mockito.inOrder(mockedModbusManager);
79         ModbusSlaveEndpoint endpoint = thingHandler.getEndpoint();
80         Objects.requireNonNull(endpoint);
81         orderedVerify.verify(mockedModbusManager).newModbusCommunicationInterface(endpoint, expectedPoolConfiguration);
82     }
83
84     @Test
85     public void testTwoDifferentEndpointWithDifferentParameters() {
86         // thing1
87         {
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);
93
94             final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
95             addThing(thing);
96             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
97
98             ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
99             assertNotNull(thingHandler);
100         }
101         {
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);
107
108             final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
109             addThing(thing);
110             // Different endpoint (port 45), so should be OK even though timeBetweenTransactionsMillis is different
111             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
112
113             ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
114             assertNotNull(thingHandler);
115         }
116     }
117
118     @Test
119     public void testTwoIdenticalEndpointWithDifferentParameters() {
120         // Real implementation needed to validate this behaviour
121         swapModbusManagerToReal();
122         // thing1
123         {
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);
129
130             final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
131             addThing(thing);
132             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
133
134             ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
135             assertNotNull(thingHandler);
136         }
137         {
138
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);
144
145             final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
146             addThing(thing);
147             assertThat(thing.getStatus(), is(equalTo(ThingStatus.OFFLINE)));
148             assertThat(thing.getStatusInfo().getStatusDetail(), is(equalTo(ThingStatusDetail.CONFIGURATION_ERROR)));
149         }
150     }
151
152     @Test
153     public void testTwoIdenticalEndpointWithSameParameters() {
154         // Real implementation needed to validate this behaviour
155         swapModbusManagerToReal();
156         // thing1
157         {
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);
163
164             final Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
165             addThing(thing);
166             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
167
168             ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
169             assertNotNull(thingHandler);
170         }
171         {
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
178
179             final Bridge thing = createTcpThingBuilder("tcpendpoint2").withConfiguration(thingConfig).build();
180             addThing(thing);
181             // Same endpoint and same parameters -> should not affect this thing
182             assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
183         }
184     }
185 }