Skip to content

Commit

Permalink
feat: Add interaction description to the verification payload sent to…
Browse files Browse the repository at this point in the history
… the Pact Broker
  • Loading branch information
rholshausen committed Apr 22, 2024
1 parent 2dc16e3 commit 3670bbd
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -869,12 +869,21 @@ open class PactBrokerClient(
jsonObject("interactionId" to mismatches.key, "success" to true)
} else {
val json = jsonObject(
"interactionId" to mismatches.key, "success" to false,
"interactionId" to mismatches.key,
"success" to false,
"mismatches" to jsonArray(values)
)
if (exceptions != null) {
json["exceptions"] = exceptions
}
val interactionDescription = mismatches.value
.firstOrNull { it["interactionDescription"]?.toString().isNotEmpty() }
?.get("interactionDescription")
?.toString()
if (interactionDescription.isNotEmpty()) {
json["interactionDescription"] = interactionDescription
}

json
}
interactionJson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import au.com.dius.pact.core.support.Json
import au.com.dius.pact.core.support.Result
import au.com.dius.pact.core.support.json.JsonParser
import au.com.dius.pact.core.support.json.JsonValue
import groovy.json.JsonOutput
import kotlin.Pair
import kotlin.collections.MapsKt
import spock.lang.Issue
Expand Down Expand Up @@ -318,6 +319,65 @@ class PactBrokerClientSpec extends Specification {
'the links have different case' | ['pb:Publish-Verification-Results': [HREF: 'URL']] | Result.Ok.simpleName
}

def 'publishing verification results with an exception should support any type of exception'() {
given:
def halClient = Mock(IHalClient)
PactBrokerClient client = Spy(PactBrokerClient, constructorArgs: ['baseUrl']) {
newHalClient() >> halClient
}
def uploadResult = new Result.Ok(true)
halClient.postJson(_, _) >> uploadResult
def result = new TestResult.Failed([
[exception: new AssertionError('boom')]
], 'Failed')
def doc = ['pb:publish-verification-results': [href: '']]

expect:
client.publishVerificationResults(doc, result, '0', null) == uploadResult
}

def 'publishing verification results includes the interaction description if it is set'() {
given:
def halClient = Mock(IHalClient)
PactBrokerClient client = Spy(PactBrokerClient, constructorArgs: ['baseUrl']) {
newHalClient() >> halClient
}
def uploadResult = new Result.Ok(true)
def result = new TestResult.Failed([
[
exception: new AssertionError('boom'),
interactionDescription: 'interaction description'
]
], 'Failed')
def doc = ['pb:publish-verification-results': [href: '']]
def expectedJson = JsonOutput.toJson([
providerApplicationVersion: '0',
success: false,
testResults: [
[
exceptions: [[exceptionClass: 'java.lang.AssertionError', message: 'boom']],
interactionDescription: 'interaction description',
interactionId: null,
mismatches: [],
success: false
]
],
verifiedBy: [
implementation: 'Pact-JVM',
version: ''
]
])
def actualJson

when:
def publishResult = client.publishVerificationResults(doc, result, '0', null)

then:
1 * halClient.postJson(_, _) >> { args -> actualJson = args[1]; uploadResult }
publishResult == uploadResult
actualJson == expectedJson
}

def 'when fetching a pact, return the results as a Map'() {
given:
def halClient = Mock(IHalClient)
Expand Down Expand Up @@ -345,23 +405,6 @@ class PactBrokerClientSpec extends Specification {
result.pactFile == Json.INSTANCE.toJson([a: 'a', b: 100, _links: [:], c: [true, 10.2, 'test']])
}

def 'publishing verification results with an exception should support any type of exception'() {
given:
def halClient = Mock(IHalClient)
PactBrokerClient client = Spy(PactBrokerClient, constructorArgs: ['baseUrl']) {
newHalClient() >> halClient
}
def uploadResult = new Result.Ok(true)
halClient.postJson(_, _) >> uploadResult
def result = new TestResult.Failed([
[exception: new AssertionError('boom')]
], 'Failed')
def doc = ['pb:publish-verification-results': [href: '']]

expect:
client.publishVerificationResults(doc, result, '0', null) == uploadResult
}

@SuppressWarnings('LineLength')
def 'fetching pacts with selectors uses the provider-pacts-for-verification link and returns a list of results'() {
given:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ open class InteractionRunner(
} catch (e: Throwable) {
testResult = VerificationResult.Failed("Request to provider failed with an exception", description.displayName,
mapOf(interaction.interactionId.orEmpty() to
listOf(VerificationFailureType.ExceptionFailure("Request to provider failed with an exception", e))),
listOf(VerificationFailureType.ExceptionFailure("Request to provider failed with an exception",
e, interaction))),
pending)
} finally {
val updateTestResult = testResultAccumulator.updateTestResult(if (pact is FilteredPact) pact.pact else pact, interaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ data class PactVerificationContext @JvmOverloads constructor(
"Request to provider failed with an exception", interactionMessage,
mapOf(
interaction.interactionId.orEmpty() to
listOf(VerificationFailureType.ExceptionFailure("Request to provider failed with an exception", e))
listOf(VerificationFailureType.ExceptionFailure("Request to provider failed with an exception",
e, interaction))
),
consumer.pending
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ open class PactVerificationExtension(
val failure = VerificationResult.Failed("Test method has failed with an exception: ${e.message}",
failures = mapOf(
interaction.interactionId.orEmpty() to
listOf(VerificationFailureType.ExceptionFailure("Test method has failed with an exception", e))
listOf(VerificationFailureType.ExceptionFailure("Test method has failed with an exception",
e, interaction))
)
)
testResultAccumulator.updateTestResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class PactVerificationStateChangeExtension(
testContext.testExecutionResult.add(VerificationResult.Failed(
description = "Provider state change callback failed",
failures = mapOf(interaction.interactionId.orEmpty() to
listOf(VerificationFailureType.StateChangeFailure("Provider state change callback failed", error))),
listOf(VerificationFailureType.StateChangeFailure("Provider state change callback failed", error,
testContext.interaction))),
pending = pending
))
if (!pending) {
Expand All @@ -68,7 +69,8 @@ class PactVerificationStateChangeExtension(
testContext.testExecutionResult.add(VerificationResult.Failed(
description = "Provider state change teardown callback failed",
failures = mapOf(interaction.interactionId.orEmpty() to listOf(
VerificationFailureType.StateChangeFailure("Provider state change teardown callback failed", error))),
VerificationFailureType.StateChangeFailure("Provider state change teardown callback failed", error,
testContext.interaction))),
pending = pending
))
if (!pending) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ open class MvcProviderVerifier(private val debugRequestResponse: Boolean = false
}
return VerificationResult.Failed("Request to provider method failed with an exception", interactionMessage,
mapOf(interaction.interactionId.orEmpty() to listOf(
VerificationFailureType.ExceptionFailure("Request to provider method failed with an exception", e))),
VerificationFailureType.ExceptionFailure("Request to provider method failed with an exception",
e, interaction))),
pending)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import au.com.dius.pact.provider.VerificationResult
import groovy.lang.Binding
import groovy.lang.Closure
import groovy.lang.GroovyShell
import io.github.oshai.kotlinlogging.KotlinLogging
import org.apache.commons.lang3.StringUtils
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
Expand All @@ -29,6 +30,8 @@ import javax.mail.internet.ContentDisposition
import javax.mail.internet.MimeMultipart
import javax.mail.util.ByteArrayDataSource

val logger = KotlinLogging.logger {}

class WebFluxProviderVerifier : ProviderVerifier() {

fun verifyResponseFromProvider(
Expand Down Expand Up @@ -61,7 +64,8 @@ class WebFluxProviderVerifier : ProviderVerifier() {
}
return VerificationResult.Failed("Request to provider method failed with an exception", interactionMessage,
mapOf(interaction.interactionId.orEmpty() to listOf(
VerificationFailureType.ExceptionFailure("Request to provider method failed with an exception", e))),
VerificationFailureType.ExceptionFailure("Request to provider method failed with an exception",
e, interaction))),
pending)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class WebFluxTargetSpec extends Specification {

def 'execute the test against router function'() {
given:
def target = new WebFluxTarget()
WebFluxTarget target = new WebFluxTarget()
target.setTestClass(new TestClass(WebFluxTargetSpec), this)
def interaction = new RequestResponseInteraction('Test Interaction')
def handler = Spy(TestHandler)
Expand All @@ -73,7 +73,7 @@ class WebFluxTargetSpec extends Specification {

def 'execute the test against controller'() {
given:
def target = new WebFluxTarget()
WebFluxTarget target = new WebFluxTarget()
target.setTestClass(new TestClass(WebFluxTargetSpec), this)
def interaction = new RequestResponseInteraction('Test Interaction')
def controller = Spy(TestController)
Expand All @@ -88,7 +88,7 @@ class WebFluxTargetSpec extends Specification {

def 'invokes any request filter'() {
given:
def target = new WebFluxTarget()
WebFluxTarget target = new WebFluxTarget()
def testInstance = Spy(TestClassWithFilter)
target.setTestClass(new TestClass(TestClassWithFilter), testInstance)
def interaction = new RequestResponseInteraction('Test Interaction')
Expand Down
Loading

0 comments on commit 3670bbd

Please sign in to comment.