Skip to content

Commit

Permalink
fix: use the supplied value resolver when working out if we should fa…
Browse files Browse the repository at this point in the history
…ll back to tags #1208
  • Loading branch information
Ronald Holshausen committed Sep 19, 2020
1 parent 7841232 commit 0f76d71
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import au.com.dius.pact.provider.junitsupport.loader.PactBrokerAuth
import au.com.dius.pact.provider.junitsupport.loader.PactBrokerLoader
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import spock.lang.Issue
import spock.lang.Specification
import spock.util.environment.RestoreSystemProperties

Expand All @@ -39,6 +40,7 @@ class PactBrokerLoaderSpec extends Specification {
private IPactBrokerClient brokerClient
private Pact mockPact
private PactReader mockReader
private ValueResolver valueResolver

void setup() {
host = 'pactbroker'
Expand All @@ -57,11 +59,12 @@ class PactBrokerLoaderSpec extends Specification {
mockReader = Mock(PactReader) {
loadPact(_) >> mockPact
}
valueResolver = null

pactBrokerLoader = { boolean failIfNoPactsFound = true ->
IPactBrokerClient client = brokerClient
def loader = new PactBrokerLoader(host, port, protocol, tags, consumerVersionSelectors, consumers,
failIfNoPactsFound, null, null, null, enablePendingPacts, providerTags, includeWipPactsSince) {
failIfNoPactsFound, null, null, valueResolver, enablePendingPacts, providerTags, includeWipPactsSince) {
@Override
IPactBrokerClient newPactBrokerClient(URI url, ValueResolver resolver) {
client
Expand Down Expand Up @@ -581,6 +584,33 @@ class PactBrokerLoaderSpec extends Specification {
1 * brokerClient.fetchConsumersWithSelectors('test', selectors, [], false, '') >> new Ok([])
}

@Issue('#1208')
@SuppressWarnings('GStringExpressionWithinString')
def 'When falling back to tags when consumer version selectors are not specified, use the supplied value resolver'() {
given:
valueResolver = Mock(ValueResolver)
valueResolver.propertyDefined(_) >> false
def selectors = [
new ConsumerVersionSelector('one', true),
new ConsumerVersionSelector('two', true),
new ConsumerVersionSelector('three', true)
]
def expected = [
new PactBrokerResult('d', '', '', [], [], false, 'one', false)
]
consumerVersionSelectors = [
createVersionSelector('${pactbroker.consumerversionselectors.tags}', 'true')
]

when:
pactBrokerLoader().load('test')

then:
valueResolver.propertyDefined('pactbroker.consumerversionselectors.tags') >> true
valueResolver.resolveValue('pactbroker.consumerversionselectors.tags') >> 'one,two,three'
1 * brokerClient.fetchConsumersWithSelectors('test', selectors, [], false, '') >> new Ok(expected)
}

def 'Loads pacts with consumer version selectors when consumer version selectors and tags are both present'() {
given:
tags = ['master', 'prod']
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package au.com.dius.pact.provider.junitsupport.loader

import au.com.dius.pact.core.matchers.util.padTo
import au.com.dius.pact.core.model.BrokerUrlSource
import au.com.dius.pact.core.model.DefaultPactReader
import au.com.dius.pact.core.model.Interaction
Expand Down Expand Up @@ -112,13 +113,15 @@ open class PactBrokerLoader(

private fun buildConsumerVersionSelectors(resolver: ValueResolver): List<ConsumerVersionSelector> {

return if (shouldFallBackToTags()) {
return if (shouldFallBackToTags(resolver)) {
pactBrokerTags.orEmpty().flatMap { parseListExpression(it, resolver) }.map { ConsumerVersionSelector(it) }
} else {
pactBrokerConsumerVersionSelectors.flatMap {
val tags = parseListExpression(it.tag, resolver)
val parsedLatest = parseListExpression(it.latest, resolver)
val latest = if (parsedLatest.isEmpty()) List(tags.size) { true.toString() } else parsedLatest
val latest = if (parsedLatest.isEmpty()) List(tags.size) { true.toString() }
else if (parsedLatest.size == 1) parsedLatest.padTo(tags.size, parsedLatest[0])
else parsedLatest
if (tags.size != latest.size) {
throw IllegalArgumentException("Invalid Consumer version selectors. Each version selector must have a tag " +
"and latest property")
Expand All @@ -128,9 +131,10 @@ open class PactBrokerLoader(
}
}

private fun shouldFallBackToTags(): Boolean {
private fun shouldFallBackToTags(resolver: ValueResolver): Boolean {
return pactBrokerConsumerVersionSelectors.isEmpty() ||
(pactBrokerConsumerVersionSelectors.size == 1 && parseListExpression(pactBrokerConsumerVersionSelectors[0].tag).isEmpty())
(pactBrokerConsumerVersionSelectors.size == 1 &&
parseListExpression(pactBrokerConsumerVersionSelectors[0].tag, resolver).isEmpty())
}

private fun setupValueResolver(): ValueResolver {
Expand Down

0 comments on commit 0f76d71

Please sign in to comment.