]> git.basschouten.com Git - openhab-addons.git/blob
895885c67f69117cd2d803ab19c793c002f7216e
[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.sdm.dto;
14
15 import static org.hamcrest.CoreMatchers.notNullValue;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.core.Is.is;
18 import static org.openhab.binding.nest.internal.sdm.dto.SDMDataUtil.*;
19
20 import java.io.IOException;
21 import java.math.BigDecimal;
22 import java.time.Duration;
23 import java.time.ZonedDateTime;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMCameraRtspStreamUrls;
28 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMExtendCameraRtspStreamRequest;
29 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMExtendCameraRtspStreamResponse;
30 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMExtendCameraRtspStreamResults;
31 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMGenerateCameraImageRequest;
32 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMGenerateCameraImageResponse;
33 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMGenerateCameraImageResults;
34 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMGenerateCameraRtspStreamRequest;
35 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMGenerateCameraRtspStreamResponse;
36 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMGenerateCameraRtspStreamResults;
37 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMSetFanTimerRequest;
38 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMSetThermostatCoolSetpointRequest;
39 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMSetThermostatEcoModeRequest;
40 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMSetThermostatHeatSetpointRequest;
41 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMSetThermostatModeRequest;
42 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMSetThermostatRangeSetpointRequest;
43 import org.openhab.binding.nest.internal.sdm.dto.SDMCommands.SDMStopCameraRtspStreamRequest;
44 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMFanTimerMode;
45 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatEcoMode;
46 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatMode;
47
48 /**
49  * Tests (de)serialization of {@link org.openhab.binding.nest.internal.sdm.dto.SDMCommands} requests
50  * and responses from/to JSON.
51  *
52  * @author Wouter Born - Initial contribution
53  */
54 @NonNullByDefault
55 public class SDMCommandsTest {
56
57     @Test
58     public void deserializeExtendCameraRtspStreamResponse() throws IOException {
59         SDMExtendCameraRtspStreamResponse response = fromJson("extend-camera-rtsp-stream-response.json",
60                 SDMExtendCameraRtspStreamResponse.class);
61         assertThat(response, is(notNullValue()));
62
63         SDMExtendCameraRtspStreamResults results = response.results;
64         assertThat(results, is(notNullValue()));
65
66         assertThat(results.streamExtensionToken, is("dGNUlTU2CjY5Y3VKaTZwR3o4Y1..."));
67         assertThat(results.streamToken, is("g.0.newStreamingToken"));
68         assertThat(results.expiresAt, is(ZonedDateTime.parse("2018-01-04T18:30:00.000Z")));
69     }
70
71     @Test
72     public void deserializeGenerateCameraImageResponse() throws IOException {
73         SDMGenerateCameraImageResponse response = fromJson("generate-camera-image-response.json",
74                 SDMGenerateCameraImageResponse.class);
75         assertThat(response, is(notNullValue()));
76
77         SDMGenerateCameraImageResults results = response.results;
78         assertThat(results, is(notNullValue()));
79         assertThat(results.url, is("https://domain/sdm_resource/dGNUlTU2CjY5Y3VKaTZwR3o4Y1..."));
80         assertThat(results.token, is("g.0.eventToken"));
81     }
82
83     @Test
84     public void deserializeGenerateCameraRtspStreamResponse() throws IOException {
85         SDMGenerateCameraRtspStreamResponse response = fromJson("generate-camera-rtsp-stream-response.json",
86                 SDMGenerateCameraRtspStreamResponse.class);
87         assertThat(response, is(notNullValue()));
88
89         SDMGenerateCameraRtspStreamResults results = response.results;
90         assertThat(results, is(notNullValue()));
91
92         SDMCameraRtspStreamUrls streamUrls = results.streamUrls;
93         assertThat(streamUrls, is(notNullValue()));
94         assertThat(streamUrls.rtspUrl, is("rtsps://someurl.com/CjY5Y3VKaTZwR3o4Y19YbTVfMF...?auth=g.0.streamingToken"));
95
96         assertThat(results.streamExtensionToken, is("CjY5Y3VKaTZwR3o4Y19YbTVfMF..."));
97         assertThat(results.streamToken, is("g.0.streamingToken"));
98         assertThat(results.expiresAt, is(ZonedDateTime.parse("2018-01-04T18:30:00.000Z")));
99     }
100
101     @Test
102     public void serializeExtendCameraRtspStreamRequest() throws IOException {
103         String json = toJson(new SDMExtendCameraRtspStreamRequest("CjY5Y3VKaTZwR3o4Y19YbTVfMF..."));
104         assertThat(json, is(fromFile("extend-camera-rtsp-stream-request.json")));
105     }
106
107     @Test
108     public void serializeGenerateCameraImageRequest() throws IOException {
109         String json = toJson(new SDMGenerateCameraImageRequest("FWWVQVUdGNUlTU2V4MGV2aTNXV..."));
110         assertThat(json, is(fromFile("generate-camera-image-request.json")));
111     }
112
113     @Test
114     public void serializeGenerateCameraRtspStreamRequest() throws IOException {
115         String json = toJson(new SDMGenerateCameraRtspStreamRequest());
116         assertThat(json, is(fromFile("generate-camera-rtsp-stream-request.json")));
117     }
118
119     @Test
120     public void serializeSetFanTimerRequestWithDuration() throws IOException {
121         String json = toJson(new SDMSetFanTimerRequest(SDMFanTimerMode.ON, Duration.ofSeconds(3600)));
122         assertThat(json, is(fromFile("set-fan-timer-request-with-duration.json")));
123     }
124
125     @Test
126     public void serializeSetFanTimerRequestWithoutDuration() throws IOException {
127         String json = toJson(new SDMSetFanTimerRequest(SDMFanTimerMode.ON));
128         assertThat(json, is(fromFile("set-fan-timer-request-without-duration.json")));
129     }
130
131     @Test
132     public void serializeSetThermostatCoolSetpointRequest() throws IOException {
133         String json = toJson(new SDMSetThermostatCoolSetpointRequest(new BigDecimal("20.0")));
134         assertThat(json, is(fromFile("set-thermostat-cool-setpoint-request.json")));
135     }
136
137     @Test
138     public void serializeSetThermostatEcoModeRequest() throws IOException {
139         String json = toJson(new SDMSetThermostatEcoModeRequest(SDMThermostatEcoMode.MANUAL_ECO));
140         assertThat(json, is(fromFile("set-thermostat-eco-mode-request.json")));
141     }
142
143     @Test
144     public void serializeSetThermostatHeatSetpointRequest() throws IOException {
145         String json = toJson(new SDMSetThermostatHeatSetpointRequest(new BigDecimal("15.0")));
146         assertThat(json, is(fromFile("set-thermostat-heat-setpoint-request.json")));
147     }
148
149     @Test
150     public void serializeSetThermostatModeRequest() throws IOException {
151         String json = toJson(new SDMSetThermostatModeRequest(SDMThermostatMode.HEATCOOL));
152         assertThat(json, is(fromFile("set-thermostat-mode-request.json")));
153     }
154
155     @Test
156     public void serializeSetThermostatRangeSetpointRequest() throws IOException {
157         String json = toJson(new SDMSetThermostatRangeSetpointRequest(new BigDecimal("15.0"), new BigDecimal("20.0")));
158         assertThat(json, is(fromFile("set-thermostat-range-setpoint-request.json")));
159     }
160
161     @Test
162     public void serializeStopCameraRtspStreamRequest() throws IOException {
163         String json = toJson(new SDMStopCameraRtspStreamRequest("CjY5Y3VKaTZwR3o4Y19YbTVfMF..."));
164         assertThat(json, is(fromFile("stop-camera-rtsp-stream-request.json")));
165     }
166 }