Skip to content

Commit

Permalink
Merge pull request #18 from fauna/feature/BT-4418-utf8faunareader_rea…
Browse files Browse the repository at this point in the history
…d_date_and_time

[BT-4418] FaunaParser LocalDate and Instant values
  • Loading branch information
pnwpedro authored Jan 30, 2024
2 parents 545a041 + 2eaa127 commit 7264729
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
32 changes: 29 additions & 3 deletions faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -120,14 +124,18 @@ 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:
case MOD_TAG:
case OBJECT_TAG:
case REF_TAG:
case SET_TAG:
case TIME_TAG:
throw new SerializationException(
"Token not implemented: " + jsonParser.currentToken());
default:
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Map.Entry<FaunaTokenType, Object>> 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<Map.Entry<FaunaTokenType, Object>> 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<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.TIME, instant)
);

assertReader(reader, expectedTokens);

}

private static void assertReader(FaunaParser reader,
List<Map.Entry<FaunaTokenType, Object>> tokens) throws IOException {
for (Map.Entry<FaunaTokenType, Object> entry : tokens) {
Expand All @@ -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;
Expand Down

0 comments on commit 7264729

Please sign in to comment.