]> git.basschouten.com Git - openhab-addons.git/blob
2ae983c8b51456d79bf0eb8ad46c6d766911b0c6
[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.math.BigDecimal;
18 import java.text.ParseException;
19 import java.time.LocalDate;
20 import java.time.ZonedDateTime;
21 import java.time.format.DateTimeFormatter;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.surepetcare.internal.SurePetcareConstants;
26 import org.openhab.binding.surepetcare.internal.dto.SurePetcarePet;
27
28 /**
29  * The {@link SurePetcarePetTest} class implements unit test case for {@link SurePetcarePet}
30  *
31  * @author Rene Scherer - Initial contribution
32  */
33 @NonNullByDefault
34 public class SurePetcarePetTest {
35
36     // {
37     // "id":34675,
38     // "name":"Cat",
39     // "gender":0,
40     // "date_of_birth":"2017-08-01T00:00:00+00:00",
41     // "weight":"3.5",
42     // "comments":"Test Comment",
43     // "household_id":87435,
44     // "breed_id":382,
45     // "photo_id":23412,
46     // "species_id":1,
47     // "tag_id":60456,
48     // "version":"Mw==",
49     // "created_at":"2019-09-02T09:27:17+00:00",
50     // "updated_at":"2019-10-03T12:17:48+00:00",
51     // "conditions":[
52     // {
53     // "id":18,
54     // "version":"MA==",
55     // "created_at":"2019-10-03T12:17:48+00:00",
56     // "updated_at":"2019-10-03T12:17:48+00:00"
57     // },
58     // {
59     // "id":17,
60     // "version":"MA==",
61     // "created_at":"2019-10-03T12:17:48+00:00",
62     // "updated_at":"2019-10-03T12:17:48+00:00"
63     // }
64     // ],
65     // "photo":{
66     // "id":79293,
67     // "location":"https:\/\/surehub.s3.amazonaws.com\/user-photos\/thm\/23412\/z70LUtqaHVhlsdfuyHKJH5HDysg5AR6GvQwdAZptCgeZU.jpg",
68     // "uploading_user_id":52815,
69     // "version":"MA==",
70     // "created_at":"2019-09-02T09:31:07+00:00",
71     // "updated_at":"2019-09-02T09:31:07+00:00"
72     // },
73     // "position":{
74     // "tag_id":60456,
75     // "device_id":318986,
76     // "where":1,
77     // "since":"2019-10-03T10:23:37+00:00"
78     // },
79     // "status":{
80     // "activity":{
81     // "tag_id":60456,
82     // "device_id":318986,
83     // "where":1,
84     // "since":"2019-10-03T10:23:37+00:00"
85     // }
86     // }
87     // }
88
89     private static final DateTimeFormatter LOCAL_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
90     private static final DateTimeFormatter ZONED_DATETIME_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
91
92     @Test
93     public void testJsonDeserialize1() throws ParseException {
94         String testReponse = "{\"id\":34675,\"name\":\"Cat\",\"gender\":0,\"date_of_birth\":\"2017-08-01T00:00:00+00:00\",\"weight\":\"3.5\",\"comments\":\"Test Comment\",\"household_id\":87435,\"breed_id\":382,\"photo_id\":23412,\"species_id\":1,\"tag_id\":60456,\"version\":\"Mw==\",\"created_at\":\"2019-09-02T09:27:17+00:00\",\"updated_at\":\"2019-10-03T12:17:48+00:00\",\"conditions\":[{\"id\":18,\"version\":\"MA==\",\"created_at\":\"2019-10-03T12:17:48+00:00\",\"updated_at\":\"2019-10-03T12:17:48+00:00\"},{\"id\":17,\"version\":\"MA==\",\"created_at\":\"2019-10-03T12:17:48+00:00\",\"updated_at\":\"2019-10-03T12:17:48+00:00\"}],\"photo\":{\"id\":79293,\"location\":\"https:\\/\\/surehub.s3.amazonaws.com\\/user-photos\\/thm\\/23412\\/z70LUtqaHVhlsdfuyHKJH5HDysg5AR6GvQwdAZptCgeZU.jpg\",\"uploading_user_id\":52815,\"version\":\"MA==\",\"created_at\":\"2019-09-02T09:31:07+00:00\",\"updated_at\":\"2019-09-02T09:31:07+00:00\"},\"position\":{\"tag_id\":60456,\"device_id\":318986,\"where\":1,\"since\":\"2019-10-03T10:23:37+00:00\"},\"status\":{\"activity\":{\"tag_id\":60456,\"device_id\":318986,\"where\":1,\"since\":\"2019-10-03T10:23:37+00:00\"}}}";
95         SurePetcarePet response = SurePetcareConstants.GSON.fromJson(testReponse, SurePetcarePet.class);
96
97         if (response != null) {
98             assertEquals(Long.valueOf(34675), response.id);
99             assertEquals("Cat", response.name);
100             assertEquals(Integer.valueOf(0), response.genderId);
101             assertEquals(LocalDate.parse("2017-08-01", LOCAL_DATE_FORMATTER), response.dateOfBirth);
102             assertEquals(BigDecimal.valueOf(3.5), response.weight);
103             assertEquals("Test Comment", response.comments);
104             assertEquals(Long.valueOf(87435), response.householdId);
105             assertEquals(Long.valueOf(23412), response.photoId);
106             assertEquals(SurePetcarePet.PetSpecies.CAT.id, response.speciesId);
107             assertEquals(Integer.valueOf(382), response.breedId);
108             assertEquals(Integer.valueOf(1), response.status.activity.where);
109             assertEquals(ZonedDateTime.parse("2019-10-03T10:23:37+00:00", ZONED_DATETIME_FORMATTER),
110                     response.status.activity.since);
111         } else {
112             fail("GSON returned null");
113         }
114     }
115
116     @Test
117     public void testJsonDeserialize2() throws ParseException {
118         String testReponse = "{\"id\":30622,\"name\":\"Cat\",\"gender\":1,\"date_of_birth\":\"2016-04-01T00:00:00+00:00\",\"weight\":\"6\",\"comments\":\"\",\"household_id\":21005,\"breed_id\":382,\"food_type_id\":1,\"photo_id\":77957,\"species_id\":1,\"tag_id\":24725,\"version\":\"OA==\",\"created_at\":\"2018-12-22T08:59:13+00:00\",\"updated_at\":\"2019-08-26T18:17:38+00:00\",\"photo\":{\"id\":77957,\"location\":\"https://surehub.s3.amazonaws.com/user-photos/thm/22360/1jhp4OtwmNvWXrsT4pWLJhoYOt7Ti9UVm5SjsFoC9Y.jpg          \",\"uploading_user_id\":22360,\"version\":\"MA==\",\"created_at\":\"2019-08-26T18:17:38+00:00\",\"updated_at\":\"2019-08-26T18:17:38+00:00\"},\"position\":{\"tag_id\":24725,\"device_id\":243573,\"where\":2,\"since\":\"2020-05-01T06:01:53+00:00\"},\"status\":{\"activity\":{\"tag_id\":24725,\"device_id\":243573,\"where\":2,\"since\":\"2020-05-01T06:01:53+00:00\"}}}";
119         SurePetcarePet response = SurePetcareConstants.GSON.fromJson(testReponse, SurePetcarePet.class);
120
121         if (response != null) {
122             assertEquals(Long.valueOf(30622), response.id);
123             assertEquals("Cat", response.name);
124             assertEquals(Integer.valueOf(1), response.genderId);
125             assertEquals(LocalDate.parse("2016-04-01", LOCAL_DATE_FORMATTER), response.dateOfBirth);
126             assertEquals(BigDecimal.valueOf(6), response.weight);
127             assertEquals("", response.comments);
128             assertEquals(Long.valueOf(21005), response.householdId);
129             assertEquals(Long.valueOf(77957), response.photoId);
130             assertEquals(SurePetcarePet.PetSpecies.CAT.id, response.speciesId);
131             assertEquals(Integer.valueOf(382), response.breedId);
132             assertEquals(Integer.valueOf(2), response.status.activity.where);
133             assertEquals(ZonedDateTime.parse("2020-05-01T06:01:53+00:00", ZONED_DATETIME_FORMATTER),
134                     response.status.activity.since);
135         } else {
136             fail("GSON returned null");
137         }
138     }
139 }