Skip to content

Commit

Permalink
(http-system): improve deserialization generic collection types
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Apr 12, 2024
1 parent f1929f0 commit d43a73c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package com.trendyol.stove.testing.e2e.http

import arrow.core.*
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.trendyol.stove.testing.e2e.serialization.StoveObjectMapper
import com.trendyol.stove.testing.e2e.system.*
Expand All @@ -16,7 +17,6 @@ import java.net.http.HttpClient.Version.HTTP_2
import java.net.http.HttpRequest.BodyPublishers
import java.net.http.HttpResponse.BodyHandlers
import java.time.Duration
import kotlin.reflect.KClass

@HttpDsl
data class HttpClientSystemOptions(val objectMapper: ObjectMapper = StoveObjectMapper.Default) : SystemOptions
Expand Down Expand Up @@ -83,7 +83,7 @@ class HttpSystem(
StoveHttpResponse.WithBody(
it.statusCode(),
it.headers().map()
) { deserialize(it, T::class) }
) { deserialize(it) }
)
this
}
Expand All @@ -99,7 +99,7 @@ class HttpSystem(
token.map { request.setHeader(Headers.AUTHORIZATION, Headers.bearer(it)) }
request.GET()
}.let {
expect(deserialize(it, TExpected::class))
expect(deserialize(it))
this
}

Expand All @@ -114,12 +114,7 @@ class HttpSystem(
token.map { request.setHeader(Headers.AUTHORIZATION, Headers.bearer(it)) }
request.GET()
}.let {
expect(
objectMapper.readValue(
it.body(),
objectMapper.typeFactory.constructCollectionType(List::class.java, TExpected::class.javaObjectType)
)
)
expect(deserialize(it))
this
}

Expand All @@ -143,7 +138,7 @@ class HttpSystem(
token: Option<String> = None,
expect: (actual: TExpected) -> Unit
): HttpSystem = doPostReq(uri, headers, token, body).let {
expect(deserialize(it, TExpected::class))
expect(deserialize(it))
this
}

Expand All @@ -158,7 +153,7 @@ class HttpSystem(
token: Option<String> = None,
expect: (actual: StoveHttpResponse.WithBody<TExpected>) -> Unit
): HttpSystem = doPostReq(uri, headers, token, body).let {
expect(StoveHttpResponse.WithBody(it.statusCode(), it.headers().map()) { deserialize(it, TExpected::class) })
expect(StoveHttpResponse.WithBody(it.statusCode(), it.headers().map()) { deserialize(it) })
this
}

Expand All @@ -182,7 +177,7 @@ class HttpSystem(
token: Option<String> = None,
expect: (actual: TExpected) -> Unit
): HttpSystem = doPUTReq(uri, headers, token, body).let {
expect(deserialize(it, TExpected::class))
expect(deserialize(it))
this
}

Expand All @@ -194,7 +189,7 @@ class HttpSystem(
token: Option<String> = None,
expect: (actual: StoveHttpResponse.WithBody<TExpected>) -> Unit
): HttpSystem = doPUTReq(uri, headers, token, body).let {
expect(StoveHttpResponse.WithBody(it.statusCode(), it.headers().map()) { deserialize(it, TExpected::class) })
expect(StoveHttpResponse.WithBody(it.statusCode(), it.headers().map()) { deserialize(it) })
this
}

Expand Down Expand Up @@ -284,12 +279,11 @@ class HttpSystem(
}

@PublishedApi
internal fun <TExpected : Any> deserialize(
it: HttpResponse<ByteArray>,
clazz: KClass<TExpected>
internal inline fun <reified TExpected : Any> deserialize(
it: HttpResponse<ByteArray>
): TExpected = when {
clazz.java.isAssignableFrom(String::class.java) -> String(it.body()) as TExpected
else -> objectMapper.readValue(it.body(), clazz.java)
TExpected::class.java.isAssignableFrom(String::class.java) -> String(it.body()) as TExpected
else -> objectMapper.readValue(it.body(), object : TypeReference<TExpected>() {})
}

override fun close() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class HttpSystemTests : FunSpec({
getMany<TestDto>("/get-many") { actual ->
actual[0] shouldBe TestDto(expectedGetDtoName)
}

get<List<TestDto>>("/get-many") { actual ->
actual[0] shouldBe TestDto(expectedGetDtoName)
}
}
}
}
Expand Down

0 comments on commit d43a73c

Please sign in to comment.