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;
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;
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;
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.
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
66 public class LaMetricTimeLocalImplIT {
67 private static final String PROP_HOST = "host";
68 private static final String PROP_API_KEY = "apiKey";
70 private static LaMetricTimeLocalImpl local;
73 public static void setup() throws IOException {
74 File file = TestUtil.getTestDataPath(LaMetricTimeLocalImplIT.class, "device.properties").toFile();
76 throw new IllegalStateException("Device configuration properties missing at " + file.getAbsolutePath());
79 try (InputStream in = new FileInputStream(file)) {
80 Properties properties = new Properties();
83 if (!properties.containsKey(PROP_HOST)) {
84 throw new IllegalStateException("Device configuration property " + PROP_HOST + " was not found");
87 if (!properties.containsKey(PROP_API_KEY)) {
88 throw new IllegalStateException("Device configuration property " + PROP_API_KEY + " was not found");
91 LocalConfiguration config = new LocalConfiguration().withHost(properties.getProperty(PROP_HOST))
92 .withApiKey(properties.getProperty(PROP_API_KEY)).withLogging(true);
93 local = new LaMetricTimeLocalImpl(config);
98 public void testGetApi() {
103 public void testGetDevice() {
108 public void testCreateAndGetNotification() throws NotificationCreationException, NotificationNotFoundException {
109 String id = local.createNotification(buildSimpleNotification(1));
110 local.getCurrentNotification();
111 local.getNotification(id);
115 public void testCreateGoalNotification() throws NotificationCreationException, NotificationNotFoundException {
116 local.createNotification(buildGoalNotification(1));
120 public void testCreateChartNotification() throws NotificationCreationException, NotificationNotFoundException {
121 local.createNotification(buildChartNotification(1));
125 public void testGetNotifications() {
126 local.getNotifications();
129 @Test(expected = NotificationNotFoundException.class)
130 public void testGetInvalidNotification() throws NotificationNotFoundException {
131 local.getNotification("invalid");
135 public void testCreateAndDeleteNotification() throws NotificationCreationException, NotificationNotFoundException {
136 String id = local.createNotification(buildSimpleNotification(0));
137 local.deleteNotification(id);
141 public void testGetDisplay() {
146 public void testUpdateDisplay() throws UpdateException {
147 local.updateDisplay(new Display().withBrightnessMode(BrightnessMode.AUTO.toRaw()));
151 public void testGetAudio() {
156 public void testUpdateAudio() throws UpdateException {
157 local.updateAudio(new Audio().withVolume(25));
161 public void testGetBluetooth() {
162 local.getBluetooth();
166 public void testUpdateBluetooth() throws UpdateException {
167 local.updateBluetooth(new Bluetooth().withActive(false));
171 public void testGetWifi() {
176 public void testGetApplications() {
177 local.getApplications();
181 public void testGetClockApplication() throws ApplicationNotFoundException {
182 local.getApplication(CoreApps.clock().getPackageName());
186 public void testGetCountdownApplication() throws ApplicationNotFoundException {
187 local.getApplication(CoreApps.countdown().getPackageName());
191 public void testGetRadioApplication() throws ApplicationNotFoundException {
192 local.getApplication(CoreApps.radio().getPackageName());
196 public void testGetStopwatchApplication() throws ApplicationNotFoundException {
197 local.getApplication(CoreApps.stopwatch().getPackageName());
201 public void testGetWeatherApplication() throws ApplicationNotFoundException {
202 local.getApplication(CoreApps.weather().getPackageName());
205 @Test(expected = ApplicationNotFoundException.class)
206 public void testGetInvalidApplication() throws ApplicationNotFoundException {
207 local.getApplication("invalid");
211 public void testActivatePreviousApplication() {
212 local.activatePreviousApplication();
216 public void testActivateNextApplication() {
217 local.activateNextApplication();
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 -> {
225 local.deleteNotification(n.getId());
226 } catch (Exception e) {
231 local.activateApplication(CoreApps.clock().getPackageName(),
232 local.getApplication(CoreApps.clock().getPackageName()).getWidgets().firstKey());
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());
242 private Notification buildSimpleNotification(int cycles) {
243 return new Notification().withPriority(Priority.CRITICAL.toRaw()).withModel(new NotificationModel()
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=="))));
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=="))));
258 private Notification buildChartNotification(int cycles) {
259 return new Notification().withPriority(Priority.CRITICAL.toRaw()).withModel(new NotificationModel()
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=="))));