2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lametrictime.internal.api.local.impl;
15 import static org.junit.jupiter.api.Assertions.assertThrows;
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;
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;
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.
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
66 * @author Gregory Moyer - Initial contribution
70 public class LaMetricTimeLocalImplIT {
71 private static final String PROP_HOST = "host";
72 private static final String PROP_API_KEY = "apiKey";
75 private static LaMetricTimeLocalImpl local;
78 public static void setup() throws IOException {
79 File file = TestUtil.getTestDataPath(LaMetricTimeLocalImplIT.class, "device.properties").toFile();
81 throw new IllegalStateException("Device configuration properties missing at " + file.getAbsolutePath());
84 try (InputStream in = new FileInputStream(file)) {
85 Properties properties = new Properties();
88 if (!properties.containsKey(PROP_HOST)) {
89 throw new IllegalStateException("Device configuration property " + PROP_HOST + " was not found");
92 if (!properties.containsKey(PROP_API_KEY)) {
93 throw new IllegalStateException("Device configuration property " + PROP_API_KEY + " was not found");
96 LocalConfiguration config = new LocalConfiguration().withHost(properties.getProperty(PROP_HOST))
97 .withApiKey(properties.getProperty(PROP_API_KEY)).withLogging(true);
98 local = new LaMetricTimeLocalImpl(config);
103 public void testGetApi() {
108 public void testGetDevice() {
113 public void testCreateAndGetNotification() throws NotificationCreationException, NotificationNotFoundException {
114 String id = local.createNotification(buildSimpleNotification(1));
115 local.getCurrentNotification();
116 local.getNotification(id);
120 public void testCreateGoalNotification() throws NotificationCreationException, NotificationNotFoundException {
121 local.createNotification(buildGoalNotification(1));
125 public void testCreateChartNotification() throws NotificationCreationException, NotificationNotFoundException {
126 local.createNotification(buildChartNotification(1));
130 public void testGetNotifications() {
131 local.getNotifications();
135 public void testGetInvalidNotification() {
136 assertThrows(NotificationNotFoundException.class, () -> local.getNotification("invalid"));
140 public void testCreateAndDeleteNotification() throws NotificationCreationException, NotificationNotFoundException {
141 String id = local.createNotification(buildSimpleNotification(0));
142 local.deleteNotification(id);
146 public void testGetDisplay() {
151 public void testUpdateDisplay() throws UpdateException {
152 local.updateDisplay(new Display().withBrightnessMode(BrightnessMode.AUTO.toRaw()));
156 public void testGetAudio() {
161 public void testUpdateAudio() throws UpdateException {
162 local.updateAudio(new Audio().withVolume(25));
166 public void testGetBluetooth() {
167 local.getBluetooth();
171 public void testUpdateBluetooth() throws UpdateException {
172 local.updateBluetooth(new Bluetooth().withActive(false));
176 public void testGetWifi() {
181 public void testGetApplications() {
182 local.getApplications();
186 public void testGetClockApplication() throws ApplicationNotFoundException {
187 local.getApplication(CoreApps.clock().getPackageName());
191 public void testGetCountdownApplication() throws ApplicationNotFoundException {
192 local.getApplication(CoreApps.countdown().getPackageName());
196 public void testGetRadioApplication() throws ApplicationNotFoundException {
197 local.getApplication(CoreApps.radio().getPackageName());
201 public void testGetStopwatchApplication() throws ApplicationNotFoundException {
202 local.getApplication(CoreApps.stopwatch().getPackageName());
206 public void testGetWeatherApplication() throws ApplicationNotFoundException {
207 local.getApplication(CoreApps.weather().getPackageName());
211 public void testGetInvalidApplication() {
212 assertThrows(ApplicationNotFoundException.class, () -> local.getApplication("invalid"));
216 public void testActivatePreviousApplication() {
217 local.activatePreviousApplication();
221 public void testActivateNextApplication() {
222 local.activateNextApplication();
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 -> {
230 local.deleteNotification(n.getId());
231 } catch (NotificationNotFoundException e) {
236 local.activateApplication(CoreApps.clock().getPackageName(),
237 local.getApplication(CoreApps.clock().getPackageName()).getWidgets().firstKey());
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());
247 private Notification buildSimpleNotification(int cycles) {
248 return new Notification().withPriority(Priority.CRITICAL.toRaw()).withModel(new NotificationModel()
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=="))));
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=="))));
263 private Notification buildChartNotification(int cycles) {
264 return new Notification().withPriority(Priority.CRITICAL.toRaw()).withModel(new NotificationModel()
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=="))));