]> git.basschouten.com Git - openhab-addons.git/blob
96f396df83a0f55867c7de45b76cb145afa9cbff
[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.OFF;
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.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 WWNSmokeDetectorHandler}.
38  *
39  * @author Wouter Born - Initial contribution
40  */
41 public class WWNSmokeDetectorHandlerTest extends WWNThingHandlerOSGiTest {
42
43     private static final ThingUID SMOKE_DETECTOR_UID = new ThingUID(THING_TYPE_SMOKE_DETECTOR, "smoke1");
44     private static final int CHANNEL_COUNT = 7;
45
46     public WWNSmokeDetectorHandlerTest() {
47         super(WWNSmokeDetectorHandler.class);
48     }
49
50     @Override
51     protected Thing buildThing(Bridge bridge) {
52         Map<String, Object> properties = new HashMap<>();
53         properties.put(WWNDeviceConfiguration.DEVICE_ID, SMOKE1_DEVICE_ID);
54
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();
58     }
59
60     @Test
61     public void completeSmokeDetectorUpdate() 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         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"));
76
77         assertThatAllItemStatesAreNotNull();
78     }
79
80     @Test
81     public void incompleteSmokeDetectorUpdate() throws IOException {
82         assertThat(thing.getChannels().size(), is(CHANNEL_COUNT));
83         assertThat(thing.getStatus(), is(ThingStatus.OFFLINE));
84
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();
89
90         putStreamingEventData(fromFile(INCOMPLETE_DATA_FILE_NAME));
91         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.UNKNOWN)));
92         assertThatAllItemStatesAreNull();
93     }
94
95     @Test
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)));
100
101         putStreamingEventData(fromFile(EMPTY_DATA_FILE_NAME));
102         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.OFFLINE)));
103         assertThat(thing.getStatusInfo().getStatusDetail(), is(ThingStatusDetail.GONE));
104     }
105
106     @Test
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();
112
113         updateAllItemStatesToNull();
114         assertThatAllItemStatesAreNull();
115
116         refreshAllChannels();
117         assertThatAllItemStatesAreNotNull();
118     }
119 }