Skip to content

Commit 2f1070b

Browse files
author
Ronald Holshausen
committedMar 7, 2020
fix: add more information to exception when there are no pacts to verify #1039
1 parent 6a081d6 commit 2f1070b

File tree

6 files changed

+40
-7
lines changed

6 files changed

+40
-7
lines changed
 

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import au.com.dius.pact.core.model.RequestResponseInteraction
77
import au.com.dius.pact.core.pactbroker.TestResult
88
import au.com.dius.pact.core.support.expressions.SystemPropertyResolver
99
import au.com.dius.pact.core.support.expressions.ValueResolver
10+
import au.com.dius.pact.core.support.isNotEmpty
1011
import au.com.dius.pact.provider.ConsumerInfo
1112
import au.com.dius.pact.provider.DefaultTestResultAccumulator
1213
import au.com.dius.pact.provider.IProviderVerifier
@@ -364,28 +365,34 @@ open class PactVerificationInvocationContextProvider : TestTemplateInvocationCon
364365
logger.debug { "provideTestTemplateInvocationContexts called" }
365366
val tests = resolvePactSources(context)
366367
return when {
367-
tests.isNotEmpty() -> tests.stream() as Stream<TestTemplateInvocationContext>
368+
tests.first.isNotEmpty() -> tests.first.stream() as Stream<TestTemplateInvocationContext>
368369
AnnotationSupport.isAnnotated(context.requiredTestClass, IgnoreNoPactsToVerify::class.java) ->
369370
listOf(DummyTestTemplate).stream() as Stream<TestTemplateInvocationContext>
370-
else -> throw NoPactsFoundException("No Pact files were found to verify")
371+
else -> throw NoPactsFoundException("No Pact files were found to verify\n${tests.second}")
371372
}
372373
}
373374

374-
private fun resolvePactSources(context: ExtensionContext): List<PactVerificationExtension> {
375+
private fun resolvePactSources(context: ExtensionContext): Pair<List<PactVerificationExtension>, String> {
376+
var description = ""
375377
val providerInfo = AnnotationSupport.findAnnotation(context.requiredTestClass, Provider::class.java)
376378
if (!providerInfo.isPresent) {
377379
throw UnsupportedOperationException("Provider name should be specified by using @${Provider::class.java.name} annotation")
378380
}
379381
val serviceName = providerInfo.get().value
382+
description += "Provider: $serviceName"
380383

381384
val consumerInfo = AnnotationSupport.findAnnotation(context.requiredTestClass, Consumer::class.java)
382385
val consumerName = consumerInfo.orElse(null)?.value
386+
if (consumerName.isNotEmpty()) {
387+
description += "\nConsumer: $consumerName"
388+
}
383389

384390
validateStateChangeMethods(context.requiredTestClass)
385391

386392
logger.debug { "Verifying pacts for provider '$serviceName' and consumer '$consumerName'" }
387393

388394
val pactSources = findPactSources(context).flatMap {
395+
description += "\nSource: ${it.description()}"
389396
val valueResolver = getValueResolver(context)
390397
if (valueResolver != null) {
391398
it.setValueResolver(valueResolver)
@@ -394,9 +401,9 @@ open class PactVerificationInvocationContextProvider : TestTemplateInvocationCon
394401
filterPactsByAnnotations(pacts, context.requiredTestClass).map { pact -> pact to it.pactSource }
395402
}.filter { p -> consumerName == null || p.first.consumer.name == consumerName }
396403

397-
return pactSources.flatMap { pact ->
404+
return Pair(pactSources.flatMap { pact ->
398405
pact.first.interactions.map { PactVerificationExtension(pact.first, pact.second, it, serviceName, consumerName) }
399-
}
406+
}, description)
400407
}
401408

402409
protected open fun getValueResolver(context: ExtensionContext): ValueResolver? = null

‎provider/pact-jvm-provider-junit5/src/test/groovy/au/com/dius/pact/provider/junit5/PactVerificationInvocationContextProviderSpec.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class PactVerificationInvocationContextProviderSpec extends Specification {
174174

175175
then:
176176
def exp = thrown(NoPactsFoundException)
177-
exp.message == 'No Pact files where found to verify'
177+
exp.message.startsWith('No Pact files were found to verify')
178178
}
179179

180180
@Issue('#768')

‎provider/pact-jvm-provider/src/main/java/au/com/dius/pact/provider/junit/loader/PactLoader.java

+5
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ public interface PactLoader {
3030
* @param valueResolver Value Resolver
3131
*/
3232
default void setValueResolver(ValueResolver valueResolver) { }
33+
34+
/**
35+
* Returns a description of this pact loader
36+
*/
37+
default String description() { return this.getClass().getSimpleName(); };
3338
}

‎provider/pact-jvm-provider/src/main/java/au/com/dius/pact/provider/junit/loader/PactUrlLoader.java

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public PactUrlLoader(final PactUrl pactUrl) {
3030
this(pactUrl.urls());
3131
}
3232

33+
@Override
34+
public String description() {
35+
return "URL(" + Arrays.toString(urls) + ")";
36+
}
37+
3338
public List<Pact> load(final String providerName) {
3439
return Arrays.stream(urls)
3540
.map(url -> {

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ open class PactBrokerLoader(
5555
pactBroker.valueResolver
5656
)
5757

58+
override fun description(): String {
59+
val resolver = setupValueResolver()
60+
val tags = pactBrokerTags?.flatMap { parseListExpression(it, resolver) }?.filter { it.isNotEmpty() }
61+
val consumers = pactBrokerConsumers.flatMap { parseListExpression(it, resolver) }.filter { it.isNotEmpty() }
62+
var source = getPactBrokerSource(resolver).description()
63+
if (tags != null && tags.isNotEmpty()) {
64+
source += " tags=$tags"
65+
}
66+
if (consumers.isNotEmpty()) {
67+
source += " consumers=$consumers"
68+
}
69+
return source
70+
}
71+
5872
override fun overridePactUrl(pactUrl: String, consumer: String) {
5973
overriddenPactUrl = pactUrl
6074
overriddenConsumer = consumer
@@ -177,7 +191,7 @@ open class PactBrokerLoader(
177191

178192
private fun getUrlForProvider(providerName: String, tag: String, pactBrokerClient: PactBrokerClient): String {
179193
return try {
180-
pactBrokerClient.getUrlForProvider(providerName, tag)!!
194+
pactBrokerClient.getUrlForProvider(providerName, tag) ?: "Unknown"
181195
} catch (e: Exception) {
182196
logger.debug(e) { "Failed to get provider URL from the pact broker" }
183197
"Unknown"

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

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class PactFolderLoader<I>(private val path: File) : PactLoader where I : Interac
2222

2323
constructor(pactFolder: PactFolder) : this(pactFolder.value)
2424

25+
override fun description() = "Directory(${pactSource.dir})"
26+
2527
override fun load(providerName: String): List<Pact<I>> {
2628
val pacts = mutableListOf<Pact<I>>()
2729
val pactFolder = resolvePath()

0 commit comments

Comments
 (0)
Please sign in to comment.