Skip to content

Commit

Permalink
Fix error handling for deserializers
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed May 19, 2023
1 parent d149a4e commit 9759f64
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.lang.reflect.Type;
import java.time.Instant;
import java.time.format.DateTimeParseException;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -35,8 +36,12 @@ public class InstantDeserializer implements JsonDeserializer<Instant> {
public @Nullable Instant deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2)
throws JsonParseException {
String content = element.getAsString();
// When writing this, the format of the provided UTC strings lacks the trailing 'Z'.
// In case this would be fixed in the future, gracefully support both with and without this.
return Instant.parse(content.endsWith("Z") ? content : content + "Z");
try {
// When writing this, the format of the provided UTC strings lacks the trailing 'Z'.
// In case this would be fixed in the future, gracefully support both with and without this.
return Instant.parse(content.endsWith("Z") ? content : content + "Z");
} catch (DateTimeParseException e) {
throw new JsonParseException("Could not parse as Instant: " + content, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -34,6 +35,10 @@ public class LocalDateDeserializer implements JsonDeserializer<LocalDate> {
@Override
public @Nullable LocalDate deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2)
throws JsonParseException {
return LocalDate.parse(element.getAsString().substring(0, 10));
try {
return LocalDate.parse(element.getAsString().substring(0, 10));
} catch (DateTimeParseException e) {
throw new JsonParseException("Could not parse as LocalDate: " + element.getAsString(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -34,6 +35,10 @@ public class LocalDateTimeDeserializer implements JsonDeserializer<LocalDateTime
@Override
public @Nullable LocalDateTime deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2)
throws JsonParseException {
return LocalDateTime.parse(element.getAsString());
try {
return LocalDateTime.parse(element.getAsString());
} catch (DateTimeParseException e) {
throw new JsonParseException("Could not parse as LocalDateTime: " + element.getAsString(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.energidataservice.internal.api.serialization;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.time.Instant;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.junit.jupiter.MockitoExtension;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;

/**
* Tests for {@link InstantDeserializer}.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
@ExtendWith(MockitoExtension.class)
public class InstantDeserializerTest {

private final Gson gson = new GsonBuilder().registerTypeAdapter(Instant.class, new InstantDeserializer()).create();

@Test
void instantWhenInvalidShouldThrowJsonParseException() {
assertThrows(JsonParseException.class, () -> {
gson.fromJson("\"invalid\"", Instant.class);
});
}

@ParameterizedTest
@ValueSource(strings = { "\"2023-04-17T20:38:01Z\"", "\"2023-04-17T20:38:01\"" })
void instantWhenValidShouldParse(String input) {
assertThat((@Nullable Instant) gson.fromJson(input, Instant.class),
is(equalTo(Instant.ofEpochSecond(1681763881))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.energidataservice.internal.api.serialization;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.time.LocalDateTime;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;

/**
* Tests for {@link LocalDateDeserializer}.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
@ExtendWith(MockitoExtension.class)
public class LocalDateDeserializerTest {

private final Gson gson = new GsonBuilder()
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer()).create();

@Test
void localDateTimeWhenInvalidShouldThrowJsonParseException() {
assertThrows(JsonParseException.class, () -> {
gson.fromJson("\"invalid\"", LocalDateTime.class);
});
}

@Test
void instantWhenValidShouldParse() {
assertThat((@Nullable LocalDateTime) gson.fromJson("\"2023-04-17T20:38:01\"", LocalDateTime.class),
is(equalTo(LocalDateTime.of(2023, 4, 17, 20, 38, 1, 0))));
}
}

0 comments on commit 9759f64

Please sign in to comment.