]> git.basschouten.com Git - openhab-addons.git/blob
f22bc39b8149bf45c1960a069bd8cf476a0d5c62
[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.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.config.NestDeviceConfiguration;
27 import org.openhab.binding.nest.internal.handler.NestSmokeDetectorHandler;
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 NestSmokeDetectorHandler}.
39  *
40  * @author Wouter Born - Increase test coverage
41  */
42 public class NestSmokeDetectorHandlerTest extends NestThingHandlerOSGiTest {
43
44     private static final ThingUID SMOKE_DETECTOR_UID = new ThingUID(THING_TYPE_SMOKE_DETECTOR, "smoke1");
45     private static final int CHANNEL_COUNT = 7;
46
47     public NestSmokeDetectorHandlerTest() {
48         super(NestSmokeDetectorHandler.class);
49     }
50
51     @Override
52     protected Thing buildThing(Bridge bridge) {
53         Map<String, Object> properties = new HashMap<>();
54         properties.put(NestDeviceConfiguration.DEVICE_ID, SMOKE1_DEVICE_ID);
55
56         return ThingBuilder.create(THING_TYPE_SMOKE_DETECTOR, SMOKE_DETECTOR_UID).withLabel("Test Smoke Detector")
57                 .withBridge(bridge.getUID()).withChannels(buildChannels(THING_TYPE_SMOKE_DETECTOR, SMOKE_DETECTOR_UID))
58                 .withConfiguration(new Configuration(properties)).build();
59     }
60
61     @Test
62     public void completeSmokeDetectorUpdate() 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         assertThatItemHasState(CHANNEL_CO_ALARM_STATE, new StringType("OK"));
71         assertThatItemHasState(CHANNEL_LAST_CONNECTION, parseDateTimeType("2017-02-02T20:53:05.338Z"));
72         assertThatItemHasState(CHANNEL_LAST_MANUAL_TEST_TIME, parseDateTimeType("2016-10-31T23:59:59.000Z"));
73         assertThatItemHasState(CHANNEL_LOW_BATTERY, OFF);
74         assertThatItemHasState(CHANNEL_MANUAL_TEST_ACTIVE, OFF);
75         assertThatItemHasState(CHANNEL_SMOKE_ALARM_STATE, new StringType("OK"));
76         assertThatItemHasState(CHANNEL_UI_COLOR_STATE, new StringType("GREEN"));
77
78         assertThatAllItemStatesAreNotNull();
79     }
80
81     @Test
82     public void incompleteSmokeDetectorUpdate() throws IOException {
83         assertThat(thing.getChannels().size(), is(CHANNEL_COUNT));
84         assertThat(thing.getStatus(), is(ThingStatus.OFFLINE));
85
86         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
87         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
88         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
89         assertThatAllItemStatesAreNotNull();
90
91         putStreamingEventData(fromFile(INCOMPLETE_DATA_FILE_NAME));
92         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.UNKNOWN)));
93         assertThatAllItemStatesAreNull();
94     }
95
96     @Test
97     public void smokeDetectorGone() throws IOException {
98         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
99         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
100         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
101
102         putStreamingEventData(fromFile(EMPTY_DATA_FILE_NAME));
103         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.OFFLINE)));
104         assertThat(thing.getStatusInfo().getStatusDetail(), is(ThingStatusDetail.GONE));
105     }
106
107     @Test
108     public void channelRefresh() throws IOException {
109         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
110         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
111         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
112         assertThatAllItemStatesAreNotNull();
113
114         updateAllItemStatesToNull();
115         assertThatAllItemStatesAreNull();
116
117         refreshAllChannels();
118         assertThatAllItemStatesAreNotNull();
119     }
120 }