]> git.basschouten.com Git - openhab-addons.git/blob
8fd11d71ec9f446eb4d2482c9398be2cd425ea02
[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.neohub.test;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
18
19 import java.io.IOException;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jetty.client.HttpClient;
23 import org.eclipse.jetty.util.ssl.SslContextFactory;
24 import org.eclipse.jetty.websocket.client.WebSocketClient;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.neohub.internal.NeoHubBindingConstants;
27 import org.openhab.binding.neohub.internal.NeoHubConfiguration;
28 import org.openhab.binding.neohub.internal.NeoHubException;
29 import org.openhab.binding.neohub.internal.NeoHubSocket;
30 import org.openhab.binding.neohub.internal.NeoHubWebSocket;
31 import org.openhab.core.io.net.http.WebSocketFactory;
32 import org.openhab.core.thing.ThingUID;
33
34 /**
35  * JUnit for testing WSS and TCP socket protocols.
36  *
37  * @author Andrew Fiddian-Green - Initial contribution
38  *
39  */
40 @NonNullByDefault
41 public class NeoHubProtocolTests {
42
43     /**
44      * Test online communication. Requires an actual Neohub to be present on the LAN. Configuration parameters must be
45      * entered for the actual specific Neohub instance as follows:
46      *
47      * - HUB_IP_ADDRESS the dotted ip address of the hub
48      * - HUB_API_TOKEN the api access token for the hub
49      * - SOCKET_TIMEOUT the connection time out
50      * - RUN_WSS_TEST enable testing the WSS communication
51      * - RUN_TCP_TEST enable testing the TCP communication
52      *
53      * NOTE: only run these tests if a device is actually available
54      *
55      */
56     private static final String HUB_IP_ADDRESS = "192.168.1.xxx";
57     private static final String HUB_API_TOKEN = "12345678-1234-1234-1234-123456789ABC";
58     private static final int SOCKET_TIMEOUT = 5;
59     private static final boolean RUN_WSS_TEST = false;
60     private static final boolean RUN_TCP_TEST = false;
61
62     /**
63      * Use web socket to send a request, and check for a response.
64      *
65      * @throws NeoHubException
66      * @throws IOException
67      */
68     @Test
69     void testWssConnection() throws NeoHubException, IOException {
70         if (RUN_WSS_TEST) {
71             if (!NeoHubJsonTests.VALID_IP_V4_ADDRESS.matcher(HUB_IP_ADDRESS).matches()) {
72                 fail();
73             }
74
75             NeoHubConfiguration config = new NeoHubConfiguration();
76             config.hostName = HUB_IP_ADDRESS;
77             config.socketTimeout = SOCKET_TIMEOUT;
78             config.apiToken = HUB_API_TOKEN;
79
80             SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
81             sslContextFactory.setTrustAll(true);
82             HttpClient httpClient = new HttpClient(sslContextFactory);
83             WebSocketClient webSocketClient = new WebSocketClient(httpClient);
84
85             WebSocketFactory webSocketFactory = mock(WebSocketFactory.class);
86             when(webSocketFactory.createWebSocketClient(anyString(), any())).thenReturn(webSocketClient);
87
88             NeoHubWebSocket socket = new NeoHubWebSocket(config, webSocketFactory, new ThingUID("neohub:account:test"));
89             String requestJson = NeoHubBindingConstants.CMD_CODE_FIRMWARE;
90             String responseJson = socket.sendMessage(requestJson);
91             assertNotEquals(0, responseJson.length());
92             socket.close();
93         }
94     }
95
96     /**
97      * Use TCP socket to send a request, and check for a response.
98      *
99      * @throws NeoHubException
100      * @throws IOException
101      */
102     @Test
103     void testTcpConnection() throws IOException, NeoHubException {
104         if (RUN_TCP_TEST) {
105             if (!NeoHubJsonTests.VALID_IP_V4_ADDRESS.matcher(HUB_IP_ADDRESS).matches()) {
106                 fail();
107             }
108
109             NeoHubConfiguration config = new NeoHubConfiguration();
110             config.hostName = HUB_IP_ADDRESS;
111             config.socketTimeout = SOCKET_TIMEOUT;
112             config.apiToken = HUB_API_TOKEN;
113
114             NeoHubSocket socket = new NeoHubSocket(config, "test");
115             String requestJson = NeoHubBindingConstants.CMD_CODE_FIRMWARE;
116             String responseJson = socket.sendMessage(requestJson);
117             assertNotEquals(0, responseJson.length());
118             socket.close();
119         }
120     }
121 }