]> git.basschouten.com Git - openhab-addons.git/blob
86cc7ae79b1df22e90ddabec34842d536b81e6b6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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
17 import java.io.IOException;
18
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;
26
27 /**
28  * JUnit for testing WSS and TCP socket protocols.
29  *
30  * @author Andrew Fiddian-Green - Initial contribution
31  *
32  */
33 @NonNullByDefault
34 public class NeoHubProtocolTests {
35
36     /**
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:
39      *
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
45      *
46      * NOTE: only run these tests if a device is actually available
47      *
48      */
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;
54
55     /**
56      * Use web socket to send a request, and check for a response.
57      *
58      * @throws NeoHubException
59      * @throws IOException
60      */
61     @Test
62     void testWssConnection() throws NeoHubException, IOException {
63         if (RUN_WSS_TEST) {
64             if (!NeoHubJsonTests.VALID_IP_V4_ADDRESS.matcher(HUB_IP_ADDRESS).matches()) {
65                 fail();
66             }
67
68             NeoHubConfiguration config = new NeoHubConfiguration();
69             config.hostName = HUB_IP_ADDRESS;
70             config.socketTimeout = SOCKET_TIMEOUT;
71             config.apiToken = HUB_API_TOKEN;
72
73             NeoHubWebSocket socket = new NeoHubWebSocket(config);
74             String requestJson = NeoHubBindingConstants.CMD_CODE_FIRMWARE;
75             String responseJson = socket.sendMessage(requestJson);
76             assertNotEquals(0, responseJson.length());
77             socket.close();
78         }
79     }
80
81     /**
82      * Use TCP socket to send a request, and check for a response.
83      *
84      * @throws NeoHubException
85      * @throws IOException
86      */
87     @Test
88     void testTcpConnection() throws IOException, NeoHubException {
89         if (RUN_TCP_TEST) {
90             if (!NeoHubJsonTests.VALID_IP_V4_ADDRESS.matcher(HUB_IP_ADDRESS).matches()) {
91                 fail();
92             }
93
94             NeoHubConfiguration config = new NeoHubConfiguration();
95             config.hostName = HUB_IP_ADDRESS;
96             config.socketTimeout = SOCKET_TIMEOUT;
97             config.apiToken = HUB_API_TOKEN;
98
99             NeoHubSocket socket = new NeoHubSocket(config);
100             String requestJson = NeoHubBindingConstants.CMD_CODE_FIRMWARE;
101             String responseJson = socket.sendMessage(requestJson);
102             assertNotEquals(0, responseJson.length());
103             socket.close();
104         }
105     }
106 }