]> git.basschouten.com Git - openhab-addons.git/blob
e51cfd540432b842ff20c2fead4b11574da9bc66
[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.surepetcare.internal.data;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.text.ParseException;
18 import java.time.ZonedDateTime;
19 import java.time.format.DateTimeFormatter;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.surepetcare.internal.SurePetcareConstants;
24 import org.openhab.binding.surepetcare.internal.dto.SurePetcarePetActivity;
25
26 /**
27  * The {@link SurePetcarePetLocationTest} class implements unit test case for {@link SurePetcarePetLocation}
28  *
29  * @author Rene Scherer - Initial contribution
30  */
31 @NonNullByDefault
32 public class SurePetcarePetActivityTest {
33
34     private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
35
36     @Test
37     public void testJsonDeserialize() throws ParseException {
38         String testReponse = "{\"tag_id\":60126,\"device_id\":376236,\"where\":2,\"since\":\"2019-09-11T13:09:07+00:00\"}";
39         SurePetcarePetActivity response = SurePetcareConstants.GSON.fromJson(testReponse, SurePetcarePetActivity.class);
40
41         if (response != null) {
42             assertEquals(Long.valueOf(60126), response.tagId);
43             assertEquals(Long.valueOf(376236), response.deviceId);
44             assertEquals(Integer.valueOf(2), response.where);
45             ZonedDateTime since = FORMATTER.parse("2019-09-11T13:09:07+00:00", ZonedDateTime::from);
46             assertEquals(since, response.since);
47         } else {
48             fail("GSON returned null");
49         }
50     }
51
52     @Test
53     public void testJsonFullSerialize() throws ParseException {
54         ZonedDateTime since = ZonedDateTime.parse("2019-09-11T13:09:07+00:00");
55         SurePetcarePetActivity location = new SurePetcarePetActivity(2, since);
56
57         String json = SurePetcareConstants.GSON.toJson(location, SurePetcarePetActivity.class);
58
59         assertEquals("{\"where\":2,\"since\":\"2019-09-11T13:09:07+00:00\"}", json);
60     }
61 }