Skip to content

Commit

Permalink
Adding support for Kafka Schema Registry JSON messages.
Browse files Browse the repository at this point in the history
Do not store magic bytes in broker:
  - use the content-type to add them to messages for Consumer Tests
  - remove first 5 bytes from provider tests before parsing as JSON
  • Loading branch information
muirandy authored and uglyog committed Aug 2, 2022
1 parent b48e37d commit 1a80e41
Show file tree
Hide file tree
Showing 12 changed files with 772 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ class FormPostBodyMatcher : BodyMatcher {
null, "Expected a form post body but was missing")))))
expectedBody.isEmpty() && actualBody.isEmpty() -> BodyMatchResult(null, emptyList())
else -> {
val expectedParameters = URLEncodedUtils.parse(expectedBody.valueAsString(), expected.contentType.asCharset(), '&')
val actualParameters = URLEncodedUtils.parse(actualBody.valueAsString(), actual.contentType.asCharset(), '&')
val expectedParameters = URLEncodedUtils.parse(expectedBody.valueAsString(), expected.contentType.asCharset(),
'&')
val actualParameters = URLEncodedUtils.parse(actualBody.valueAsString(), actual.contentType.asCharset(),
'&')
BodyMatchResult(null, compareParameters(expectedParameters, actualParameters, matchingRules,
allowUnexpectedKeys))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package au.com.dius.pact.core.matchers

import au.com.dius.pact.core.model.ContentType
import au.com.dius.pact.core.model.OptionalBody
import au.com.dius.pact.core.model.matchingrules.MatchingRules
import au.com.dius.pact.core.support.Json
import au.com.dius.pact.core.support.json.JsonException
import au.com.dius.pact.core.support.json.JsonParser
import au.com.dius.pact.core.support.json.KafkaSchemaRegistryWireFormatter
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result
import mu.KLogging

class KafkaJsonSchemaContentMatcher() : BodyMatcher {

override fun matchBody(
expected: OptionalBody,
actual: OptionalBody,
allowUnexpectedKeys: Boolean,
matchingRules: MatchingRules
): BodyMatchResult {

val raw = removeMagicBytes(actual)

if (isInvalidActualValue(expected, raw))
return getInvalidActualJsonResult(expected, raw)

return JsonBodyMatcher.matchBody(expected, raw, allowUnexpectedKeys, matchingRules)
}

private fun removeMagicBytes(optionalBody: OptionalBody): OptionalBody {
return optionalBody.copy(value = KafkaSchemaRegistryWireFormatter.removeMagicBytes(optionalBody.value))
}

private fun isInvalidActualValue(
expected: OptionalBody,
decodedActualOptionalBody: OptionalBody
) = expected.isPresent() && !isValidJson(decodedActualOptionalBody.value)

private fun getInvalidActualJsonResult(
expected: OptionalBody,
actual: OptionalBody
) = BodyMatchResult(
null, listOf(
BodyItemMatchResult(
"$",
listOf(
BodyMismatch(
expected.valueAsString(),
actual.valueAsString(),
"Expected json body but received '${actual.valueAsString()}'"
)
)
)
)
)

private fun isValidJson(value: ByteArray?): Boolean {
if(value == null)
return false

return value.isEmpty() || isParsableJson(value)
}

private fun isParsableJson(value: ByteArray): Boolean = try {
JsonParser.parseString(String(value))
true
} catch (e: JsonException) {
logger.debug("Swallowed Exception deliberately", e)
false
}

companion object : KLogging()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ object QueryMatcher : KLogging() {
logger.debug { "compareQueryParameterValues: Matcher defined for query parameter '$parameter'" }
Matchers.domatch(matchers, "query", listOf(parameter), expected, actual, QueryMismatchFactory)
} else {
logger.debug { "compareQueryParameterValues: No matcher defined for query parameter '$parameter', using equality" }
logger.debug {
"compareQueryParameterValues: No matcher defined for query parameter '$parameter', using equality"
}
if (expected == actual) {
emptyList()
} else {
Expand Down
Loading

0 comments on commit 1a80e41

Please sign in to comment.