All Downloads are FREE. Search and download functionalities are using the official Maven repository.

run.qontract.core.value.KafkaMessage.kt Maven / Gradle / Ivy

Go to download

A Contract Testing Tool that leverages Gherkin to describe APIs in a human readable and machine enforceable manner

There is a newer version: 0.23.1
Show newest version
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)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy