From 8ed1950b43b4fda9112af7a677e8312ff142619d Mon Sep 17 00:00:00 2001 From: Michael von Bargen Date: Tue, 21 May 2024 16:55:38 +0200 Subject: [PATCH] fix(#988): adjust test for payload mapping --- .../src/test/kotlin/JsonPathWithValueTest.kt | 265 +++++++++--------- 1 file changed, 140 insertions(+), 125 deletions(-) diff --git a/integration/common/variable-serializer/src/test/kotlin/JsonPathWithValueTest.kt b/integration/common/variable-serializer/src/test/kotlin/JsonPathWithValueTest.kt index c7dd12926..1edf72698 100644 --- a/integration/common/variable-serializer/src/test/kotlin/JsonPathWithValueTest.kt +++ b/integration/common/variable-serializer/src/test/kotlin/JsonPathWithValueTest.kt @@ -40,11 +40,10 @@ internal class JsonPathWithValueTest { .putValue("key-float", 101.1F) val result = payload.toJsonPathsWithValues(limit = -1) - val keys = result.map { it.first } - assertThat(keys).containsExactlyInAnyOrderElementsOf(payload.keys) + assertThat(result.keys()).containsExactlyInAnyOrderElementsOf(payload.keys) payload.entries.forEach { - assertThat(keys).contains(it.key) + assertThat(result.keys()).contains(it.key) assertThat(result.first { entry -> entry.first == it.key }.second).isEqualTo(it.value) } } @@ -72,9 +71,8 @@ internal class JsonPathWithValueTest { } val result = deep.toJsonPathsWithValues(limit = -1) - val keys = result.map { it.first } - assertThat(keys).containsExactlyInAnyOrderElementsOf( + assertThat(result.keys()).containsExactlyInAnyOrderElementsOf( flat.keys.plus( listOf( "key-map.child1", @@ -84,7 +82,7 @@ internal class JsonPathWithValueTest { ) ) flat.entries.forEach { - assertThat(keys).contains(it.key) + assertThat(result.keys()).contains(it.key) assertThat(result.first { entry -> entry.first == it.key }.second).isEqualTo(it.value) } @@ -93,124 +91,141 @@ internal class JsonPathWithValueTest { assertThat(result).contains("key-map.child2.grand-child2" to 451.01F) } -// @Test -// fun `should convert a deep map with primitives limited by level`() { -// val flat = mapOf( -// "key-string" to "value", -// "key-long" to 19L, -// "key-date" to now, -// "key-int" to 56, -// "key-bool" to true -// ) -// val deep = createVariables().apply { -// putAll(flat) -// putValue( -// "key-map", mapOf( -// "child1" to "string", -// "child2" to mapOf( -// "grand-child1" to "grand-child-value", -// "grand-child2" to 451.01F -// ) -// ) -// ) -// } -// -// val result = deep.toJsonPathsWithValues(limit = 1) -// -// assertThat(result.keys).containsExactlyInAnyOrderElementsOf( -// flat.keys.plus( -// listOf( -// "key-map.child1" -// ) -// ) -// ) -// flat.entries.forEach { -// assertThat(result).containsKey(it.key) -// assertThat(result[it.key]).isEqualTo(it.value) -// } -// assertThat(result["key-map.child1"]).isEqualTo("string") -// assertThat(result["key-map.child2"]).isNull() -// } -// -// @Test -// fun `should ignore complex object`() { -// val payload = createVariables().apply { -// put("key", Pojo1("value", listOf(Pojo2("value", listOf())))) -// } -// assertThat(payload.toJsonPathsWithValues()).isEmpty() -// } -// -// -// @Test -// fun `should ignore attribute by name`() { -// val payload = createVariables().apply { -// put("key", "value") -// put("to-ignore", "should not be there") -// } -// assertThat(payload.toJsonPathsWithValues(filters = listOf(eqExclude("to-ignore")))).containsOnlyKeys("key") -// -// } -// -// @Test -// fun `should include attribute by name`() { -// val payload = createVariables().apply { -// put("key", "value") -// put("to-ignore", "should not be there") -// } -// assertThat(payload.toJsonPathsWithValues(filters = listOf(eqInclude("key")))).containsOnlyKeys("key") -// } -// -// @Test -// fun `should include and exclude attribute by name`() { -// val payload = createVariables().apply { -// put("include1", "value") -// put("include2", "value") -// put("to-ignore", "should not be there") -// } -// assertThat( -// payload.toJsonPathsWithValues( -// filters = listOf( -// eqInclude("include1"), -// eqInclude("include2"), -// eqExclude("to-ignore") -// ) -// ) -// ).containsOnlyKeys("include1", "include2") -// } -// -// @Test -// fun `should include nested attributes by name`() { -// val payload = createVariables().apply { -// put("include1", mapOf("ignore" to "should not be there", "key" to "value")) -// put("include2", "value") -// } -// assertThat( -// payload.toJsonPathsWithValues( -// filters = listOf( -// eqInclude("include1.key"), -// eqInclude("include2"), -// ) -// ) -// ).containsOnlyKeys("include1.key", "include2") -// } -// -// -// @Test -// fun `should accept all attributes`() { -// val payload = createVariables().apply { -// put("key", "value") -// put("other", "value2") -// } -// assertThat(payload.toJsonPathsWithValues(filters = listOf(all()))).containsOnlyKeys("key", "other") -// } -// -// @Test -// fun `should ignore all attributes`() { -// val payload = createVariables().apply { -// put("key", "value") -// put("other", "value2") -// } -// assertThat(payload.toJsonPathsWithValues(filters = listOf(none()))).isEmpty() -// } + @Test + fun `should convert a deep map with primitives limited by level`() { + val flat = mapOf( + "key-string" to "value", + "key-long" to 19L, + "key-date" to now, + "key-int" to 56, + "key-bool" to true + ) + val deep = createVariables().apply { + putAll(flat) + putValue( + "key-map", mapOf( + "child1" to "string", + "child2" to mapOf( + "grand-child1" to "grand-child-value", + "grand-child2" to 451.01F + ) + ) + ) + } + + val result = deep.toJsonPathsWithValues(limit = 1) + + assertThat(result.keys()).containsExactlyInAnyOrderElementsOf( + flat.keys.plus( + listOf( + "key-map.child1" + ) + ) + ) + flat.entries.forEach { + assertThat(result.keys()).contains(it.key) + assertThat(result).contains(it.key to it.value) + } + assertThat(result).contains("key-map.child1" to "string") + assertThat(result.keys()).doesNotContain("key-map.child2") + } + + @Test + fun `should ignore complex object`() { + val payload = createVariables().apply { + put("key", Pojo1("value", listOf(Pojo2("value", listOf())))) + } + assertThat(payload.toJsonPathsWithValues()).isEmpty() + } + + + @Test + fun `should ignore attribute by name`() { + val payload = createVariables().apply { + put("key", "value") + put("to-ignore", "should not be there") + } + assertThat(payload.toJsonPathsWithValues(filters = listOf(eqExclude("to-ignore"))).keys()).containsOnly("key") + + } + + @Test + fun `should include attribute by name`() { + val payload = createVariables().apply { + put("key", "value") + put("to-ignore", "should not be there") + } + assertThat(payload.toJsonPathsWithValues(filters = listOf(eqInclude("key"))).keys()).containsOnly("key") + } + + @Test + fun `should include and exclude attribute by name`() { + val payload = createVariables().apply { + put("include1", "value") + put("include2", "value") + put("to-ignore", "should not be there") + } + assertThat( + payload.toJsonPathsWithValues( + filters = listOf( + eqInclude("include1"), + eqInclude("include2"), + eqExclude("to-ignore") + ) + ).keys() + ).containsOnly("include1", "include2") + } + + @Test + fun `should include nested attributes by name`() { + val payload = createVariables().apply { + put("include1", mapOf("ignore" to "should not be there", "key" to "value")) + put("include2", "value") + } + assertThat( + payload.toJsonPathsWithValues( + filters = listOf( + eqInclude("include1.key"), + eqInclude("include2"), + ) + ).keys() + ).containsOnly("include1.key", "include2") + } + + + @Test + fun `should accept all attributes`() { + val payload = createVariables().apply { + put("key", "value") + put("other", "value2") + } + assertThat(payload.toJsonPathsWithValues(filters = listOf(all())).keys()).containsOnly("key", "other") + } + + @Test + fun `should ignore all attributes`() { + val payload = createVariables().apply { + put("key", "value") + put("other", "value2") + } + assertThat(payload.toJsonPathsWithValues(filters = listOf(none()))).isEmpty() + } + + @Test + fun `should map list to multiple pairs`() { + val payload = createVariables().apply { + put("multiple", listOf("value-1", "value-2")) + } + + val result = payload.toJsonPathsWithValues() + + assertThat(result).hasSize(2) + assertThat(result).contains("multiple" to "value-1") + assertThat(result).contains("multiple" to "value-2") + } + +} +internal fun Set>.keys(): List { + return this.map { it.first } }