From aeb09e905f3c64f3db6eaa275d993b8e39555816 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Tue, 15 Aug 2023 16:24:53 +1000 Subject: [PATCH] fix(compatibility-suite): Correct error messages to be consistant --- .../test/groovy/steps/shared/SharedSteps.groovy | 6 +++++- .../src/test/groovy/steps/v3/HttpMatching.groovy | 7 ++++++- .../src/test/groovy/steps/v4/Generators.groovy | 1 + .../src/test/groovy/steps/v4/HttpMatching.groovy | 2 +- .../dius/pact/core/matchers/JsonContentMatcher.kt | 9 +++++---- .../com/dius/pact/core/matchers/MatcherExecutor.kt | 14 +++++++++++--- .../au/com/dius/pact/core/matchers/Mismatches.kt | 2 +- .../core/matchers/JsonContentMatcherSpec.groovy | 7 +++---- .../KafkaJsonSchemaContentMatcherSpec.groovy | 7 +++---- .../core/matchers/MatcherExecutorKtSpec.groovy | 4 ++-- 10 files changed, 38 insertions(+), 21 deletions(-) diff --git a/compatibility-suite/src/test/groovy/steps/shared/SharedSteps.groovy b/compatibility-suite/src/test/groovy/steps/shared/SharedSteps.groovy index 9173732c6..29994f3aa 100644 --- a/compatibility-suite/src/test/groovy/steps/shared/SharedSteps.groovy +++ b/compatibility-suite/src/test/groovy/steps/shared/SharedSteps.groovy @@ -158,8 +158,12 @@ class SharedSteps { } } } else { + def contents = entry + if (entry == 'EMPTY') { + contents = '' + } request.headers['content-type'] = [detectedContentType] - request.body = OptionalBody.body(entry) + request.body = OptionalBody.body(contents) } request } diff --git a/compatibility-suite/src/test/groovy/steps/v3/HttpMatching.groovy b/compatibility-suite/src/test/groovy/steps/v3/HttpMatching.groovy index 5ec532279..130315e89 100644 --- a/compatibility-suite/src/test/groovy/steps/v3/HttpMatching.groovy +++ b/compatibility-suite/src/test/groovy/steps/v3/HttpMatching.groovy @@ -76,8 +76,13 @@ class HttpMatching { void the_following_requests_are_received(DataTable dataTable) { for (entry in dataTable.entries()) { def request = new Request() + if (entry['body']) { - def part = configureBody(entry['body'], determineContentType(entry['body'], request.contentTypeHeader())) + def body = entry['body'] + if (entry['body'] == 'EMPTY') { + body = '' + } + def part = configureBody(body, determineContentType(body, request.contentTypeHeader())) request.body = part.body request.headers.putAll(part.headers) } diff --git a/compatibility-suite/src/test/groovy/steps/v4/Generators.groovy b/compatibility-suite/src/test/groovy/steps/v4/Generators.groovy index 682317441..31240b79a 100644 --- a/compatibility-suite/src/test/groovy/steps/v4/Generators.groovy +++ b/compatibility-suite/src/test/groovy/steps/v4/Generators.groovy @@ -76,6 +76,7 @@ class Generators { matchTypeOfElement(type, element) } + @SuppressWarnings('AbcMetric') static void matchTypeOfElement(String type, JsonValue element) { switch (type) { case 'integer' -> { diff --git a/compatibility-suite/src/test/groovy/steps/v4/HttpMatching.groovy b/compatibility-suite/src/test/groovy/steps/v4/HttpMatching.groovy index 1e05e9b72..d0a2764f6 100644 --- a/compatibility-suite/src/test/groovy/steps/v4/HttpMatching.groovy +++ b/compatibility-suite/src/test/groovy/steps/v4/HttpMatching.groovy @@ -78,7 +78,7 @@ class HttpMatching { @Then('the response mismatches will contain a {string} mismatch with error {string}') void the_response_mismatches_will_contain_a_mismatch_with_error(String type, String error) { assert responseResults.find { - it.type() == type && it.description() == error + it.type() == type && it.description().toLowerCase() == error.toLowerCase() } } diff --git a/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/JsonContentMatcher.kt b/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/JsonContentMatcher.kt index c0bc99e30..12dae4b72 100644 --- a/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/JsonContentMatcher.kt +++ b/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/JsonContentMatcher.kt @@ -60,9 +60,9 @@ object JsonContentMatcher : ContentMatcher, KLogging() { private fun typeOf(value: Any?) = when { value is Map<*, *> -> "Map" - value is JsonValue.Object -> "Map" + value is JsonValue.Object -> "Object" value is List<*> -> "List" - value is JsonValue.Array -> "List" + value is JsonValue.Array -> "Array" value is JsonValue.Null -> "Null" value is JsonValue -> value.name value == null -> "Null" @@ -83,8 +83,9 @@ object JsonContentMatcher : ContentMatcher, KLogging() { expected is JsonValue.Object && actual !is JsonValue.Object || expected is JsonValue.Array && actual !is JsonValue.Array -> listOf(BodyItemMatchResult(constructPath(path), - listOf(BodyMismatch(expected, actual, "Type mismatch: Expected ${typeOf(actual)} " + - "${valueOf(actual)} to be equal to ${typeOf(expected)} ${valueOf(expected)}", constructPath(path), + listOf(BodyMismatch(expected, actual, "Type mismatch: Expected " + + "${valueOf(actual)} (${typeOf(actual)}) to be the same type as ${valueOf(expected)} (${typeOf(expected)})", + constructPath(path), generateJsonDiff(expected, actual))))) else -> compareValues(path, expected, actual, context) } diff --git a/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatcherExecutor.kt b/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatcherExecutor.kt index f43644a26..cba259767 100755 --- a/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatcherExecutor.kt +++ b/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatcherExecutor.kt @@ -65,7 +65,7 @@ fun valueOf(value: Any?): String { is Element -> "<${QualifiedName(value)}>" is Text -> "'${value.wholeText}'" is JsonValue -> value.serialise() - is ByteArray -> "${value.size} byte(s)" + is ByteArray -> "${value.asList()}" else -> value.toString() } } @@ -75,6 +75,9 @@ fun typeOf(value: Any?): String { null -> "Null" is JsonValue -> value.type() is Attr -> "XmlAttr" + is List<*> -> "Array" + is Array<*> -> "Array" + is ByteArray -> "${value.size} bytes" else -> value.javaClass.simpleName } } @@ -229,8 +232,13 @@ fun matchType( mismatchFactory: MismatchFactory, allowEmpty: Boolean ): List { + val kotlinClass = if (actual != null) { + actual::class.qualifiedName + } else { + "NULL" + } logger.debug { - "comparing type of [$actual] (${actual?.javaClass?.simpleName}) to " + + "comparing type of [$actual] ($kotlinClass, ${actual?.javaClass?.simpleName}) to " + "[$expected] (${expected?.javaClass?.simpleName}) at $path" } return if (expected is Number && actual is Number || @@ -790,7 +798,7 @@ fun matchSemver( emptyList() } else { listOf(mismatchFactory.create(expected, actual, - "Expected ${valueOf(actual)} (${typeOf(actual)}) to be a semantic version", path)) + "${valueOf(actual)} is not a valid semantic version", path)) } } diff --git a/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/Mismatches.kt b/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/Mismatches.kt index 5501334f2..6ad5ce52a 100755 --- a/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/Mismatches.kt +++ b/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/Mismatches.kt @@ -30,7 +30,7 @@ data class StatusMismatch( return when (statusType) { null -> "expected status of $expected but was $actual" HttpStatus.StatusCodes -> "expected a status in $statusCodes but was $actual" - else -> "expected $statusType but was $actual" + else -> "expected status code $actual to be a $statusType" } } override fun description(t: TermColors): String { diff --git a/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/JsonContentMatcherSpec.groovy b/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/JsonContentMatcherSpec.groovy index b3c8b7d06..c419be80a 100644 --- a/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/JsonContentMatcherSpec.groovy +++ b/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/JsonContentMatcherSpec.groovy @@ -219,9 +219,8 @@ class JsonContentMatcherSpec extends Specification { def 'matching json bodies - returns a mismatch - when comparing a map to a list'() { expect: matcher.matchBody(expectedBody, actualBody, context).mismatches.find { - it instanceof BodyMismatch && - it.mismatch.contains( - 'Type mismatch: Expected List [100,100] to be equal to Map {"something":100,"somethingElse":100}') + it instanceof BodyMismatch && it.mismatch.contains('Type mismatch: Expected [100,100] (Array) to be the same' + + ' type as {"something":100,"somethingElse":100} (Object)') } where: @@ -234,7 +233,7 @@ class JsonContentMatcherSpec extends Specification { expect: matcher.matchBody(expectedBody, actualBody, context).mismatches.find { it instanceof BodyMismatch && - it.mismatch.contains('Type mismatch: Expected Integer 100 to be equal to List [100,100]') + it.mismatch.contains('Type mismatch: Expected 100 (Integer) to be the same type as [100,100] (Array)') } where: diff --git a/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/KafkaJsonSchemaContentMatcherSpec.groovy b/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/KafkaJsonSchemaContentMatcherSpec.groovy index 905db7406..d7586a526 100644 --- a/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/KafkaJsonSchemaContentMatcherSpec.groovy +++ b/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/KafkaJsonSchemaContentMatcherSpec.groovy @@ -221,9 +221,8 @@ class KafkaJsonSchemaContentMatcherSpec extends Specification { def 'matching json bodies - returns a mismatch - when comparing a map to a list'() { expect: matcher.matchBody(expectedBody, actualBody, context).mismatches.find { - it instanceof BodyMismatch && - it.mismatch.contains( - 'Type mismatch: Expected List [100,100] to be equal to Map {"something":100,"somethingElse":100}') + it instanceof BodyMismatch && it.mismatch.contains('Type mismatch: Expected [100,100] (Array) to be the same' + + ' type as {"something":100,"somethingElse":100} (Object)') } where: @@ -236,7 +235,7 @@ class KafkaJsonSchemaContentMatcherSpec extends Specification { expect: matcher.matchBody(expectedBody, actualBody, context).mismatches.find { it instanceof BodyMismatch && - it.mismatch.contains('Type mismatch: Expected Integer 100 to be equal to List [100,100]') + it.mismatch.contains('Type mismatch: Expected 100 (Integer) to be the same type as [100,100] (Array)') } where: diff --git a/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatcherExecutorKtSpec.groovy b/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatcherExecutorKtSpec.groovy index c9dd8a968..21e026abd 100644 --- a/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatcherExecutorKtSpec.groovy +++ b/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatcherExecutorKtSpec.groovy @@ -24,8 +24,8 @@ class MatcherExecutorKtSpec extends Specification { actual | result '4.5.7' | [] - '4.5.7.8' | [new HeaderMismatch('test', '', '4.5.7.8', "Expected '4.5.7.8' (String) to be a semantic version")] - '04.5.7' | [new HeaderMismatch('test', '', '04.5.7', "Expected '04.5.7' (String) to be a semantic version")] + '4.5.7.8' | [new HeaderMismatch('test', '', '4.5.7.8', "'4.5.7.8' is not a valid semantic version")] + '04.5.7' | [new HeaderMismatch('test', '', '04.5.7', "'04.5.7' is not a valid semantic version")] new JsonValue.StringValue('4.5.7') | [] } }