diff --git a/provider/maven/src/main/kotlin/au/com/dius/pact/provider/maven/PactProviderMojo.kt b/provider/maven/src/main/kotlin/au/com/dius/pact/provider/maven/PactProviderMojo.kt index 1e6f390a60..010b472c90 100644 --- a/provider/maven/src/main/kotlin/au/com/dius/pact/provider/maven/PactProviderMojo.kt +++ b/provider/maven/src/main/kotlin/au/com/dius/pact/provider/maven/PactProviderMojo.kt @@ -1,6 +1,8 @@ package au.com.dius.pact.provider.maven import au.com.dius.pact.core.pactbroker.ConsumerVersionSelector +import au.com.dius.pact.core.pactbroker.NotFoundHalResponse +import au.com.dius.pact.core.support.handleWith import au.com.dius.pact.core.support.toUrl import au.com.dius.pact.provider.ConsumerInfo import au.com.dius.pact.provider.IConsumerInfo @@ -12,6 +14,7 @@ import au.com.dius.pact.provider.ProviderVerifier import au.com.dius.pact.provider.ProviderVersion import au.com.dius.pact.provider.VerificationResult import au.com.dius.pact.provider.reporters.ReporterManager +import com.github.michaelbull.result.getOrElse import org.apache.maven.plugin.MojoFailureException import org.apache.maven.plugins.annotations.Mojo import org.apache.maven.plugins.annotations.Parameter @@ -187,7 +190,21 @@ open class PactProviderMojo : PactBaseMojo() { ConsumerVersionSelector(it, true, fallbackTag = pactBroker.fallbackTag) } consumers.addAll(provider.hasPactsFromPactBrokerWithSelectors(options, pactBrokerUrl.toString(), selectors)) } - else -> consumers.addAll(provider.hasPactsFromPactBrokerWithSelectors(options, pactBrokerUrl.toString(), emptyList())) + else -> consumers.addAll( + handleWith> { + provider.hasPactsFromPactBrokerWithSelectors(options, pactBrokerUrl.toString(), emptyList()) + }.getOrElse { handleException(it) } + ) + } + } + + private fun handleException(exception: Exception): List { + return when (exception) { + is NotFoundHalResponse -> when { + failIfNoPactsFound -> throw exception + else -> emptyList() + } + else -> throw exception } } diff --git a/provider/maven/src/test/groovy/au/com/dius/pact/provider/maven/PactProviderMojoSpec.groovy b/provider/maven/src/test/groovy/au/com/dius/pact/provider/maven/PactProviderMojoSpec.groovy index f77dee4488..d80732305a 100644 --- a/provider/maven/src/test/groovy/au/com/dius/pact/provider/maven/PactProviderMojoSpec.groovy +++ b/provider/maven/src/test/groovy/au/com/dius/pact/provider/maven/PactProviderMojoSpec.groovy @@ -1,6 +1,7 @@ package au.com.dius.pact.provider.maven import au.com.dius.pact.core.pactbroker.ConsumerVersionSelector +import au.com.dius.pact.core.pactbroker.NotFoundHalResponse import au.com.dius.pact.provider.ConsumerInfo import au.com.dius.pact.provider.IProviderVerifier import org.apache.maven.plugin.MojoFailureException @@ -289,6 +290,42 @@ class PactProviderMojoSpec extends Specification { noExceptionThrown() } + def 'do fail the build if the Broker returns 404 and failIfNoPactsFound is true'() { + given: + def provider = Spy(new Provider('TestProvider', null as File, new URL('http://broker:1234'), + new PactBroker(null, null, null, null))) + def list = [] + mojo.failIfNoPactsFound = true + + when: + mojo.loadPactsFromPactBroker(provider, list, [:]) + + then: + 1 * provider.hasPactsFromPactBrokerWithSelectors([:], 'http://broker:1234', []) >> { + throw new NotFoundHalResponse() + } + thrown(NotFoundHalResponse) + list.size() == 0 + } + + def 'do not fail the build if the Broker returns 404 and failIfNoPactsFound is false'() { + given: + def provider = Spy(new Provider('TestProvider', null as File, new URL('http://broker:1234'), + new PactBroker(null, null, null, null))) + def list = [] + mojo.failIfNoPactsFound = false + + when: + mojo.loadPactsFromPactBroker(provider, list, [:]) + + then: + 1 * provider.hasPactsFromPactBrokerWithSelectors([:], 'http://broker:1234', []) >> { + throw new NotFoundHalResponse() + } + noExceptionThrown() + list.size() == 0 + } + @RestoreSystemProperties def 'system property pact.verifier.publishResults true when set with systemPropertyVariables' () { given: