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.OFF;
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 WWNSmokeDetectorHandler}.
39 * @author Wouter Born - Initial contribution
41 public class WWNSmokeDetectorHandlerTest extends WWNThingHandlerOSGiTest {
43 private static final ThingUID SMOKE_DETECTOR_UID = new ThingUID(THING_TYPE_SMOKE_DETECTOR, "smoke1");
44 private static final int CHANNEL_COUNT = 7;
46 public WWNSmokeDetectorHandlerTest() {
47 super(WWNSmokeDetectorHandler.class);
51 protected Thing buildThing(Bridge bridge) {
52 Map<String, Object> properties = new HashMap<>();
53 properties.put(WWNDeviceConfiguration.DEVICE_ID, SMOKE1_DEVICE_ID);
55 return ThingBuilder.create(THING_TYPE_SMOKE_DETECTOR, SMOKE_DETECTOR_UID).withLabel("Test Smoke Detector")
56 .withBridge(bridge.getUID()).withChannels(buildChannels(THING_TYPE_SMOKE_DETECTOR, SMOKE_DETECTOR_UID))
57 .withConfiguration(new Configuration(properties)).build();
61 public void completeSmokeDetectorUpdate() 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 assertThatItemHasState(CHANNEL_CO_ALARM_STATE, new StringType("OK"));
70 assertThatItemHasState(CHANNEL_LAST_CONNECTION, parseDateTimeType("2017-02-02T20:53:05.338Z"));
71 assertThatItemHasState(CHANNEL_LAST_MANUAL_TEST_TIME, parseDateTimeType("2016-10-31T23:59:59.000Z"));
72 assertThatItemHasState(CHANNEL_LOW_BATTERY, OFF);
73 assertThatItemHasState(CHANNEL_MANUAL_TEST_ACTIVE, OFF);
74 assertThatItemHasState(CHANNEL_SMOKE_ALARM_STATE, new StringType("OK"));
75 assertThatItemHasState(CHANNEL_UI_COLOR_STATE, new StringType("GREEN"));
77 assertThatAllItemStatesAreNotNull();
81 public void incompleteSmokeDetectorUpdate() throws IOException {
82 assertThat(thing.getChannels().size(), is(CHANNEL_COUNT));
83 assertThat(thing.getStatus(), is(ThingStatus.OFFLINE));
85 waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
86 putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
87 waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
88 assertThatAllItemStatesAreNotNull();
90 putStreamingEventData(fromFile(INCOMPLETE_DATA_FILE_NAME));
91 waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.UNKNOWN)));
92 assertThatAllItemStatesAreNull();
96 public void smokeDetectorGone() throws IOException {
97 waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
98 putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
99 waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
101 putStreamingEventData(fromFile(EMPTY_DATA_FILE_NAME));
102 waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.OFFLINE)));
103 assertThat(thing.getStatusInfo().getStatusDetail(), is(ThingStatusDetail.GONE));
107 public void channelRefresh() throws IOException {
108 waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
109 putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
110 waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
111 assertThatAllItemStatesAreNotNull();
113 updateAllItemStatesToNull();
114 assertThatAllItemStatesAreNull();
116 refreshAllChannels();
117 assertThatAllItemStatesAreNotNull();