]> git.basschouten.com Git - openhab-addons.git/blob
5c9afdb7b140ed713538f5ddcb61ea180a3c66a9
[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.NestStructureConfiguration;
27 import org.openhab.binding.nest.internal.handler.NestStructureHandler;
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 NestStructureHandler}.
39  *
40  * @author Wouter Born - Increase test coverage
41  */
42 public class NestStructureHandlerTest extends NestThingHandlerOSGiTest {
43
44     private static final ThingUID STRUCTURE_UID = new ThingUID(THING_TYPE_STRUCTURE, "structure1");
45     private static final int CHANNEL_COUNT = 11;
46
47     public NestStructureHandlerTest() {
48         super(NestStructureHandler.class);
49     }
50
51     @Override
52     protected Thing buildThing(Bridge bridge) {
53         Map<String, Object> properties = new HashMap<>();
54         properties.put(NestStructureConfiguration.STRUCTURE_ID, STRUCTURE1_STRUCTURE_ID);
55
56         return ThingBuilder.create(THING_TYPE_STRUCTURE, STRUCTURE_UID).withLabel("Test Structure")
57                 .withBridge(bridge.getUID()).withChannels(buildChannels(THING_TYPE_STRUCTURE, STRUCTURE_UID))
58                 .withConfiguration(new Configuration(properties)).build();
59     }
60
61     @Test
62     public void completeStructureUpdate() 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_AWAY, new StringType("HOME"));
71         assertThatItemHasState(CHANNEL_CO_ALARM_STATE, new StringType("OK"));
72         assertThatItemHasState(CHANNEL_COUNTRY_CODE, new StringType("US"));
73         assertThatItemHasState(CHANNEL_ETA_BEGIN, parseDateTimeType("2017-02-02T03:10:08.000Z"));
74         assertThatItemHasState(CHANNEL_PEAK_PERIOD_END_TIME, parseDateTimeType("2017-07-01T01:03:08.400Z"));
75         assertThatItemHasState(CHANNEL_PEAK_PERIOD_START_TIME, parseDateTimeType("2017-06-01T13:31:10.870Z"));
76         assertThatItemHasState(CHANNEL_POSTAL_CODE, new StringType("98056"));
77         assertThatItemHasState(CHANNEL_RUSH_HOUR_REWARDS_ENROLLMENT, OFF);
78         assertThatItemHasState(CHANNEL_SECURITY_STATE, new StringType("OK"));
79         assertThatItemHasState(CHANNEL_SMOKE_ALARM_STATE, new StringType("OK"));
80         assertThatItemHasState(CHANNEL_TIME_ZONE, new StringType("America/Los_Angeles"));
81
82         assertThatAllItemStatesAreNotNull();
83     }
84
85     @Test
86     public void incompleteStructureUpdate() throws IOException {
87         assertThat(thing.getChannels().size(), is(CHANNEL_COUNT));
88         assertThat(thing.getStatus(), is(ThingStatus.OFFLINE));
89
90         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
91         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
92         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
93         assertThatAllItemStatesAreNotNull();
94
95         putStreamingEventData(fromFile(INCOMPLETE_DATA_FILE_NAME));
96         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
97         assertThatAllItemStatesAreNull();
98     }
99
100     @Test
101     public void structureGone() throws IOException {
102         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
103         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
104         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
105
106         putStreamingEventData(fromFile(EMPTY_DATA_FILE_NAME));
107         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.OFFLINE)));
108         assertThat(thing.getStatusInfo().getStatusDetail(), is(ThingStatusDetail.GONE));
109     }
110
111     @Test
112     public void channelRefresh() throws IOException {
113         waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
114         putStreamingEventData(fromFile(COMPLETE_DATA_FILE_NAME));
115         waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
116         assertThatAllItemStatesAreNotNull();
117
118         updateAllItemStatesToNull();
119         assertThatAllItemStatesAreNull();
120
121         refreshAllChannels();
122         assertThatAllItemStatesAreNotNull();
123     }
124
125     @Test
126     public void handleAwayCommands() throws IOException {
127         handleCommand(CHANNEL_AWAY, new StringType("AWAY"));
128         assertNestApiPropertyState(STRUCTURE1_STRUCTURE_ID, "away", "away");
129
130         handleCommand(CHANNEL_AWAY, new StringType("HOME"));
131         assertNestApiPropertyState(STRUCTURE1_STRUCTURE_ID, "away", "home");
132
133         handleCommand(CHANNEL_AWAY, new StringType("AWAY"));
134         assertNestApiPropertyState(STRUCTURE1_STRUCTURE_ID, "away", "away");
135     }
136 }