From 51cc5b640d31198e526b9974b226cdc5c13ade38 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 3 Oct 2019 22:54:58 +0200 Subject: [PATCH] fix and improve cucumber step "findJsonElement" --- .../kotlin/feature/KtorServerRequestSteps.kt | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/test/kotlin/feature/KtorServerRequestSteps.kt b/src/test/kotlin/feature/KtorServerRequestSteps.kt index acf6fa3..4fa1584 100644 --- a/src/test/kotlin/feature/KtorServerRequestSteps.kt +++ b/src/test/kotlin/feature/KtorServerRequestSteps.kt @@ -1,5 +1,6 @@ package feature +import com.google.gson.JsonArray import com.google.gson.JsonElement import com.google.gson.JsonParser import com.google.gson.JsonPrimitive @@ -59,27 +60,24 @@ class KtorServerRequestSteps : En { } } - private fun findJsonElement(node: String): JsonElement { + private fun findJsonElement(path: String): JsonElement { var jsonElement: JsonElement = responseJsonElement - val elements = node.split("].", "[", ".") - elements + path + .split("].", "[", ".") .filter { it.trim().isNotBlank() } .map { it.trim() } .forEach { - val asArrayIndex = """^\d+$""".toRegex().find(it) - - jsonElement = if (asArrayIndex != null) { - val index = asArrayIndex.groups.first()!! - jsonElement.asJsonArray.get(index.value.toInt()) + jsonElement = if (jsonElement is JsonArray) { + jsonElement.asJsonArray.get(it.toInt()) } else { - jsonElement.asJsonObject.get(it) ?: throw AssertionError("\"$node\" element not found on json response") - } + 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).getAsJsonObject() ?: fail("The response isn't valid JSON") + get() = JsonParser().parse(KtorServerContext.defaultServer.call?.response?.content) }