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