From 28e6dfc4af09a6808385812e7714abb37cd4c75e Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Tue, 30 Jan 2024 21:46:05 -0300 Subject: [PATCH] Fixed exception classes and improved tests --- .../com/fauna/serialization/FaunaParser.java | 5 +- .../fauna/serialization/FaunaParserTest.java | 79 ++++++++++++++----- 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java index 9714a4b9..9a085d90 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java @@ -19,6 +19,7 @@ import java.io.InputStream; import java.time.Instant; import java.time.LocalDate; +import java.time.format.DateTimeParseException; import java.util.Arrays; import java.util.HashSet; import java.util.Objects; @@ -216,7 +217,7 @@ public LocalDate getValueAsLocalDate() { validateTaggedType(DATE); try { return LocalDate.parse(taggedTokenValue); - } catch (NumberFormatException e) { + } catch (DateTimeParseException e) { throw new SerializationException("Error getting the current token as LocalDate", e); } } @@ -225,7 +226,7 @@ public Instant getValueAsTime() { validateTaggedType(TIME); try { return Instant.parse(taggedTokenValue); - } catch (NumberFormatException e) { + } catch (DateTimeParseException e) { throw new SerializationException("Error getting the current token as LocalDateTime", e); } } diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java index d40da205..ffa9c603 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -29,7 +29,7 @@ public void testGetValueAsString() throws IOException { Map.entry(FaunaTokenType.STRING, "hello") ); - assertReader(reader, expectedTokens); + assertReader(reader, expectedTokens, false); } @Test @@ -42,13 +42,17 @@ public void testGetValueAsInt() throws IOException { Map.entry(FaunaTokenType.INT, 123) ); - assertReader(reader, expectedTokens); + assertReader(reader, expectedTokens, false); String invalidJson = "{\"@int\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); FaunaParser invalidReader = new FaunaParser(invalidInputStream); - assertThrows(RuntimeException.class, invalidReader::getValueAsInt); + expectedTokens = List.of( + Map.entry(FaunaTokenType.INT, "abc") + ); + + assertReader(invalidReader, expectedTokens, true); } @Test @@ -70,7 +74,7 @@ public void testGetValueAsBooleanTrue() throws IOException { Map.entry(FaunaTokenType.TRUE, true) ); - assertReader(reader, expectedTokens); + assertReader(reader, expectedTokens, false); } @Test @@ -82,7 +86,7 @@ public void testGetValueAsBooleanFalse() throws IOException { Map.entry(FaunaTokenType.FALSE, false) ); - assertReader(reader, expectedTokens); + assertReader(reader, expectedTokens, false); } @Test @@ -95,13 +99,17 @@ public void testeGetValueAsLocalDate() throws IOException { Map.entry(FaunaTokenType.DATE, LocalDate.of(2024, 01, 23)) ); - assertReader(reader, expectedTokens); + assertReader(reader, expectedTokens, false); String invalidJson = "{\"@date\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); FaunaParser invalidReader = new FaunaParser(invalidInputStream); - assertThrows(RuntimeException.class, invalidReader::getValueAsLocalDate); + expectedTokens = List.of( + Map.entry(FaunaTokenType.DATE, "abc") + ); + + assertReader(invalidReader, expectedTokens, true); } @Test @@ -116,13 +124,17 @@ public void testeGetValueAsTime() throws IOException { Map.entry(FaunaTokenType.TIME, instant) ); - assertReader(reader, expectedTokens); + assertReader(reader, expectedTokens, false); String invalidJson = "{\"@time\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); FaunaParser invalidReader = new FaunaParser(invalidInputStream); - assertThrows(RuntimeException.class, invalidReader::getValueAsLocalDate); + expectedTokens = List.of( + Map.entry(FaunaTokenType.TIME, "abc") + ); + + assertReader(invalidReader, expectedTokens, true); } @Test @@ -137,7 +149,7 @@ public void testeGetValueAsTimeNonUTC() throws IOException { Map.entry(FaunaTokenType.TIME, instant) ); - assertReader(reader, expectedTokens); + assertReader(reader, expectedTokens, false); } @@ -151,17 +163,22 @@ public void testGetValueAsDouble() throws IOException { Map.entry(FaunaTokenType.DOUBLE, 1.23D) ); - assertReader(reader, expectedTokens); + assertReader(reader, expectedTokens, false); String invalidJson = "{\"@double\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); FaunaParser invalidReader = new FaunaParser(invalidInputStream); - assertThrows(SerializationException.class, invalidReader::getValueAsDouble); + expectedTokens = List.of( + Map.entry(FaunaTokenType.DOUBLE, "abc") + ); + + assertReader(invalidReader, expectedTokens, true); } private static void assertReader(FaunaParser reader, - List> tokens) throws IOException { + List> tokens, + boolean assertExceptions) throws IOException { for (Map.Entry entry : tokens) { reader.read(); assertNotNull(entry.getKey()); @@ -171,23 +188,47 @@ private static void assertReader(FaunaParser reader, switch (entry.getKey()) { case FIELD_NAME: case STRING: - assertEquals(entry.getValue(), reader.getValueAsString()); + if (assertExceptions) { + assertThrows(SerializationException.class, reader::getValueAsString); + } else { + assertEquals(entry.getValue(), reader.getValueAsString()); + } break; case INT: - assertEquals(entry.getValue(), reader.getValueAsInt()); + if (assertExceptions) { + assertThrows(SerializationException.class, reader::getValueAsInt); + } else { + assertEquals(entry.getValue(), reader.getValueAsInt()); + } break; case TRUE: case FALSE: - assertEquals(entry.getValue(), reader.getValueAsBoolean()); + if (assertExceptions) { + assertThrows(SerializationException.class, reader::getValueAsBoolean); + } else { + assertEquals(entry.getValue(), reader.getValueAsBoolean()); + } break; case DATE: - assertEquals(entry.getValue(), reader.getValueAsLocalDate()); + if (assertExceptions) { + assertThrows(SerializationException.class, reader::getValueAsLocalDate); + } else { + assertEquals(entry.getValue(), reader.getValueAsLocalDate()); + } break; case TIME: - assertEquals(entry.getValue(), reader.getValueAsTime()); + if (assertExceptions) { + assertThrows(SerializationException.class, reader::getValueAsTime); + } else { + assertEquals(entry.getValue(), reader.getValueAsTime()); + } break; case DOUBLE: - assertEquals(entry.getValue(), reader.getValueAsDouble()); + if (assertExceptions) { + assertThrows(SerializationException.class, reader::getValueAsDouble); + } else { + assertEquals(entry.getValue(), reader.getValueAsDouble()); + } break; default: assertNull(entry.getValue() == null);