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