]> git.basschouten.com Git - openhab-addons.git/blob
b8670bb91ec92b2f2601784ee3bfd1f3b6b88a86
[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.hdpowerview;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.hdpowerview.internal.api.CoordinateSystem.*;
17
18 import java.util.List;
19 import java.util.regex.Pattern;
20
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;
37
38 /**
39  * Unit tests for HD PowerView binding.
40  *
41  * @author Andrew Fiddian-Green - Initial contribution
42  */
43 @NonNullByDefault
44 public class OnlineCommunicationTest {
45
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");
48
49     /**
50      * Run a series of ONLINE tests on the communication with a hub.
51      *
52      * @param hubIPAddress must be a valid hub IP address to run the
53      *            tests on; or an INVALID IP address to
54      *            suppress the tests
55      * @param allowShadeMovementCommands set to true if you accept that the tests
56      *            shall physically move the shades
57      */
58     @Test
59     public void testOnlineCommunication() {
60         /*
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"
64          */
65         String hubIPAddress = "192.168.1.xxx";
66
67         /*
68          * NOTE: set allowShadeMovementCommands = true if you accept physically moving
69          * the shades during these tests
70          */
71         boolean allowShadeMovementCommands = false;
72
73         if (VALID_IP_V4_ADDRESS.matcher(hubIPAddress).matches()) {
74             // ==== initialize stuff ====
75             HttpClient client = new HttpClient();
76             assertNotNull(client);
77
78             // ==== start the client ====
79             try {
80                 client.start();
81                 assertTrue(client.isStarted());
82             } catch (Exception e) {
83                 fail(e.getMessage());
84             }
85
86             HDPowerViewWebTargets webTargets = new HDPowerViewWebTargets(client, hubIPAddress);
87             assertNotNull(webTargets);
88
89             int shadeId = 0;
90             ShadePosition shadePos = null;
91             Shades shadesX = null;
92
93             // ==== get all shades ====
94             try {
95                 shadesX = webTargets.getShades();
96                 assertNotNull(shadesX);
97                 List<ShadeData> shadesData = shadesX.shadeData;
98                 assertNotNull(shadesData);
99
100                 assertTrue(!shadesData.isEmpty());
101                 ShadeData shadeData;
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);
111
112                 for (ShadeData shadexData : shadesData) {
113                     String shadeName = shadexData.getName();
114                     assertNotNull(shadeName);
115                 }
116             } catch (HubException e) {
117                 fail(e.getMessage());
118             }
119
120             // ==== get all scenes ====
121             int sceneId = 0;
122             try {
123                 Scenes scenes = webTargets.getScenes();
124                 assertNotNull(scenes);
125
126                 List<Scene> scenesData = scenes.sceneData;
127                 assertNotNull(scenesData);
128
129                 assertTrue(!scenesData.isEmpty());
130                 Scene sceneZero = scenesData.get(0);
131                 assertNotNull(sceneZero);
132                 sceneId = sceneZero.id;
133                 assertTrue(sceneId > 0);
134
135                 for (Scene scene : scenesData) {
136                     String sceneName = scene.getName();
137                     assertNotNull(sceneName);
138                 }
139             } catch (HubException e) {
140                 fail(e.getMessage());
141             }
142
143             // ==== refresh a specific shade ====
144             ShadeData shadeData = null;
145             try {
146                 assertNotEquals(0, shadeId);
147                 shadeData = webTargets.refreshShadePosition(shadeId);
148             } catch (HubException e) {
149                 fail(e.getMessage());
150             }
151
152             // ==== move a specific shade ====
153             try {
154                 assertNotEquals(0, shadeId);
155
156                 if (shadeData != null) {
157                     ShadePosition positions = shadeData.positions;
158                     assertNotNull(positions);
159                     Integer capabilitiesValue = shadeData.capabilities;
160                     assertNotNull(capabilitiesValue);
161
162                     Capabilities capabilities = new ShadeCapabilitiesDatabase()
163                             .getCapabilities(capabilitiesValue.intValue());
164
165                     State pos = positions.getState(capabilities, PRIMARY_POSITION);
166                     assertEquals(PercentType.class, pos.getClass());
167
168                     int position = ((PercentType) pos).intValue();
169                     position = position + ((position <= 10) ? 5 : -5);
170
171                     ShadePosition targetPosition = new ShadePosition().setPosition(capabilities, PRIMARY_POSITION,
172                             position);
173                     assertNotNull(targetPosition);
174
175                     if (allowShadeMovementCommands) {
176                         webTargets.moveShade(shadeId, targetPosition);
177
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));
183                     }
184                 }
185             } catch (HubException e) {
186                 fail(e.getMessage());
187             }
188
189             // ==== activate a specific scene ====
190             if (allowShadeMovementCommands) {
191                 try {
192                     assertNotNull(sceneId);
193                     webTargets.activateScene(sceneId);
194                 } catch (HubProcessingException | HubMaintenanceException e) {
195                     fail(e.getMessage());
196                 }
197             }
198
199             // ==== test stop command ====
200             if (allowShadeMovementCommands) {
201                 try {
202                     assertNotNull(sceneId);
203                     webTargets.stopShade(shadeId);
204                 } catch (HubException e) {
205                     fail(e.getMessage());
206                 }
207             }
208
209             // ==== stop the client ====
210             if (client.isRunning()) {
211                 try {
212                     client.stop();
213                 } catch (Exception e) {
214                     fail(e.getMessage());
215                 }
216             }
217         }
218     }
219 }