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.surepetcare.internal.data;
15 import static org.junit.jupiter.api.Assertions.*;
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;
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;
29 * The {@link SurePetcarePetTest} class implements unit test case for {@link SurePetcarePet}
31 * @author Rene Scherer - Initial contribution
34 public class SurePetcarePetTest {
40 // "date_of_birth":"2017-08-01T00:00:00+00:00",
42 // "comments":"Test Comment",
43 // "household_id":87435,
49 // "created_at":"2019-09-02T09:27:17+00:00",
50 // "updated_at":"2019-10-03T12:17:48+00:00",
55 // "created_at":"2019-10-03T12:17:48+00:00",
56 // "updated_at":"2019-10-03T12:17:48+00:00"
61 // "created_at":"2019-10-03T12:17:48+00:00",
62 // "updated_at":"2019-10-03T12:17:48+00:00"
67 // "location":"https:\/\/surehub.s3.amazonaws.com\/user-photos\/thm\/23412\/z70LUtqaHVhlsdfuyHKJH5HDysg5AR6GvQwdAZptCgeZU.jpg",
68 // "uploading_user_id":52815,
70 // "created_at":"2019-09-02T09:31:07+00:00",
71 // "updated_at":"2019-09-02T09:31:07+00:00"
75 // "device_id":318986,
77 // "since":"2019-10-03T10:23:37+00:00"
82 // "device_id":318986,
84 // "since":"2019-10-03T10:23:37+00:00"
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;
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);
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);
112 fail("GSON returned null");
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);
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);
136 fail("GSON returned null");