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