2 * Copyright 2017-2018 Gregory Moyer and contributors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openhab.binding.lametrictime.api.local.impl;
18 import static org.junit.jupiter.api.Assertions.assertThrows;
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;
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;
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.
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
68 public class LaMetricTimeLocalImplIT {
69 private static final String PROP_HOST = "host";
70 private static final String PROP_API_KEY = "apiKey";
72 private static LaMetricTimeLocalImpl local;
75 public static void setup() throws IOException {
76 File file = TestUtil.getTestDataPath(LaMetricTimeLocalImplIT.class, "device.properties").toFile();
78 throw new IllegalStateException("Device configuration properties missing at " + file.getAbsolutePath());
81 try (InputStream in = new FileInputStream(file)) {
82 Properties properties = new Properties();
85 if (!properties.containsKey(PROP_HOST)) {
86 throw new IllegalStateException("Device configuration property " + PROP_HOST + " was not found");
89 if (!properties.containsKey(PROP_API_KEY)) {
90 throw new IllegalStateException("Device configuration property " + PROP_API_KEY + " was not found");
93 LocalConfiguration config = new LocalConfiguration().withHost(properties.getProperty(PROP_HOST))
94 .withApiKey(properties.getProperty(PROP_API_KEY)).withLogging(true);
95 local = new LaMetricTimeLocalImpl(config);
100 public void testGetApi() {
105 public void testGetDevice() {
110 public void testCreateAndGetNotification() throws NotificationCreationException, NotificationNotFoundException {
111 String id = local.createNotification(buildSimpleNotification(1));
112 local.getCurrentNotification();
113 local.getNotification(id);
117 public void testCreateGoalNotification() throws NotificationCreationException, NotificationNotFoundException {
118 local.createNotification(buildGoalNotification(1));
122 public void testCreateChartNotification() throws NotificationCreationException, NotificationNotFoundException {
123 local.createNotification(buildChartNotification(1));
127 public void testGetNotifications() {
128 local.getNotifications();
132 public void testGetInvalidNotification() {
133 assertThrows(NotificationNotFoundException.class, () -> local.getNotification("invalid"));
137 public void testCreateAndDeleteNotification() throws NotificationCreationException, NotificationNotFoundException {
138 String id = local.createNotification(buildSimpleNotification(0));
139 local.deleteNotification(id);
143 public void testGetDisplay() {
148 public void testUpdateDisplay() throws UpdateException {
149 local.updateDisplay(new Display().withBrightnessMode(BrightnessMode.AUTO.toRaw()));
153 public void testGetAudio() {
158 public void testUpdateAudio() throws UpdateException {
159 local.updateAudio(new Audio().withVolume(25));
163 public void testGetBluetooth() {
164 local.getBluetooth();
168 public void testUpdateBluetooth() throws UpdateException {
169 local.updateBluetooth(new Bluetooth().withActive(false));
173 public void testGetWifi() {
178 public void testGetApplications() {
179 local.getApplications();
183 public void testGetClockApplication() throws ApplicationNotFoundException {
184 local.getApplication(CoreApps.clock().getPackageName());
188 public void testGetCountdownApplication() throws ApplicationNotFoundException {
189 local.getApplication(CoreApps.countdown().getPackageName());
193 public void testGetRadioApplication() throws ApplicationNotFoundException {
194 local.getApplication(CoreApps.radio().getPackageName());
198 public void testGetStopwatchApplication() throws ApplicationNotFoundException {
199 local.getApplication(CoreApps.stopwatch().getPackageName());
203 public void testGetWeatherApplication() throws ApplicationNotFoundException {
204 local.getApplication(CoreApps.weather().getPackageName());
208 public void testGetInvalidApplication() {
209 assertThrows(ApplicationNotFoundException.class, () -> local.getApplication("invalid"));
213 public void testActivatePreviousApplication() {
214 local.activatePreviousApplication();
218 public void testActivateNextApplication() {
219 local.activateNextApplication();
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 -> {
227 local.deleteNotification(n.getId());
228 } catch (Exception e) {
233 local.activateApplication(CoreApps.clock().getPackageName(),
234 local.getApplication(CoreApps.clock().getPackageName()).getWidgets().firstKey());
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());
244 private Notification buildSimpleNotification(int cycles) {
245 return new Notification().withPriority(Priority.CRITICAL.toRaw()).withModel(new NotificationModel()
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=="))));
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=="))));
260 private Notification buildChartNotification(int cycles) {
261 return new Notification().withPriority(Priority.CRITICAL.toRaw()).withModel(new NotificationModel()
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=="))));