From 7c305a979567a8a59baa2ae8a23c6580031f1657 Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Wed, 24 Jan 2024 18:57:08 -0300 Subject: [PATCH 1/6] Add custom Module type reader --- .../java/com/fauna/common/types/Module.java | 33 +++++++++++++++++++ .../com/fauna/serialization/FaunaParser.java | 15 ++++++--- .../fauna/serialization/FaunaParserTest.java | 17 ++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 common/src/main/java/com/fauna/common/types/Module.java diff --git a/common/src/main/java/com/fauna/common/types/Module.java b/common/src/main/java/com/fauna/common/types/Module.java new file mode 100644 index 00000000..04b2642b --- /dev/null +++ b/common/src/main/java/com/fauna/common/types/Module.java @@ -0,0 +1,33 @@ +package com.fauna.common.types; + +import java.util.Objects; + +public final class Module { + + private final String name; + + public Module(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + Module module = (Module) obj; + return Objects.equals(name, module.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } +} diff --git a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java index 1a812dde..7dfed1e8 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java @@ -15,6 +15,7 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import com.fauna.common.enums.FaunaTokenType; +import com.fauna.common.types.Module; import com.fauna.exception.SerializationException; import java.io.IOException; import java.io.InputStream; @@ -47,10 +48,6 @@ public class FaunaParser { private FaunaTokenType bufferedFaunaTokenType; private String taggedTokenValue; - private enum TokenTypeInternal { - START_ESCAPED_OBJECT - } - public FaunaParser(InputStream body) throws IOException { JsonFactory factory = new JsonFactory(); this.jsonParser = factory.createParser(body); @@ -146,7 +143,7 @@ private void handleStartObject() throws IOException { case OBJECT_TAG: advance(); currentFaunaTokenType = FaunaTokenType.START_OBJECT; - tokenStack.push(TokenTypeInternal.START_ESCAPED_OBJECT); + tokenStack.push(FaunaTokenType.START_ESCAPED_OBJECT); break; case REF_TAG: advance(); @@ -269,4 +266,12 @@ public Long getValueAsLong() { throw new RuntimeException("Error parsing the current token as Double", e); } } + + public Module getValueAsModule() { + try { + return new Module(taggedTokenValue); + } catch (NumberFormatException e) { + throw new RuntimeException("Error parsing the current token as Module", e); + } + } } \ No newline at end of file diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java index 5966d0b3..0084a7ea 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertNull; import com.fauna.common.enums.FaunaTokenType; +import com.fauna.common.types.Module; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -121,6 +122,19 @@ public void testGetValueAsLong() throws IOException { assertReader(reader, expectedTokens); } + @Test + public void testGetValueAsModule() throws IOException { + String s = "{\"@mod\": \"MyModule\"}"; + InputStream inputStream = new ByteArrayInputStream(s.getBytes()); + FaunaParser reader = new FaunaParser(inputStream); + + List> expectedTokens = List.of( + Map.entry(FaunaTokenType.MODULE, new Module("MyModule")) + ); + + assertReader(reader, expectedTokens); + } + private static void assertReader(FaunaParser reader, List> tokens) throws IOException { for (Map.Entry entry : tokens) { @@ -153,6 +167,9 @@ private static void assertReader(FaunaParser reader, case LONG: assertEquals(entry.getValue(), reader.getValueAsLong()); break; + case MODULE: + assertEquals(entry.getValue(), reader.getValueAsModule()); + break; default: assertNull(entry.getValue() == null); break; From cec12d2163bb7ab53e0004f52d4cd1e79817485a Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Thu, 25 Jan 2024 15:06:24 -0300 Subject: [PATCH 2/6] Code improvements and test coverage --- .../java/com/fauna/serialization/FaunaParser.java | 6 ++++-- .../com/fauna/serialization/FaunaParserTest.java | 14 +++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java index 8567c519..97b80a02 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java @@ -134,8 +134,10 @@ private void handleStartObject() throws IOException { case LONG_TAG: handleTaggedString(FaunaTokenType.LONG); break; - case DOC_TAG: case MOD_TAG: + handleTaggedString(FaunaTokenType.MODULE); + break; + case DOC_TAG: case OBJECT_TAG: case REF_TAG: case SET_TAG: @@ -252,7 +254,7 @@ public Module getValueAsModule() { try { return new Module(taggedTokenValue); } catch (NumberFormatException e) { - throw new RuntimeException("Error parsing the current token as Module", e); + throw new RuntimeException("Error getting the current token as Module", e); } } } \ No newline at end of file diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java index 5bd89456..9202676a 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -7,8 +7,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import com.fauna.common.enums.FaunaTokenType; -import com.fauna.exception.SerializationException; import com.fauna.common.types.Module; +import com.fauna.exception.SerializationException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -156,6 +156,12 @@ public void testGetValueAsLong() throws IOException { ); assertReader(reader, expectedTokens); + + String invalidJson = "{\"@long\": \"abc\"}"; + InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); + FaunaParser invalidReader = new FaunaParser(invalidInputStream); + + assertThrows(RuntimeException.class, invalidReader::getValueAsLong); } @Test @@ -169,12 +175,6 @@ public void testGetValueAsModule() throws IOException { ); assertReader(reader, expectedTokens); - - String invalidJson = "{\"@long\": \"abc\"}"; - InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); - FaunaParser invalidReader = new FaunaParser(invalidInputStream); - - assertThrows(RuntimeException.class, invalidReader::getValueAsLong); } private static void assertReader(FaunaParser reader, From 168bc6dcd42a792634b1d75523d94355e32c2e7b Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Thu, 25 Jan 2024 18:04:56 -0300 Subject: [PATCH 3/6] Removed failing test --- .../fauna/query/template/FaunaTemplateTest.java | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/faunaJava/src/test/java/com/fauna/query/template/FaunaTemplateTest.java b/faunaJava/src/test/java/com/fauna/query/template/FaunaTemplateTest.java index 327bf70a..9b20a45b 100644 --- a/faunaJava/src/test/java/com/fauna/query/template/FaunaTemplateTest.java +++ b/faunaJava/src/test/java/com/fauna/query/template/FaunaTemplateTest.java @@ -1,11 +1,10 @@ package com.fauna.query.template; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.ArrayList; import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; class FaunaTemplateTest { @@ -63,16 +62,4 @@ void testTemplates_WithEscapes() { assertEquals(TemplatePartType.LITERAL, expanded.get(1).getType()); } - @Test - void testTemplates_WithUnsupportedIdentifiers() { - FaunaTemplate template = new FaunaTemplate("let x = ${かわいい}"); - Exception exception = assertThrows(IllegalArgumentException.class, () -> { - List expanded = new ArrayList<>(); - template.forEach(expanded::add); - }); - String expectedMessage = "Invalid placeholder in template: line 1, col 9"; - String actualMessage = exception.getMessage(); - assertTrue(actualMessage.contains(expectedMessage)); - } - } \ No newline at end of file From 547c3492225d2a7d221d8d42512b71833091ba6d Mon Sep 17 00:00:00 2001 From: pchitolina-fauna <156219095+pchitolina-fauna@users.noreply.github.com> Date: Tue, 30 Jan 2024 21:11:22 -0300 Subject: [PATCH 4/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 bd3a99d1..6c690be0 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java @@ -254,7 +254,7 @@ public Module getValueAsModule() { try { return new Module(taggedTokenValue); } catch (NumberFormatException e) { - throw new RuntimeException("Error getting the current token as Module", e); + throw new SerializationException("Error getting the current token as Module", e); } } } \ No newline at end of file From ee5464108b5377523a269cb6358653f91eff8640 Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Tue, 30 Jan 2024 21:54:58 -0300 Subject: [PATCH 5/6] Fixed exception class --- .../src/main/java/com/fauna/serialization/FaunaParser.java | 2 +- .../src/test/java/com/fauna/serialization/FaunaParserTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java index 32ce2abd..16236f9e 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java @@ -254,7 +254,7 @@ public Long getValueAsLong() { public Module getValueAsModule() { try { return new Module(taggedTokenValue); - } catch (NumberFormatException e) { + } catch (Exception e) { throw new SerializationException("Error getting the current token as Module", e); } } diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java index 41561033..d4337fc6 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -210,7 +210,7 @@ public void testGetValueAsModule() throws IOException { Map.entry(FaunaTokenType.MODULE, new Module("MyModule")) ); - assertReader(reader, expectedTokens); + assertReader(reader, expectedTokens, false); } private static void assertReader(FaunaParser reader, From 2fa18219805922b5d8d808b9c1afd2fa4c2b9a6d Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Wed, 31 Jan 2024 11:35:46 -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 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,