Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BT-4418] FaunaParser Double values #19

Merged
merged 11 commits into from
Jan 31, 2024
19 changes: 16 additions & 3 deletions faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fauna.serialization;

import static com.fauna.common.enums.FaunaTokenType.DATE;
import static com.fauna.common.enums.FaunaTokenType.DOUBLE;
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;
Expand All @@ -18,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 @@ -129,8 +131,10 @@ private void handleStartObject() throws IOException {
case TIME_TAG:
handleTaggedString(FaunaTokenType.TIME);
break;
case DOC_TAG:
case DOUBLE_TAG:
handleTaggedString(FaunaTokenType.DOUBLE);
break;
case DOC_TAG:
case LONG_TAG:
case MOD_TAG:
case OBJECT_TAG:
Expand Down Expand Up @@ -213,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 @@ -222,8 +226,17 @@ 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);
}
}

public Double getValueAsDouble() {
validateTaggedType(DOUBLE);
try {
return Double.parseDouble(taggedTokenValue);
} catch (NumberFormatException e) {
throw new SerializationException("Error getting the current token as Double", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,22 @@ public void testGetValueAsInt() throws IOException {
);

assertReader(reader, expectedTokens);
}

@Test
public void testGetValueAsIntFail() throws IOException {
String invalidJson = "{\"@int\": \"abc\"}";
InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes());
FaunaParser invalidReader = new FaunaParser(invalidInputStream);

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

Exception ex = assertThrows(SerializationException.class,
() -> assertReader(invalidReader, expectedTokens));

assertEquals("Error getting the current token as Integer", ex.getMessage());
}

@Test
Expand All @@ -58,7 +68,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 Down Expand Up @@ -86,26 +99,36 @@ public void testGetValueAsBooleanFalse() throws IOException {
}

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

@Test
public void testGetValueAsLocalDateFail() throws IOException {
String invalidJson = "{\"@date\": \"abc\"}";
InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes());
FaunaParser invalidReader = new FaunaParser(invalidInputStream);

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

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 @@ -117,12 +140,22 @@ public void testeGetValueAsTime() throws IOException {
);

assertReader(reader, expectedTokens);
}

@Test
public void testGetValueAsTimeFail() throws IOException {
String invalidJson = "{\"@time\": \"abc\"}";
InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes());
FaunaParser invalidReader = new FaunaParser(invalidInputStream);

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

Exception ex = assertThrows(SerializationException.class,
() -> assertReader(invalidReader, expectedTokens));

assertEquals("Error getting the current token as LocalDateTime", ex.getMessage());
}

@Test
Expand All @@ -141,6 +174,35 @@ public void testeGetValueAsTimeNonUTC() throws IOException {

}

@Test
public void testGetValueAsDouble() throws IOException {
String s = "{\"@double\": \"1.23\"}";
InputStream inputStream = new ByteArrayInputStream(s.getBytes());
FaunaParser reader = new FaunaParser(inputStream);

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.DOUBLE, 1.23D)
);

assertReader(reader, expectedTokens);
}

@Test
public void testGetValueAsDoubleFail() throws IOException {
String invalidJson = "{\"@double\": \"abc\"}";
InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes());
FaunaParser invalidReader = new FaunaParser(invalidInputStream);

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.DOUBLE, "abc")
);

Exception ex = assertThrows(SerializationException.class,
() -> assertReader(invalidReader, expectedTokens));

assertEquals("Error getting the current token as Double", ex.getMessage());
}

private static void assertReader(FaunaParser reader,
List<Map.Entry<FaunaTokenType, Object>> tokens) throws IOException {
for (Map.Entry<FaunaTokenType, Object> entry : tokens) {
Expand All @@ -167,6 +229,9 @@ private static void assertReader(FaunaParser reader,
case TIME:
assertEquals(entry.getValue(), reader.getValueAsTime());
break;
case DOUBLE:
assertEquals(entry.getValue(), reader.getValueAsDouble());
break;
default:
assertNull(entry.getValue() == null);
break;
Expand Down
Loading