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