Skip to content

Commit

Permalink
fix: small refactor + fixed missed pending status with MockMVC support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed May 18, 2020
1 parent 7d692e1 commit 1fd0360
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ sealed class TestResult {

abstract fun toBoolean(): Boolean
abstract fun merge(result: TestResult): TestResult

companion object {
fun fromBoolean(result: Boolean) = if (result) Ok else Failed()
}
}

sealed class Latest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class PactVerificationTaskSpec extends Specification {
1 * verifier.verifyProviderReturnResult(_) >> [new VerificationResult.Failed([], '', '', [
new VerificationFailureType.MismatchFailure(new StatusMismatch(200, 400),
new RequestResponseInteraction('Test'), new RequestResponsePact(provider, consumer))
], false) ]
], false, null) ]
}

def 'does not raise an exception if the verification passed'() {
Expand All @@ -71,6 +71,6 @@ class PactVerificationTaskSpec extends Specification {

then:
noExceptionThrown()
1 * verifier.verifyProviderReturnResult(_) >> [new VerificationResult.Failed([], '', '', [], true) ]
1 * verifier.verifyProviderReturnResult(_) >> [new VerificationResult.Failed([], '', '', [], true, null) ]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ open class InteractionRunner<I>(
testResult = VerificationResult.Failed(listOf(mapOf("message" to "Request to provider failed with an exception",
"exception" to e, "interactionId" to interaction.interactionId)),
"Request to provider failed with an exception", description.displayName,
listOf(VerificationFailureType.ExceptionFailure(e)), pending)
listOf(VerificationFailureType.ExceptionFailure(e)), pending, interaction.interactionId)
} finally {
if (pact is FilteredPact) {
testResultAccumulator.updateTestResult(pact.pact, interaction, testResult.toTestResult(), pactSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ data class PactVerificationContext @JvmOverloads constructor(
listOf(VerificationResult.Failed(listOf(mapOf("message" to "Request to provider failed with an exception",
"exception" to e, "interactionId" to interaction.interactionId)),
"Request to provider failed with an exception", interactionMessage,
listOf(VerificationFailureType.ExceptionFailure(e)), consumer.pending))
listOf(VerificationFailureType.ExceptionFailure(e)), consumer.pending, interaction.interactionId))
}
} else {
return listOf(verifier!!.verifyResponseByInvokingProviderMethods(providerInfo, consumer, interaction,
Expand Down Expand Up @@ -316,7 +316,8 @@ class PactVerificationStateChangeExtension(
testContext.testExecutionResult.add(VerificationResult.Failed(description = "Provider state change teardown callback failed",
results = listOf(mapOf("exception" to e)),
failures = listOf(VerificationFailureType.StateChangeFailure(StateChangeResult(Err(e)))),
pending = pending
pending = pending,
interactionId = interaction.interactionId
))
if (!pending) {
throw AssertionError("Provider state change callback failed", e)
Expand All @@ -337,7 +338,8 @@ class PactVerificationStateChangeExtension(
testContext.testExecutionResult.add(VerificationResult.Failed(description = "Provider state change teardown callback failed",
results = listOf(mapOf("exception" to e)),
failures = listOf(VerificationFailureType.StateChangeFailure(StateChangeResult(Err(e)))),
pending = pending
pending = pending,
interactionId = interaction.interactionId
))
if (!pending) {
throw AssertionError("Provider state change callback failed", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ open class MvcProviderVerifier(private val debugRequestResponse: Boolean = false
interaction: RequestResponseInteraction,
interactionMessage: String,
failures: MutableMap<String, Any>,
mockMvc: MockMvc
mockMvc: MockMvc,
context: Map<String, Any>,
pending: Boolean
): VerificationResult {
return try {
val request = interaction.request
Expand All @@ -67,13 +69,13 @@ open class MvcProviderVerifier(private val debugRequestResponse: Boolean = false
return VerificationResult.Failed(listOf(mapOf("message" to "Request to provider method failed with an exception",
"exception" to e, "interactionId" to interaction.interactionId)),
"Request to provider method failed with an exception", interactionMessage,
listOf(VerificationFailureType.ExceptionFailure(e)))
listOf(VerificationFailureType.ExceptionFailure(e)), pending, interaction.interactionId)
}
}

fun executeMockMvcRequest(mockMvc: MockMvc, request: Request, provider: ProviderInfo): MvcResult {
val body = request.body
val requestBuilder = if (body != null && body.isPresent()) {
val requestBuilder = if (body.isPresent()) {
if (request.isMultipartFileUpload()) {
val multipart = MimeMultipart(ByteArrayDataSource(body.unwrap(), request.contentTypeHeader()))
val multipartRequest = MockMvcRequestBuilders.fileUpload(requestUriString(request))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package au.com.dius.pact.provider.spring.target
import au.com.dius.pact.core.model.Interaction
import au.com.dius.pact.core.model.PactSource
import au.com.dius.pact.core.model.RequestResponseInteraction
import au.com.dius.pact.provider.ConsumerInfo
import au.com.dius.pact.provider.IConsumerInfo
import au.com.dius.pact.provider.IProviderInfo
import au.com.dius.pact.provider.IProviderVerifier
Expand Down Expand Up @@ -63,7 +62,7 @@ class MockMvcTarget @JvmOverloads constructor(
context: Map<String, Any>
) {
val provider = getProviderInfo(source)
val consumer = ConsumerInfo(consumerName)
val consumer = consumerInfo(consumerName, source)
provider.verificationType = PactVerification.ANNOTATED_METHOD

val mockMvc = buildMockMvc()
Expand All @@ -74,7 +73,7 @@ class MockMvcTarget @JvmOverloads constructor(

val results = 1.rangeTo(runTimes).map {
verifier.verifyResponseFromProvider(provider, interaction as RequestResponseInteraction, interaction.description,
failures, mockMvc)
failures, mockMvc, context, consumer.pending)
}
val result = results.fold(VerificationResult.Ok) { acc: VerificationResult, r -> acc.merge(r) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ open class ProviderVerifier @JvmOverloads constructor (
return VerificationResult.Failed(listOf(mapOf("message" to "Request to provider method failed with an exception",
"exception" to e, "interactionId" to interaction.interactionId)),
"Request to provider method failed with an exception", interactionMessage,
listOf(VerificationFailureType.ExceptionFailure(e)))
listOf(VerificationFailureType.ExceptionFailure(e)), consumer.pending, interaction.interactionId)
}
}

Expand All @@ -354,14 +354,14 @@ open class ProviderVerifier @JvmOverloads constructor (
VerificationResult.Failed(listOf(comparison.error.toMap() +
mapOf("interactionId" to interactionId, "type" to "body")),
"Body had differences", comparisonDescription,
listOf(VerificationFailureType.MismatchFailure(comparison.error)), pending)
listOf(VerificationFailureType.MismatchFailure(comparison.error)), pending, interactionId)
}
is Ok -> {
failures["$comparisonDescription has a matching body"] = comparison.value
VerificationResult.Failed(listOf(comparison.value.mismatches +
mapOf("interactionId" to interactionId, "type" to "body")),
"Body had differences", comparisonDescription, comparison.value.mismatches.values.flatten()
.map { VerificationFailureType.MismatchFailure(it) }, pending)
.map { VerificationFailureType.MismatchFailure(it) }, pending, interactionId)
}
}
}
Expand Down Expand Up @@ -427,7 +427,7 @@ open class ProviderVerifier @JvmOverloads constructor (
} else {
reporters.forEach { it.includesMetadata() }
var result: VerificationResult = VerificationResult.Failed(emptyList(), "Metadata had differences",
comparisonDescription, pending = pending)
comparisonDescription, pending = pending, interactionId = interactionId)
comparison.forEach { (key, metadataComparison) ->
val expectedValue = expectedMetadata[key]
if (metadataComparison.isEmpty()) {
Expand All @@ -439,7 +439,9 @@ open class ProviderVerifier @JvmOverloads constructor (
result = result.merge(VerificationResult.Failed(listOf(mapOf(key to metadataComparison,
"interactionId" to interactionId, "type" to "metadata")),
verificationDescription = comparisonDescription,
failures = metadataComparison.map { VerificationFailureType.MismatchFailure(it) }, pending = pending))
failures = metadataComparison.map { VerificationFailureType.MismatchFailure(it) }, pending = pending,
interactionId = interactionId
))
}
}
result
Expand Down Expand Up @@ -505,7 +507,7 @@ open class ProviderVerifier @JvmOverloads constructor (
"interactionId" to interaction.interactionId)), "State change request failed",
stateChangeResult.message,
listOf(VerificationFailureType.StateChangeFailure(stateChangeResult)),
consumer.pending
consumer.pending, interaction.interactionId
)
}
}
Expand Down Expand Up @@ -557,7 +559,7 @@ open class ProviderVerifier @JvmOverloads constructor (
failures["$comparisonDescription has status code $status"] = mismatch.description()
VerificationResult.Failed(listOf(mismatch.toMap() + mapOf("interactionId" to interactionId,
"type" to "status")), "Response status did not match", comparisonDescription,
listOf(VerificationFailureType.MismatchFailure(mismatch)), pending)
listOf(VerificationFailureType.MismatchFailure(mismatch)), pending, interactionId)
}
}

Expand Down Expand Up @@ -585,7 +587,7 @@ open class ProviderVerifier @JvmOverloads constructor (
result = result.merge(VerificationResult.Failed(headerComparison.map { it.toMap() },
"Headers had differences", comparisonDescription, headerComparison.map {
VerificationFailureType.MismatchFailure(it)
}, pending
}, pending, interactionId
))
}
}
Expand Down Expand Up @@ -625,7 +627,7 @@ open class ProviderVerifier @JvmOverloads constructor (
VerificationResult.Failed(listOf(mapOf("message" to "Request to provider failed with an exception",
"exception" to e, "interactionId" to interaction.interactionId)),
"Request to provider method failed with an exception", interactionMessage,
listOf(VerificationFailureType.ExceptionFailure(e)), pending)
listOf(VerificationFailureType.ExceptionFailure(e)), pending, interaction.interactionId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ sealed class VerificationResult {
val description: String = "",
val verificationDescription: String = "",
val failures: List<VerificationFailureType> = emptyList(),
val pending: Boolean = false
val pending: Boolean = false,
val interactionId: String? = null
) : VerificationResult() {
override fun merge(result: VerificationResult) = when (result) {
is Ok -> this
Expand All @@ -113,7 +114,8 @@ sealed class VerificationResult {
"$description, ${result.description}"
description.isNotEmpty() -> description
else -> result.description
}, verificationDescription, failures + result.failures, pending && result.pending)
}, verificationDescription, failures + result.failures, pending && result.pending,
interactionId ?: result.interactionId)
}

override fun toTestResult() = TestResult.Failed(results, description)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ class VerificationResultSpec extends Specification {
}

private static VerificationResult.Failed failed(List<LinkedHashMap<String, String>> details, String s, pending = false) {
new VerificationResult.Failed(details, s, '', [], pending)
new VerificationResult.Failed(details, s, '', [], pending, null)
}
}

0 comments on commit 1fd0360

Please sign in to comment.