diff --git a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java index 5254f962..e47cee38 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java @@ -1,11 +1,13 @@ package com.fauna.serialization; +import static com.fauna.common.enums.FaunaTokenType.DATE; import static com.fauna.common.enums.FaunaTokenType.END_ARRAY; import static com.fauna.common.enums.FaunaTokenType.END_DOCUMENT; import static com.fauna.common.enums.FaunaTokenType.END_OBJECT; import static com.fauna.common.enums.FaunaTokenType.END_PAGE; import static com.fauna.common.enums.FaunaTokenType.END_REF; import static com.fauna.common.enums.FaunaTokenType.NONE; +import static com.fauna.common.enums.FaunaTokenType.TIME; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; @@ -14,6 +16,8 @@ import com.fauna.exception.SerializationException; import java.io.IOException; import java.io.InputStream; +import java.time.Instant; +import java.time.LocalDate; import java.util.Arrays; import java.util.HashSet; import java.util.Objects; @@ -120,6 +124,11 @@ private void handleStartObject() throws IOException { handleTaggedString(FaunaTokenType.INT); break; case DATE_TAG: + handleTaggedString(FaunaTokenType.DATE); + break; + case TIME_TAG: + handleTaggedString(FaunaTokenType.TIME); + break; case DOC_TAG: case DOUBLE_TAG: case LONG_TAG: @@ -127,7 +136,6 @@ private void handleStartObject() throws IOException { case OBJECT_TAG: case REF_TAG: case SET_TAG: - case TIME_TAG: throw new SerializationException( "Token not implemented: " + jsonParser.currentToken()); default: @@ -180,7 +188,7 @@ public String getValueAsString() { try { return jsonParser.getValueAsString(); } catch (IOException e) { - throw new RuntimeException("Error getting the current token as String", e); + throw new SerializationException("Error getting the current token as String", e); } } @@ -189,7 +197,7 @@ public Integer getValueAsInt() { try { return Integer.parseInt(taggedTokenValue); } catch (NumberFormatException e) { - throw new RuntimeException("Error getting the current token as Integer", e); + throw new SerializationException("Error getting the current token as Integer", e); } } @@ -200,4 +208,22 @@ public Boolean getValueAsBoolean() { throw new SerializationException("Error getting the current token as Boolean", e); } } + + public LocalDate getValueAsLocalDate() { + validateTaggedType(DATE); + try { + return LocalDate.parse(taggedTokenValue); + } catch (NumberFormatException e) { + throw new SerializationException("Error getting the current token as LocalDate", e); + } + } + + public Instant getValueAsTime() { + validateTaggedType(TIME); + try { + return Instant.parse(taggedTokenValue); + } catch (NumberFormatException e) { + throw new SerializationException("Error getting the current token as LocalDateTime", 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 7d1e896f..2863daa2 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -11,6 +11,8 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.time.Instant; +import java.time.LocalDate; import java.util.List; import java.util.Map; import org.junit.jupiter.api.Test; @@ -83,6 +85,62 @@ public void testGetValueAsBooleanFalse() throws IOException { assertReader(reader, expectedTokens); } + @Test + public void testeGetValueAsLocalDate() throws IOException { + String s = "{\"@date\":\"2024-01-23\"}"; + InputStream inputStream = new ByteArrayInputStream(s.getBytes()); + FaunaParser reader = new FaunaParser(inputStream); + + List> expectedTokens = List.of( + Map.entry(FaunaTokenType.DATE, LocalDate.of(2024, 01, 23)) + ); + + assertReader(reader, expectedTokens); + + String invalidJson = "{\"@date\": \"abc\"}"; + InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); + FaunaParser invalidReader = new FaunaParser(invalidInputStream); + + assertThrows(RuntimeException.class, invalidReader::getValueAsLocalDate); + } + + @Test + public void testeGetValueAsTime() throws IOException { + String s = "{\"@time\":\"2024-01-23T13:33:10.300Z\"}"; + InputStream inputStream = new ByteArrayInputStream(s.getBytes()); + FaunaParser reader = new FaunaParser(inputStream); + + Instant instant = Instant.parse("2024-01-23T13:33:10.300Z"); + + List> expectedTokens = List.of( + Map.entry(FaunaTokenType.TIME, instant) + ); + + assertReader(reader, expectedTokens); + + String invalidJson = "{\"@time\": \"abc\"}"; + InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); + FaunaParser invalidReader = new FaunaParser(invalidInputStream); + + assertThrows(RuntimeException.class, invalidReader::getValueAsLocalDate); + } + + @Test + public void testeGetValueAsTimeNonUTC() throws IOException { + String s = "{\"@time\":\"2023-12-03T05:52:10.000001-09:00\"}"; + InputStream inputStream = new ByteArrayInputStream(s.getBytes()); + FaunaParser reader = new FaunaParser(inputStream); + + Instant instant = Instant.parse("2023-12-03T05:52:10.000001-09:00"); + + List> expectedTokens = List.of( + Map.entry(FaunaTokenType.TIME, instant) + ); + + assertReader(reader, expectedTokens); + + } + private static void assertReader(FaunaParser reader, List> tokens) throws IOException { for (Map.Entry entry : tokens) { @@ -103,6 +161,12 @@ private static void assertReader(FaunaParser reader, case FALSE: assertEquals(entry.getValue(), reader.getValueAsBoolean()); break; + case DATE: + assertEquals(entry.getValue(), reader.getValueAsLocalDate()); + break; + case TIME: + assertEquals(entry.getValue(), reader.getValueAsTime()); + break; default: assertNull(entry.getValue() == null); break;