Skip to content

Commit

Permalink
feat: add gson custom factory (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
amagyar-iohk authored Sep 14, 2023
1 parent 9a4e6b4 commit 03c791b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
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,13 @@ 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
7 changes: 7 additions & 0 deletions src/test/kotlin/io/iohk/atala/automation/WithMockServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ open class WithMockServer {
.withBody("""{ "list": [1, 2, 3] }""")
)
)

WireMock.stubFor(
WireMock.get(WireMock.urlEqualTo("/offsetdatetime")).willReturn(
WireMock.aResponse().withStatus(200).withHeader("Content-Type", "application/json")
.withBody("""{ "date": "2023-09-14T11:24:46.868625Z" }""")
)
)
}

@After
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package io.iohk.atala.automation.serenity.objectfactory

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 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 {
class AtalaObjectFactoryTest: WithMockServer() {
object ObjectTestClass
private class PrivateTestClass
class ParameterizedTestClass(val parameter: String)
Expand All @@ -22,6 +33,11 @@ class AtalaObjectFactoryTest {
lateinit var injectable: Injectable
}

data class Date (
@SerializedName("date")
val date: OffsetDateTime
)

@Test
fun `AtalaObjectFactory should create new object instance if not present`() {
val test: TestClass = AtalaObjectFactory.getInstance(TestClass::class)
Expand Down Expand Up @@ -64,4 +80,14 @@ class AtalaObjectFactoryTest {
val test = AtalaObjectFactory.getInstance(TestClass::class)
assertThat(test, notNullValue())
}

@Test
fun `Should parse OffsetDateTime when using AtalaObjectFactory`() {
AtalaObjectFactory
val actor = Actor.named("tester").whoCan(CallAnApi.at("http://localhost"))
actor.attemptsTo(Get.resource("/offsetdatetime"))
val date = SerenityRest.lastResponse().get<Date>()
assertThat(date, notNullValue())
assertThat(date.date.toString(), equalTo("2023-09-14T11:24:46.868625Z"))
}
}

0 comments on commit 03c791b

Please sign in to comment.