mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 07:02:02 +01:00
[surepetcare] Fix DateTimeParseException (#16087)
Fixes #16082 Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
parent
eb850e5175
commit
94d110a98c
@ -15,6 +15,7 @@ package org.openhab.binding.surepetcare.internal.utils;
|
|||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.format.DateTimeParseException;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
@ -76,6 +77,11 @@ public class GsonZonedDateTimeTypeAdapter implements JsonSerializer<ZonedDateTim
|
|||||||
@Override
|
@Override
|
||||||
public @Nullable ZonedDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
public @Nullable ZonedDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
||||||
throws JsonParseException {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,9 @@ package org.openhab.binding.surepetcare.internal.data;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.openhab.binding.surepetcare.internal.SurePetcareConstants;
|
import org.openhab.binding.surepetcare.internal.SurePetcareConstants;
|
||||||
@ -78,4 +81,49 @@ public class SurePetcareTopologyTest {
|
|||||||
fail("GSON returned null");
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user