Skip to content

Commit

Permalink
Merge branch 'feature/BT-4418-utf8faunareader_read_long' into feature/B…
Browse files Browse the repository at this point in the history
  • Loading branch information
pchitolina-fauna committed Jan 31, 2024
2 parents ee54641 + 8169db0 commit ea7b13b
Showing 1 changed file with 64 additions and 60 deletions.
124 changes: 64 additions & 60 deletions faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void testGetValueAsString() throws IOException {
Map.entry(FaunaTokenType.STRING, "hello")
);

assertReader(reader, expectedTokens, false);
assertReader(reader, expectedTokens);
}

@Test
Expand All @@ -43,17 +43,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<Map.Entry<FaunaTokenType, Object>> 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
Expand All @@ -63,7 +69,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
Expand All @@ -75,7 +84,7 @@ public void testGetValueAsBooleanTrue() throws IOException {
Map.entry(FaunaTokenType.TRUE, true)
);

assertReader(reader, expectedTokens, false);
assertReader(reader, expectedTokens);
}

@Test
Expand All @@ -87,34 +96,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<Map.Entry<FaunaTokenType, Object>> 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<Map.Entry<FaunaTokenType, Object>> 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);
Expand All @@ -125,17 +140,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<Map.Entry<FaunaTokenType, Object>> 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
Expand All @@ -150,7 +171,7 @@ public void testeGetValueAsTimeNonUTC() throws IOException {
Map.entry(FaunaTokenType.TIME, instant)
);

assertReader(reader, expectedTokens, false);
assertReader(reader, expectedTokens);

}

Expand All @@ -164,17 +185,23 @@ 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<Map.Entry<FaunaTokenType, Object>> 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());
}

@Test
Expand All @@ -187,17 +214,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<Map.Entry<FaunaTokenType, Object>> 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());
}

@Test
Expand All @@ -214,8 +247,7 @@ public void testGetValueAsModule() throws IOException {
}

private static void assertReader(FaunaParser reader,
List<Map.Entry<FaunaTokenType, Object>> tokens,
boolean assertExceptions) throws IOException {
List<Map.Entry<FaunaTokenType, Object>> tokens) throws IOException {
for (Map.Entry<FaunaTokenType, Object> entry : tokens) {
reader.read();
assertNotNull(entry.getKey());
Expand All @@ -225,54 +257,26 @@ 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;
case LONG:
if (assertExceptions) {
assertThrows(SerializationException.class, reader::getValueAsLong);
} else {
assertEquals(entry.getValue(), reader.getValueAsLong());
}
assertEquals(entry.getValue(), reader.getValueAsLong());
break;
case MODULE:
assertEquals(entry.getValue(), reader.getValueAsModule());
Expand Down

0 comments on commit ea7b13b

Please sign in to comment.