]> git.basschouten.com Git - openhab-addons.git/blob
5d34b130836ee7018c7b31b04e5746829bc7b841
[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.dto;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.nest.internal.wwn.dto.WWNDataUtil.*;
17
18 import java.io.IOException;
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.core.library.unit.SIUnits;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Test cases for gson parsing of model classes
30  *
31  * @author David Bennett - Initial contribution
32  * @author Wouter Born - Increase test coverage
33  */
34 @NonNullByDefault
35 public class WWNGsonParsingTest {
36
37     private final Logger logger = LoggerFactory.getLogger(WWNGsonParsingTest.class);
38
39     private static void assertEqualDateTime(String expected, Date actual) {
40         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
41         assertEquals(expected, sdf.format(actual));
42     }
43
44     @Test
45     public void verifyCompleteInput() throws IOException {
46         WWNTopLevelData topLevel = fromJson("top-level-data.json", WWNTopLevelData.class);
47
48         assertEquals(topLevel.getDevices().getThermostats().size(), 1);
49         assertNotNull(topLevel.getDevices().getThermostats().get(THERMOSTAT1_DEVICE_ID));
50         assertEquals(topLevel.getDevices().getCameras().size(), 2);
51         assertNotNull(topLevel.getDevices().getCameras().get(CAMERA1_DEVICE_ID));
52         assertNotNull(topLevel.getDevices().getCameras().get(CAMERA2_DEVICE_ID));
53         assertEquals(topLevel.getDevices().getSmokeCoAlarms().size(), 4);
54         assertNotNull(topLevel.getDevices().getSmokeCoAlarms().get(SMOKE1_DEVICE_ID));
55         assertNotNull(topLevel.getDevices().getSmokeCoAlarms().get(SMOKE2_DEVICE_ID));
56         assertNotNull(topLevel.getDevices().getSmokeCoAlarms().get(SMOKE3_DEVICE_ID));
57         assertNotNull(topLevel.getDevices().getSmokeCoAlarms().get(SMOKE4_DEVICE_ID));
58     }
59
60     @Test
61     public void verifyCompleteStreamingInput() throws IOException {
62         WWNTopLevelStreamingData topLevelStreamingData = fromJson("top-level-streaming-data.json",
63                 WWNTopLevelStreamingData.class);
64
65         assertEquals("/", topLevelStreamingData.getPath());
66
67         WWNTopLevelData data = topLevelStreamingData.getData();
68         assertEquals(data.getDevices().getThermostats().size(), 1);
69         assertNotNull(data.getDevices().getThermostats().get(THERMOSTAT1_DEVICE_ID));
70         assertEquals(data.getDevices().getCameras().size(), 2);
71         assertNotNull(data.getDevices().getCameras().get(CAMERA1_DEVICE_ID));
72         assertNotNull(data.getDevices().getCameras().get(CAMERA2_DEVICE_ID));
73         assertEquals(data.getDevices().getSmokeCoAlarms().size(), 4);
74         assertNotNull(data.getDevices().getSmokeCoAlarms().get(SMOKE1_DEVICE_ID));
75         assertNotNull(data.getDevices().getSmokeCoAlarms().get(SMOKE2_DEVICE_ID));
76         assertNotNull(data.getDevices().getSmokeCoAlarms().get(SMOKE3_DEVICE_ID));
77         assertNotNull(data.getDevices().getSmokeCoAlarms().get(SMOKE4_DEVICE_ID));
78     }
79
80     @Test
81     public void verifyThermostat() throws IOException {
82         WWNThermostat thermostat = fromJson("thermostat-data.json", WWNThermostat.class);
83         logger.debug("Thermostat: {}", thermostat);
84
85         assertTrue(thermostat.isOnline());
86         assertTrue(thermostat.isCanHeat());
87         assertTrue(thermostat.isHasLeaf());
88         assertFalse(thermostat.isCanCool());
89         assertFalse(thermostat.isFanTimerActive());
90         assertFalse(thermostat.isLocked());
91         assertFalse(thermostat.isSunlightCorrectionActive());
92         assertTrue(thermostat.isSunlightCorrectionEnabled());
93         assertFalse(thermostat.isUsingEmergencyHeat());
94         assertEquals(THERMOSTAT1_DEVICE_ID, thermostat.getDeviceId());
95         assertEquals(Integer.valueOf(15), thermostat.getFanTimerDuration());
96         assertEqualDateTime("2017-02-02T21:00:06.000Z", thermostat.getLastConnection());
97         assertEqualDateTime("1970-01-01T00:00:00.000Z", thermostat.getFanTimerTimeout());
98         assertEquals(Double.valueOf(24.0), thermostat.getEcoTemperatureHigh());
99         assertEquals(Double.valueOf(12.5), thermostat.getEcoTemperatureLow());
100         assertEquals(Double.valueOf(22.0), thermostat.getLockedTempMax());
101         assertEquals(Double.valueOf(20.0), thermostat.getLockedTempMin());
102         assertEquals(WWNThermostat.Mode.HEAT, thermostat.getMode());
103         assertEquals("Living Room (Living Room)", thermostat.getName());
104         assertEquals("Living Room Thermostat (Living Room)", thermostat.getNameLong());
105         assertEquals(null, thermostat.getPreviousHvacMode());
106         assertEquals("5.6-7", thermostat.getSoftwareVersion());
107         assertEquals(WWNThermostat.State.OFF, thermostat.getHvacState());
108         assertEquals(STRUCTURE1_STRUCTURE_ID, thermostat.getStructureId());
109         assertEquals(Double.valueOf(15.5), thermostat.getTargetTemperature());
110         assertEquals(Double.valueOf(24.0), thermostat.getTargetTemperatureHigh());
111         assertEquals(Double.valueOf(20.0), thermostat.getTargetTemperatureLow());
112         assertEquals(SIUnits.CELSIUS, thermostat.getTemperatureUnit());
113         assertEquals(Integer.valueOf(0), thermostat.getTimeToTarget());
114         assertEquals(THERMOSTAT1_WHERE_ID, thermostat.getWhereId());
115         assertEquals("Living Room", thermostat.getWhereName());
116     }
117
118     @Test
119     public void thermostatTimeToTargetSupportedValueParsing() {
120         assertEquals((Integer) 0, WWNThermostat.parseTimeToTarget("~0"));
121         assertEquals((Integer) 5, WWNThermostat.parseTimeToTarget("<5"));
122         assertEquals((Integer) 10, WWNThermostat.parseTimeToTarget("<10"));
123         assertEquals((Integer) 15, WWNThermostat.parseTimeToTarget("~15"));
124         assertEquals((Integer) 90, WWNThermostat.parseTimeToTarget("~90"));
125         assertEquals((Integer) 120, WWNThermostat.parseTimeToTarget(">120"));
126     }
127
128     @Test
129     public void thermostatTimeToTargetUnsupportedValueParsing() {
130         assertThrows(NumberFormatException.class, () -> WWNThermostat.parseTimeToTarget("#5"));
131     }
132
133     @Test
134     public void verifyCamera() throws IOException {
135         WWNCamera camera = fromJson("camera-data.json", WWNCamera.class);
136         logger.debug("Camera: {}", camera);
137
138         assertTrue(camera.isOnline());
139         assertEquals("Upstairs", camera.getName());
140         assertEquals("Upstairs Camera", camera.getNameLong());
141         assertEquals(STRUCTURE1_STRUCTURE_ID, camera.getStructureId());
142         assertEquals(CAMERA1_WHERE_ID, camera.getWhereId());
143         assertTrue(camera.isAudioInputEnabled());
144         assertFalse(camera.isPublicShareEnabled());
145         assertFalse(camera.isStreaming());
146         assertFalse(camera.isVideoHistoryEnabled());
147         assertEquals("https://camera_app_url", camera.getAppUrl());
148         assertEquals(CAMERA1_DEVICE_ID, camera.getDeviceId());
149         assertNull(camera.getLastConnection());
150         assertEqualDateTime("2017-01-22T08:19:20.000Z", camera.getLastIsOnlineChange());
151         assertNull(camera.getPublicShareUrl());
152         assertEquals("https://camera_snapshot_url", camera.getSnapshotUrl());
153         assertEquals("205-600052", camera.getSoftwareVersion());
154         assertEquals("https://camera_web_url", camera.getWebUrl());
155         assertEquals("https://last_event_animated_image_url", camera.getLastEvent().getAnimatedImageUrl());
156         assertEquals(2, camera.getLastEvent().getActivityZones().size());
157         assertEquals("id1", camera.getLastEvent().getActivityZones().get(0));
158         assertEquals("https://last_event_app_url", camera.getLastEvent().getAppUrl());
159         assertEqualDateTime("2017-01-22T07:40:38.680Z", camera.getLastEvent().getEndTime());
160         assertEquals("https://last_event_image_url", camera.getLastEvent().getImageUrl());
161         assertEqualDateTime("2017-01-22T07:40:19.020Z", camera.getLastEvent().getStartTime());
162         assertEqualDateTime("2017-02-05T07:40:19.020Z", camera.getLastEvent().getUrlsExpireTime());
163         assertEquals("https://last_event_web_url", camera.getLastEvent().getWebUrl());
164         assertTrue(camera.getLastEvent().isHasMotion());
165         assertFalse(camera.getLastEvent().isHasPerson());
166         assertFalse(camera.getLastEvent().isHasSound());
167     }
168
169     @Test
170     public void verifySmokeDetector() throws IOException {
171         WWNSmokeDetector smokeDetector = fromJson("smoke-detector-data.json", WWNSmokeDetector.class);
172         logger.debug("SmokeDetector: {}", smokeDetector);
173
174         assertTrue(smokeDetector.isOnline());
175         assertEquals(SMOKE1_WHERE_ID, smokeDetector.getWhereId());
176         assertEquals(SMOKE1_DEVICE_ID, smokeDetector.getDeviceId());
177         assertEquals("Downstairs", smokeDetector.getName());
178         assertEquals("Downstairs Nest Protect", smokeDetector.getNameLong());
179         assertEqualDateTime("2017-02-02T20:53:05.338Z", smokeDetector.getLastConnection());
180         assertEquals(WWNSmokeDetector.BatteryHealth.OK, smokeDetector.getBatteryHealth());
181         assertEquals(WWNSmokeDetector.AlarmState.OK, smokeDetector.getCoAlarmState());
182         assertEquals(WWNSmokeDetector.AlarmState.OK, smokeDetector.getSmokeAlarmState());
183         assertEquals("3.1rc9", smokeDetector.getSoftwareVersion());
184         assertEquals(STRUCTURE1_STRUCTURE_ID, smokeDetector.getStructureId());
185         assertEquals(WWNSmokeDetector.UiColorState.GREEN, smokeDetector.getUiColorState());
186     }
187
188     @Test
189     public void verifyAccessToken() throws IOException {
190         WWNAccessTokenData accessToken = fromJson("access-token-data.json", WWNAccessTokenData.class);
191         logger.debug("AccessTokenData: {}", accessToken);
192
193         assertEquals("access_token", accessToken.getAccessToken());
194         assertEquals(Long.valueOf(315360000L), accessToken.getExpiresIn());
195     }
196
197     @Test
198     public void verifyStructure() throws IOException {
199         WWNStructure structure = fromJson("structure-data.json", WWNStructure.class);
200         logger.debug("Structure: {}", structure);
201
202         assertEquals("Home", structure.getName());
203         assertEquals("US", structure.getCountryCode());
204         assertEquals("98056", structure.getPostalCode());
205         assertEquals(WWNStructure.HomeAwayState.HOME, structure.getAway());
206         assertEqualDateTime("2017-02-02T03:10:08.000Z", structure.getEtaBegin());
207         assertNull(structure.getEta());
208         assertNull(structure.getPeakPeriodEndTime());
209         assertNull(structure.getPeakPeriodStartTime());
210         assertEquals(STRUCTURE1_STRUCTURE_ID, structure.getStructureId());
211         assertEquals("America/Los_Angeles", structure.getTimeZone());
212         assertFalse(structure.isRhrEnrollment());
213     }
214
215     @Test
216     public void verifyError() throws IOException {
217         WWNErrorData error = fromJson("error-data.json", WWNErrorData.class);
218         logger.debug("ErrorData: {}", error);
219
220         assertEquals("blocked", error.getError());
221         assertEquals("https://developer.nest.com/documentation/cloud/error-messages#blocked", error.getType());
222         assertEquals("blocked", error.getMessage());
223         assertEquals("bb514046-edc9-4bca-8239-f7a3cfb0925a", error.getInstance());
224     }
225 }