@@ -30,11 +30,19 @@ import au.com.dius.pact.core.support.expressions.DataType
30
30
import au.com.dius.pact.core.support.expressions.ExpressionParser
31
31
import au.com.dius.pact.core.support.isNotEmpty
32
32
import io.github.oshai.kotlinlogging.KLogging
33
- import org.apache.hc.core5.util.ReflectionUtils
34
33
import org.junit.jupiter.api.Disabled
35
34
import org.junit.jupiter.api.Nested
36
35
import org.junit.jupiter.api.TestTemplate
37
- import org.junit.jupiter.api.extension.*
36
+ import org.junit.jupiter.api.extension.AfterAllCallback
37
+ import org.junit.jupiter.api.extension.AfterTestExecutionCallback
38
+ import org.junit.jupiter.api.extension.BeforeAllCallback
39
+ import org.junit.jupiter.api.extension.BeforeTestExecutionCallback
40
+ import org.junit.jupiter.api.extension.Extension
41
+ import org.junit.jupiter.api.extension.ExtensionContext
42
+ import org.junit.jupiter.api.extension.ParameterContext
43
+ import org.junit.jupiter.api.extension.ParameterResolver
44
+ import org.junit.jupiter.api.extension.TestTemplateInvocationContext
45
+ import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider
38
46
import org.junit.platform.commons.support.AnnotationSupport
39
47
import org.junit.platform.commons.support.HierarchyTraversalMode
40
48
import org.junit.platform.commons.support.ReflectionSupport
@@ -45,6 +53,10 @@ import java.util.concurrent.ConcurrentHashMap
45
53
import java.util.stream.Stream
46
54
import kotlin.reflect.full.findAnnotation
47
55
56
+ private const val PACT_DSL_WITH_PROVIDER = " au.com.dius.pact.consumer.dsl.PactDslWithProvider"
57
+ private const val MESSAGE_PACT_BUILDER = " au.com.dius.pact.consumer.MessagePactBuilder"
58
+ private const val SYNCHRONOUS_MESSAGE_PACT_BUILDER = " au.com.dius.pact.consumer.dsl.SynchronousMessagePactBuilder"
59
+
48
60
class PactConsumerTestExt : Extension , BeforeTestExecutionCallback , BeforeAllCallback , ParameterResolver ,
49
61
AfterTestExecutionCallback , AfterAllCallback , TestTemplateInvocationContextProvider {
50
62
@@ -107,10 +119,14 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal
107
119
return testTemplate != null && testTemplate.parameters[0 ].type == AsynchronousMessageContext ::class .java
108
120
}
109
121
110
- override fun provideTestTemplateInvocationContexts (extensionContext : ExtensionContext ): Stream <TestTemplateInvocationContext > {
122
+ override fun provideTestTemplateInvocationContexts (
123
+ extensionContext : ExtensionContext
124
+ ): Stream <TestTemplateInvocationContext > {
111
125
val providerInfo = this .lookupProviderInfo(extensionContext)
112
126
val pact = setupPactForTest(providerInfo[0 ].first, providerInfo[0 ].second, extensionContext)
113
- return pact.asV4Pact().unwrap().interactions.map { AsynchronousMessageContext (it.asAsynchronousMessage()!! ) }.stream() as Stream <TestTemplateInvocationContext >
127
+ return pact.asV4Pact().unwrap().interactions.map {
128
+ AsynchronousMessageContext (it.asAsynchronousMessage()!! )
129
+ }.stream() as Stream <TestTemplateInvocationContext >
114
130
}
115
131
116
132
override fun resolveParameter (parameterContext : ParameterContext , extensionContext : ExtensionContext ): Any {
@@ -271,8 +287,8 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal
271
287
val key = " pact:${providerInfo.providerName} "
272
288
var methods = pactMethods
273
289
if (methods.isEmpty()) {
274
- methods = AnnotationSupport .findAnnotatedMethods(context.requiredTestClass, Pact ::class .java, HierarchyTraversalMode . TOP_DOWN )
275
- .map { m -> m.name}
290
+ methods = AnnotationSupport .findAnnotatedMethods(context.requiredTestClass, Pact ::class .java,
291
+ HierarchyTraversalMode . TOP_DOWN ) .map { m -> m.name}
276
292
}
277
293
278
294
return when {
@@ -513,19 +529,23 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal
513
529
if (method == null ) {
514
530
throw UnsupportedOperationException (" No method annotated with @Pact was found on test class " +
515
531
context.requiredTestClass.simpleName + " for provider '${providerInfo.providerName} '" )
516
- } else if (providerType == ProviderType .SYNCH && ! JUnitTestSupport .conformsToSignature(method, providerInfo.pactVersion ? : PactSpecVersion .V4 )) {
532
+ } else if (providerType == ProviderType .SYNCH &&
533
+ ! JUnitTestSupport .conformsToSignature(method, providerInfo.pactVersion ? : PactSpecVersion .V4 )) {
517
534
throw UnsupportedOperationException (" Method ${method.name} does not conform to required method signature " +
518
535
" 'public [RequestResponsePact|V4Pact] xxx(PactBuilder builder)'" )
519
- } else if (providerType == ProviderType .ASYNCH && ! JUnitTestSupport .conformsToMessagePactSignature(method, providerInfo.pactVersion ? : PactSpecVersion .V4 )) {
536
+ } else if (providerType == ProviderType .ASYNCH &&
537
+ ! JUnitTestSupport .conformsToMessagePactSignature(method, providerInfo.pactVersion ? : PactSpecVersion .V4 )) {
520
538
throw UnsupportedOperationException (" Method ${method.name} does not conform to required method signature " +
521
539
" 'public [MessagePact|V4Pact] xxx(PactBuilder builder)'" )
522
- } else if (providerType == ProviderType .SYNCH_MESSAGE && ! JUnitTestSupport .conformsToSynchMessagePactSignature(method, providerInfo.pactVersion ? : PactSpecVersion .V4 )) {
540
+ } else if (providerType == ProviderType .SYNCH_MESSAGE &&
541
+ ! JUnitTestSupport .conformsToSynchMessagePactSignature(method, providerInfo.pactVersion ? : PactSpecVersion .V4 )) {
523
542
throw UnsupportedOperationException (" Method ${method.name} does not conform to required method signature " +
524
543
" 'public V4Pact xxx(PactBuilder builder)'" )
525
544
}
526
545
527
546
val pactAnnotation = AnnotationSupport .findAnnotation(method, Pact ::class .java).get()
528
- val pactConsumer = ep.parseExpression(pactAnnotation.consumer, DataType .STRING )?.toString() ? : pactAnnotation.consumer
547
+ val pactConsumer = ep.parseExpression(pactAnnotation.consumer, DataType .STRING )?.toString()
548
+ ? : pactAnnotation.consumer
529
549
logger.debug {
530
550
" Invoking method '${method.name} ' to get Pact for the test " +
531
551
" '${context.testMethod.map { it.name }.orElse(" unknown" )} '"
@@ -535,7 +555,7 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal
535
555
val providerNameToUse = if (provider.isNullOrEmpty()) providerName else provider
536
556
val pact = when (providerType) {
537
557
ProviderType .SYNCH , ProviderType .UNSPECIFIED -> {
538
- if (method.parameterTypes[0 ].isAssignableFrom(Class .forName(" au.com.dius.pact.consumer.dsl.PactDslWithProvider " ))) {
558
+ if (method.parameterTypes[0 ].isAssignableFrom(Class .forName(PACT_DSL_WITH_PROVIDER ))) {
539
559
val consumerPactBuilder = ConsumerPactBuilder .consumer(pactConsumer)
540
560
if (providerInfo.pactVersion != null ) {
541
561
consumerPactBuilder.pactSpecVersion(providerInfo.pactVersion)
@@ -551,7 +571,7 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal
551
571
}
552
572
}
553
573
ProviderType .ASYNCH -> {
554
- if (method.parameterTypes[0 ].isAssignableFrom(Class .forName(" au.com.dius.pact.consumer.MessagePactBuilder " ))) {
574
+ if (method.parameterTypes[0 ].isAssignableFrom(Class .forName(MESSAGE_PACT_BUILDER ))) {
555
575
ReflectionSupport .invokeMethod(
556
576
method, context.testInstance,
557
577
MessagePactBuilder (providerInfo.pactVersion ? : PactSpecVersion .V3 )
@@ -566,7 +586,7 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal
566
586
}
567
587
}
568
588
ProviderType .SYNCH_MESSAGE -> {
569
- if (method.parameterTypes[0 ].isAssignableFrom(Class .forName(" au.com.dius.pact.consumer.dsl.SynchronousMessagePactBuilder " ))) {
589
+ if (method.parameterTypes[0 ].isAssignableFrom(Class .forName(SYNCHRONOUS_MESSAGE_PACT_BUILDER ))) {
570
590
ReflectionSupport .invokeMethod(
571
591
method, context.requiredTestInstance,
572
592
SynchronousMessagePactBuilder (providerInfo.pactVersion ? : PactSpecVersion .V4 )
@@ -642,7 +662,8 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal
642
662
} else {
643
663
JUnitTestSupport .validateMockServerResult(result)
644
664
}
645
- } else if (provider.providerType == ProviderType .ASYNCH || provider.providerType == ProviderType .SYNCH_MESSAGE ) {
665
+ } else if (provider.providerType == ProviderType .ASYNCH ||
666
+ provider.providerType == ProviderType .SYNCH_MESSAGE ) {
646
667
if (! context.executionException.isPresent) {
647
668
storePactForWrite(store, provider, null )
648
669
}
0 commit comments