]> git.basschouten.com Git - openhab-addons.git/blob
c1f186a47a4efa15fdb52e5bd7d1e9754490732b
[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.onewire.owserver;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
17
18 import java.io.IOException;
19 import java.util.List;
20 import java.util.concurrent.CompletableFuture;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.junit.jupiter.MockitoExtension;
31 import org.openhab.binding.onewire.internal.OwException;
32 import org.openhab.binding.onewire.internal.OwPageBuffer;
33 import org.openhab.binding.onewire.internal.SensorId;
34 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
35 import org.openhab.binding.onewire.internal.owserver.OwserverConnection;
36 import org.openhab.binding.onewire.internal.owserver.OwserverConnectionState;
37 import org.openhab.binding.onewire.test.OwserverTestServer;
38 import org.openhab.core.library.types.DecimalType;
39 import org.openhab.core.library.types.OnOffType;
40 import org.openhab.core.test.TestPortUtil;
41 import org.openhab.core.test.java.JavaTest;
42 import org.openhab.core.types.State;
43
44 /**
45  * Tests cases for {@link OwserverConnection}.
46  *
47  * @author Jan N. Klug - Initial contribution
48  */
49 @ExtendWith(MockitoExtension.class)
50 @NonNullByDefault
51 public class OwserverConnectionTest extends JavaTest {
52     private static final String TEST_HOST = "127.0.0.1";
53
54     private @Nullable OwserverTestServer testServer;
55     private @Nullable OwserverConnection owserverConnection;
56
57     private @Mock @NonNullByDefault({}) OwserverBridgeHandler bridgeHandler;
58
59     private int testPort;
60
61     @BeforeEach
62     public void setup() throws Exception {
63         CompletableFuture<Boolean> serverStarted = new CompletableFuture<>();
64         testPort = TestPortUtil.findFreePort();
65         try {
66             final OwserverTestServer testServer = new OwserverTestServer(testPort);
67             testServer.startServer(serverStarted);
68             this.testServer = testServer;
69         } catch (IOException e) {
70             fail("could not start test server");
71         }
72
73         final OwserverConnection owserverConnection = new OwserverConnection(bridgeHandler);
74         owserverConnection.setHost(TEST_HOST);
75         owserverConnection.setPort(testPort);
76         this.owserverConnection = owserverConnection;
77
78         serverStarted.get(); // wait for the server thread to start
79     }
80
81     @AfterEach
82     public void tearDown() {
83         try {
84             final OwserverTestServer testServer = this.testServer;
85             if (testServer != null) {
86                 testServer.stopServer();
87             }
88         } catch (IOException e) {
89             fail("could not stop test server");
90         }
91     }
92
93     @Test
94     public void successfulConnectionReportedToBridgeHandler() {
95         final OwserverConnection owserverConnection = this.owserverConnection;
96         if (owserverConnection == null) {
97             fail("connection is null");
98             return;
99         }
100         owserverConnection.start();
101
102         Mockito.verify(bridgeHandler).reportConnectionState(OwserverConnectionState.OPENED);
103     }
104
105     @Test
106     public void failedConnectionReportedToBridgeHandler() {
107         final OwserverConnection owserverConnection = this.owserverConnection;
108         if (owserverConnection == null) {
109             fail("connection is null");
110             return;
111         }
112         owserverConnection.setPort(1);
113
114         owserverConnection.start();
115
116         Mockito.verify(bridgeHandler, timeout(100)).reportConnectionState(OwserverConnectionState.FAILED);
117     }
118
119     @Test
120     public void testGetDirectory() throws OwException {
121         final OwserverConnection owserverConnection = this.owserverConnection;
122         if (owserverConnection == null) {
123             fail("connection is null");
124             return;
125         }
126         owserverConnection.start();
127
128         List<SensorId> directory = owserverConnection.getDirectory("/");
129
130         assertEquals(3, directory.size());
131         assertEquals(new SensorId("/00.0123456789ab"), directory.get(0));
132         assertEquals(new SensorId("/00.0123456789ac"), directory.get(1));
133         assertEquals(new SensorId("/00.0123456789ad"), directory.get(2));
134     }
135
136     @Test
137     public void testCheckPresence() {
138         final OwserverConnection owserverConnection = this.owserverConnection;
139         if (owserverConnection == null) {
140             fail("connection is null");
141             return;
142         }
143         owserverConnection.start();
144         State presence = owserverConnection.checkPresence("present");
145         assertEquals(OnOffType.ON, presence);
146
147         presence = owserverConnection.checkPresence("notpresent");
148         assertEquals(OnOffType.OFF, presence);
149     }
150
151     @Test
152     public void testReadDecimalType() throws OwException {
153         final OwserverConnection owserverConnection = this.owserverConnection;
154         if (owserverConnection == null) {
155             fail("connection is null");
156             return;
157         }
158         owserverConnection.start();
159
160         DecimalType number = (DecimalType) owserverConnection.readDecimalType("testsensor/decimal");
161
162         assertEquals(17.4, number.doubleValue(), 0.01);
163     }
164
165     @Test
166     public void testReadDecimalTypeArray() throws OwException {
167         final OwserverConnection owserverConnection = this.owserverConnection;
168         if (owserverConnection == null) {
169             fail("connection is null");
170             return;
171         }
172         owserverConnection.start();
173
174         List<State> numbers = owserverConnection.readDecimalTypeArray("testsensor/decimalarray");
175
176         assertEquals(3834, ((DecimalType) numbers.get(0)).intValue());
177         assertEquals(0, ((DecimalType) numbers.get(1)).intValue());
178     }
179
180     @Test
181     public void testGetPages() throws OwException {
182         final OwserverConnection owserverConnection = this.owserverConnection;
183         if (owserverConnection == null) {
184             fail("connection is null");
185             return;
186         }
187         owserverConnection.start();
188
189         OwPageBuffer pageBuffer = owserverConnection.readPages("testsensor");
190         assertEquals(31, pageBuffer.getByte(5, 7));
191     }
192
193     @Test
194     public void testWriteDecimalType() throws OwException {
195         final OwserverConnection owserverConnection = this.owserverConnection;
196         if (owserverConnection == null) {
197             fail("connection is null");
198             return;
199         }
200         owserverConnection.start();
201
202         owserverConnection.writeDecimalType("testsensor/decimal", new DecimalType(2009));
203
204         Mockito.verify(bridgeHandler, never()).reportConnectionState(OwserverConnectionState.FAILED);
205     }
206 }