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.hdpowerview;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.hdpowerview.internal.api.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.HDPowerViewWebTargets;
25 import org.openhab.binding.hdpowerview.internal.api.ShadePosition;
26 import org.openhab.binding.hdpowerview.internal.api.responses.Scenes;
27 import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene;
28 import org.openhab.binding.hdpowerview.internal.api.responses.Shades;
29 import org.openhab.binding.hdpowerview.internal.api.responses.Shades.ShadeData;
30 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase;
31 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase.Capabilities;
32 import org.openhab.binding.hdpowerview.internal.exceptions.HubException;
33 import org.openhab.binding.hdpowerview.internal.exceptions.HubMaintenanceException;
34 import org.openhab.binding.hdpowerview.internal.exceptions.HubProcessingException;
35 import org.openhab.core.library.types.PercentType;
36 import org.openhab.core.types.State;
39 * Unit tests for HD PowerView binding.
41 * @author Andrew Fiddian-Green - Initial contribution
44 public class OnlineCommunicationTest {
46 private static final Pattern VALID_IP_V4_ADDRESS = Pattern
47 .compile("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}\\b");
50 * Run a series of ONLINE tests on the communication with a hub.
52 * @param hubIPAddress must be a valid hub IP address to run the
53 * tests on; or an INVALID IP address to
55 * @param allowShadeMovementCommands set to true if you accept that the tests
56 * shall physically move the shades
59 public void testOnlineCommunication() {
61 * NOTE: in order to actually run these tests you must have a hub physically
62 * available, and its IP address must be correctly configured in the
63 * "hubIPAddress" string constant e.g. "192.168.1.123"
65 String hubIPAddress = "192.168.1.xxx";
68 * NOTE: set allowShadeMovementCommands = true if you accept physically moving
69 * the shades during these tests
71 boolean allowShadeMovementCommands = false;
73 if (VALID_IP_V4_ADDRESS.matcher(hubIPAddress).matches()) {
74 // ==== initialize stuff ====
75 HttpClient client = new HttpClient();
76 assertNotNull(client);
78 // ==== start the client ====
81 assertTrue(client.isStarted());
82 } catch (Exception e) {
86 HDPowerViewWebTargets webTargets = new HDPowerViewWebTargets(client, hubIPAddress);
87 assertNotNull(webTargets);
90 ShadePosition shadePos = null;
91 Shades shadesX = null;
93 // ==== get all shades ====
95 shadesX = webTargets.getShades();
96 assertNotNull(shadesX);
97 List<ShadeData> shadesData = shadesX.shadeData;
98 assertNotNull(shadesData);
100 assertTrue(!shadesData.isEmpty());
102 shadeData = shadesData.get(0);
103 assertNotNull(shadeData);
104 assertTrue(shadeData.getName().length() > 0);
105 shadePos = shadeData.positions;
106 assertNotNull(shadePos);
107 ShadeData shadeZero = shadesData.get(0);
108 assertNotNull(shadeZero);
109 shadeId = shadeZero.id;
110 assertNotEquals(0, shadeId);
112 for (ShadeData shadexData : shadesData) {
113 String shadeName = shadexData.getName();
114 assertNotNull(shadeName);
116 } catch (HubException e) {
117 fail(e.getMessage());
120 // ==== get all scenes ====
123 Scenes scenes = webTargets.getScenes();
124 assertNotNull(scenes);
126 List<Scene> scenesData = scenes.sceneData;
127 assertNotNull(scenesData);
129 assertTrue(!scenesData.isEmpty());
130 Scene sceneZero = scenesData.get(0);
131 assertNotNull(sceneZero);
132 sceneId = sceneZero.id;
133 assertTrue(sceneId > 0);
135 for (Scene scene : scenesData) {
136 String sceneName = scene.getName();
137 assertNotNull(sceneName);
139 } catch (HubException e) {
140 fail(e.getMessage());
143 // ==== refresh a specific shade ====
144 ShadeData shadeData = null;
146 assertNotEquals(0, shadeId);
147 shadeData = webTargets.refreshShadePosition(shadeId);
148 } catch (HubException e) {
149 fail(e.getMessage());
152 // ==== move a specific shade ====
154 assertNotEquals(0, shadeId);
156 if (shadeData != null) {
157 ShadePosition positions = shadeData.positions;
158 assertNotNull(positions);
159 Integer capabilitiesValue = shadeData.capabilities;
160 assertNotNull(capabilitiesValue);
162 Capabilities capabilities = new ShadeCapabilitiesDatabase()
163 .getCapabilities(capabilitiesValue.intValue());
165 State pos = positions.getState(capabilities, PRIMARY_POSITION);
166 assertEquals(PercentType.class, pos.getClass());
168 int position = ((PercentType) pos).intValue();
169 position = position + ((position <= 10) ? 5 : -5);
171 ShadePosition targetPosition = new ShadePosition().setPosition(capabilities, PRIMARY_POSITION,
173 assertNotNull(targetPosition);
175 if (allowShadeMovementCommands) {
176 webTargets.moveShade(shadeId, targetPosition);
178 ShadeData newData = webTargets.getShade(shadeId);
179 ShadePosition actualPosition = newData.positions;
180 assertNotNull(actualPosition);
181 assertEquals(targetPosition.getState(capabilities, PRIMARY_POSITION),
182 actualPosition.getState(capabilities, PRIMARY_POSITION));
185 } catch (HubException e) {
186 fail(e.getMessage());
189 // ==== activate a specific scene ====
190 if (allowShadeMovementCommands) {
192 assertNotNull(sceneId);
193 webTargets.activateScene(sceneId);
194 } catch (HubProcessingException | HubMaintenanceException e) {
195 fail(e.getMessage());
199 // ==== test stop command ====
200 if (allowShadeMovementCommands) {
202 assertNotNull(sceneId);
203 webTargets.stopShade(shadeId);
204 } catch (HubException e) {
205 fail(e.getMessage());
209 // ==== stop the client ====
210 if (client.isRunning()) {
213 } catch (Exception e) {
214 fail(e.getMessage());