run.qontract.core.value.KafkaMessage.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qontract-core Show documentation
Show all versions of qontract-core Show documentation
A Contract Testing Tool that leverages Gherkin to describe APIs in a human readable and machine enforceable manner
package run.qontract.core.value
import run.qontract.core.GherkinClause
import run.qontract.core.GherkinSection.*
data class KafkaMessage(val topic: String = "", val key: StringValue? = null, val value: Value = EmptyString) {
fun toDisplayableString(): String {
return """Topic: $topic; Key: ${key?.displayableValue()}; Value: ${value.displayableValue()}"""
}
}
fun toGherkinClauses(kafkaMessage: KafkaMessage): List {
val keyTypeDeclaration = kafkaMessage.key?.typeDeclarationWithKey("KeyType", emptyMap(), UseExampleDeclarations())
val valueTypeDeclaration = kafkaMessage.value.typeDeclarationWithKey("ValueType", keyTypeDeclaration?.first?.types ?: emptyMap(), keyTypeDeclaration?.second ?: UseExampleDeclarations())
val newTypes = valueTypeDeclaration.first.types.plus(keyTypeDeclaration?.first?.types ?: emptyMap())
val gherkinTypeDeclarations = run.qontract.core.toGherkinClauses(newTypes)
val gherkinContent = listOfNotNull("kafka-message ${kafkaMessage.topic}", keyTypeDeclaration?.first?.typeValue, valueTypeDeclaration.first.typeValue).joinToString(" ")
val gherkinSection = if(gherkinTypeDeclarations.isEmpty()) `*` else Then
val messageClause = GherkinClause(gherkinContent, gherkinSection)
return listOf(messageClause).plus(gherkinTypeDeclarations)
}