]> git.basschouten.com Git - openhab-addons.git/blob
2a47a6f517cade2349d0ba53c529f2578e4313ad
[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.*;
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;
20
21 import java.io.IOException;
22 import java.math.BigDecimal;
23 import java.time.ZonedDateTime;
24 import java.util.List;
25
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;
37
38 /**
39  * Tests deserialization of {@link org.openhab.binding.nest.internal.sdm.dto.SDMEvent}s from JSON.
40  *
41  * @author Wouter Born - Initial contribution
42  */
43 @NonNullByDefault
44 public class SDMEventTest {
45
46     @Test
47     public void deserializeResourceUpdateEvent() throws IOException {
48         SDMEvent event = fromJson("resource-update-event.json", SDMEvent.class);
49         assertThat(event, is(notNullValue()));
50
51         assertThat(event.eventId, is("053a5f98-8c9d-426e-acf1-6b8660558832"));
52         assertThat(event.timestamp, is(ZonedDateTime.parse("2019-01-01T00:00:01Z")));
53
54         assertThat(event.relationUpdate, is(nullValue()));
55
56         SDMResourceUpdate resourceUpdate = event.resourceUpdate;
57         assertThat(resourceUpdate, is(notNullValue()));
58         assertThat(resourceUpdate.name.name, is("enterprises/project-id/devices/device-id"));
59
60         SDMTraits traits = resourceUpdate.traits;
61         assertThat(traits, is(notNullValue()));
62         assertThat(traits.traitList(), hasSize(3));
63
64         SDMResourceUpdateEvents events = resourceUpdate.events;
65         assertThat(events, is(notNullValue()));
66         assertThat(events.eventList(), hasSize(4));
67
68         SDMDeviceEvent cameraMotionEvent = events.cameraMotionEvent;
69         assertThat(cameraMotionEvent, is(notNullValue()));
70         assertThat(cameraMotionEvent.eventSessionId, is("ESI1"));
71         assertThat(cameraMotionEvent.eventId, is("EID1"));
72
73         SDMDeviceEvent cameraPersonEvent = events.cameraPersonEvent;
74         assertThat(cameraPersonEvent, is(notNullValue()));
75         assertThat(cameraPersonEvent.eventSessionId, is("ESI2"));
76         assertThat(cameraPersonEvent.eventId, is("EID2"));
77
78         SDMDeviceEvent cameraSoundEvent = events.cameraSoundEvent;
79         assertThat(cameraSoundEvent, is(notNullValue()));
80         assertThat(cameraSoundEvent.eventSessionId, is("ESI3"));
81         assertThat(cameraSoundEvent.eventId, is("EID3"));
82
83         SDMDeviceEvent doorbellChimeEvent = events.doorbellChimeEvent;
84         assertThat(doorbellChimeEvent, is(notNullValue()));
85         assertThat(doorbellChimeEvent.eventSessionId, is("ESI4"));
86         assertThat(doorbellChimeEvent.eventId, is("EID4"));
87
88         SDMTemperatureTrait temperature = traits.temperature;
89         assertThat(temperature, is(notNullValue()));
90         assertThat(temperature.ambientTemperatureCelsius, is(new BigDecimal("19.73")));
91
92         SDMThermostatHvacTrait thermostatHvac = traits.thermostatHvac;
93         assertThat(thermostatHvac, is(notNullValue()));
94         assertThat(thermostatHvac.status, is(SDMHvacStatus.OFF));
95
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()));
100
101         assertThat(event.userId, is("AVPHwEuBfnPOnTqzVFT4IONX2Qqhu9EJ4ubO-bNnQ-yi"));
102         assertThat(event.resourceGroup, is(List.of(new SDMResourceName("enterprises/project-id/devices/device-id"))));
103     }
104
105     @Test
106     public void deserializeRelationCreatedEvent() throws IOException {
107         SDMEvent event = fromJson("relation-created-event.json", SDMEvent.class);
108         assertThat(event, is(notNullValue()));
109
110         assertThat(event.eventId, is("0120ecc7-3b57-4eb4-9941-91609f189fb4"));
111         assertThat(event.timestamp, is(ZonedDateTime.parse("2019-01-01T00:00:01Z")));
112
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"));
118
119         assertThat(event.resourceUpdate, is(nullValue()));
120         assertThat(event.userId, is("AVPHwEuBfnPOnTqzVFT4IONX2Qqhu9EJ4ubO-bNnQ-yi"));
121         assertThat(event.resourceGroup, is(nullValue()));
122     }
123
124     @Test
125     public void deserializeRelationDeletedEvent() throws IOException {
126         SDMEvent event = fromJson("relation-deleted-event.json", SDMEvent.class);
127         assertThat(event, is(notNullValue()));
128
129         assertThat(event.eventId, is("0120ecc7-3b57-4eb4-9941-91609f189fb4"));
130         assertThat(event.timestamp, is(ZonedDateTime.parse("2019-01-01T00:00:01Z")));
131
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"));
137
138         assertThat(event.resourceUpdate, is(nullValue()));
139         assertThat(event.userId, is("AVPHwEuBfnPOnTqzVFT4IONX2Qqhu9EJ4ubO-bNnQ-yi"));
140         assertThat(event.resourceGroup, is(nullValue()));
141     }
142
143     @Test
144     public void deserializeRelationUpdatedEvent() throws IOException {
145         SDMEvent event = fromJson("relation-updated-event.json", SDMEvent.class);
146         assertThat(event, is(notNullValue()));
147
148         assertThat(event.eventId, is("0120ecc7-3b57-4eb4-9941-91609f189fb4"));
149         assertThat(event.timestamp, is(ZonedDateTime.parse("2019-01-01T00:00:01Z")));
150
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"));
156
157         assertThat(event.resourceUpdate, is(nullValue()));
158         assertThat(event.userId, is("AVPHwEuBfnPOnTqzVFT4IONX2Qqhu9EJ4ubO-bNnQ-yi"));
159         assertThat(event.resourceGroup, is(nullValue()));
160     }
161 }