]> git.basschouten.com Git - openhab-addons.git/blob
d7f9ef2c18cf1a3a4ae4cba0b9211e0d0297b8e1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.internal.wwn.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.wwn.WWNBindingConstants.*;
18 import static org.openhab.binding.nest.internal.wwn.dto.WWNDataUtil.*;
19 import static org.openhab.core.library.types.OnOffType.*;
20
21 import java.io.IOException;
22 import java.util.Map;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.nest.internal.wwn.config.WWNDeviceConfiguration;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.builder.ThingBuilder;
35
36 /**
37  * Tests for {@link WWNCameraHandler}.
38  *
39  * @author Wouter Born - Initial contribution
40  */
41 @NonNullByDefault
42 public class WWNCameraHandlerTest extends WWNThingHandlerOSGiTest {
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 WWNCameraHandlerTest() {
48         super(WWNCameraHandler.class);
49     }
50
51     @Override
52     protected Thing buildThing(Bridge bridge) {
53         Map<String, Object> properties = Map.of(WWNDeviceConfiguration.DEVICE_ID, CAMERA1_DEVICE_ID);
54
55         return ThingBuilder.create(THING_TYPE_CAMERA, CAMERA_UID).withLabel("Test Camera").withBridge(bridge.getUID())
56                 .withChannels(buildChannels(THING_TYPE_CAMERA, CAMERA_UID))
57                 .withConfiguration(new Configuration(properties)).build();
58     }
59
60     @Test
61     public void completeCameraUpdate() throws IOException {
62         assertThat(thing.getChannels().size(), is(CHANNEL_COUNT));
63         assertThat(thing.getStatus(), is(ThingStatus.OFFLINE));
64
65         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
66         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
67         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
68
69         // Camera channel group
70         assertThatItemHasState(CHANNEL_CAMERA_APP_URL, new StringType("https://camera_app_url"));
71         assertThatItemHasState(CHANNEL_CAMERA_AUDIO_INPUT_ENABLED, ON);
72         assertThatItemHasState(CHANNEL_CAMERA_LAST_ONLINE_CHANGE, parseDateTimeType("2017-01-22T08:19:20.000Z"));
73         assertThatItemHasState(CHANNEL_CAMERA_PUBLIC_SHARE_ENABLED, OFF);
74         assertThatItemHasState(CHANNEL_CAMERA_PUBLIC_SHARE_URL, new StringType("https://camera_public_share_url"));
75         assertThatItemHasState(CHANNEL_CAMERA_SNAPSHOT_URL, new StringType("https://camera_snapshot_url"));
76         assertThatItemHasState(CHANNEL_CAMERA_STREAMING, OFF);
77         assertThatItemHasState(CHANNEL_CAMERA_VIDEO_HISTORY_ENABLED, OFF);
78         assertThatItemHasState(CHANNEL_CAMERA_WEB_URL, new StringType("https://camera_web_url"));
79
80         // Last event channel group
81         assertThatItemHasState(CHANNEL_LAST_EVENT_ACTIVITY_ZONES, new StringType("id1,id2"));
82         assertThatItemHasState(CHANNEL_LAST_EVENT_ANIMATED_IMAGE_URL,
83                 new StringType("https://last_event_animated_image_url"));
84         assertThatItemHasState(CHANNEL_LAST_EVENT_APP_URL, new StringType("https://last_event_app_url"));
85         assertThatItemHasState(CHANNEL_LAST_EVENT_END_TIME, parseDateTimeType("2017-01-22T07:40:38.680Z"));
86         assertThatItemHasState(CHANNEL_LAST_EVENT_HAS_MOTION, ON);
87         assertThatItemHasState(CHANNEL_LAST_EVENT_HAS_PERSON, OFF);
88         assertThatItemHasState(CHANNEL_LAST_EVENT_HAS_SOUND, OFF);
89         assertThatItemHasState(CHANNEL_LAST_EVENT_IMAGE_URL, new StringType("https://last_event_image_url"));
90         assertThatItemHasState(CHANNEL_LAST_EVENT_START_TIME, parseDateTimeType("2017-01-22T07:40:19.020Z"));
91         assertThatItemHasState(CHANNEL_LAST_EVENT_URLS_EXPIRE_TIME, parseDateTimeType("2017-02-05T07:40:19.020Z"));
92         assertThatItemHasState(CHANNEL_LAST_EVENT_WEB_URL, new StringType("https://last_event_web_url"));
93
94         assertThatAllItemStatesAreNotNull();
95     }
96
97     @Test
98     public void incompleteCameraUpdate() throws IOException {
99         assertThat(thing.getChannels().size(), is(CHANNEL_COUNT));
100         assertThat(thing.getStatus(), is(ThingStatus.OFFLINE));
101
102         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
103         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
104         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
105         assertThatAllItemStatesAreNotNull();
106
107         putStreamingEventData(fromFile(INCOMPLETE_DATA_FILE_NAME));
108         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.UNKNOWN)));
109         assertThatAllItemStatesAreNull();
110     }
111
112     @Test
113     public void cameraGone() throws IOException {
114         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
115         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
116         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
117
118         putStreamingEventData(fromFile(EMPTY_DATA_FILE_NAME));
119         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.OFFLINE)));
120         assertThat(thing.getStatusInfo().getStatusDetail(), is(ThingStatusDetail.GONE));
121     }
122
123     @Test
124     public void channelRefresh() throws IOException {
125         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
126         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
127         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
128         assertThatAllItemStatesAreNotNull();
129
130         updateAllItemStatesToNull();
131         assertThatAllItemStatesAreNull();
132
133         refreshAllChannels();
134         assertThatAllItemStatesAreNotNull();
135     }
136
137     @Test
138     public void handleStreamingCommands() throws IOException {
139         handleCommand(CHANNEL_CAMERA_STREAMING, ON);
140         assertNestApiPropertyState(CAMERA1_DEVICE_ID, "is_streaming", "true");
141
142         handleCommand(CHANNEL_CAMERA_STREAMING, OFF);
143         assertNestApiPropertyState(CAMERA1_DEVICE_ID, "is_streaming", "false");
144
145         handleCommand(CHANNEL_CAMERA_STREAMING, ON);
146         assertNestApiPropertyState(CAMERA1_DEVICE_ID, "is_streaming", "true");
147     }
148 }