From 205b449fe3412f9f09625cda7d6be22288d0ed23 Mon Sep 17 00:00:00 2001 From: Allain Magyar Date: Tue, 30 Apr 2024 14:03:19 -0300 Subject: [PATCH] feat: add OffsetDateTime serializer Signed-off-by: Allain Magyar --- .../CustomGsonObjectMapperFactory.kt | 15 +++++++++-- .../objectfactory/AtalaObjectFactoryTest.kt | 27 +++++++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/io/iohk/atala/automation/restassured/CustomGsonObjectMapperFactory.kt b/src/main/kotlin/io/iohk/atala/automation/restassured/CustomGsonObjectMapperFactory.kt index ad4170c..6afa15e 100644 --- a/src/main/kotlin/io/iohk/atala/automation/restassured/CustomGsonObjectMapperFactory.kt +++ b/src/main/kotlin/io/iohk/atala/automation/restassured/CustomGsonObjectMapperFactory.kt @@ -6,6 +6,9 @@ import com.google.gson.JsonDeserializationContext import com.google.gson.JsonDeserializer import com.google.gson.JsonElement import com.google.gson.JsonParseException +import com.google.gson.JsonPrimitive +import com.google.gson.JsonSerializationContext +import com.google.gson.JsonSerializer import io.restassured.path.json.mapper.factory.GsonObjectMapperFactory import java.lang.reflect.Type import java.time.OffsetDateTime @@ -14,11 +17,11 @@ import java.time.format.DateTimeParseException class CustomGsonObjectMapperFactory: GsonObjectMapperFactory { override fun create(cls: Type?, charset: String?): Gson { return GsonBuilder() - .registerTypeAdapter(OffsetDateTime::class.java, OffsetDateTimeDeserializer()) + .registerTypeAdapter(OffsetDateTime::class.java, OffsetDateTimeTypeAdapter()) .create() } - class OffsetDateTimeDeserializer : JsonDeserializer { + class OffsetDateTimeTypeAdapter : JsonDeserializer, JsonSerializer { override fun deserialize( json: JsonElement, typeOfT: Type?, @@ -31,5 +34,13 @@ class CustomGsonObjectMapperFactory: GsonObjectMapperFactory { throw JsonParseException("Error parsing OffsetDateTime", e) } } + + override fun serialize( + src: OffsetDateTime, + typeOfSrc: Type?, + context: JsonSerializationContext? + ): JsonElement { + return JsonPrimitive(src.toString()) + } } } diff --git a/src/test/kotlin/io/iohk/atala/automation/serenity/objectfactory/AtalaObjectFactoryTest.kt b/src/test/kotlin/io/iohk/atala/automation/serenity/objectfactory/AtalaObjectFactoryTest.kt index 670d1a0..cd79a79 100644 --- a/src/test/kotlin/io/iohk/atala/automation/serenity/objectfactory/AtalaObjectFactoryTest.kt +++ b/src/test/kotlin/io/iohk/atala/automation/serenity/objectfactory/AtalaObjectFactoryTest.kt @@ -1,26 +1,25 @@ package io.iohk.atala.automation.serenity.objectfactory +import com.google.gson.GsonBuilder import com.google.gson.annotations.SerializedName import io.cucumber.core.exception.CucumberException import io.iohk.atala.automation.WithMockServer -import io.iohk.atala.automation.extensions.ResponseTest import io.iohk.atala.automation.extensions.get +import io.iohk.atala.automation.restassured.CustomGsonObjectMapperFactory import net.serenitybdd.rest.SerenityRest import net.serenitybdd.screenplay.Actor import net.serenitybdd.screenplay.rest.abilities.CallAnApi import net.serenitybdd.screenplay.rest.interactions.Get -import org.hamcrest.CoreMatchers import org.hamcrest.CoreMatchers.containsString import org.hamcrest.CoreMatchers.equalTo import org.hamcrest.CoreMatchers.notNullValue -import org.hamcrest.MatcherAssert import org.hamcrest.MatcherAssert.assertThat import org.junit.Assert import org.junit.Test import java.time.OffsetDateTime import javax.inject.Inject -class AtalaObjectFactoryTest: WithMockServer() { +class AtalaObjectFactoryTest : WithMockServer() { object ObjectTestClass private class PrivateTestClass class ParameterizedTestClass(val parameter: String) @@ -28,12 +27,13 @@ class AtalaObjectFactoryTest: WithMockServer() { class Injectable { val test = "Test" } + class Injected { @Inject lateinit var injectable: Injectable } - data class Date ( + data class Date( @SerializedName("date") val date: OffsetDateTime ) @@ -90,4 +90,21 @@ class AtalaObjectFactoryTest: WithMockServer() { assertThat(date, notNullValue()) assertThat(date.date.toString(), equalTo("2023-09-14T11:24:46.868625Z")) } + + @Test + fun `Should be able to serialize and deserialize OffsetDateTime`() { + data class DateModelTest( + @SerializedName("dateTime") + var dateTime: OffsetDateTime, + ) + + val adapter = CustomGsonObjectMapperFactory.OffsetDateTimeTypeAdapter() + val gson = GsonBuilder().registerTypeAdapter(OffsetDateTime::class.java, adapter).create() + val body = DateModelTest(OffsetDateTime.now()) + val json = gson.toJson(body) + assertThat(json, containsString(body.dateTime.toString())) + + val deserialized = gson.fromJson(json, DateModelTest::class.java) + assertThat(deserialized.dateTime, equalTo(body.dateTime)) + } }