Skip to content

Commit

Permalink
feat: add gson custom factory
Browse files Browse the repository at this point in the history
  • Loading branch information
amagyar-iohk committed Sep 14, 2023
1 parent 9a4e6b4 commit 6b31173
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.iohk.atala.automation.restassured

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonParseException
import io.restassured.path.json.mapper.factory.GsonObjectMapperFactory
import java.lang.reflect.Type
import java.time.OffsetDateTime
import java.time.format.DateTimeParseException

class CustomGsonObjectMapperFactory: GsonObjectMapperFactory {
override fun create(cls: Type?, charset: String?): Gson {
return GsonBuilder()
.registerTypeAdapter(OffsetDateTime::class.java, OffsetDateTimeDeserializer())
.create()
}

class OffsetDateTimeDeserializer : JsonDeserializer<OffsetDateTime> {
override fun deserialize(
json: JsonElement,
typeOfT: Type?,
context: JsonDeserializationContext?
): OffsetDateTime {
try {
val dateTimeString = json.asString
return OffsetDateTime.parse(dateTimeString)
} catch (e: DateTimeParseException) {
throw JsonParseException("Error parsing OffsetDateTime", e)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package io.iohk.atala.automation.serenity.objectfactory
import io.cucumber.core.backend.ObjectFactory
import io.cucumber.core.exception.CucumberException
import io.cucumber.core.plugin.ConfigureDriverFromTags
import io.iohk.atala.automation.restassured.CustomGsonObjectMapperFactory
import io.restassured.config.ObjectMapperConfig
import io.restassured.config.RestAssuredConfig
import io.restassured.mapper.ObjectMapperType
import net.serenitybdd.core.Serenity
import net.serenitybdd.core.annotations.events.BeforeScenario
import net.serenitybdd.core.lifecycle.LifecycleRegister
import net.serenitybdd.rest.SerenityRest
import net.thucydides.core.steps.StepEventBus
import java.util.*
import javax.inject.Inject
Expand All @@ -24,6 +29,12 @@ class AtalaObjectFactory : ObjectFactory {
private val classes = Collections.synchronizedSet(HashSet<Class<*>>())
private val instances: MutableMap<KClass<*>, Any> = Collections.synchronizedMap(HashMap())

init {
val objectMapperConfig = ObjectMapperConfig(ObjectMapperType.GSON).gsonObjectMapperFactory(CustomGsonObjectMapperFactory())
val config = RestAssuredConfig.newConfig().objectMapperConfig(objectMapperConfig)
SerenityRest.setDefaultConfig(config)
}

fun <T : Any> getInstance(type: KClass<T>): T {
ConfigureDriverFromTags.inTheCurrentTestOutcome()

Expand Down

0 comments on commit 6b31173

Please sign in to comment.