au.com.dius.pact.provider.reporters.MarkdownReporter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pact-jvm-provider Show documentation
Show all versions of pact-jvm-provider Show documentation
Pact provider
=============
sub project of https://github.com/DiUS/pact-jvm
The pact provider is responsible for verifying that an API provider adheres to a number of pacts authored by its clients
This library provides the basic tools required to automate the process, and should be usable on its own in many instances.
Framework and build tool specific bindings will be provided in separate libraries that build on top of this core functionality.
### Provider State
Before each interaction is executed, the provider under test will have the opportunity to enter a state.
Generally the state maps to a set of fixture data for mocking out services that the provider is a consumer of (they will have their own pacts)
The pact framework will instruct the test server to enter that state by sending:
POST "${config.stateChangeUrl.url}/setup" { "state" : "${interaction.stateName}" }
### An example of running provider verification with junit
This example uses Groovy, JUnit 4 and Hamcrest matchers to run the provider verification.
As the provider service is a DropWizard application, it uses the DropwizardAppRule to startup the service before running any test.
**Warning:** It only grabs the first interaction from the pact file with the consumer, where there could be many. (This could possibly be solved with a parameterized test)
```groovy
class ReadmeExamplePactJVMProviderJUnitTest {
@ClassRule
public static final TestRule startServiceRule = new DropwizardAppRule<DropwizardConfiguration>(
TestDropwizardApplication, ResourceHelpers.resourceFilePath('dropwizard/test-config.yaml'))
private static ProviderInfo serviceProvider
private static Pact<RequestResponseInteraction> testConsumerPact
private static ConsumerInfo consumer
@BeforeClass
static void setupProvider() {
serviceProvider = new ProviderInfo('Dropwizard App')
serviceProvider.setProtocol('http')
serviceProvider.setHost('localhost')
serviceProvider.setPort(8080)
serviceProvider.setPath('/')
consumer = new ConsumerInfo()
consumer.setName('test_consumer')
consumer.setPactSource(new UrlSource(
ReadmeExamplePactJVMProviderJUnitTest.getResource('/pacts/zoo_app-animal_service.json').toString()))
testConsumerPact = DefaultPactReader.INSTANCE.loadPact(consumer.getPactSource()) as Pact<RequestResponseInteraction>
}
@Test
void runConsumerPacts() {
// grab the first interaction from the pact with consumer
Interaction interaction = testConsumerPact.interactions.get(0)
// setup the verifier
ProviderVerifier verifier = setupVerifier(interaction, serviceProvider, consumer)
// setup any provider state
// setup the client and interaction to fire against the provider
ProviderClient client = new ProviderClient(serviceProvider, new HttpClientFactory())
Map<String, Object> failures = new HashMap<>()
verifier.verifyResponseFromProvider(serviceProvider, interaction, interaction.getDescription(), failures, client)
// normally assert all good, but in this example it will fail
assertThat(failures, is(not(empty())))
verifier.displayFailures(failures)
}
private ProviderVerifier setupVerifier(Interaction interaction, ProviderInfo provider, ConsumerInfo consumer) {
ProviderVerifier verifier = new ProviderVerifier()
verifier.initialiseReporters(provider)
verifier.reportVerificationForConsumer(consumer, provider, new UrlSource('http://example.example'))
if (!interaction.getProviderStates().isEmpty()) {
for (ProviderState providerState: interaction.getProviderStates()) {
verifier.reportStateForInteraction(providerState.getName(), provider, consumer, true)
}
}
verifier.reportInteractionDescription(interaction)
return verifier
}
}
```
### An example of running provider verification with spock
This example uses groovy and spock to run the provider verification.
Again the provider service is a DropWizard application, and is using the DropwizardAppRule to startup the service.
This example runs all interactions using spocks Unroll feature
```groovy
class ReadmeExamplePactJVMProviderSpockSpec extends Specification {
@ClassRule @Shared
TestRule startServiceRule = new DropwizardAppRule<DropwizardConfiguration>(TestDropwizardApplication,
ResourceHelpers.resourceFilePath('dropwizard/test-config.yaml'))
@Shared
ProviderInfo serviceProvider
ProviderVerifier verifier
def setupSpec() {
serviceProvider = new ProviderInfo('Dropwizard App')
serviceProvider.protocol = 'http'
serviceProvider.host = 'localhost'
serviceProvider.port = 8080
serviceProvider.path = '/'
serviceProvider.hasPactWith('zoo_app') { consumer ->
consumer.pactSource = new FileSource(new File(ResourceHelpers.resourceFilePath('pacts/zoo_app-animal_service.json')))
}
}
def setup() {
verifier = new ProviderVerifier()
}
def cleanup() {
// cleanup provider state
// ie. db.truncateAllTables()
}
def cleanupSpec() {
// cleanup provider
}
@Unroll
def "Provider Pact - With Consumer #consumer"() {
expect:
!verifyConsumerPact(consumer).empty
where:
consumer << serviceProvider.consumers
}
private Map verifyConsumerPact(ConsumerInfo consumer) {
Map failures = [:]
verifier.initialiseReporters(serviceProvider)
verifier.runVerificationForConsumer(failures, serviceProvider, consumer)
if (!failures.empty) {
verifier.displayFailures(failures)
}
failures
}
}
```
package au.com.dius.pact.provider.reporters
import arrow.core.Either
import au.com.dius.pact.core.matchers.BodyTypeMismatch
import au.com.dius.pact.core.matchers.HeaderMismatch
import au.com.dius.pact.core.model.BasePact
import au.com.dius.pact.core.model.Interaction
import au.com.dius.pact.core.model.Pact
import au.com.dius.pact.core.model.PactSource
import au.com.dius.pact.core.model.UrlPactSource
import au.com.dius.pact.core.support.hasProperty
import au.com.dius.pact.core.support.property
import au.com.dius.pact.provider.BodyComparisonResult
import au.com.dius.pact.provider.IConsumerInfo
import au.com.dius.pact.provider.IProviderInfo
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.io.PrintWriter
import java.time.ZonedDateTime
/**
* Pact verifier reporter that displays the results of the verification in a markdown document
*/
class MarkdownReporter(
var name: String,
override var reportDir: File?,
override var ext: String
) : VerifierReporter {
constructor(name: String, reportDir: File?) : this(name, reportDir, ".md")
override lateinit var reportFile: File
init {
if (reportDir == null) {
reportDir = File(System.getProperty("user.dir"))
}
reportFile = File(reportDir, "$name$ext")
}
private var pw: PrintWriter? = null
override fun initialise(provider: IProviderInfo) {
if (pw != null) {
pw!!.close()
}
reportDir!!.mkdirs()
reportFile = File(reportDir, provider.name + ext)
pw = PrintWriter(BufferedWriter(FileWriter(reportFile, true)))
pw!!.write("""
# ${provider.name}
| Description | Value |
| -------------- | ----- |
| Date Generated | ${ZonedDateTime.now()} |
| Pact Version | ${BasePact.lookupVersion()} |
""".trimIndent())
}
override fun finaliseReport() {
pw!!.close()
}
override fun reportVerificationForConsumer(consumer: IConsumerInfo, provider: IProviderInfo, tag: String?) {
val report = StringBuilder("## Verifying a pact between _${consumer.name}_ and _${provider.name}_")
if (tag != null) {
report.append(" for tag $tag")
}
report.append("\n\n")
pw!!.write(report.toString())
}
override fun verifyConsumerFromUrl(pactUrl: UrlPactSource, consumer: IConsumerInfo) {
pw!!.write("From `${pactUrl.description()}`
\n")
}
override fun verifyConsumerFromFile(pactFile: PactSource, consumer: IConsumerInfo) {
pw!!.write("From `${pactFile.description()}`
\n")
}
override fun pactLoadFailureForConsumer(consumer: IConsumerInfo, message: String) { }
override fun warnProviderHasNoConsumers(provider: IProviderInfo) { }
override fun warnPactFileHasNoInteractions(pact: Pact) { }
override fun interactionDescription(interaction: Interaction) {
pw!!.write("${interaction.description} \n")
}
override fun stateForInteraction(state: String, provider: IProviderInfo, consumer: IConsumerInfo, isSetup: Boolean) {
pw!!.write("Given **$state** \n")
}
override fun warnStateChangeIgnored(state: String, provider: IProviderInfo, consumer: IConsumerInfo) {
pw!!.write(" WARNING: State Change ignored as " +
"there is no stateChange URL \n")
}
override fun stateChangeRequestFailedWithException(
state: String,
provider: IProviderInfo,
consumer: IConsumerInfo,
isSetup: Boolean,
e: Exception,
printStackTrace: Boolean
) {
reportFile.printWriter().use {
it.write(" State Change Request Failed - ${e.message}" +
"\n\n```\n")
e.printStackTrace(it)
it.write("\n```\n\n")
}
}
override fun stateChangeRequestFailed(state: String, provider: IProviderInfo, isSetup: Boolean, httpStatus: String) {
pw!!.write(" State Change Request Failed - $httpStatus" +
" \n")
}
override fun warnStateChangeIgnoredDueToInvalidUrl(
state: String,
provider: IProviderInfo,
isSetup: Boolean,
stateChangeHandler: Any
) {
pw!!.write(" WARNING: State Change ignored as " +
"there is no stateChange URL, received `$stateChangeHandler` \n")
}
override fun requestFailed(
provider: IProviderInfo,
interaction: Interaction,
interactionMessage: String,
e: Exception,
printStackTrace: Boolean
) {
pw!!.write(" Request Failed - ${e.message}\n\n```\n")
e.printStackTrace(pw!!)
pw!!.write("\n```\n\n")
}
override fun returnsAResponseWhich() {
pw!!.write(" returns a response which \n")
}
override fun statusComparisonOk(status: Int) {
pw!!.write(" has status code **$status** " +
"(OK) \n")
}
override fun statusComparisonFailed(status: Int, comparison: Any) {
pw!!.write(" has status code **$status** " +
"(FAILED)\n\n```\n")
if (comparison.hasProperty("message")) {
pw!!.write(comparison.property("message")?.get(comparison).toString())
} else {
pw!!.write(comparison.toString())
}
pw!!.write("\n```\n\n")
}
override fun includesHeaders() {
pw!!.write(" includes headers \n")
}
override fun headerComparisonOk(key: String, value: List) {
pw!!.write(" \"**$key**\" with value \"**$value**\" " +
"(OK) \n")
}
override fun headerComparisonFailed(key: String, value: List, comparison: Any) {
pw!!.write(" \"**$key**\" with value \"**$value**\" " +
"(FAILED) \n\n```\n")
when (comparison) {
is List<*> -> comparison.forEach {
when (it) {
is HeaderMismatch -> pw!!.write(it.mismatch)
else -> pw!!.write(it.toString())
}
}
else -> pw!!.write(comparison.toString())
}
pw!!.write("\n```\n\n")
}
override fun bodyComparisonOk() {
pw!!.write(" has a matching body (OK) \n")
}
override fun bodyComparisonFailed(comparison: Any) {
pw!!.write(" has a matching body (FAILED) \n\n")
// val property = comparison.property("comparison")?.get(comparison)
// when {
// comparison is String -> pw!!.write("|\$|$comparison|\n")
// property is Map<*, *> -> pw!!.write(property.map {
// val mismatches = (it.value as List