Skip to content

Commit

Permalink
Merge pull request #1421 from JapuDCret/plugin-handle-404-correctly
Browse files Browse the repository at this point in the history
feat: add handling for 404 responses in Maven plugin #1419
  • Loading branch information
uglyog authored Aug 29, 2021
2 parents b462966 + a7d66d8 commit 89f2a56
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<List<ConsumerInfo>> {
provider.hasPactsFromPactBrokerWithSelectors(options, pactBrokerUrl.toString(), emptyList())
}.getOrElse { handleException(it) }
)
}
}

private fun handleException(exception: Exception): List<ConsumerInfo> {
return when (exception) {
is NotFoundHalResponse -> when {
failIfNoPactsFound -> throw exception
else -> emptyList()
}
else -> throw exception
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 89f2a56

Please sign in to comment.