]> git.basschouten.com Git - openhab-addons.git/blob
cc6b461dfdee84e0a7b7c711eb2bff389362b507
[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.collection.IsCollectionWithSize.hasSize;
18 import static org.hamcrest.core.Is.is;
19 import static org.openhab.binding.nest.internal.sdm.dto.SDMDataUtil.*;
20
21 import java.io.IOException;
22 import java.time.ZonedDateTime;
23 import java.util.List;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.binding.nest.internal.sdm.dto.PubSubRequestsResponses.PubSubAcknowledgeRequest;
28 import org.openhab.binding.nest.internal.sdm.dto.PubSubRequestsResponses.PubSubCreateRequest;
29 import org.openhab.binding.nest.internal.sdm.dto.PubSubRequestsResponses.PubSubMessage;
30 import org.openhab.binding.nest.internal.sdm.dto.PubSubRequestsResponses.PubSubPullRequest;
31 import org.openhab.binding.nest.internal.sdm.dto.PubSubRequestsResponses.PubSubPullResponse;
32 import org.openhab.binding.nest.internal.sdm.dto.PubSubRequestsResponses.PubSubReceivedMessage;
33
34 /**
35  * Tests (de)serialization of {@link
36  * org.openhab.binding.nest.internal.sdm.dto.PubSubRequestsResponses} from/to JSON.
37  *
38  * @author Wouter Born - Initial contribution
39  */
40 @NonNullByDefault
41 public class PubSubRequestsResponsesTest {
42
43     @Test
44     public void deserializePullSubscriptionResponse() throws IOException {
45         PubSubPullResponse response = fromJson("pull-subscription-response.json", PubSubPullResponse.class);
46         assertThat(response, is(notNullValue()));
47
48         List<PubSubReceivedMessage> receivedMessages = response.receivedMessages;
49         assertThat(receivedMessages, is(notNullValue()));
50         assertThat(receivedMessages, hasSize(3));
51
52         PubSubReceivedMessage receivedMessage = receivedMessages.get(0);
53         assertThat(receivedMessage, is(notNullValue()));
54         assertThat(receivedMessage.ackId, is("AID1"));
55         PubSubMessage message = receivedMessage.message;
56         assertThat(message, is(notNullValue()));
57         assertThat(message.data, is("ZGF0YTE="));
58         assertThat(message.messageId, is("1000000000000001"));
59         assertThat(message.publishTime, is(ZonedDateTime.parse("2021-01-01T01:00:00.000Z")));
60
61         receivedMessage = receivedMessages.get(1);
62         assertThat(receivedMessage, is(notNullValue()));
63         assertThat(receivedMessage.ackId, is("AID2"));
64         message = receivedMessage.message;
65         assertThat(message, is(notNullValue()));
66         assertThat(message.data, is("ZGF0YTI="));
67         assertThat(message.messageId, is("2000000000000002"));
68         assertThat(message.publishTime, is(ZonedDateTime.parse("2021-02-02T02:00:00.000Z")));
69
70         receivedMessage = receivedMessages.get(2);
71         assertThat(receivedMessage, is(notNullValue()));
72         assertThat(receivedMessage.ackId, is("AID3"));
73         message = receivedMessage.message;
74         assertThat(message, is(notNullValue()));
75         assertThat(message.data, is("ZGF0YTM="));
76         assertThat(message.messageId, is("3000000000000003"));
77         assertThat(message.publishTime, is(ZonedDateTime.parse("2021-03-03T03:00:00.000Z")));
78     }
79
80     @Test
81     public void serializeAcknowledgeSubscriptionRequest() throws IOException {
82         String json = toJson(new PubSubAcknowledgeRequest(List.of("AID1", "AID2", "AID3")));
83         assertThat(json, is(fromFile("acknowledge-subscription-request.json")));
84     }
85
86     @Test
87     public void serializeCreateSubscriptionRequest() throws IOException {
88         String json = toJson(new PubSubCreateRequest("projects/sdm-prod/topics/enterprise-project-id", true));
89         assertThat(json, is(fromFile("create-subscription-request.json")));
90     }
91
92     @Test
93     public void serializePullSubscriptionRequest() throws IOException {
94         String json = toJson(new PubSubPullRequest(123));
95         assertThat(json, is(fromFile("pull-subscription-request.json")));
96     }
97 }