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.nest.internal.wwn.handler;
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.*;
21 import java.io.IOException;
22 import java.util.HashMap;
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;
37 * Tests for {@link WWNCameraHandler}.
39 * @author Wouter Born - Initial contribution
41 public class WWNCameraHandlerTest extends WWNThingHandlerOSGiTest {
43 private static final ThingUID CAMERA_UID = new ThingUID(THING_TYPE_CAMERA, "camera1");
44 private static final int CHANNEL_COUNT = 20;
46 public WWNCameraHandlerTest() {
47 super(WWNCameraHandler.class);
51 protected Thing buildThing(Bridge bridge) {
52 Map<String, Object> properties = new HashMap<>();
53 properties.put(WWNDeviceConfiguration.DEVICE_ID, CAMERA1_DEVICE_ID);
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();
61 public void completeCameraUpdate() throws IOException {
62 assertThat(thing.getChannels().size(), is(CHANNEL_COUNT));
63 assertThat(thing.getStatus(), is(ThingStatus.OFFLINE));
65 waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
66 putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
67 waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
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"));
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"));
94 assertThatAllItemStatesAreNotNull();
98 public void incompleteCameraUpdate() throws IOException {
99 assertThat(thing.getChannels().size(), is(CHANNEL_COUNT));
100 assertThat(thing.getStatus(), is(ThingStatus.OFFLINE));
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();
107 putStreamingEventData(fromFile(INCOMPLETE_DATA_FILE_NAME));
108 waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.UNKNOWN)));
109 assertThatAllItemStatesAreNull();
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)));
118 putStreamingEventData(fromFile(EMPTY_DATA_FILE_NAME));
119 waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.OFFLINE)));
120 assertThat(thing.getStatusInfo().getStatusDetail(), is(ThingStatusDetail.GONE));
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();
130 updateAllItemStatesToNull();
131 assertThatAllItemStatesAreNull();
133 refreshAllChannels();
134 assertThatAllItemStatesAreNotNull();
138 public void handleStreamingCommands() throws IOException {
139 handleCommand(CHANNEL_CAMERA_STREAMING, ON);
140 assertNestApiPropertyState(CAMERA1_DEVICE_ID, "is_streaming", "true");
142 handleCommand(CHANNEL_CAMERA_STREAMING, OFF);
143 assertNestApiPropertyState(CAMERA1_DEVICE_ID, "is_streaming", "false");
145 handleCommand(CHANNEL_CAMERA_STREAMING, ON);
146 assertNestApiPropertyState(CAMERA1_DEVICE_ID, "is_streaming", "true");