From 7efe46d90f2e493307855aacd3e822b77c0b5732 Mon Sep 17 00:00:00 2001 From: pchitolina-fauna <156219095+pchitolina-fauna@users.noreply.github.com> Date: Mon, 29 Jan 2024 14:05:08 -0300 Subject: [PATCH 1/6] Update faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java Co-authored-by: Lucas Pedroza <40873230+pnwpedro@users.noreply.github.com> --- .../src/main/java/com/fauna/serialization/FaunaParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java index e4de6775..5254f962 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java @@ -197,7 +197,7 @@ public Boolean getValueAsBoolean() { try { return jsonParser.getValueAsBoolean(); } catch (IOException e) { - throw new RuntimeException("Error getting the current token as Boolean", e); + throw new SerializationException("Error getting the current token as Boolean", e); } } } \ No newline at end of file From 87694bed6d42147104116230857bc35118fe2bbf Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Wed, 31 Jan 2024 11:23:53 -0300 Subject: [PATCH 2/6] Improved test cases --- .../fauna/serialization/FaunaParserTest.java | 106 +++++++++--------- 1 file changed, 54 insertions(+), 52 deletions(-) diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java index ffa9c603..ba1568a6 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, false); + assertReader(reader, expectedTokens); } @Test @@ -42,17 +42,23 @@ public void testGetValueAsInt() throws IOException { Map.entry(FaunaTokenType.INT, 123) ); - assertReader(reader, expectedTokens, false); + assertReader(reader, expectedTokens); + } + @Test + public void testGetValueAsIntFail() throws IOException { String invalidJson = "{\"@int\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); FaunaParser invalidReader = new FaunaParser(invalidInputStream); - expectedTokens = List.of( + List> expectedTokens = List.of( Map.entry(FaunaTokenType.INT, "abc") ); - assertReader(invalidReader, expectedTokens, true); + Exception ex = assertThrows(SerializationException.class, + () -> assertReader(invalidReader, expectedTokens)); + + assertEquals("Error getting the current token as Integer", ex.getMessage()); } @Test @@ -62,7 +68,10 @@ public void testUnexpectedEndDuringAdvance() throws IOException { InputStream inputStream = new ByteArrayInputStream(json.getBytes()); FaunaParser reader = new FaunaParser(inputStream); - assertThrows(SerializationException.class, reader::read); + Exception ex = assertThrows(SerializationException.class, + () -> reader.read()); + + assertEquals("Failed to advance underlying JSON reader.", ex.getMessage()); } @Test @@ -74,7 +83,7 @@ public void testGetValueAsBooleanTrue() throws IOException { Map.entry(FaunaTokenType.TRUE, true) ); - assertReader(reader, expectedTokens, false); + assertReader(reader, expectedTokens); } @Test @@ -86,34 +95,40 @@ public void testGetValueAsBooleanFalse() throws IOException { Map.entry(FaunaTokenType.FALSE, false) ); - assertReader(reader, expectedTokens, false); + assertReader(reader, expectedTokens); } @Test - public void testeGetValueAsLocalDate() throws IOException { + public void testGetValueAsLocalDate() throws IOException { String s = "{\"@date\":\"2024-01-23\"}"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); FaunaParser reader = new FaunaParser(inputStream); List> expectedTokens = List.of( - Map.entry(FaunaTokenType.DATE, LocalDate.of(2024, 01, 23)) + Map.entry(FaunaTokenType.DATE, LocalDate.of(2024, 1, 23)) ); - assertReader(reader, expectedTokens, false); + assertReader(reader, expectedTokens); + } + @Test + public void testGetValueAsLocalDateFail() throws IOException { String invalidJson = "{\"@date\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); FaunaParser invalidReader = new FaunaParser(invalidInputStream); - expectedTokens = List.of( + List> expectedTokens = List.of( Map.entry(FaunaTokenType.DATE, "abc") ); - assertReader(invalidReader, expectedTokens, true); + Exception ex = assertThrows(SerializationException.class, + () -> assertReader(invalidReader, expectedTokens)); + + assertEquals("Error getting the current token as LocalDate", ex.getMessage()); } @Test - public void testeGetValueAsTime() throws IOException { + public void testGetValueAsTime() throws IOException { String s = "{\"@time\":\"2024-01-23T13:33:10.300Z\"}"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); FaunaParser reader = new FaunaParser(inputStream); @@ -124,17 +139,23 @@ public void testeGetValueAsTime() throws IOException { Map.entry(FaunaTokenType.TIME, instant) ); - assertReader(reader, expectedTokens, false); + assertReader(reader, expectedTokens); + } + @Test + public void testGetValueAsTimeFail() throws IOException { String invalidJson = "{\"@time\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); FaunaParser invalidReader = new FaunaParser(invalidInputStream); - expectedTokens = List.of( + List> expectedTokens = List.of( Map.entry(FaunaTokenType.TIME, "abc") ); - assertReader(invalidReader, expectedTokens, true); + Exception ex = assertThrows(SerializationException.class, + () -> assertReader(invalidReader, expectedTokens)); + + assertEquals("Error getting the current token as LocalDateTime", ex.getMessage()); } @Test @@ -149,7 +170,7 @@ public void testeGetValueAsTimeNonUTC() throws IOException { Map.entry(FaunaTokenType.TIME, instant) ); - assertReader(reader, expectedTokens, false); + assertReader(reader, expectedTokens); } @@ -163,22 +184,27 @@ public void testGetValueAsDouble() throws IOException { Map.entry(FaunaTokenType.DOUBLE, 1.23D) ); - assertReader(reader, expectedTokens, false); + assertReader(reader, expectedTokens); + } + @Test + public void testGetValueAsDoubleFail() throws IOException { String invalidJson = "{\"@double\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); FaunaParser invalidReader = new FaunaParser(invalidInputStream); - expectedTokens = List.of( + List> expectedTokens = List.of( Map.entry(FaunaTokenType.DOUBLE, "abc") ); - assertReader(invalidReader, expectedTokens, true); + Exception ex = assertThrows(SerializationException.class, + () -> assertReader(invalidReader, expectedTokens)); + + assertEquals("Error getting the current token as Double", ex.getMessage()); } private static void assertReader(FaunaParser reader, - List> tokens, - boolean assertExceptions) throws IOException { + List> tokens) throws IOException { for (Map.Entry entry : tokens) { reader.read(); assertNotNull(entry.getKey()); @@ -188,47 +214,23 @@ private static void assertReader(FaunaParser reader, switch (entry.getKey()) { case FIELD_NAME: case STRING: - if (assertExceptions) { - assertThrows(SerializationException.class, reader::getValueAsString); - } else { - assertEquals(entry.getValue(), reader.getValueAsString()); - } + assertEquals(entry.getValue(), reader.getValueAsString()); break; case INT: - if (assertExceptions) { - assertThrows(SerializationException.class, reader::getValueAsInt); - } else { - assertEquals(entry.getValue(), reader.getValueAsInt()); - } + assertEquals(entry.getValue(), reader.getValueAsInt()); break; case TRUE: case FALSE: - if (assertExceptions) { - assertThrows(SerializationException.class, reader::getValueAsBoolean); - } else { - assertEquals(entry.getValue(), reader.getValueAsBoolean()); - } + assertEquals(entry.getValue(), reader.getValueAsBoolean()); break; case DATE: - if (assertExceptions) { - assertThrows(SerializationException.class, reader::getValueAsLocalDate); - } else { - assertEquals(entry.getValue(), reader.getValueAsLocalDate()); - } + assertEquals(entry.getValue(), reader.getValueAsLocalDate()); break; case TIME: - if (assertExceptions) { - assertThrows(SerializationException.class, reader::getValueAsTime); - } else { - assertEquals(entry.getValue(), reader.getValueAsTime()); - } + assertEquals(entry.getValue(), reader.getValueAsTime()); break; case DOUBLE: - if (assertExceptions) { - assertThrows(SerializationException.class, reader::getValueAsDouble); - } else { - assertEquals(entry.getValue(), reader.getValueAsDouble()); - } + assertEquals(entry.getValue(), reader.getValueAsDouble()); break; default: assertNull(entry.getValue() == null); From 8169db068e45d30306ab9b79a4d8f1c6bed5368f Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Wed, 31 Jan 2024 11:34:54 -0300 Subject: [PATCH 3/6] Improved test cases --- .../fauna/serialization/FaunaParserTest.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java index 7500664a..8030cb9b 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -213,17 +213,23 @@ public void testGetValueAsLong() throws IOException { Map.entry(FaunaTokenType.LONG, 123L) ); - assertReader(reader, expectedTokens, false); + assertReader(reader, expectedTokens); + } + @Test + public void testGetValueAsLongFail() throws IOException { String invalidJson = "{\"@long\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); FaunaParser invalidReader = new FaunaParser(invalidInputStream); - expectedTokens = List.of( + List> expectedTokens = List.of( Map.entry(FaunaTokenType.LONG, "abc") ); - assertReader(invalidReader, expectedTokens, true); + Exception ex = assertThrows(SerializationException.class, + () -> assertReader(invalidReader, expectedTokens)); + + assertEquals("Error getting the current token as Long", ex.getMessage()); } private static void assertReader(FaunaParser reader, @@ -256,11 +262,7 @@ private static void assertReader(FaunaParser reader, assertEquals(entry.getValue(), reader.getValueAsDouble()); break; case LONG: - if (assertExceptions) { - assertThrows(SerializationException.class, reader::getValueAsLong); - } else { - assertEquals(entry.getValue(), reader.getValueAsLong()); - } + assertEquals(entry.getValue(), reader.getValueAsLong()); break; default: assertNull(entry.getValue() == null); From 2fa18219805922b5d8d808b9c1afd2fa4c2b9a6d Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Wed, 31 Jan 2024 11:35:46 -0300 Subject: [PATCH 4/6] Improved test cases --- .../src/test/java/com/fauna/serialization/FaunaParserTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java index 19c3d2ca..b786f75c 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -243,7 +243,7 @@ public void testGetValueAsModule() throws IOException { Map.entry(FaunaTokenType.MODULE, new Module("MyModule")) ); - assertReader(reader, expectedTokens, false); + assertReader(reader, expectedTokens); } private static void assertReader(FaunaParser reader, From 6dd3f9ed2f85514997877259048ba72742274901 Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Wed, 31 Jan 2024 11:36:37 -0300 Subject: [PATCH 5/6] Improved test cases --- .../src/test/java/com/fauna/serialization/FaunaParserTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java index 21e8e312..9a25885e 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -260,7 +260,7 @@ public void readArrayWithEmptyObject() throws IOException { new AbstractMap.SimpleEntry<>(FaunaTokenType.END_ARRAY, null) ); - assertReader(reader, expectedTokens, false); + assertReader(reader, expectedTokens); } private static void assertReader(FaunaParser reader, From fc51a22e2c478de582c8baf7b8301def62e1642c Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Wed, 31 Jan 2024 11:37:27 -0300 Subject: [PATCH 6/6] Improved test cases --- .../src/test/java/com/fauna/serialization/FaunaParserTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java index 83020417..d60c9a0d 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -291,7 +291,7 @@ public void testReadEscapedObject() throws IOException { new AbstractMap.SimpleEntry<>(FaunaTokenType.END_OBJECT, null) ); - assertReader(reader, expectedTokens, false); + assertReader(reader, expectedTokens); } private static void assertReader(FaunaParser reader,