]> git.basschouten.com Git - openhab-addons.git/blob
92f3d66b56bb4f0cd392bf282ab4f6618726b2e6
[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.lametrictime.internal.api.local.impl;
14
15 import static org.junit.jupiter.api.Assertions.assertThrows;
16
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.Arrays;
22 import java.util.Properties;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.junit.jupiter.api.BeforeAll;
27 import org.junit.jupiter.api.Disabled;
28 import org.junit.jupiter.api.Test;
29 import org.openhab.binding.lametrictime.internal.api.dto.CoreApps;
30 import org.openhab.binding.lametrictime.internal.api.dto.enums.BrightnessMode;
31 import org.openhab.binding.lametrictime.internal.api.dto.enums.Priority;
32 import org.openhab.binding.lametrictime.internal.api.dto.enums.SoundCategory;
33 import org.openhab.binding.lametrictime.internal.api.local.ApplicationActionException;
34 import org.openhab.binding.lametrictime.internal.api.local.ApplicationActivationException;
35 import org.openhab.binding.lametrictime.internal.api.local.ApplicationNotFoundException;
36 import org.openhab.binding.lametrictime.internal.api.local.LocalConfiguration;
37 import org.openhab.binding.lametrictime.internal.api.local.NotificationCreationException;
38 import org.openhab.binding.lametrictime.internal.api.local.NotificationNotFoundException;
39 import org.openhab.binding.lametrictime.internal.api.local.UpdateException;
40 import org.openhab.binding.lametrictime.internal.api.local.dto.Audio;
41 import org.openhab.binding.lametrictime.internal.api.local.dto.Bluetooth;
42 import org.openhab.binding.lametrictime.internal.api.local.dto.Display;
43 import org.openhab.binding.lametrictime.internal.api.local.dto.Frame;
44 import org.openhab.binding.lametrictime.internal.api.local.dto.GoalData;
45 import org.openhab.binding.lametrictime.internal.api.local.dto.Notification;
46 import org.openhab.binding.lametrictime.internal.api.local.dto.NotificationModel;
47 import org.openhab.binding.lametrictime.internal.api.local.dto.Sound;
48 import org.openhab.binding.lametrictime.internal.api.test.TestUtil;
49
50 /**
51  * <p>
52  * This test is excluded from the normal battery of tests because it is not a
53  * unit test, but rather a live test against an actual device. The purpose of
54  * this test is to make sure that after a firmware upgrade, the device still
55  * responds in a backwards compatible way.
56  * </p>
57  * <br>
58  * <p>
59  * To run this test, first create a file called 'device.properties' in the
60  * matching package as this class under 'src/test/resources' with two
61  * properties: 'host' and 'apiKey'. After putting the configuration in place,
62  * either execute the test via your IDE or run 'mvn -DskipITs=false
63  * integration-test'.
64  * </p>
65  *
66  * @author Gregory Moyer - Initial contribution
67  */
68 @Disabled
69 @NonNullByDefault
70 public class LaMetricTimeLocalImplIT {
71     private static final String PROP_HOST = "host";
72     private static final String PROP_API_KEY = "apiKey";
73
74     @Nullable
75     private static LaMetricTimeLocalImpl local;
76
77     @BeforeAll
78     public static void setup() throws IOException {
79         File file = TestUtil.getTestDataPath(LaMetricTimeLocalImplIT.class, "device.properties").toFile();
80         if (!file.exists()) {
81             throw new IllegalStateException("Device configuration properties missing at " + file.getAbsolutePath());
82         }
83
84         try (InputStream in = new FileInputStream(file)) {
85             Properties properties = new Properties();
86             properties.load(in);
87
88             if (!properties.containsKey(PROP_HOST)) {
89                 throw new IllegalStateException("Device configuration property " + PROP_HOST + " was not found");
90             }
91
92             if (!properties.containsKey(PROP_API_KEY)) {
93                 throw new IllegalStateException("Device configuration property " + PROP_API_KEY + " was not found");
94             }
95
96             LocalConfiguration config = new LocalConfiguration().withHost(properties.getProperty(PROP_HOST))
97                     .withApiKey(properties.getProperty(PROP_API_KEY)).withLogging(true);
98             local = new LaMetricTimeLocalImpl(config);
99         }
100     }
101
102     @Test
103     public void testGetApi() {
104         local.getApi();
105     }
106
107     @Test
108     public void testGetDevice() {
109         local.getDevice();
110     }
111
112     @Test
113     public void testCreateAndGetNotification() throws NotificationCreationException, NotificationNotFoundException {
114         String id = local.createNotification(buildSimpleNotification(1));
115         local.getCurrentNotification();
116         local.getNotification(id);
117     }
118
119     @Test
120     public void testCreateGoalNotification() throws NotificationCreationException, NotificationNotFoundException {
121         local.createNotification(buildGoalNotification(1));
122     }
123
124     @Test
125     public void testCreateChartNotification() throws NotificationCreationException, NotificationNotFoundException {
126         local.createNotification(buildChartNotification(1));
127     }
128
129     @Test
130     public void testGetNotifications() {
131         local.getNotifications();
132     }
133
134     @Test
135     public void testGetInvalidNotification() {
136         assertThrows(NotificationNotFoundException.class, () -> local.getNotification("invalid"));
137     }
138
139     @Test
140     public void testCreateAndDeleteNotification() throws NotificationCreationException, NotificationNotFoundException {
141         String id = local.createNotification(buildSimpleNotification(0));
142         local.deleteNotification(id);
143     }
144
145     @Test
146     public void testGetDisplay() {
147         local.getDisplay();
148     }
149
150     @Test
151     public void testUpdateDisplay() throws UpdateException {
152         local.updateDisplay(new Display().withBrightnessMode(BrightnessMode.AUTO.toRaw()));
153     }
154
155     @Test
156     public void testGetAudio() {
157         local.getAudio();
158     }
159
160     @Test
161     public void testUpdateAudio() throws UpdateException {
162         local.updateAudio(new Audio().withVolume(25));
163     }
164
165     @Test
166     public void testGetBluetooth() {
167         local.getBluetooth();
168     }
169
170     @Test
171     public void testUpdateBluetooth() throws UpdateException {
172         local.updateBluetooth(new Bluetooth().withActive(false));
173     }
174
175     @Test
176     public void testGetWifi() {
177         local.getWifi();
178     }
179
180     @Test
181     public void testGetApplications() {
182         local.getApplications();
183     }
184
185     @Test
186     public void testGetClockApplication() throws ApplicationNotFoundException {
187         local.getApplication(CoreApps.clock().getPackageName());
188     }
189
190     @Test
191     public void testGetCountdownApplication() throws ApplicationNotFoundException {
192         local.getApplication(CoreApps.countdown().getPackageName());
193     }
194
195     @Test
196     public void testGetRadioApplication() throws ApplicationNotFoundException {
197         local.getApplication(CoreApps.radio().getPackageName());
198     }
199
200     @Test
201     public void testGetStopwatchApplication() throws ApplicationNotFoundException {
202         local.getApplication(CoreApps.stopwatch().getPackageName());
203     }
204
205     @Test
206     public void testGetWeatherApplication() throws ApplicationNotFoundException {
207         local.getApplication(CoreApps.weather().getPackageName());
208     }
209
210     @Test
211     public void testGetInvalidApplication() {
212         assertThrows(ApplicationNotFoundException.class, () -> local.getApplication("invalid"));
213     }
214
215     @Test
216     public void testActivatePreviousApplication() {
217         local.activatePreviousApplication();
218     }
219
220     @Test
221     public void testActivateNextApplication() {
222         local.activateNextApplication();
223     }
224
225     @Test
226     public void testActivateApplication() throws ApplicationActivationException, ApplicationNotFoundException {
227         // delete any notifications on the device or else the activate fails
228         local.getNotifications().stream().forEach(n -> {
229             try {
230                 local.deleteNotification(n.getId());
231             } catch (NotificationNotFoundException e) {
232                 // ignore
233             }
234         });
235
236         local.activateApplication(CoreApps.clock().getPackageName(),
237                 local.getApplication(CoreApps.clock().getPackageName()).getWidgets().firstKey());
238     }
239
240     @Test
241     public void testDoAction() throws ApplicationActionException, ApplicationNotFoundException {
242         local.doAction(CoreApps.weather().getPackageName(),
243                 local.getApplication(CoreApps.weather().getPackageName()).getWidgets().firstKey(),
244                 CoreApps.weather().forecast().getAction());
245     }
246
247     private Notification buildSimpleNotification(int cycles) {
248         return new Notification().withPriority(Priority.CRITICAL.toRaw()).withModel(new NotificationModel()
249                 .withCycles(cycles)
250                 .withSound(new Sound().withCategory(SoundCategory.NOTIFICATIONS.toRaw())
251                         .withId(org.openhab.binding.lametrictime.internal.api.dto.enums.Sound.CAT.toRaw()))
252                 .withFrames(Arrays.asList(new Frame().withText("CAT!").withIcon(
253                         "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAUklEQVQYlWNUVFBgYGBgYBC98uE/AxJ4rSPAyMDAwMCETRJZjAnGgOlAZote+fCfCV0nOmA0+yKAYTwygJuAzQoGBgYGRkUFBQZ0dyDzGQl5EwCTESNpFb6zEwAAAABJRU5ErkJggg=="))));
254     }
255
256     private Notification buildGoalNotification(int cycles) {
257         return new Notification().withPriority(Priority.CRITICAL.toRaw())
258                 .withModel(new NotificationModel().withCycles(cycles).withFrames(Arrays.asList(new Frame()
259                         .withGoalData(new GoalData().withStart(0).withCurrent(50).withEnd(100).withUnit("%")).withIcon(
260                                 "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAUklEQVQYlWNUVFBgYGBgYBC98uE/AxJ4rSPAyMDAwMCETRJZjAnGgOlAZote+fCfCV0nOmA0+yKAYTwygJuAzQoGBgYGRkUFBQZ0dyDzGQl5EwCTESNpFb6zEwAAAABJRU5ErkJggg=="))));
261     }
262
263     private Notification buildChartNotification(int cycles) {
264         return new Notification().withPriority(Priority.CRITICAL.toRaw()).withModel(new NotificationModel()
265                 .withCycles(cycles)
266                 .withFrames(Arrays.asList(new Frame().withChartData(Arrays.asList(1, 2, 3, 4, 5, 6, 7)).withIcon(
267                         "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAUklEQVQYlWNUVFBgYGBgYBC98uE/AxJ4rSPAyMDAwMCETRJZjAnGgOlAZote+fCfCV0nOmA0+yKAYTwygJuAzQoGBgYGRkUFBQZ0dyDzGQl5EwCTESNpFb6zEwAAAABJRU5ErkJggg=="))));
268     }
269 }