Skip to content

Commit

Permalink
Merge pull request #27 from fauna/feature/BT-4418-faunaparser_increas…
Browse files Browse the repository at this point in the history
…e_coverage

[BT-4418] FaunaParserTest increase coverage
  • Loading branch information
pchitolina-fauna authored Jan 31, 2024
2 parents c4369a9 + 10ab034 commit 4e78324
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class FaunaParser {
private static final String DOC_TAG = "@doc";
private static final String MOD_TAG = "@mod";
private static final String SET_TAG = "@set";
private static final String OBJECT_TAG = "@object";//TODO Understand Module
private static final String OBJECT_TAG = "@object";
private final JsonParser jsonParser;
private final Stack<Object> tokenStack = new Stack<>();
private FaunaTokenType currentFaunaTokenType;
Expand Down Expand Up @@ -75,7 +75,7 @@ public FaunaTokenType getCurrentTokenType() {
END_REF,
END_ARRAY
));

public boolean read() throws IOException {
taggedTokenValue = null;

Expand Down Expand Up @@ -121,6 +121,9 @@ public boolean read() throws IOException {
case FIELD_NAME:
currentFaunaTokenType = FaunaTokenType.FIELD_NAME;
break;
case VALUE_NULL:
currentFaunaTokenType = FaunaTokenType.NULL;
break;
default:
throw new SerializationException(
"Unhandled JSON token type " + currentToken + ".");
Expand Down
145 changes: 145 additions & 0 deletions faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,151 @@ public void testReadRef() throws IOException {
assertReader(reader, expectedTokens);
}

@Test
public void testReadObjectTokens() throws IOException {
String s = "{\n" +
" \"aString\": \"foo\",\n" +
" \"anObject\": { \"baz\": \"luhrmann\" },\n" +
" \"anInt\": { \"@int\": \"2147483647\" },\n" +
" \"aLong\":{ \"@long\": \"9223372036854775807\" },\n" +
" \"aDouble\":{ \"@double\": \"3.14159\" },\n" +
" \"aDecimal\":{ \"@double\": \"0.1\" },\n" +
" \"aDate\":{ \"@date\": \"2023-12-03\" },\n" +
" \"aTime\":{ \"@time\": \"2023-12-03T14:52:10.001001Z\" },\n" +
" \"anEscapedObject\": { \"@object\": { \"@int\": \"escaped\" } },\n" +
" \"anArray\": [],\n" +
" \"true\": true,\n" +
" \"false\": false,\n" +
" \"null\": null\n" +
"}";
FaunaParser reader = new FaunaParser(new ByteArrayInputStream(s.getBytes()));

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
new AbstractMap.SimpleEntry<>(FaunaTokenType.START_OBJECT, null),

Map.entry(FaunaTokenType.FIELD_NAME, "aString"),
Map.entry(FaunaTokenType.STRING, "foo"),

Map.entry(FaunaTokenType.FIELD_NAME, "anObject"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.START_OBJECT, null),
Map.entry(FaunaTokenType.FIELD_NAME, "baz"),
Map.entry(FaunaTokenType.STRING, "luhrmann"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_OBJECT, null),

Map.entry(FaunaTokenType.FIELD_NAME, "anInt"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.INT, 2147483647),

Map.entry(FaunaTokenType.FIELD_NAME, "aLong"),
Map.entry(FaunaTokenType.LONG, 9223372036854775807L),

Map.entry(FaunaTokenType.FIELD_NAME, "aDouble"),
Map.entry(FaunaTokenType.DOUBLE, 3.14159d),

Map.entry(FaunaTokenType.FIELD_NAME, "aDecimal"),
Map.entry(FaunaTokenType.DOUBLE, 0.1d),

Map.entry(FaunaTokenType.FIELD_NAME, "aDate"),
Map.entry(FaunaTokenType.DATE, LocalDate.of(2023, 12, 3)),

Map.entry(FaunaTokenType.FIELD_NAME, "aTime"),
Map.entry(FaunaTokenType.TIME, Instant.parse("2023-12-03T14:52:10.001001Z")),

Map.entry(FaunaTokenType.FIELD_NAME, "anEscapedObject"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.START_OBJECT, null),
Map.entry(FaunaTokenType.FIELD_NAME, "@int"),
Map.entry(FaunaTokenType.STRING, "escaped"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_OBJECT, null),

Map.entry(FaunaTokenType.FIELD_NAME, "anArray"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.START_ARRAY, null),
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_ARRAY, null),

Map.entry(FaunaTokenType.FIELD_NAME, "true"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.TRUE, true),

Map.entry(FaunaTokenType.FIELD_NAME, "false"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.FALSE, false),

Map.entry(FaunaTokenType.FIELD_NAME, "null"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.NULL, null),

new AbstractMap.SimpleEntry<>(FaunaTokenType.END_OBJECT, null)
);

assertReader(reader, expectedTokens);
}

@Test
public void testReadArray() throws IOException {
String s = "[\n" +
" \"foo\",\n" +
" { \"baz\": \"luhrmann\" },\n" +
" { \"@int\": \"2147483647\" },\n" +
" { \"@long\": \"9223372036854775807\" },\n" +
" { \"@double\": \"3.14159\" },\n" +
" { \"@double\": \"0.1\" },\n" +
" { \"@date\": \"2023-12-03\" },\n" +
" { \"@time\": \"2023-12-03T14:52:10.001001Z\" },\n" +
" { \"@object\": { \"@int\": \"escaped\" } },\n" +
" [],\n" +
" true,\n" +
" false,\n" +
" null\n" +
"]";
FaunaParser reader = new FaunaParser(new ByteArrayInputStream(s.getBytes()));

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
new AbstractMap.SimpleEntry<>(FaunaTokenType.START_ARRAY, null),

Map.entry(FaunaTokenType.STRING, "foo"),

new AbstractMap.SimpleEntry<>(FaunaTokenType.START_OBJECT, null),
Map.entry(FaunaTokenType.FIELD_NAME, "baz"),
Map.entry(FaunaTokenType.STRING, "luhrmann"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_OBJECT, null),

Map.entry(FaunaTokenType.INT, 2147483647),

Map.entry(FaunaTokenType.LONG, 9223372036854775807L),

Map.entry(FaunaTokenType.DOUBLE, 3.14159d),

Map.entry(FaunaTokenType.DOUBLE, 0.1d),

Map.entry(FaunaTokenType.DATE, LocalDate.of(2023, 12, 3)),

Map.entry(FaunaTokenType.TIME, Instant.parse("2023-12-03T14:52:10.001001Z")),

new AbstractMap.SimpleEntry<>(FaunaTokenType.START_OBJECT, null),
Map.entry(FaunaTokenType.FIELD_NAME, "@int"),
Map.entry(FaunaTokenType.STRING, "escaped"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_OBJECT, null),

new AbstractMap.SimpleEntry<>(FaunaTokenType.START_ARRAY, null),
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_ARRAY, null),

Map.entry(FaunaTokenType.TRUE, true),

Map.entry(FaunaTokenType.FALSE, false),

new AbstractMap.SimpleEntry<>(FaunaTokenType.NULL, null),

new AbstractMap.SimpleEntry<>(FaunaTokenType.END_ARRAY, null)
);

assertReader(reader, expectedTokens);
}

@Test
public void throwsOnMalformedJson() {
String s = "{";
assertThrows(SerializationException.class, () -> {
FaunaParser reader = new FaunaParser(new ByteArrayInputStream(s.getBytes()));
reader.read();
reader.read();
}, "Failed to advance underlying JSON reader.");
}

private static void assertReader(FaunaParser reader,
List<Map.Entry<FaunaTokenType, Object>> tokens) throws IOException {
for (Map.Entry<FaunaTokenType, Object> entry : tokens) {
Expand Down

0 comments on commit 4e78324

Please sign in to comment.