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.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
18 import static org.hamcrest.core.Is.is;
19 import static org.openhab.binding.nest.internal.sdm.dto.SDMDataUtil.fromJson;
21 import java.io.IOException;
22 import java.math.BigDecimal;
23 import java.time.ZonedDateTime;
24 import java.util.List;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.binding.nest.internal.sdm.dto.SDMEvent.SDMDeviceEvent;
29 import org.openhab.binding.nest.internal.sdm.dto.SDMEvent.SDMRelationUpdate;
30 import org.openhab.binding.nest.internal.sdm.dto.SDMEvent.SDMRelationUpdateType;
31 import org.openhab.binding.nest.internal.sdm.dto.SDMEvent.SDMResourceUpdate;
32 import org.openhab.binding.nest.internal.sdm.dto.SDMEvent.SDMResourceUpdateEvents;
33 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMHvacStatus;
34 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMTemperatureTrait;
35 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatHvacTrait;
36 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatTemperatureSetpointTrait;
39 * Tests deserialization of {@link org.openhab.binding.nest.internal.sdm.dto.SDMEvent}s from JSON.
41 * @author Wouter Born - Initial contribution
44 public class SDMEventTest {
47 public void deserializeResourceUpdateEvent() throws IOException {
48 SDMEvent event = fromJson("resource-update-event.json", SDMEvent.class);
49 assertThat(event, is(notNullValue()));
51 assertThat(event.eventId, is("053a5f98-8c9d-426e-acf1-6b8660558832"));
52 assertThat(event.timestamp, is(ZonedDateTime.parse("2019-01-01T00:00:01Z")));
54 assertThat(event.relationUpdate, is(nullValue()));
56 SDMResourceUpdate resourceUpdate = event.resourceUpdate;
57 assertThat(resourceUpdate, is(notNullValue()));
58 assertThat(resourceUpdate.name.name, is("enterprises/project-id/devices/device-id"));
60 SDMTraits traits = resourceUpdate.traits;
61 assertThat(traits, is(notNullValue()));
62 assertThat(traits.traitList(), hasSize(3));
64 SDMResourceUpdateEvents events = resourceUpdate.events;
65 assertThat(events, is(notNullValue()));
66 assertThat(events.eventList(), hasSize(4));
68 SDMDeviceEvent cameraMotionEvent = events.cameraMotionEvent;
69 assertThat(cameraMotionEvent, is(notNullValue()));
70 assertThat(cameraMotionEvent.eventSessionId, is("ESI1"));
71 assertThat(cameraMotionEvent.eventId, is("EID1"));
73 SDMDeviceEvent cameraPersonEvent = events.cameraPersonEvent;
74 assertThat(cameraPersonEvent, is(notNullValue()));
75 assertThat(cameraPersonEvent.eventSessionId, is("ESI2"));
76 assertThat(cameraPersonEvent.eventId, is("EID2"));
78 SDMDeviceEvent cameraSoundEvent = events.cameraSoundEvent;
79 assertThat(cameraSoundEvent, is(notNullValue()));
80 assertThat(cameraSoundEvent.eventSessionId, is("ESI3"));
81 assertThat(cameraSoundEvent.eventId, is("EID3"));
83 SDMDeviceEvent doorbellChimeEvent = events.doorbellChimeEvent;
84 assertThat(doorbellChimeEvent, is(notNullValue()));
85 assertThat(doorbellChimeEvent.eventSessionId, is("ESI4"));
86 assertThat(doorbellChimeEvent.eventId, is("EID4"));
88 SDMTemperatureTrait temperature = traits.temperature;
89 assertThat(temperature, is(notNullValue()));
90 assertThat(temperature.ambientTemperatureCelsius, is(new BigDecimal("19.73")));
92 SDMThermostatHvacTrait thermostatHvac = traits.thermostatHvac;
93 assertThat(thermostatHvac, is(notNullValue()));
94 assertThat(thermostatHvac.status, is(SDMHvacStatus.OFF));
96 SDMThermostatTemperatureSetpointTrait thermostatTemperatureSetpoint = traits.thermostatTemperatureSetpoint;
97 assertThat(thermostatTemperatureSetpoint, is(notNullValue()));
98 assertThat(thermostatTemperatureSetpoint.heatCelsius, is(new BigDecimal("14.92249")));
99 assertThat(thermostatTemperatureSetpoint.coolCelsius, is(nullValue()));
101 assertThat(event.userId, is("AVPHwEuBfnPOnTqzVFT4IONX2Qqhu9EJ4ubO-bNnQ-yi"));
102 assertThat(event.resourceGroup, is(List.of(new SDMResourceName("enterprises/project-id/devices/device-id"))));
106 public void deserializeRelationCreatedEvent() throws IOException {
107 SDMEvent event = fromJson("relation-created-event.json", SDMEvent.class);
108 assertThat(event, is(notNullValue()));
110 assertThat(event.eventId, is("0120ecc7-3b57-4eb4-9941-91609f189fb4"));
111 assertThat(event.timestamp, is(ZonedDateTime.parse("2019-01-01T00:00:01Z")));
113 SDMRelationUpdate relationUpdate = event.relationUpdate;
114 assertThat(relationUpdate, is(notNullValue()));
115 assertThat(relationUpdate.type, is(SDMRelationUpdateType.CREATED));
116 assertThat(relationUpdate.subject.name, is("enterprises/project-id/structures/structure-id"));
117 assertThat(relationUpdate.object.name, is("enterprises/project-id/devices/device-id"));
119 assertThat(event.resourceUpdate, is(nullValue()));
120 assertThat(event.userId, is("AVPHwEuBfnPOnTqzVFT4IONX2Qqhu9EJ4ubO-bNnQ-yi"));
121 assertThat(event.resourceGroup, is(nullValue()));
125 public void deserializeRelationDeletedEvent() throws IOException {
126 SDMEvent event = fromJson("relation-deleted-event.json", SDMEvent.class);
127 assertThat(event, is(notNullValue()));
129 assertThat(event.eventId, is("0120ecc7-3b57-4eb4-9941-91609f189fb4"));
130 assertThat(event.timestamp, is(ZonedDateTime.parse("2019-01-01T00:00:01Z")));
132 SDMRelationUpdate relationUpdate = event.relationUpdate;
133 assertThat(relationUpdate, is(notNullValue()));
134 assertThat(relationUpdate.type, is(SDMRelationUpdateType.DELETED));
135 assertThat(relationUpdate.subject.name, is("enterprises/project-id/structures/structure-id"));
136 assertThat(relationUpdate.object.name, is("enterprises/project-id/devices/device-id"));
138 assertThat(event.resourceUpdate, is(nullValue()));
139 assertThat(event.userId, is("AVPHwEuBfnPOnTqzVFT4IONX2Qqhu9EJ4ubO-bNnQ-yi"));
140 assertThat(event.resourceGroup, is(nullValue()));
144 public void deserializeRelationUpdatedEvent() throws IOException {
145 SDMEvent event = fromJson("relation-updated-event.json", SDMEvent.class);
146 assertThat(event, is(notNullValue()));
148 assertThat(event.eventId, is("0120ecc7-3b57-4eb4-9941-91609f189fb4"));
149 assertThat(event.timestamp, is(ZonedDateTime.parse("2019-01-01T00:00:01Z")));
151 SDMRelationUpdate relationUpdate = event.relationUpdate;
152 assertThat(relationUpdate, is(notNullValue()));
153 assertThat(relationUpdate.type, is(SDMRelationUpdateType.UPDATED));
154 assertThat(relationUpdate.subject.name, is("enterprises/project-id/structures/structure-id"));
155 assertThat(relationUpdate.object.name, is("enterprises/project-id/devices/device-id"));
157 assertThat(event.resourceUpdate, is(nullValue()));
158 assertThat(event.userId, is("AVPHwEuBfnPOnTqzVFT4IONX2Qqhu9EJ4ubO-bNnQ-yi"));
159 assertThat(event.resourceGroup, is(nullValue()));