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.hdpowerview.internal;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.hdpowerview.internal.dto.CoordinateSystem.*;
18 import java.util.List;
19 import java.util.regex.Pattern;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jetty.client.HttpClient;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase;
25 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase.Capabilities;
26 import org.openhab.binding.hdpowerview.internal.dto.Scene;
27 import org.openhab.binding.hdpowerview.internal.dto.ShadeData;
28 import org.openhab.binding.hdpowerview.internal.dto.ShadePosition;
29 import org.openhab.binding.hdpowerview.internal.dto.responses.Scenes;
30 import org.openhab.binding.hdpowerview.internal.dto.responses.Shades;
31 import org.openhab.binding.hdpowerview.internal.exceptions.HubException;
32 import org.openhab.binding.hdpowerview.internal.exceptions.HubMaintenanceException;
33 import org.openhab.binding.hdpowerview.internal.exceptions.HubProcessingException;
34 import org.openhab.core.library.types.PercentType;
35 import org.openhab.core.types.State;
38 * Unit tests for HD PowerView binding.
40 * @author Andrew Fiddian-Green - Initial contribution
43 public class OnlineCommunicationTest {
45 private static final Pattern VALID_IP_V4_ADDRESS = Pattern
46 .compile("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}\\b");
49 * Run a series of ONLINE tests on the communication with a hub.
51 * @param hubIPAddress must be a valid hub IP address to run the
52 * tests on; or an INVALID IP address to
54 * @param allowShadeMovementCommands set to true if you accept that the tests
55 * shall physically move the shades
58 public void testOnlineCommunication() {
60 * NOTE: in order to actually run these tests you must have a hub physically
61 * available, and its IP address must be correctly configured in the
62 * "hubIPAddress" string constant e.g. "192.168.1.123"
64 String hubIPAddress = "192.168.1.xxx";
67 * NOTE: set allowShadeMovementCommands = true if you accept physically moving
68 * the shades during these tests
70 boolean allowShadeMovementCommands = false;
72 if (VALID_IP_V4_ADDRESS.matcher(hubIPAddress).matches()) {
73 // ==== initialize stuff ====
74 HttpClient client = new HttpClient();
75 assertNotNull(client);
77 // ==== start the client ====
80 assertTrue(client.isStarted());
81 } catch (Exception e) {
85 HDPowerViewWebTargets webTargets = new HDPowerViewWebTargets(client, hubIPAddress);
86 assertNotNull(webTargets);
89 ShadePosition shadePos = null;
90 Shades shadesX = null;
92 // ==== get all shades ====
94 shadesX = webTargets.getShades();
95 assertNotNull(shadesX);
96 List<ShadeData> shadesData = shadesX.shadeData;
97 assertNotNull(shadesData);
99 assertTrue(!shadesData.isEmpty());
101 shadeData = shadesData.get(0);
102 assertNotNull(shadeData);
103 assertTrue(shadeData.getName().length() > 0);
104 shadePos = shadeData.positions;
105 assertNotNull(shadePos);
106 ShadeData shadeZero = shadesData.get(0);
107 assertNotNull(shadeZero);
108 shadeId = shadeZero.id;
109 assertNotEquals(0, shadeId);
111 for (ShadeData shadexData : shadesData) {
112 String shadeName = shadexData.getName();
113 assertNotNull(shadeName);
115 } catch (HubException e) {
116 fail(e.getMessage());
119 // ==== get all scenes ====
122 Scenes scenes = webTargets.getScenes();
123 assertNotNull(scenes);
125 List<Scene> scenesData = scenes.sceneData;
126 assertNotNull(scenesData);
128 assertTrue(!scenesData.isEmpty());
129 Scene sceneZero = scenesData.get(0);
130 assertNotNull(sceneZero);
131 sceneId = sceneZero.id;
132 assertTrue(sceneId > 0);
134 for (Scene scene : scenesData) {
135 String sceneName = scene.getName();
136 assertNotNull(sceneName);
138 } catch (HubException e) {
139 fail(e.getMessage());
142 // ==== refresh a specific shade ====
143 ShadeData shadeData = null;
145 assertNotEquals(0, shadeId);
146 shadeData = webTargets.refreshShadePosition(shadeId);
147 } catch (HubException e) {
148 fail(e.getMessage());
151 // ==== move a specific shade ====
153 assertNotEquals(0, shadeId);
155 if (shadeData != null) {
156 ShadePosition positions = shadeData.positions;
157 assertNotNull(positions);
158 Integer capabilitiesValue = shadeData.capabilities;
159 assertNotNull(capabilitiesValue);
161 Capabilities capabilities = new ShadeCapabilitiesDatabase()
162 .getCapabilities(capabilitiesValue.intValue());
164 State pos = positions.getState(capabilities, PRIMARY_POSITION);
165 assertEquals(PercentType.class, pos.getClass());
167 int position = ((PercentType) pos).intValue();
168 position = position + ((position <= 10) ? 5 : -5);
170 ShadePosition targetPosition = new ShadePosition().setPosition(capabilities, PRIMARY_POSITION,
172 assertNotNull(targetPosition);
174 if (allowShadeMovementCommands) {
175 webTargets.moveShade(shadeId, targetPosition);
177 ShadeData newData = webTargets.getShade(shadeId);
178 ShadePosition actualPosition = newData.positions;
179 assertNotNull(actualPosition);
180 assertEquals(targetPosition.getState(capabilities, PRIMARY_POSITION),
181 actualPosition.getState(capabilities, PRIMARY_POSITION));
184 } catch (HubException e) {
185 fail(e.getMessage());
188 // ==== activate a specific scene ====
189 if (allowShadeMovementCommands) {
191 assertNotNull(sceneId);
192 webTargets.activateScene(sceneId);
193 } catch (HubProcessingException | HubMaintenanceException e) {
194 fail(e.getMessage());
198 // ==== test stop command ====
199 if (allowShadeMovementCommands) {
201 assertNotNull(sceneId);
202 webTargets.stopShade(shadeId);
203 } catch (HubException e) {
204 fail(e.getMessage());
208 // ==== stop the client ====
209 if (client.isRunning()) {
212 } catch (Exception e) {
213 fail(e.getMessage());