Improve KtorServerRequestSteps
This commit is contained in:
@@ -4,7 +4,6 @@ import fr.dcproject.entity.*
|
|||||||
import fr.dcproject.utils.contentToString
|
import fr.dcproject.utils.contentToString
|
||||||
import fr.dcproject.utils.getJsonField
|
import fr.dcproject.utils.getJsonField
|
||||||
import fr.dcproject.utils.toIso
|
import fr.dcproject.utils.toIso
|
||||||
import io.ktor.util.sha1
|
|
||||||
import org.elasticsearch.client.Request
|
import org.elasticsearch.client.Request
|
||||||
import org.elasticsearch.client.Response
|
import org.elasticsearch.client.Response
|
||||||
import org.elasticsearch.client.RestClient
|
import org.elasticsearch.client.RestClient
|
||||||
@@ -14,7 +13,6 @@ import java.util.*
|
|||||||
class ArticleViewManager(private val restClient: RestClient) : ViewManager<ArticleRefVersioning> {
|
class ArticleViewManager(private val restClient: RestClient) : ViewManager<ArticleRefVersioning> {
|
||||||
override fun addView(ip: String, article: ArticleRefVersioning, citizen: CitizenRef?, dateTime: DateTime): Response? {
|
override fun addView(ip: String, article: ArticleRefVersioning, citizen: CitizenRef?, dateTime: DateTime): Response? {
|
||||||
val isLogged = (citizen != null).toString()
|
val isLogged = (citizen != null).toString()
|
||||||
sha1("plop".toByteArray())
|
|
||||||
val ref = citizen?.id ?: UUID.nameUUIDFromBytes(ip.toByteArray())!!
|
val ref = citizen?.id ?: UUID.nameUUIDFromBytes(ip.toByteArray())!!
|
||||||
val request = Request(
|
val request = Request(
|
||||||
"POST",
|
"POST",
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
package feature
|
package feature
|
||||||
|
|
||||||
import com.google.gson.JsonArray
|
import com.jayway.jsonpath.JsonPath
|
||||||
import com.google.gson.JsonElement
|
|
||||||
import com.google.gson.JsonParser
|
|
||||||
import com.google.gson.JsonPrimitive
|
|
||||||
import io.cucumber.datatable.DataTable
|
import io.cucumber.datatable.DataTable
|
||||||
import io.cucumber.java8.En
|
import io.cucumber.java8.En
|
||||||
import io.ktor.http.ContentType
|
import io.ktor.http.ContentType
|
||||||
@@ -14,7 +11,6 @@ import io.ktor.server.testing.setBody
|
|||||||
import io.ktor.util.KtorExperimentalAPI
|
import io.ktor.util.KtorExperimentalAPI
|
||||||
import kotlinx.serialization.ImplicitReflectionSerializer
|
import kotlinx.serialization.ImplicitReflectionSerializer
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
import kotlin.test.fail
|
|
||||||
|
|
||||||
@ImplicitReflectionSerializer
|
@ImplicitReflectionSerializer
|
||||||
@KtorExperimentalAPI
|
@KtorExperimentalAPI
|
||||||
@@ -50,8 +46,7 @@ class KtorServerRequestSteps : En {
|
|||||||
|
|
||||||
Then("the response should contain object:") { expected: DataTable ->
|
Then("the response should contain object:") { expected: DataTable ->
|
||||||
expected.asMap<String, String>(String::class.java, String::class.java).forEach { (key, valueExpected) ->
|
expected.asMap<String, String>(String::class.java, String::class.java).forEach { (key, valueExpected) ->
|
||||||
val jsonPrimitive = findJsonElement(key) as? JsonPrimitive ?: fail("\"$key\" element isn't json primitive")
|
assertEquals(valueExpected, JsonPath.read<Any>(response, key)?.toString() ?: throw AssertionError("\"$key\" element not found on json response"))
|
||||||
assertEquals(valueExpected, jsonPrimitive.asString)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,24 +55,6 @@ class KtorServerRequestSteps : En {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findJsonElement(path: String): JsonElement {
|
private val response: String?
|
||||||
var jsonElement: JsonElement = responseJsonElement
|
get() = KtorServerContext.defaultServer.call?.response?.content
|
||||||
|
|
||||||
path
|
|
||||||
.split("].", "[", ".")
|
|
||||||
.filter { it.trim().isNotBlank() }
|
|
||||||
.map { it.trim() }
|
|
||||||
.forEach {
|
|
||||||
jsonElement = if (jsonElement is JsonArray) {
|
|
||||||
jsonElement.asJsonArray.get(it.toInt())
|
|
||||||
} else {
|
|
||||||
jsonElement.asJsonObject.get(it)
|
|
||||||
} ?: throw AssertionError("\"$path\" element not found on json response")
|
|
||||||
}
|
|
||||||
|
|
||||||
return jsonElement
|
|
||||||
}
|
|
||||||
|
|
||||||
private val responseJsonElement: JsonElement
|
|
||||||
get() = JsonParser().parse(KtorServerContext.defaultServer.call?.response?.content)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user