Skip to content

Commit

Permalink
fix(compatibility-suite): Correct error messages to be consistant
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Aug 15, 2023
1 parent a163785 commit aeb09e9
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Generators {
matchTypeOfElement(type, element)
}

@SuppressWarnings('AbcMetric')
static void matchTypeOfElement(String type, JsonValue element) {
switch (type) {
case 'integer' -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -229,8 +232,13 @@ fun <M : Mismatch> matchType(
mismatchFactory: MismatchFactory<M>,
allowEmpty: Boolean
): List<M> {
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 ||
Expand Down Expand Up @@ -790,7 +798,7 @@ fun <M : Mismatch> 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))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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') | []
}
}

0 comments on commit aeb09e9

Please sign in to comment.