-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for Kafka Schema Registry JSON messages.
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
Showing
12 changed files
with
772 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
.../matchers/src/main/kotlin/au/com/dius/pact/core/matchers/KafkaJsonSchemaContentMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.