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