2 * Copyright (c) 2010-2022 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.*;
17 import java.io.IOException;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.neohub.internal.NeoHubBindingConstants;
22 import org.openhab.binding.neohub.internal.NeoHubConfiguration;
23 import org.openhab.binding.neohub.internal.NeoHubException;
24 import org.openhab.binding.neohub.internal.NeoHubSocket;
25 import org.openhab.binding.neohub.internal.NeoHubWebSocket;
28 * JUnit for testing WSS and TCP socket protocols.
30 * @author Andrew Fiddian-Green - Initial contribution
34 public class NeoHubProtocolTests {
37 * Test online communication. Requires an actual Neohub to be present on the LAN. Configuration parameters must be
38 * entered for the actual specific Neohub instance as follows:
40 * - HUB_IP_ADDRESS the dotted ip address of the hub
41 * - HUB_API_TOKEN the api access token for the hub
42 * - SOCKET_TIMEOUT the connection time out
43 * - RUN_WSS_TEST enable testing the WSS communication
44 * - RUN_TCP_TEST enable testing the TCP communication
46 * NOTE: only run these tests if a device is actually available
49 private static final String HUB_IP_ADDRESS = "192.168.1.xxx";
50 private static final String HUB_API_TOKEN = "12345678-1234-1234-1234-123456789ABC";
51 private static final int SOCKET_TIMEOUT = 5;
52 private static final boolean RUN_WSS_TEST = false;
53 private static final boolean RUN_TCP_TEST = false;
56 * Use web socket to send a request, and check for a response.
58 * @throws NeoHubException
62 void testWssConnection() throws NeoHubException, IOException {
64 if (!NeoHubJsonTests.VALID_IP_V4_ADDRESS.matcher(HUB_IP_ADDRESS).matches()) {
68 NeoHubConfiguration config = new NeoHubConfiguration();
69 config.hostName = HUB_IP_ADDRESS;
70 config.socketTimeout = SOCKET_TIMEOUT;
71 config.apiToken = HUB_API_TOKEN;
73 NeoHubWebSocket socket = new NeoHubWebSocket(config);
74 String requestJson = NeoHubBindingConstants.CMD_CODE_FIRMWARE;
75 String responseJson = socket.sendMessage(requestJson);
76 assertNotEquals(0, responseJson.length());
82 * Use TCP socket to send a request, and check for a response.
84 * @throws NeoHubException
88 void testTcpConnection() throws IOException, NeoHubException {
90 if (!NeoHubJsonTests.VALID_IP_V4_ADDRESS.matcher(HUB_IP_ADDRESS).matches()) {
94 NeoHubConfiguration config = new NeoHubConfiguration();
95 config.hostName = HUB_IP_ADDRESS;
96 config.socketTimeout = SOCKET_TIMEOUT;
97 config.apiToken = HUB_API_TOKEN;
99 NeoHubSocket socket = new NeoHubSocket(config);
100 String requestJson = NeoHubBindingConstants.CMD_CODE_FIRMWARE;
101 String responseJson = socket.sendMessage(requestJson);
102 assertNotEquals(0, responseJson.length());