2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.neohub.test;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
19 import java.io.IOException;
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;
35 * JUnit for testing WSS and TCP socket protocols.
37 * @author Andrew Fiddian-Green - Initial contribution
41 public class NeoHubProtocolTests {
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:
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
53 * NOTE: only run these tests if a device is actually available
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;
63 * Use web socket to send a request, and check for a response.
65 * @throws NeoHubException
69 void testWssConnection() throws NeoHubException, IOException {
71 if (!NeoHubJsonTests.VALID_IP_V4_ADDRESS.matcher(HUB_IP_ADDRESS).matches()) {
75 NeoHubConfiguration config = new NeoHubConfiguration();
76 config.hostName = HUB_IP_ADDRESS;
77 config.socketTimeout = SOCKET_TIMEOUT;
78 config.apiToken = HUB_API_TOKEN;
80 SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
81 sslContextFactory.setTrustAll(true);
82 HttpClient httpClient = new HttpClient(sslContextFactory);
83 WebSocketClient webSocketClient = new WebSocketClient(httpClient);
85 WebSocketFactory webSocketFactory = mock(WebSocketFactory.class);
86 when(webSocketFactory.createWebSocketClient(anyString(), any())).thenReturn(webSocketClient);
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());
97 * Use TCP socket to send a request, and check for a response.
99 * @throws NeoHubException
100 * @throws IOException
103 void testTcpConnection() throws IOException, NeoHubException {
105 if (!NeoHubJsonTests.VALID_IP_V4_ADDRESS.matcher(HUB_IP_ADDRESS).matches()) {
109 NeoHubConfiguration config = new NeoHubConfiguration();
110 config.hostName = HUB_IP_ADDRESS;
111 config.socketTimeout = SOCKET_TIMEOUT;
112 config.apiToken = HUB_API_TOKEN;
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());