Skip to content

Commit

Permalink
feat: enable filter for interactions with JUnit 4 #1104
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jun 11, 2020
1 parent e1667d9 commit 8bb39c0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,33 +146,46 @@ open class InteractionRunner<I>(
val description = describeChild(interaction)
var testResult: VerificationResult = VerificationResult.Ok
val pending = pact.source is BrokerUrlSource && (pact.source as BrokerUrlSource).result?.pending == true
if (!pending) {
val included = interactionIncluded(interaction)
if (!pending && included) {
notifier.fireTestStarted(description)
} else {
if (!included) {
logger.warn { "Ignoring interaction '${interaction.description}' as it does not match the filter " +
"pact.filter.interaction='${System.getProperty("pact.filter.interaction")}'" }
}
notifier.fireTestIgnored(description)
}
try {
interactionBlock(interaction, pactSource, testContext).evaluate()
notifier.fireTestFinished(description)
} catch (e: Throwable) {
if (!pending) {
notifier.fireTestFailure(Failure(description, e))

if (included) {
try {
interactionBlock(interaction, pactSource, testContext).evaluate()
notifier.fireTestFinished(description)
}
testResult = VerificationResult.Failed(listOf(mapOf("message" to "Request to provider failed with an exception",
"exception" to e)),
"Request to provider failed with an exception", description.displayName,
listOf(VerificationFailureType.ExceptionFailure(e)), pending, interaction.interactionId)
} finally {
if (pact is FilteredPact) {
testResultAccumulator.updateTestResult(pact.pact, interaction, testResult.toTestResult(), pactSource)
} else {
testResultAccumulator.updateTestResult(pact, interaction, testResult.toTestResult(), pactSource)
} catch (e: Throwable) {
if (!pending) {
notifier.fireTestFailure(Failure(description, e))
notifier.fireTestFinished(description)
}
testResult = VerificationResult.Failed(listOf(mapOf("message" to "Request to provider failed with an exception",
"exception" to e)),
"Request to provider failed with an exception", description.displayName,
listOf(VerificationFailureType.ExceptionFailure(e)), pending, interaction.interactionId)
} finally {
if (pact is FilteredPact) {
testResultAccumulator.updateTestResult(pact.pact, interaction, testResult.toTestResult(), pactSource)
} else {
testResultAccumulator.updateTestResult(pact, interaction, testResult.toTestResult(), pactSource)
}
}
}
}
}

private fun interactionIncluded(interaction: Interaction): Boolean {
val interactionFilter = System.getProperty("pact.filter.interaction")
return interactionFilter.isNullOrEmpty() || interaction.description.matches(Regex(interactionFilter))
}

private fun providerVersion(): String {
val version = System.getProperty("pact.provider.version")
return if (version != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import au.com.dius.pact.core.model.Consumer
import au.com.dius.pact.core.model.FilteredPact
import au.com.dius.pact.core.model.Provider
import au.com.dius.pact.core.model.ProviderState
import au.com.dius.pact.model.Request
import au.com.dius.pact.core.model.Request
import au.com.dius.pact.core.model.RequestResponseInteraction
import au.com.dius.pact.core.model.RequestResponsePact
import au.com.dius.pact.model.Response
import au.com.dius.pact.core.model.Response
import au.com.dius.pact.core.model.UnknownPactSource
import au.com.dius.pact.provider.DefaultTestResultAccumulator
Expand Down Expand Up @@ -120,4 +118,29 @@ class InteractionRunnerSpec extends Specification {
then:
0 * testResultAccumulator.verificationReporter.reportResults(_, _, _, _)
}

@RestoreSystemProperties
@SuppressWarnings('ClosureAsLastMethodParameter')
def 'If interaction is excluded via properties than it should be marked as ignored'() {
given:
System.properties.setProperty('pact.filter.interaction', 'interaction1')
def interaction1 = new RequestResponseInteraction('interaction1', [], new Request(), new Response())
def interaction2 = new RequestResponseInteraction('interaction2', [], new Request(), new Response())
def pact = new RequestResponsePact(new Provider(), new Consumer(), [ interaction1, interaction2 ])
def notifier = Mock(RunNotifier)
def testResultAccumulator = DefaultTestResultAccumulator.INSTANCE
testResultAccumulator.verificationReporter = Mock(VerificationReporter) {
publishingResultsDisabled() >> false
}
def runner = new InteractionRunner(clazz, pact, UnknownPactSource.INSTANCE)

when:
runner.run(notifier)

then:
1 * notifier.fireTestStarted({ it.displayName.startsWith('consumer - Upon interaction1') })
0 * notifier.fireTestStarted({ it.displayName.startsWith('consumer - Upon interaction2') })
0 * notifier.fireTestIgnored({ it.displayName.startsWith('consumer - Upon interaction1') })
1 * notifier.fireTestIgnored({ it.displayName.startsWith('consumer - Upon interaction2') })
}
}

0 comments on commit 8bb39c0

Please sign in to comment.