Skip to content

Commit

Permalink
feat: add OffsetDateTime serializer
Browse files Browse the repository at this point in the history
Signed-off-by: Allain Magyar <allain.magyar@iohk.io>
  • Loading branch information
amagyar-iohk committed Apr 30, 2024
1 parent 527de28 commit 205b449
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<OffsetDateTime> {
class OffsetDateTimeTypeAdapter : JsonDeserializer<OffsetDateTime>, JsonSerializer<OffsetDateTime> {
override fun deserialize(
json: JsonElement,
typeOfT: Type?,
Expand All @@ -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())
}
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
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)
class TestClass
class Injectable {
val test = "Test"
}

class Injected {
@Inject
lateinit var injectable: Injectable
}

data class Date (
data class Date(
@SerializedName("date")
val date: OffsetDateTime
)
Expand Down Expand Up @@ -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))
}
}

0 comments on commit 205b449

Please sign in to comment.