Skip to content

Commit 5b367c9

Browse files
author
Ronald Holshausen
committedFeb 8, 2020
feat: Allow just the changed pact specified in the webhook to be run (JUnit 5) #998
1 parent 2604f29 commit 5b367c9

File tree

4 files changed

+133
-33
lines changed

4 files changed

+133
-33
lines changed
 

‎provider/pact-jvm-provider-junit/src/main/kotlin/au/com/dius/pact/provider/junit/PactRunner.kt

+3-30
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package au.com.dius.pact.provider.junit
33
import au.com.dius.pact.core.model.Interaction
44
import au.com.dius.pact.core.model.Pact
55
import au.com.dius.pact.core.support.expressions.SystemPropertyResolver
6-
import au.com.dius.pact.core.support.isNotEmpty
7-
import au.com.dius.pact.provider.ProviderVerifier
6+
import au.com.dius.pact.provider.junit.JUnitProviderTestSupport.checkForOverriddenPactUrl
87
import au.com.dius.pact.provider.junit.JUnitProviderTestSupport.filterPactsByAnnotations
98
import au.com.dius.pact.provider.junit.loader.NoPactsFoundException
10-
import au.com.dius.pact.provider.junit.loader.OverrideablePactLoader
119
import au.com.dius.pact.provider.junit.loader.PactBroker
1210
import au.com.dius.pact.provider.junit.loader.PactFolder
1311
import au.com.dius.pact.provider.junit.loader.PactLoader
@@ -170,7 +168,8 @@ open class PactRunner<I>(clazz: Class<*>) : ParentRunner<InteractionRunner<I>>(c
170168
.getConstructor(annotation.annotationClass.java).newInstance(annotation)
171169
}
172170

173-
checkForOverriddenPactUrl(clazz, loader)
171+
checkForOverriddenPactUrl(loader, clazz.getAnnotation(AllowOverridePactUrl::class.java),
172+
clazz.getAnnotation(Consumer::class.java))
174173

175174
return loader
176175
} catch (e: ReflectiveOperationException) {
@@ -179,32 +178,6 @@ open class PactRunner<I>(clazz: Class<*>) : ParentRunner<InteractionRunner<I>>(c
179178
}
180179
}
181180

182-
private fun checkForOverriddenPactUrl(clazz: TestClass, loader: PactLoader?) {
183-
val overridePactUrl: AllowOverridePactUrl? = clazz.getAnnotation(AllowOverridePactUrl::class.java)
184-
var pactUrl = System.getProperty(ProviderVerifier.PACT_FILTER_PACTURL)
185-
if (pactUrl.isNullOrEmpty()) {
186-
pactUrl = System.getenv(ProviderVerifier.PACT_FILTER_PACTURL)
187-
}
188-
189-
if (loader is OverrideablePactLoader && overridePactUrl != null && pactUrl.isNotEmpty()) {
190-
val consumer = clazz.getAnnotation(Consumer::class.java)
191-
var consumerProperty = System.getProperty(ProviderVerifier.PACT_FILTER_CONSUMERS)
192-
if (consumerProperty.isNullOrEmpty()) {
193-
consumerProperty = System.getenv(ProviderVerifier.PACT_FILTER_CONSUMERS)
194-
}
195-
when {
196-
consumerProperty.isNotEmpty() -> loader.overridePactUrl(pactUrl, consumerProperty)
197-
consumer != null -> loader.overridePactUrl(pactUrl, consumer.value)
198-
else -> {
199-
logger.warn {
200-
"The property ${ProviderVerifier.PACT_FILTER_PACTURL} has been set, but no consumer filter" +
201-
" or @Consumer annotation has been provided, Ignoring"
202-
}
203-
}
204-
}
205-
}
206-
}
207-
208181
companion object : KLogging() {
209182
const val WARNING_ON_IGNORED_IOERROR = """
210183
---------------------------------------------------------------------------
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,98 @@
11
package au.com.dius.pact.provider.junit
22

3+
import au.com.dius.pact.provider.ProviderVerifier
4+
import au.com.dius.pact.provider.junit.loader.OverrideablePactLoader
5+
import au.com.dius.pact.provider.junit.loader.PactLoader
36
import spock.lang.Specification
7+
import spock.util.environment.RestoreSystemProperties
48

9+
@AllowOverridePactUrl
10+
@Consumer('Test')
511
class JUnitProviderTestSupportSpec extends Specification {
612

13+
private AllowOverridePactUrl allowOverridePactUrl
14+
private Consumer consumer
15+
16+
def setup() {
17+
allowOverridePactUrl = JUnitProviderTestSupportSpec.getAnnotation(AllowOverridePactUrl)
18+
consumer = JUnitProviderTestSupportSpec.getAnnotation(Consumer)
19+
}
20+
721
def 'exceptionMessage should handle an exception with a null message'() {
822
expect:
923
JUnitProviderTestSupport.exceptionMessage(new NullPointerException(), 5) == 'null\n'
1024
}
1125

26+
def 'checkForOverriddenPactUrl - does nothing if there is no pact loader'() {
27+
when:
28+
JUnitProviderTestSupport.checkForOverriddenPactUrl(null, allowOverridePactUrl, null)
29+
30+
then:
31+
noExceptionThrown()
32+
}
33+
34+
def 'checkForOverriddenPactUrl - does nothing with a normal pact loader'() {
35+
given:
36+
PactLoader loader = Mock(PactLoader)
37+
38+
when:
39+
JUnitProviderTestSupport.checkForOverriddenPactUrl(loader, allowOverridePactUrl, null)
40+
41+
then:
42+
0 * loader._
43+
}
44+
45+
@RestoreSystemProperties
46+
def 'checkForOverriddenPactUrl - does nothing if there is no overridden pact annotation'() {
47+
given:
48+
PactLoader loader = Mock(OverrideablePactLoader)
49+
System.setProperty(ProviderVerifier.PACT_FILTER_PACTURL, 'http://overridden.url')
50+
51+
when:
52+
JUnitProviderTestSupport.checkForOverriddenPactUrl(loader, null, null)
53+
54+
then:
55+
0 * loader._
56+
}
57+
58+
@RestoreSystemProperties
59+
def 'checkForOverriddenPactUrl - does nothing if there is no consumer filter'() {
60+
given:
61+
PactLoader loader = Mock(OverrideablePactLoader)
62+
System.setProperty(ProviderVerifier.PACT_FILTER_PACTURL, 'http://overridden.url')
63+
64+
when:
65+
JUnitProviderTestSupport.checkForOverriddenPactUrl(loader, allowOverridePactUrl, null)
66+
67+
then:
68+
0 * loader._
69+
}
70+
71+
@RestoreSystemProperties
72+
def 'checkForOverriddenPactUrl - uses the consumer annotation'() {
73+
given:
74+
PactLoader loader = Mock(OverrideablePactLoader)
75+
System.setProperty(ProviderVerifier.PACT_FILTER_PACTURL, 'http://overridden.url')
76+
77+
when:
78+
JUnitProviderTestSupport.checkForOverriddenPactUrl(loader, allowOverridePactUrl, consumer)
79+
80+
then:
81+
1 * loader.overridePactUrl('http://overridden.url', 'Test')
82+
}
83+
84+
@RestoreSystemProperties
85+
def 'checkForOverriddenPactUrl - falls back to the consumer filter property'() {
86+
given:
87+
PactLoader loader = Mock(OverrideablePactLoader)
88+
System.setProperty(ProviderVerifier.PACT_FILTER_PACTURL, 'http://overridden.url')
89+
System.setProperty(ProviderVerifier.PACT_FILTER_CONSUMERS, 'Bob')
90+
91+
when:
92+
JUnitProviderTestSupport.checkForOverriddenPactUrl(loader, allowOverridePactUrl, null)
93+
94+
then:
95+
1 * loader.overridePactUrl('http://overridden.url', 'Bob')
96+
}
97+
1298
}

‎provider/pact-jvm-provider-junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactJUnit5VerificationProvider.kt

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ import au.com.dius.pact.core.model.Pact
55
import au.com.dius.pact.core.model.ProviderState
66
import au.com.dius.pact.core.model.RequestResponseInteraction
77
import au.com.dius.pact.core.pactbroker.TestResult
8+
import au.com.dius.pact.core.support.expressions.SystemPropertyResolver
9+
import au.com.dius.pact.core.support.expressions.ValueResolver
810
import au.com.dius.pact.provider.ConsumerInfo
911
import au.com.dius.pact.provider.DefaultTestResultAccumulator
1012
import au.com.dius.pact.provider.IProviderVerifier
1113
import au.com.dius.pact.provider.PactVerification
1214
import au.com.dius.pact.provider.ProviderInfo
1315
import au.com.dius.pact.provider.ProviderVerifier
1416
import au.com.dius.pact.provider.TestResultAccumulator
17+
import au.com.dius.pact.provider.junit.AllowOverridePactUrl
1518
import au.com.dius.pact.provider.junit.Consumer
1619
import au.com.dius.pact.provider.junit.JUnitProviderTestSupport
20+
import au.com.dius.pact.provider.junit.JUnitProviderTestSupport.checkForOverriddenPactUrl
1721
import au.com.dius.pact.provider.junit.JUnitProviderTestSupport.filterPactsByAnnotations
1822
import au.com.dius.pact.provider.junit.MissingStateChangeMethod
1923
import au.com.dius.pact.provider.junit.Provider
@@ -23,8 +27,6 @@ import au.com.dius.pact.provider.junit.VerificationReports
2327
import au.com.dius.pact.provider.junit.loader.PactLoader
2428
import au.com.dius.pact.provider.junit.loader.PactSource
2529
import au.com.dius.pact.provider.reporters.ReporterManager
26-
import au.com.dius.pact.core.support.expressions.SystemPropertyResolver
27-
import au.com.dius.pact.core.support.expressions.ValueResolver
2830
import mu.KLogging
2931
import org.apache.http.HttpRequest
3032
import org.junit.jupiter.api.extension.AfterTestExecutionCallback
@@ -428,6 +430,11 @@ class PactVerificationInvocationContextProvider : TestTemplateInvocationContextP
428430
it.annotationClass.findAnnotation<PactSource>()!!.value.java
429431
.getConstructor(it.annotationClass.java).newInstance(it)
430432
}
433+
}.map {
434+
checkForOverriddenPactUrl(it,
435+
context.requiredTestClass.getAnnotation(AllowOverridePactUrl::class.java),
436+
context.requiredTestClass.getAnnotation(Consumer::class.java))
437+
it
431438
}
432439
}
433440

‎provider/pact-jvm-provider/src/main/kotlin/au/com/dius/pact/provider/junit/JUnitProviderTestSupport.kt

+35-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ package au.com.dius.pact.provider.junit
33
import au.com.dius.pact.core.model.FilteredPact
44
import au.com.dius.pact.core.model.Interaction
55
import au.com.dius.pact.core.model.Pact
6+
import au.com.dius.pact.core.support.isNotEmpty
7+
import au.com.dius.pact.provider.ProviderVerifier
8+
import au.com.dius.pact.provider.junit.loader.OverrideablePactLoader
69
import au.com.dius.pact.provider.junit.loader.PactFilter
10+
import au.com.dius.pact.provider.junit.loader.PactLoader
11+
import mu.KLogging
712
import org.apache.commons.lang3.StringUtils
813
import org.apache.commons.lang3.exception.ExceptionUtils
914
import java.util.function.Predicate
1015

11-
object JUnitProviderTestSupport {
16+
object JUnitProviderTestSupport : KLogging() {
1217
fun <I> filterPactsByAnnotations(pacts: List<Pact<I>>, testClass: Class<*>): List<Pact<I>> where I : Interaction {
1318
val pactFilterValues = testClass.getAnnotation(PactFilter::class.java)?.value
1419
return if (pactFilterValues != null && pactFilterValues.any { it.isNotEmpty() }) {
@@ -79,4 +84,33 @@ object JUnitProviderTestSupport {
7984
private fun mapToString(comparison: Map<String, *>): String {
8085
return comparison.entries.joinToString(System.lineSeparator()) { (key, value) -> "$key -> $value" }
8186
}
87+
88+
@JvmStatic
89+
fun checkForOverriddenPactUrl(
90+
loader: PactLoader?,
91+
overridePactUrl: AllowOverridePactUrl?,
92+
consumer: Consumer?
93+
) {
94+
var pactUrl = System.getProperty(ProviderVerifier.PACT_FILTER_PACTURL)
95+
if (pactUrl.isNullOrEmpty()) {
96+
pactUrl = System.getenv(ProviderVerifier.PACT_FILTER_PACTURL)
97+
}
98+
99+
if (loader is OverrideablePactLoader && overridePactUrl != null && pactUrl.isNotEmpty()) {
100+
var consumerProperty = System.getProperty(ProviderVerifier.PACT_FILTER_CONSUMERS)
101+
if (consumerProperty.isNullOrEmpty()) {
102+
consumerProperty = System.getenv(ProviderVerifier.PACT_FILTER_CONSUMERS)
103+
}
104+
when {
105+
consumerProperty.isNotEmpty() -> loader.overridePactUrl(pactUrl, consumerProperty)
106+
consumer != null -> loader.overridePactUrl(pactUrl, consumer.value)
107+
else -> {
108+
logger.warn {
109+
"The property ${ProviderVerifier.PACT_FILTER_PACTURL} has been set, but no consumer filter" +
110+
" or @Consumer annotation has been provided, Ignoring"
111+
}
112+
}
113+
}
114+
}
115+
}
82116
}

0 commit comments

Comments
 (0)
Please sign in to comment.