Skip to content

Commit

Permalink
Introducing OpType.Size
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellifTelus committed Oct 22, 2022
1 parent 99aa417 commit d0a13b3
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 53 deletions.
82 changes: 49 additions & 33 deletions capsa-it/src/main/kotlin/digital/capsa/it/json/JsonPathValidator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.jayway.jsonpath.JsonPath
import digital.capsa.it.validation.OpType
import digital.capsa.it.validation.ValidationRule
import net.minidev.json.JSONArray
import org.opentest4j.AssertionFailedError
import kotlin.test.assertEquals

object JsonPathValidator {
Expand All @@ -15,7 +16,7 @@ object JsonPathValidator {
val document = Configuration.defaultConfiguration().jsonProvider().parse(json)

for (rule in rules) {
val valueList: List<Any?> = if (rule.value is List<*>) {
val values: List<Any?> = if (rule.value is List<*>) {
rule.value
} else {
listOf(rule.value)
Expand All @@ -24,68 +25,83 @@ object JsonPathValidator {
try {
jsonPath = JsonPath.read(document, rule.path)
} catch (e: Exception) {
throw Error("Path not found, document: $document, path: ${rule.path}", e)
throw AssertionFailedError("Path not found, document: $document, path: ${rule.path}", e)
}
if (jsonPath == null) {
assertEquals(
valueList[0], null, "json path ${rule.path} validation failed, document: $document"
values[0], null, "json path ${rule.path} validation failed, document: $document"
)
} else
when (jsonPath) {
is JSONArray -> {
if (rule.op != OpType.equal) {
throw Error("'${rule.op}' op is not supported for JSONArray result. Use 'equal' op")
}
if (jsonPath.size == 0) {
assertEquals(
valueList.size,
0,
"json path ${rule.path} validation failed, document: $document"
)
} else {
assertEquals(
valueList.toSet(),
jsonPath.toSet(),
"json path ${rule.path} validation failed, document: $document"
when (rule.op) {
OpType.Equal -> {
if (jsonPath.size == 0) {
assertEquals(
values.size,
0,
"Json path ${rule.path} validation failed. Document: $document"
)
} else {
assertEquals(
values.toSet(),
jsonPath.toSet(),
"Json path ${rule.path} validation failed. Document: $document"
)
}
}
OpType.Size -> {
assertEquals(
values[0],
jsonPath.size,
"Json path ${rule.path} validation failed. Document: $document"
)
}
else -> throw AssertionFailedError(
throw AssertionFailedError("'${rule.op}' op is not supported for JSONArray result. Use 'equal' op")
)
}
}
is Number -> {
if (rule.op != OpType.equal) {
throw Error("${rule.op} op is not supported for Number result. Use 'equal' op")
if (rule.op != OpType.Equal) {
throw AssertionFailedError("${rule.op} op is not supported for Number result. Use 'equal' op")
}
assertEquals(
valueList[0], jsonPath, "json path ${rule.path} validation failed, document: $document"
values[0], jsonPath, "Json path ${rule.path} validation failed. Document: $document"
)
}
is Boolean -> {
if (rule.op != OpType.equal) {
throw Error("${rule.op} op is not supported for Boolean result. Use 'equal' op")
if (rule.op != OpType.Equal) {
throw AssertionFailedError("${rule.op} op is not supported for Boolean result. Use 'equal' op")
}
assertEquals(
valueList[0], jsonPath, "json path ${rule.path} validation failed, document: $document"
values[0], jsonPath, "Json path ${rule.path} validation failed. Document: $document"
)
}
is String ->
when (rule.op) {
OpType.regex ->
OpType.Regex ->
assertThat(
jsonPath,
"json path ${rule.path} validation failed, document: $document"
).matches(Regex(valueList[0].toString(), RegexOption.DOT_MATCHES_ALL))
OpType.equal ->
"json path ${rule.path} validation failed. Document: $document"
).matches(Regex(values[0].toString(), RegexOption.DOT_MATCHES_ALL))
OpType.Equal ->
assertEquals(
valueList[0],
values[0],
jsonPath,
"json path ${rule.path} validation failed, document: $document"
"Json path ${rule.path} validation failed. Document: $document"
)
OpType.like ->
OpType.Like ->
assertThat(
jsonPath,
"json path ${rule.path} validation failed, document: $document"
).matches(Regex(".*${valueList[0]}.*", RegexOption.DOT_MATCHES_ALL))
"Json path ${rule.path} validation failed. Document: $document"
).matches(Regex(".*${values[0]}.*", RegexOption.DOT_MATCHES_ALL))
OpType.Size ->
throw AssertionFailedError(
"'Size' op is not supported for String result."
)
}
else -> throw Error("Expected JSONArray or Number or String, received ${jsonPath.let { it::class }}")
else -> throw AssertionFailedError("Expected JSONArray or Number or String, received ${jsonPath.let { it::class }}")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package digital.capsa.it.validation

enum class OpType {
equal, like, regex
Equal, Like, Regex, Size
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import assertk.assertions.matches
import digital.capsa.it.validation.OpType
import digital.capsa.it.validation.ValidationRule
import org.jetbrains.kotlin.cli.common.environment.setIdeaIoUseFallback
import org.opentest4j.AssertionFailedError
import org.w3c.dom.NodeList
import org.xml.sax.InputSource
import java.io.StringReader
Expand Down Expand Up @@ -45,7 +46,7 @@ object XmlPathValidator {
if (nodes.length <= 0) {
throw Error("Path not found, document: $document, path: ${rule.path}")
} else if (nodes.length > 1) {
if (rule.op != OpType.equal) {
if (rule.op != OpType.Equal) {
throw Error("'${rule.op}' op is not supported for XML array result. Use 'equal' op")
}
val valueSet = mutableSetOf<String>()
Expand All @@ -60,22 +61,26 @@ object XmlPathValidator {
} else {
val value = nodes.item(0).textContent
when (rule.op) {
OpType.regex ->
OpType.Regex ->
assertThat(
value,
"XML path ${rule.path} validation failed, document: $document"
).matches(Regex(valueList[0].toString()))
OpType.equal ->
OpType.Equal ->
assertEquals(
valueList[0],
value,
"XML path ${rule.path} validation failed, document: $document"
)
OpType.like ->
OpType.Like ->
assertThat(
value,
"XML path ${rule.path} validation failed, document: $document"
).matches(Regex(".*${valueList[0]}.*", RegexOption.DOT_MATCHES_ALL))
OpType.Size ->
throw AssertionFailedError(
"'Size' op is not supported for String result."
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ internal class TestRunner {
}.then { response ->
assertThat(response.statusCode.value()).isEqualTo(200)
assertThat(response.body).isJsonWhere(
ValidationRule("numberOfFilesReceived", OpType.equal, numberOfFiles),
ValidationRule("numberOfFilesSent", OpType.equal, numberOfFiles)
ValidationRule("numberOfFilesReceived", OpType.Equal, numberOfFiles),
ValidationRule("numberOfFilesSent", OpType.Equal, numberOfFiles)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import kotlin.test.assertTrue
class JsonPathValidatorTest {

@Test
fun `Validator - Happy path`() {
fun testValidator_happyPath() {
given {
"""
[{
Expand All @@ -30,27 +30,27 @@ class JsonPathValidatorTest {
}.then {
JsonPathValidator.assertJson(
it, listOf(
ValidationRule("$.*.id", OpType.equal, listOf("12345", "23456")),
ValidationRule("@[?(@.id == '12345')].data", OpType.equal, "abcd"),
ValidationRule("@[?(@.id == '23456')].num", OpType.equal, 23456)
ValidationRule("$.*.id", OpType.Equal, listOf("12345", "23456")),
ValidationRule("@[?(@.id == '12345')].data", OpType.Equal, "abcd"),
ValidationRule("@[?(@.id == '23456')].num", OpType.Equal, 23456)
)
)
}
}

@Test
fun `Validator - empty`() {
fun testValidator_empty() {
JsonPathValidator.assertJson(
"""
[]
""".trimIndent(), listOf(
ValidationRule("$.*.id", OpType.equal, emptyList<String>())
ValidationRule("$.*.id", OpType.Equal, emptyList<String>())
)
)
}

@Test
fun `Validator - empty negative`() {
fun testValidator_empty_negative() {
var exception: AssertionError? = null
try {
JsonPathValidator.assertJson(
Expand All @@ -61,7 +61,7 @@ class JsonPathValidatorTest {
"id": "23456"
}]
""".trimIndent(), listOf(
ValidationRule("$.*.id", OpType.equal, "")
ValidationRule("$.*.id", OpType.Equal, "")
)
)
} catch (e: AssertionError) {
Expand All @@ -71,28 +71,46 @@ class JsonPathValidatorTest {
}

@Test
fun `Validator - regex positive`() {
fun testValidator_regex_positive() {
JsonPathValidator.assertJson(
"""
{
"id": "12345"
}
""".trimIndent(), listOf(
ValidationRule("$.id", OpType.regex, ".*")
ValidationRule("$.id", OpType.Regex, ".*")
)
)
}

@Test
fun `Validator - like positive`() {
fun testValidator_like_positive() {
JsonPathValidator.assertJson(
"""
{
"cause": "Cannot deserialize value of type `java.util.UUID` from String \"a2f674455-e5f4-4946-a19a-xdace6e1a598\": UUID has to be represented by standard 36-char representation\n at [Source: (String)\"{\"region\":\"qc\",\"listOfId\":[\"a2f674455-e5f4-4946-a19a-xdace6e1a598\"]}\"; line: 1, column: 28]"
}
""".trimIndent(), listOf(
ValidationRule("$.cause", OpType.like, "Cannot deserialize value of type"),
ValidationRule("$.cause", OpType.like, "UUID has to be represented by standard 36-char representation")
ValidationRule("$.cause", OpType.Like, "Cannot deserialize value of type"),
ValidationRule("$.cause", OpType.Like, "UUID has to be represented by standard 36-char representation")
)
)
}

@Test
@Suppress("FunctionNaming")
fun testValidator_size() {
JsonPathValidator.assertJson(
"""
{
"array1": ["123", "234", "345"],
"array2": ["123", "123"],
"array3": []
}
""".trimIndent(), listOf(
ValidationRule("$.array1", OpType.Size, 3),
ValidationRule("$.array2", OpType.Size, 2),
ValidationRule("$.array3", OpType.Size, 0)
)
)
}
Expand Down

0 comments on commit d0a13b3

Please sign in to comment.