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

feat: add OffsetDateTime serializer #14

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
Loading