From ed5be21a17433aed8558c8d519eb75c1697411b9 Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Thu, 1 Feb 2024 14:51:47 -0300 Subject: [PATCH 1/3] Add support to data types --- .../fauna/serialization/FaunaGenerator.java | 120 +++++++++++++++++- .../serialization/FaunaGeneratorTest.java | 77 ++++++++++- 2 files changed, 190 insertions(+), 7 deletions(-) diff --git a/faunaJava/src/main/java/com/fauna/serialization/FaunaGenerator.java b/faunaJava/src/main/java/com/fauna/serialization/FaunaGenerator.java index 7ae82a88..c1e99c97 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaGenerator.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaGenerator.java @@ -5,10 +5,14 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.util.ByteArrayBuilder; import com.fauna.common.types.Module; +import com.fauna.exception.SerializationException; import java.io.IOException; import java.io.OutputStream; +import java.time.DateTimeException; +import java.time.Instant; import java.time.LocalDate; -import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; public class FaunaGenerator implements AutoCloseable { @@ -49,6 +53,7 @@ public void flush() throws IOException { * @throws IOException If an I/O error occurs. */ public void writeStartObject() throws IOException { + jsonGenerator.writeStartObject(); } /** @@ -57,6 +62,7 @@ public void writeStartObject() throws IOException { * @throws IOException If an I/O error occurs. */ public void writeEndObject() throws IOException { + jsonGenerator.writeEndObject(); } /** @@ -115,6 +121,8 @@ public void writeEndRef() throws IOException { * @throws IOException If an I/O error occurs. */ public void writeDouble(String fieldName, double value) throws IOException { + writeFieldName(fieldName); + writeDoubleValue(value); } /** @@ -125,6 +133,8 @@ public void writeDouble(String fieldName, double value) throws IOException { * @throws IOException If an I/O error occurs. */ public void writeInt(String fieldName, int value) throws IOException { + writeFieldName(fieldName); + writeIntValue(value); } /** @@ -135,6 +145,8 @@ public void writeInt(String fieldName, int value) throws IOException { * @throws IOException If an I/O error occurs. */ public void writeLong(String fieldName, long value) throws IOException { + writeFieldName(fieldName); + writeLongValue(value); } /** @@ -145,6 +157,8 @@ public void writeLong(String fieldName, long value) throws IOException { * @throws IOException If an I/O error occurs. */ public void writeString(String fieldName, String value) throws IOException { + writeFieldName(fieldName); + writeStringValue(value); } /** @@ -155,6 +169,8 @@ public void writeString(String fieldName, String value) throws IOException { * @throws IOException If an I/O error occurs. */ public void writeDate(String fieldName, LocalDate value) throws IOException { + writeFieldName(fieldName); + writeDateValue(value); } /** @@ -164,7 +180,9 @@ public void writeDate(String fieldName, LocalDate value) throws IOException { * @param value The LocalDateTime value to write. * @throws IOException If an I/O error occurs. */ - public void writeTime(String fieldName, LocalDateTime value) throws IOException { + public void writeTime(String fieldName, Instant value) throws IOException { + writeFieldName(fieldName); + writeTimeValue(value); } /** @@ -175,6 +193,8 @@ public void writeTime(String fieldName, LocalDateTime value) throws IOException * @throws IOException If an I/O error occurs. */ public void writeBoolean(String fieldName, boolean value) throws IOException { + writeFieldName(fieldName); + writeBooleanValue(value); } /** @@ -184,6 +204,8 @@ public void writeBoolean(String fieldName, boolean value) throws IOException { * @throws IOException If an I/O error occurs. */ public void writeNull(String fieldName) throws IOException { + writeFieldName(fieldName); + writeNullValue(); } /** @@ -203,6 +225,7 @@ public void writeModule(String fieldName, Module value) throws IOException { * @throws IOException If an I/O error occurs. */ public void writeFieldName(String value) throws IOException { + jsonGenerator.writeFieldName(value); } /** @@ -213,6 +236,99 @@ public void writeFieldName(String value) throws IOException { * @throws IOException If an I/O error occurs. */ public void writeTaggedValue(String tag, String value) throws IOException { + writeStartObject(); + writeString(tag, value); + writeEndObject(); + } + + /** + * Writes a double value as a tagged element. + * + * @param value The double value to write. + * @throws IOException If an I/O error occurs. + */ + public void writeDoubleValue(double value) throws IOException { + writeTaggedValue("@double", Double.toString(value)); + } + + /** + * Writes an integer value as a tagged element. + * + * @param value The integer value to write. + * @throws IOException If an I/O error occurs. + */ + public void writeIntValue(int value) throws IOException { + writeTaggedValue("@int", Integer.toString(value)); + } + + /** + * Writes a long integer value as a tagged element. + * + * @param value The long integer value to write. + * @throws IOException If an I/O error occurs. + */ + public void writeLongValue(long value) throws IOException { + writeTaggedValue("@long", Long.toString(value)); + } + + /** + * Writes a string value as a tagged element. + * + * @param value The string value to write. + * @throws IOException If an I/O error occurs. + */ + public void writeStringValue(String value) throws IOException { + jsonGenerator.writeString(value); + } + + /** + * Writes a date value as a tagged element. + * + * @param value The date value to write. + * @throws IOException If an I/O error occurs. + */ + public void writeDateValue(LocalDate value) throws IOException { + try { + String str = value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); + writeTaggedValue("@date", str); + } catch (DateTimeException e) { + throw new SerializationException("Error writing date value", e); + } + } + + /** + * Writes a time value as a tagged element. + * + * @param value The time value to write. + * @throws IOException If an I/O error occurs. + */ + public void writeTimeValue(Instant value) throws IOException { + try { + Instant instant = value.atZone(ZoneOffset.UTC).toInstant(); + String formattedTime = instant.toString(); + writeTaggedValue("@time", formattedTime); + } catch (DateTimeException e) { + throw new SerializationException("Error writing time value", e); + } + } + + /** + * Writes a boolean value to the stream. + * + * @param value The boolean value to write. + * @throws IOException If an I/O error occurs. + */ + public void writeBooleanValue(boolean value) throws IOException { + jsonGenerator.writeBoolean(value); + } + + /** + * Writes a null value to the stream. + * + * @throws IOException If an I/O error occurs. + */ + public void writeNullValue() throws IOException { + jsonGenerator.writeNull(); } @Override diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaGeneratorTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaGeneratorTest.java index 1867669a..dffb78e5 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaGeneratorTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaGeneratorTest.java @@ -1,25 +1,92 @@ package com.fauna.serialization; +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.io.ByteArrayOutputStream; import java.io.IOException; -import org.junit.After; -import org.junit.Before; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.LocalDate; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -class FaunaGeneratorTest { +public class FaunaGeneratorTest { private FaunaGenerator writer; private ByteArrayOutputStream stream; - @Before + @BeforeEach public void setUp() throws IOException { stream = new ByteArrayOutputStream(); writer = new FaunaGenerator(stream); } - @After + @AfterEach public void tearDown() throws IOException { writer.close(); stream.close(); } + @Test + public void writeIntValue() throws IOException { + writer.writeIntValue(42); + assertWriter("{\"@int\":\"42\"}"); + } + + @Test + public void writeLongValue() throws IOException { + writer.writeLongValue(42L); + assertWriter("{\"@long\":\"42\"}"); + } + + @Test + public void writeDoubleValue() throws IOException { + writer.writeDoubleValue(1.2d); + assertWriter("{\"@double\":\"1.2\"}"); + } + + @Test + public void writeTrueValue() throws IOException { + writer.writeBooleanValue(true); + assertWriter("true"); + } + + @Test + public void writeFalseValue() throws IOException { + writer.writeBooleanValue(false); + assertWriter("false"); + } + + @Test + public void writeNullValue() throws IOException { + writer.writeNullValue(); + assertWriter("null"); + } + + @Test + public void writeDate() throws IOException { + LocalDate dateTime = LocalDate.of(2023, 1, 1); + writer.writeDateValue(dateTime); + assertWriter("{\"@date\":\"2023-01-01\"}"); + } + + @Test + public void writeTime() throws IOException { + Instant instant = Instant.parse("2024-01-23T13:33:10.300Z"); + writer.writeTimeValue(instant); + assertWriter("{\"@time\":\"2024-01-23T13:33:10.300Z\"}"); + } + + private void assertWriter(String expected) throws IOException { + writer.flush(); + String actual = new String(stream.toByteArray(), StandardCharsets.UTF_8); + System.out.println("Expected: " + expected); + System.out.println("Actual : " + actual); + + // Add this line to explicitly print the stream content + System.out.println("Stream Content: " + stream.toString(StandardCharsets.UTF_8)); + + assertEquals(expected, actual); + } } \ No newline at end of file From d9325ea4184bc7f7b261b80246c77df3b1f044e1 Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Thu, 1 Feb 2024 15:18:52 -0300 Subject: [PATCH 2/3] Removed log --- .../java/com/fauna/serialization/FaunaGeneratorTest.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaGeneratorTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaGeneratorTest.java index dffb78e5..9e3a72a4 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaGeneratorTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaGeneratorTest.java @@ -81,12 +81,6 @@ public void writeTime() throws IOException { private void assertWriter(String expected) throws IOException { writer.flush(); String actual = new String(stream.toByteArray(), StandardCharsets.UTF_8); - System.out.println("Expected: " + expected); - System.out.println("Actual : " + actual); - - // Add this line to explicitly print the stream content - System.out.println("Stream Content: " + stream.toString(StandardCharsets.UTF_8)); - assertEquals(expected, actual); } } \ No newline at end of file From c99346ca1fe5730f3a3feb80507510860b662901 Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Fri, 2 Feb 2024 12:33:19 -0300 Subject: [PATCH 3/3] Add missing tests --- .../fauna/serialization/FaunaGeneratorTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaGeneratorTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaGeneratorTest.java index 9e3a72a4..4c5dfd36 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaGeneratorTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaGeneratorTest.java @@ -78,6 +78,20 @@ public void writeTime() throws IOException { assertWriter("{\"@time\":\"2024-01-23T13:33:10.300Z\"}"); } + @Test + public void writeTimeWithSixDecimalPrecision() throws IOException { + Instant instant = Instant.parse("2024-01-23T13:33:10.300001Z"); + writer.writeTimeValue(instant); + assertWriter("{\"@time\":\"2024-01-23T13:33:10.300001Z\"}"); + } + + @Test + public void writeNonUTCTime() throws IOException { + Instant instant = Instant.parse("2024-01-23T13:33:10.300001-07:00"); + writer.writeTimeValue(instant); + assertWriter("{\"@time\":\"2024-01-23T20:33:10.300001Z\"}"); + } + private void assertWriter(String expected) throws IOException { writer.flush(); String actual = new String(stream.toByteArray(), StandardCharsets.UTF_8);