]> git.basschouten.com Git - openhab-addons.git/blob
2c1fb1a9dcd71f1d7cab09a88bad4b211ec280be
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.nest.handler;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.core.Is.is;
17 import static org.openhab.binding.nest.internal.NestBindingConstants.*;
18 import static org.openhab.binding.nest.internal.data.NestDataUtil.*;
19 import static org.openhab.core.library.types.OnOffType.*;
20
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.nest.internal.config.NestDeviceConfiguration;
27 import org.openhab.binding.nest.internal.handler.NestCameraHandler;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingStatusDetail;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.builder.ThingBuilder;
36
37 /**
38  * Tests for {@link NestCameraHandler}.
39  *
40  * @author Wouter Born - Increase test coverage
41  */
42 public class NestCameraHandlerTest extends NestThingHandlerOSGiTest {
43
44     private static final ThingUID CAMERA_UID = new ThingUID(THING_TYPE_CAMERA, "camera1");
45     private static final int CHANNEL_COUNT = 20;
46
47     public NestCameraHandlerTest() {
48         super(NestCameraHandler.class);
49     }
50
51     @Override
52     protected Thing buildThing(Bridge bridge) {
53         Map<String, Object> properties = new HashMap<>();
54         properties.put(NestDeviceConfiguration.DEVICE_ID, CAMERA1_DEVICE_ID);
55
56         return ThingBuilder.create(THING_TYPE_CAMERA, CAMERA_UID).withLabel("Test Camera").withBridge(bridge.getUID())
57                 .withChannels(buildChannels(THING_TYPE_CAMERA, CAMERA_UID))
58                 .withConfiguration(new Configuration(properties)).build();
59     }
60
61     @Test
62     public void completeCameraUpdate() throws IOException {
63         assertThat(thing.getChannels().size(), is(CHANNEL_COUNT));
64         assertThat(thing.getStatus(), is(ThingStatus.OFFLINE));
65
66         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
67         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
68         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
69
70         // Camera channel group
71         assertThatItemHasState(CHANNEL_CAMERA_APP_URL, new StringType("https://camera_app_url"));
72         assertThatItemHasState(CHANNEL_CAMERA_AUDIO_INPUT_ENABLED, ON);
73         assertThatItemHasState(CHANNEL_CAMERA_LAST_ONLINE_CHANGE, parseDateTimeType("2017-01-22T08:19:20.000Z"));
74         assertThatItemHasState(CHANNEL_CAMERA_PUBLIC_SHARE_ENABLED, OFF);
75         assertThatItemHasState(CHANNEL_CAMERA_PUBLIC_SHARE_URL, new StringType("https://camera_public_share_url"));
76         assertThatItemHasState(CHANNEL_CAMERA_SNAPSHOT_URL, new StringType("https://camera_snapshot_url"));
77         assertThatItemHasState(CHANNEL_CAMERA_STREAMING, OFF);
78         assertThatItemHasState(CHANNEL_CAMERA_VIDEO_HISTORY_ENABLED, OFF);
79         assertThatItemHasState(CHANNEL_CAMERA_WEB_URL, new StringType("https://camera_web_url"));
80
81         // Last event channel group
82         assertThatItemHasState(CHANNEL_LAST_EVENT_ACTIVITY_ZONES, new StringType("id1,id2"));
83         assertThatItemHasState(CHANNEL_LAST_EVENT_ANIMATED_IMAGE_URL,
84                 new StringType("https://last_event_animated_image_url"));
85         assertThatItemHasState(CHANNEL_LAST_EVENT_APP_URL, new StringType("https://last_event_app_url"));
86         assertThatItemHasState(CHANNEL_LAST_EVENT_END_TIME, parseDateTimeType("2017-01-22T07:40:38.680Z"));
87         assertThatItemHasState(CHANNEL_LAST_EVENT_HAS_MOTION, ON);
88         assertThatItemHasState(CHANNEL_LAST_EVENT_HAS_PERSON, OFF);
89         assertThatItemHasState(CHANNEL_LAST_EVENT_HAS_SOUND, OFF);
90         assertThatItemHasState(CHANNEL_LAST_EVENT_IMAGE_URL, new StringType("https://last_event_image_url"));
91         assertThatItemHasState(CHANNEL_LAST_EVENT_START_TIME, parseDateTimeType("2017-01-22T07:40:19.020Z"));
92         assertThatItemHasState(CHANNEL_LAST_EVENT_URLS_EXPIRE_TIME, parseDateTimeType("2017-02-05T07:40:19.020Z"));
93         assertThatItemHasState(CHANNEL_LAST_EVENT_WEB_URL, new StringType("https://last_event_web_url"));
94
95         assertThatAllItemStatesAreNotNull();
96     }
97
98     @Test
99     public void incompleteCameraUpdate() throws IOException {
100         assertThat(thing.getChannels().size(), is(CHANNEL_COUNT));
101         assertThat(thing.getStatus(), is(ThingStatus.OFFLINE));
102
103         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
104         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
105         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
106         assertThatAllItemStatesAreNotNull();
107
108         putStreamingEventData(fromFile(INCOMPLETE_DATA_FILE_NAME));
109         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.UNKNOWN)));
110         assertThatAllItemStatesAreNull();
111     }
112
113     @Test
114     public void cameraGone() throws IOException {
115         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
116         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
117         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
118
119         putStreamingEventData(fromFile(EMPTY_DATA_FILE_NAME));
120         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.OFFLINE)));
121         assertThat(thing.getStatusInfo().getStatusDetail(), is(ThingStatusDetail.GONE));
122     }
123
124     @Test
125     public void channelRefresh() throws IOException {
126         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
127         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
128         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
129         assertThatAllItemStatesAreNotNull();
130
131         updateAllItemStatesToNull();
132         assertThatAllItemStatesAreNull();
133
134         refreshAllChannels();
135         assertThatAllItemStatesAreNotNull();
136     }
137
138     @Test
139     public void handleStreamingCommands() throws IOException {
140         handleCommand(CHANNEL_CAMERA_STREAMING, ON);
141         assertNestApiPropertyState(CAMERA1_DEVICE_ID, "is_streaming", "true");
142
143         handleCommand(CHANNEL_CAMERA_STREAMING, OFF);
144         assertNestApiPropertyState(CAMERA1_DEVICE_ID, "is_streaming", "false");
145
146         handleCommand(CHANNEL_CAMERA_STREAMING, ON);
147         assertNestApiPropertyState(CAMERA1_DEVICE_ID, "is_streaming", "true");
148     }
149 }