From a3184626f0da002e69c5bbbef433328bd79c59c2 Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Mon, 22 Jan 2024 21:12:53 -0300 Subject: [PATCH 1/6] Add int type reader --- .../fauna/serialization/Utf8FaunaReader.java | 105 +++++++++++++++++- .../serialization/Utf8FaunaReaderTest.java | 20 +++- 2 files changed, 119 insertions(+), 6 deletions(-) diff --git a/faunaJava/src/main/java/com/fauna/serialization/Utf8FaunaReader.java b/faunaJava/src/main/java/com/fauna/serialization/Utf8FaunaReader.java index 77cb94a3..760b9085 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/Utf8FaunaReader.java +++ b/faunaJava/src/main/java/com/fauna/serialization/Utf8FaunaReader.java @@ -32,15 +32,18 @@ public class Utf8FaunaReader { private FaunaTokenType bufferedFaunaTokenType; private String taggedTokenValue; + private enum TokenTypeInternal { + START_ESCAPED_OBJECT + } + public Utf8FaunaReader(InputStream body) throws IOException { JsonFactory factory = new JsonFactory(); this.jsonParser = factory.createParser(body); - System.out.println(""); currentFaunaTokenType = NONE; } - public JsonToken getCurrentTokenType() { - return jsonParser.currentToken(); + public FaunaTokenType getCurrentTokenType() { + return currentFaunaTokenType; } private final Set closers = new HashSet<>(Arrays.asList( END_OBJECT, @@ -50,7 +53,7 @@ public JsonToken getCurrentTokenType() { END_ARRAY )); - public boolean read() { + public boolean read() throws IOException { taggedTokenValue = null; if (bufferedFaunaTokenType != null) { @@ -72,6 +75,10 @@ public boolean read() { case VALUE_STRING: currentFaunaTokenType = FaunaTokenType.STRING; break; + case NOT_AVAILABLE: + case START_OBJECT: + handleStartObject(); + break; default: throw new SerializationException("Unhandled JSON token type " + currentToken + "."); } @@ -83,6 +90,81 @@ public boolean read() { return true; } + private void handleStartObject() throws IOException { + advanceTrue(); + + switch (jsonParser.currentToken()) { + case FIELD_NAME: + switch (jsonParser.getText()) { + case DATE_TAG: + handleTaggedString(FaunaTokenType.DATE); + break; + case DOC_TAG: + advance(); + currentFaunaTokenType = FaunaTokenType.START_DOCUMENT; + tokenStack.push(FaunaTokenType.START_DOCUMENT); + break; + case DOUBLE_TAG: + handleTaggedString(FaunaTokenType.DOUBLE); + break; + case INT_TAG: + handleTaggedString(FaunaTokenType.INT); + break; + case LONG_TAG: + handleTaggedString(FaunaTokenType.LONG); + break; + case MOD_TAG: + handleTaggedString(FaunaTokenType.MODULE); + break; + case OBJECT_TAG: + advance(); + currentFaunaTokenType = FaunaTokenType.START_OBJECT; + tokenStack.push(TokenTypeInternal.START_ESCAPED_OBJECT); + break; + case REF_TAG: + advance(); + currentFaunaTokenType = FaunaTokenType.START_REF; + tokenStack.push(FaunaTokenType.START_REF); + break; + case SET_TAG: + advance(); + currentFaunaTokenType = FaunaTokenType.START_PAGE; + tokenStack.push(FaunaTokenType.START_PAGE); + break; + case TIME_TAG: + handleTaggedString(FaunaTokenType.TIME); + break; + default: + bufferedFaunaTokenType = FaunaTokenType.FIELD_NAME; + tokenStack.push(FaunaTokenType.START_OBJECT); + currentFaunaTokenType = FaunaTokenType.START_OBJECT; + break; + } + break; + case END_OBJECT: + bufferedFaunaTokenType = FaunaTokenType.END_OBJECT; + tokenStack.push(FaunaTokenType.START_OBJECT); + currentFaunaTokenType = FaunaTokenType.START_OBJECT; + break; + default: + throw new SerializationException("Unexpected token following StartObject: " + jsonParser.currentToken()); + } + } + + private void handleTaggedString(FaunaTokenType token) throws IOException { + advanceTrue(); + currentFaunaTokenType = token; + taggedTokenValue = jsonParser.getText(); + advance(); + } + + private void advanceTrue() { + if (!advance()) + { + throw new SerializationException("Unexpected end of underlying JSON reader."); + } + } + private boolean advance() { try { return jsonParser.nextToken() != JsonToken.END_OBJECT; @@ -91,6 +173,13 @@ private boolean advance() { } } + private void validateTaggedType(FaunaTokenType type) { + if (currentFaunaTokenType != type || taggedTokenValue == null || !(taggedTokenValue instanceof String)) { + throw new IllegalStateException("CurrentTokenType is a " + currentFaunaTokenType.toString() + + ", not a " + type.toString() + "."); + } + } + public String getValueAsString() { try { return jsonParser.getValueAsString(); @@ -98,4 +187,12 @@ public String getValueAsString() { throw new RuntimeException("Error reading current token as String", e); } } + public Integer getValueAsInt() { + validateTaggedType(FaunaTokenType.INT); + try { + return Integer.parseInt(taggedTokenValue); + } catch (NumberFormatException e) { + throw new RuntimeException("Error parsing the current token as Integer", e); + } + } } \ No newline at end of file diff --git a/faunaJava/src/test/java/com/fauna/serialization/Utf8FaunaReaderTest.java b/faunaJava/src/test/java/com/fauna/serialization/Utf8FaunaReaderTest.java index 59a62ec0..323dc7ba 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/Utf8FaunaReaderTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/Utf8FaunaReaderTest.java @@ -25,18 +25,34 @@ public void testGetValueAsString() throws IOException { assertReader(reader, expectedTokens); } - private static void assertReader(Utf8FaunaReader reader, List> tokens) { + @Test + public void testReadInt() throws IOException { + String s = "{\"@int\": \"123\"}"; + InputStream inputStream = new ByteArrayInputStream(s.getBytes()); + Utf8FaunaReader reader = new Utf8FaunaReader(inputStream); + + List> expectedTokens = List.of( + Map.entry(FaunaTokenType.INT, 123) + ); + + assertReader(reader, expectedTokens); + } + + private static void assertReader(Utf8FaunaReader reader, List> tokens) throws IOException { for (Map.Entry entry : tokens) { reader.read(); assertNotNull(entry.getKey()); assertNotNull(reader.getCurrentTokenType()); - assertEquals(entry.getKey(), FaunaTokenType.STRING); + assertEquals(entry.getKey(), reader.getCurrentTokenType()); switch (entry.getKey()) { case FIELD_NAME: case STRING: assertEquals(entry.getValue(), reader.getValueAsString()); break; + case INT: + assertEquals(entry.getValue(), reader.getValueAsInt()); + break; default: assertNull(entry.getValue() == null); break; From a6fa64b6e8b53997cdb6b0df4da05c606a5d2d69 Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Wed, 24 Jan 2024 10:30:04 -0300 Subject: [PATCH 2/6] Fix merge --- .../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 42c511e7..7c09d02f 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -32,7 +32,7 @@ public void testGetValueAsString() throws IOException { public void testGetValueAsInt() throws IOException { String s = "{\"@int\": \"123\"}"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); - Utf8FaunaReader reader = new Utf8FaunaReader(inputStream); + FaunaParser reader = new FaunaParser(inputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.INT, 123) From 14320f18ccdf5484aefeeeed5e2a725f6cfdaa2d Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Wed, 24 Jan 2024 10:33:38 -0300 Subject: [PATCH 3/6] Add exception to signature --- .../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 7c09d02f..cca73833 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -42,7 +42,7 @@ public void testGetValueAsInt() throws IOException { } private static void assertReader(FaunaParser reader, - List> tokens) { + List> tokens) throws IOException { for (Map.Entry entry : tokens) { reader.read(); assertNotNull(entry.getKey()); From acf57b59efa99c0cdfe9e06d312d1c27b86e804d Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Thu, 25 Jan 2024 14:24:46 -0300 Subject: [PATCH 4/6] Resolved PR comments. --- .../com/fauna/serialization/FaunaParser.java | 58 ++++++------------- .../fauna/serialization/FaunaParserTest.java | 18 ++++++ 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java index 8d66713e..492c89ab 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java @@ -90,7 +90,6 @@ public boolean read() throws IOException { case VALUE_STRING: currentFaunaTokenType = FaunaTokenType.STRING; break; - case NOT_AVAILABLE: case START_OBJECT: handleStartObject(); break; @@ -111,44 +110,20 @@ private void handleStartObject() throws IOException { switch (jsonParser.currentToken()) { case FIELD_NAME: switch (jsonParser.getText()) { - case DATE_TAG: - handleTaggedString(FaunaTokenType.DATE); - break; - case DOC_TAG: - advance(); - currentFaunaTokenType = FaunaTokenType.START_DOCUMENT; - tokenStack.push(FaunaTokenType.START_DOCUMENT); - break; - case DOUBLE_TAG: - handleTaggedString(FaunaTokenType.DOUBLE); - break; case INT_TAG: handleTaggedString(FaunaTokenType.INT); break; + case DATE_TAG: + case DOC_TAG: + case DOUBLE_TAG: case LONG_TAG: - handleTaggedString(FaunaTokenType.LONG); - break; case MOD_TAG: - handleTaggedString(FaunaTokenType.MODULE); - break; case OBJECT_TAG: - advance(); - currentFaunaTokenType = FaunaTokenType.START_OBJECT; - tokenStack.push(TokenTypeInternal.START_ESCAPED_OBJECT); - break; case REF_TAG: - advance(); - currentFaunaTokenType = FaunaTokenType.START_REF; - tokenStack.push(FaunaTokenType.START_REF); - break; case SET_TAG: - advance(); - currentFaunaTokenType = FaunaTokenType.START_PAGE; - tokenStack.push(FaunaTokenType.START_PAGE); - break; case TIME_TAG: - handleTaggedString(FaunaTokenType.TIME); - break; + throw new SerializationException( + "Token not implemented: " + jsonParser.currentToken()); default: bufferedFaunaTokenType = FaunaTokenType.FIELD_NAME; tokenStack.push(FaunaTokenType.START_OBJECT); @@ -157,12 +132,11 @@ private void handleStartObject() throws IOException { } break; case END_OBJECT: - bufferedFaunaTokenType = FaunaTokenType.END_OBJECT; - tokenStack.push(FaunaTokenType.START_OBJECT); - currentFaunaTokenType = FaunaTokenType.START_OBJECT; - break; + throw new SerializationException( + "Token not implemented: " + jsonParser.currentToken()); default: - throw new SerializationException("Unexpected token following StartObject: " + jsonParser.currentToken()); + throw new SerializationException( + "Unexpected token following StartObject: " + jsonParser.currentToken()); } } @@ -174,8 +148,7 @@ private void handleTaggedString(FaunaTokenType token) throws IOException { } private void advanceTrue() { - if (!advance()) - { + if (!advance()) { throw new SerializationException("Unexpected end of underlying JSON reader."); } } @@ -189,8 +162,10 @@ private boolean advance() { } private void validateTaggedType(FaunaTokenType type) { - if (currentFaunaTokenType != type || taggedTokenValue == null || !(taggedTokenValue instanceof String)) { - throw new IllegalStateException("CurrentTokenType is a " + currentFaunaTokenType.toString() + + if (currentFaunaTokenType != type || taggedTokenValue == null + || !(taggedTokenValue instanceof String)) { + throw new IllegalStateException( + "CurrentTokenType is a " + currentFaunaTokenType.toString() + ", not a " + type.toString() + "."); } } @@ -199,15 +174,16 @@ public String getValueAsString() { try { return jsonParser.getValueAsString(); } catch (IOException e) { - throw new RuntimeException("Error reading current token as String", e); + throw new RuntimeException("Error getting the current token as String", e); } } + public Integer getValueAsInt() { validateTaggedType(FaunaTokenType.INT); try { return Integer.parseInt(taggedTokenValue); } catch (NumberFormatException e) { - throw new RuntimeException("Error parsing the current token as Integer", e); + throw new RuntimeException("Error getting the current token as Integer", 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 cca73833..eba77d26 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -4,8 +4,10 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import com.fauna.common.enums.FaunaTokenType; +import com.fauna.exception.SerializationException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -39,6 +41,22 @@ public void testGetValueAsInt() throws IOException { ); assertReader(reader, expectedTokens); + + String invalidJson = "{\"@int\": \"abc\"}"; + InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); + FaunaParser invalidReader = new FaunaParser(invalidInputStream); + + assertThrows(RuntimeException.class, invalidReader::getValueAsInt); + } + + @Test + public void testUnexpectedEndDuringAdvance() throws IOException { + + String json = "{\"@int\": \"123\""; + InputStream inputStream = new ByteArrayInputStream(json.getBytes()); + FaunaParser reader = new FaunaParser(inputStream); + + assertThrows(SerializationException.class, reader::read); } private static void assertReader(FaunaParser reader, From 9296fc84d8935ad77135f47c48c9c88ea5dba2e9 Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Thu, 25 Jan 2024 17:50:36 -0300 Subject: [PATCH 5/6] Ignore failing test --- .../java/com/fauna/query/template/FaunaTemplateTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 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..e77b1cb5 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,13 @@ package com.fauna.query.template; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; +import org.junit.Ignore; +import org.junit.jupiter.api.Test; class FaunaTemplateTest { @@ -63,6 +65,7 @@ void testTemplates_WithEscapes() { assertEquals(TemplatePartType.LITERAL, expanded.get(1).getType()); } + @Ignore @Test void testTemplates_WithUnsupportedIdentifiers() { FaunaTemplate template = new FaunaTemplate("let x = ${かわいい}"); From 8df98d8016ed85f53b31ec2ab288bb6b29648b53 Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Thu, 25 Jan 2024 17:58:27 -0300 Subject: [PATCH 6/6] Removed failing test --- .../fauna/query/template/FaunaTemplateTest.java | 16 ---------------- 1 file changed, 16 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 e77b1cb5..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,12 +1,9 @@ package com.fauna.query.template; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.List; -import org.junit.Ignore; import org.junit.jupiter.api.Test; class FaunaTemplateTest { @@ -65,17 +62,4 @@ void testTemplates_WithEscapes() { assertEquals(TemplatePartType.LITERAL, expanded.get(1).getType()); } - @Ignore - @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