Skip to content

Commit

Permalink
fix(#988): adjust test for payload mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael von Bargen committed May 21, 2024
1 parent 1d4b2c1 commit 8ed1950
Showing 1 changed file with 140 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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",
Expand All @@ -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)
}

Expand All @@ -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<Pair<String, Any>>.keys(): List<String> {
return this.map { it.first }
}

0 comments on commit 8ed1950

Please sign in to comment.