Skip to content

Commit

Permalink
Fixed exception classes and improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pchitolina-fauna committed Jan 31, 2024
1 parent 2788bd3 commit 28e6dfc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.InputStream;
import java.time.Instant;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
Expand Down Expand Up @@ -216,7 +217,7 @@ public LocalDate getValueAsLocalDate() {
validateTaggedType(DATE);
try {
return LocalDate.parse(taggedTokenValue);
} catch (NumberFormatException e) {
} catch (DateTimeParseException e) {
throw new SerializationException("Error getting the current token as LocalDate", e);
}
}
Expand All @@ -225,7 +226,7 @@ public Instant getValueAsTime() {
validateTaggedType(TIME);
try {
return Instant.parse(taggedTokenValue);
} catch (NumberFormatException e) {
} catch (DateTimeParseException e) {
throw new SerializationException("Error getting the current token as LocalDateTime", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void testGetValueAsString() throws IOException {
Map.entry(FaunaTokenType.STRING, "hello")
);

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

@Test
Expand All @@ -42,13 +42,17 @@ public void testGetValueAsInt() throws IOException {
Map.entry(FaunaTokenType.INT, 123)
);

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

String invalidJson = "{\"@int\": \"abc\"}";
InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes());
FaunaParser invalidReader = new FaunaParser(invalidInputStream);

assertThrows(RuntimeException.class, invalidReader::getValueAsInt);
expectedTokens = List.of(
Map.entry(FaunaTokenType.INT, "abc")
);

assertReader(invalidReader, expectedTokens, true);
}

@Test
Expand All @@ -70,7 +74,7 @@ public void testGetValueAsBooleanTrue() throws IOException {
Map.entry(FaunaTokenType.TRUE, true)
);

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

@Test
Expand All @@ -82,7 +86,7 @@ public void testGetValueAsBooleanFalse() throws IOException {
Map.entry(FaunaTokenType.FALSE, false)
);

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

@Test
Expand All @@ -95,13 +99,17 @@ public void testeGetValueAsLocalDate() throws IOException {
Map.entry(FaunaTokenType.DATE, LocalDate.of(2024, 01, 23))
);

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

String invalidJson = "{\"@date\": \"abc\"}";
InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes());
FaunaParser invalidReader = new FaunaParser(invalidInputStream);

assertThrows(RuntimeException.class, invalidReader::getValueAsLocalDate);
expectedTokens = List.of(
Map.entry(FaunaTokenType.DATE, "abc")
);

assertReader(invalidReader, expectedTokens, true);
}

@Test
Expand All @@ -116,13 +124,17 @@ public void testeGetValueAsTime() throws IOException {
Map.entry(FaunaTokenType.TIME, instant)
);

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

String invalidJson = "{\"@time\": \"abc\"}";
InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes());
FaunaParser invalidReader = new FaunaParser(invalidInputStream);

assertThrows(RuntimeException.class, invalidReader::getValueAsLocalDate);
expectedTokens = List.of(
Map.entry(FaunaTokenType.TIME, "abc")
);

assertReader(invalidReader, expectedTokens, true);
}

@Test
Expand All @@ -137,7 +149,7 @@ public void testeGetValueAsTimeNonUTC() throws IOException {
Map.entry(FaunaTokenType.TIME, instant)
);

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

}

Expand All @@ -151,17 +163,22 @@ public void testGetValueAsDouble() throws IOException {
Map.entry(FaunaTokenType.DOUBLE, 1.23D)
);

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

String invalidJson = "{\"@double\": \"abc\"}";
InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes());
FaunaParser invalidReader = new FaunaParser(invalidInputStream);

assertThrows(SerializationException.class, invalidReader::getValueAsDouble);
expectedTokens = List.of(
Map.entry(FaunaTokenType.DOUBLE, "abc")
);

assertReader(invalidReader, expectedTokens, true);
}

private static void assertReader(FaunaParser reader,
List<Map.Entry<FaunaTokenType, Object>> tokens) throws IOException {
List<Map.Entry<FaunaTokenType, Object>> tokens,
boolean assertExceptions) throws IOException {
for (Map.Entry<FaunaTokenType, Object> entry : tokens) {
reader.read();
assertNotNull(entry.getKey());
Expand All @@ -171,23 +188,47 @@ private static void assertReader(FaunaParser reader,
switch (entry.getKey()) {
case FIELD_NAME:
case STRING:
assertEquals(entry.getValue(), reader.getValueAsString());
if (assertExceptions) {
assertThrows(SerializationException.class, reader::getValueAsString);
} else {
assertEquals(entry.getValue(), reader.getValueAsString());
}
break;
case INT:
assertEquals(entry.getValue(), reader.getValueAsInt());
if (assertExceptions) {
assertThrows(SerializationException.class, reader::getValueAsInt);
} else {
assertEquals(entry.getValue(), reader.getValueAsInt());
}
break;
case TRUE:
case FALSE:
assertEquals(entry.getValue(), reader.getValueAsBoolean());
if (assertExceptions) {
assertThrows(SerializationException.class, reader::getValueAsBoolean);
} else {
assertEquals(entry.getValue(), reader.getValueAsBoolean());
}
break;
case DATE:
assertEquals(entry.getValue(), reader.getValueAsLocalDate());
if (assertExceptions) {
assertThrows(SerializationException.class, reader::getValueAsLocalDate);
} else {
assertEquals(entry.getValue(), reader.getValueAsLocalDate());
}
break;
case TIME:
assertEquals(entry.getValue(), reader.getValueAsTime());
if (assertExceptions) {
assertThrows(SerializationException.class, reader::getValueAsTime);
} else {
assertEquals(entry.getValue(), reader.getValueAsTime());
}
break;
case DOUBLE:
assertEquals(entry.getValue(), reader.getValueAsDouble());
if (assertExceptions) {
assertThrows(SerializationException.class, reader::getValueAsDouble);
} else {
assertEquals(entry.getValue(), reader.getValueAsDouble());
}
break;
default:
assertNull(entry.getValue() == null);
Expand Down

0 comments on commit 28e6dfc

Please sign in to comment.