]> git.basschouten.com Git - openhab-addons.git/commitdiff
[surepetcare] Fix DateTimeParseException (#16087)
authorJacob Laursen <jacob-github@vindvejr.dk>
Wed, 20 Dec 2023 17:38:53 +0000 (18:38 +0100)
committerGitHub <noreply@github.com>
Wed, 20 Dec 2023 17:38:53 +0000 (18:38 +0100)
Fixes #16082

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/utils/GsonZonedDateTimeTypeAdapter.java
bundles/org.openhab.binding.surepetcare/src/test/java/org/openhab/binding/surepetcare/internal/data/SurePetcareTopologyTest.java

index 4b4f19d12dc24db0ee88bc38b0abc93638dcad51..573570eb889bbdd44d4b7ae44afa33eee8be2b7d 100644 (file)
@@ -15,6 +15,7 @@ package org.openhab.binding.surepetcare.internal.utils;
 import java.lang.reflect.Type;
 import java.time.ZonedDateTime;
 import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
@@ -76,6 +77,11 @@ public class GsonZonedDateTimeTypeAdapter implements JsonSerializer<ZonedDateTim
     @Override
     public @Nullable ZonedDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
             throws JsonParseException {
-        return ZONED_FORMATTER.parse(json.getAsString(), ZonedDateTime::from);
+        String content = json.getAsString();
+        try {
+            return ZonedDateTime.parse(content);
+        } catch (DateTimeParseException e) {
+            throw new JsonParseException("Could not parse as ZonedDateTime: " + content, e);
+        }
     }
 }
index a333543396d11ecee4a5b9f8e3feb7242cf6cdd1..d4b43ed43f70c8124921885317114bd4f239c100 100644 (file)
@@ -14,6 +14,9 @@ package org.openhab.binding.surepetcare.internal.data;
 
 import static org.junit.jupiter.api.Assertions.*;
 
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
+
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.surepetcare.internal.SurePetcareConstants;
@@ -78,4 +81,49 @@ public class SurePetcareTopologyTest {
             fail("GSON returned null");
         }
     }
+
+    @Test
+    public void testDateFormats() {
+        String testResponse = """
+                {
+                    "devices": [],
+                    "households": [
+                        {
+                            "id": 0,
+                            "name": "***",
+                            "share_code": "***",
+                            "created_user_id": 0,
+                            "timezone_id": 374,
+                            "version": "MTE=",
+                            "created_at": "2021-04-24T11:41:15+00:00",
+                            "updated_at": "2023-12-16T21:08:19.637892+00:00",
+                            "invites": [],
+                            "users": [],
+                            "timezone": {
+                                "id": 374,
+                                "name": "(UTC+02:00) Europe/Zurich",
+                                "timezone": "Europe/Zurich",
+                                "utc_offset": 7200,
+                                "created_at": "2017-08-03T08:35:34+00:00",
+                                "updated_at": "2017-08-03T08:37:15+00:00"
+                            }
+                        }
+                    ],
+                    "pets": [],
+                    "photos": [],
+                    "tags": [],
+                    "user": {}
+                }
+                """;
+
+        SurePetcareTopology response = SurePetcareConstants.GSON.fromJson(testResponse, SurePetcareTopology.class);
+
+        assertNotNull(response);
+        assertNotNull(response.households);
+        assertEquals(1, response.households.size());
+        assertEquals(ZonedDateTime.of(2021, 4, 24, 11, 41, 15, 0, ZoneOffset.UTC),
+                response.households.get(0).createdAt);
+        assertEquals(ZonedDateTime.of(2023, 12, 16, 21, 8, 19, 637892000, ZoneOffset.UTC),
+                response.households.get(0).updatedAt);
+    }
 }