run.qontract.core.Contract.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
import run.qontract.core.pattern.ContractException
import run.qontract.core.utilities.contractFilePath
import run.qontract.core.utilities.readFile
import run.qontract.stub.HttpStub
import run.qontract.test.HttpClient
data class Contract(val contractGherkin: String) {
fun startFake(port: Int) = HttpStub(contractGherkin, emptyList(), "localhost", port)
fun test(endPoint: String) {
val contractBehaviour = Feature(contractGherkin)
val results = contractBehaviour.executeTests(HttpClient(endPoint))
if (results.hasFailures())
throw ContractException(results.report())
}
fun test(fake: HttpStub) = test(fake.endPoint)
fun samples(fake: HttpStub) = samples(fake.endPoint)
fun samples(endPoint: String) {
val contractBehaviour = Feature(contractGherkin)
val httpClient = HttpClient(endPoint)
contractBehaviour.generateTestScenarios(emptyList()).fold(Results()) { results, scenario ->
when(val kafkaMessagePattern = scenario.kafkaMessagePattern) {
null -> Results(results = results.results.plus(executeTest(scenario, httpClient)).toMutableList())
else -> {
val message = """KAFKA MESSAGE
${kafkaMessagePattern.generate(scenario.resolver).toDisplayableString()}""".trimMargin().prependIndent("| ")
println(message)
Results(results = results.results.plus(Result.Success()).toMutableList())
}
}
}
}
}
fun fromGherkin(contractGherkin: String): Contract {
return Contract(contractGherkin)
}