2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nest.internal.sdm.dto;
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.*;
20 import java.io.IOException;
21 import java.math.BigDecimal;
22 import java.time.Duration;
23 import java.time.ZonedDateTime;
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;
49 * Tests (de)serialization of {@link org.openhab.binding.nest.internal.sdm.dto.SDMCommands} requests
50 * and responses from/to JSON.
52 * @author Wouter Born - Initial contribution
55 public class SDMCommandsTest {
58 public void deserializeExtendCameraRtspStreamResponse() throws IOException {
59 SDMExtendCameraRtspStreamResponse response = fromJson("extend-camera-rtsp-stream-response.json",
60 SDMExtendCameraRtspStreamResponse.class);
61 assertThat(response, is(notNullValue()));
63 SDMExtendCameraRtspStreamResults results = response.results;
64 assertThat(results, is(notNullValue()));
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")));
72 public void deserializeGenerateCameraImageResponse() throws IOException {
73 SDMGenerateCameraImageResponse response = fromJson("generate-camera-image-response.json",
74 SDMGenerateCameraImageResponse.class);
75 assertThat(response, is(notNullValue()));
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"));
84 public void deserializeGenerateCameraRtspStreamResponse() throws IOException {
85 SDMGenerateCameraRtspStreamResponse response = fromJson("generate-camera-rtsp-stream-response.json",
86 SDMGenerateCameraRtspStreamResponse.class);
87 assertThat(response, is(notNullValue()));
89 SDMGenerateCameraRtspStreamResults results = response.results;
90 assertThat(results, is(notNullValue()));
92 SDMCameraRtspStreamUrls streamUrls = results.streamUrls;
93 assertThat(streamUrls, is(notNullValue()));
94 assertThat(streamUrls.rtspUrl, is("rtsps://someurl.com/CjY5Y3VKaTZwR3o4Y19YbTVfMF...?auth=g.0.streamingToken"));
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")));
102 public void serializeExtendCameraRtspStreamRequest() throws IOException {
103 String json = toJson(new SDMExtendCameraRtspStreamRequest("CjY5Y3VKaTZwR3o4Y19YbTVfMF..."));
104 assertThat(json, is(fromFile("extend-camera-rtsp-stream-request.json")));
108 public void serializeGenerateCameraImageRequest() throws IOException {
109 String json = toJson(new SDMGenerateCameraImageRequest("FWWVQVUdGNUlTU2V4MGV2aTNXV..."));
110 assertThat(json, is(fromFile("generate-camera-image-request.json")));
114 public void serializeGenerateCameraRtspStreamRequest() throws IOException {
115 String json = toJson(new SDMGenerateCameraRtspStreamRequest());
116 assertThat(json, is(fromFile("generate-camera-rtsp-stream-request.json")));
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")));
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")));
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")));
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")));
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")));
150 public void serializeSetThermostatModeRequest() throws IOException {
151 String json = toJson(new SDMSetThermostatModeRequest(SDMThermostatMode.HEATCOOL));
152 assertThat(json, is(fromFile("set-thermostat-mode-request.json")));
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")));
162 public void serializeStopCameraRtspStreamRequest() throws IOException {
163 String json = toJson(new SDMStopCameraRtspStreamRequest("CjY5Y3VKaTZwR3o4Y19YbTVfMF..."));
164 assertThat(json, is(fromFile("stop-camera-rtsp-stream-request.json")));