-
-
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.
Utilise the Content-Type to indicate KSR messages. Deal with the 5 "magic" bytes at the start of the JSON.
- Loading branch information
Showing
4 changed files
with
212 additions
and
6 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
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
33 changes: 33 additions & 0 deletions
33
...pport/src/main/kotlin/au/com/dius/pact/core/support/json/KafkaSchemaRegistryJsonParser.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,33 @@ | ||
package au.com.dius.pact.core.support.json | ||
|
||
import java.io.InputStream | ||
import java.io.Reader | ||
|
||
object KafkaSchemaRegistryJsonParser { | ||
|
||
|
||
@Throws(JsonException::class) | ||
@JvmStatic | ||
fun parseString(json: String): JsonValue { | ||
if (json.length > 5) | ||
return JsonParser.parseString(removeMagicBytes(json)) | ||
return JsonParser.parseString(json) | ||
} | ||
|
||
private fun removeMagicBytes(contentIncludingMagicBytes: String): String { | ||
val contentOnly = contentIncludingMagicBytes.drop(5) | ||
return contentOnly | ||
} | ||
|
||
@Throws(JsonException::class) | ||
@JvmStatic | ||
fun parseStream(json: InputStream): JsonValue { | ||
return JsonParser.parseStream(json) | ||
} | ||
|
||
@Throws(JsonException::class) | ||
@JvmStatic | ||
fun parseReader(reader: Reader): JsonValue { | ||
return JsonParser.parseReader(reader) | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
...c/test/groovy/au/com/dius/pact/core/support/json/KafkaSchemaRegistryJsonParserSpec.groovy
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,122 @@ | ||
package au.com.dius.pact.core.support.json | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
@SuppressWarnings('LineLength') | ||
class KafkaSchemaRegistryJsonParserSpec extends Specification { | ||
|
||
@Unroll | ||
def 'invalid document - #description'() { | ||
when: | ||
KafkaSchemaRegistryJsonParser.INSTANCE.parseString(json) | ||
|
||
then: | ||
thrown(JsonException) | ||
|
||
where: | ||
|
||
description | json | ||
'empty document' | '' | ||
'empty document after magic bytes' | ' ' | ||
'whitespace' | ' \t\n\r' | ||
'whitespace after magic bytes' | ' \t\n\r' | ||
'minus with no following digits' | ' -' | ||
'invalid case' | 'Null' | ||
'invalid value after other' | ' null true' | ||
'unterminated string' | ' "null true' | ||
'unterminated array' | '["null", true' | ||
'unterminated object' | '{"null": true' | ||
'invalid end array' | '12]' | ||
'invalid end object' | 'true}' | ||
'invalid comma' | '1234,' | ||
'invalid object key' | '{null: true}' | ||
'unterminated object key' | '{"null: true}' | ||
'invalid object key' | '{"nu\\ll": true}' | ||
'missing colon' | '{"null" true}' | ||
'missing comma in array' | '["null" true]' | ||
'missing comma in object' | '{"null": true "other": false}' | ||
} | ||
|
||
@Unroll | ||
def 'valid document - #description'() { | ||
when: | ||
def value = KafkaSchemaRegistryJsonParser.INSTANCE.parseString(json) | ||
|
||
then: | ||
value == result | ||
|
||
where: | ||
|
||
description | json | result | ||
'integer' | ' 1234' | new JsonValue.Integer('1234'.chars) | ||
'decimal' | ' 1234.56 ' | new JsonValue.Decimal('1234.56'.chars) | ||
'true' | ' true' | JsonValue.True.INSTANCE | ||
'false' | ' false' | JsonValue.False.INSTANCE | ||
'null' | ' null' | JsonValue.Null.INSTANCE | ||
'string' | ' "null"' | new JsonValue.StringValue('null'.chars) | ||
'array' | ' [1, 200, 3, "4"]' | new JsonValue.Array([new JsonValue.Integer('1'.chars), new JsonValue.Integer('200'.chars), new JsonValue.Integer('3'.chars), new JsonValue.StringValue('4'.chars)]) | ||
'2d array' | ' [[1, 2], 3, "4"]' | new JsonValue.Array([new JsonValue.Array([new JsonValue.Integer('1'.chars), new JsonValue.Integer('2'.chars)]), new JsonValue.Integer('3'.chars), new JsonValue.StringValue('4'.chars)]) | ||
'object' | ' {"1": 200, "3": "4"}' | new JsonValue.Object(['1': new JsonValue.Integer('200'.chars), '3': new JsonValue.StringValue('4'.chars)]) | ||
'object with decimal value 1' | ' {"1": 20.25}' | new JsonValue.Object(['1': new JsonValue.Decimal('20.25'.chars)]) | ||
'object with decimal value 2' | ' {"1": 200.25}' | new JsonValue.Object(['1': new JsonValue.Decimal('200.25'.chars)]) | ||
'2d object' | ' {"1": 2, "3": {"4":5}}' | new JsonValue.Object(['1': new JsonValue.Integer('2'.chars), '3': new JsonValue.Object(['4': new JsonValue.Integer('5'.chars)])]) | ||
'empty object' | ' {}' | new JsonValue.Object([:]) | ||
'empty array' | ' []' | new JsonValue.Array([]) | ||
'empty string' | ' ""' | new JsonValue.StringValue(''.chars) | ||
'keys with special chars' | ' {"ä": "äbc"}' | new JsonValue.Object(['ä': new JsonValue.StringValue('äbc'.chars)]) | ||
} | ||
|
||
@SuppressWarnings('TrailingWhitespace') | ||
def 'parse a basic message pact'() { | ||
given: | ||
def pact = ''' { | ||
"consumer": { | ||
"name": "consumer" | ||
}, | ||
"provider": { | ||
"name": "provider" | ||
}, | ||
"messages": [ | ||
{ | ||
"metaData": { | ||
"contentType": "application/json" | ||
}, | ||
"providerStates": [ | ||
{ | ||
"name": "message exists", | ||
"params": {} | ||
} | ||
], | ||
"contents": "Hello", | ||
"matchingRules": { | ||
}, | ||
"description": "a hello message" | ||
} | ||
], | ||
"metadata": { | ||
"pactSpecification": { | ||
"version": "3.0.0" | ||
}, | ||
"pact-jvm": { | ||
"version": "4.0.10" | ||
} | ||
} | ||
} | ||
''' | ||
|
||
when: | ||
def value = KafkaSchemaRegistryJsonParser.INSTANCE.parseString(pact) | ||
|
||
then: | ||
value instanceof JsonValue.Object | ||
value.entries.keySet() == ['consumer', 'provider', 'messages', 'metadata'] as Set | ||
value.entries['consumer'] == new JsonValue.Object(['name': new JsonValue.StringValue('consumer'.chars)]) | ||
value.entries['provider'] == new JsonValue.Object(['name': new JsonValue.StringValue('provider'.chars)]) | ||
value.entries['metadata'] == new JsonValue.Object([ | ||
'pactSpecification': new JsonValue.Object(['version': new JsonValue.StringValue('3.0.0'.chars)]), | ||
'pact-jvm': new JsonValue.Object(['version': new JsonValue.StringValue('4.0.10'.chars)]) | ||
]) | ||
} | ||
} |