Skip to content

Commit

Permalink
fix: Omitting consumer version selectors should fall back to tags (#1158
Browse files Browse the repository at this point in the history
)

* fix: Omitting consumer version selectors should fall back to tags

* fix: Fix tests by removing incorrect assertion

Co-authored-by: Phil Endsley <[email protected]>
  • Loading branch information
pendsley and Phil Endsley authored Jul 3, 2020
1 parent 2e9b03c commit a5765fd
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,73 @@ class PactBrokerLoaderSpec extends Specification {
result.size() == 3
}

def 'Falls back to tags when consumer version selectors are not specified'() {
given:
pactBrokerLoader = {
new PactBrokerLoader(PactBrokerAnnotationWithTags.getAnnotation(PactBroker)) {
@Override
IPactBrokerClient newPactBrokerClient(URI url, ValueResolver resolver) {
assert url.host == 'pactbroker.host'
assert url.port == 1000
brokerClient
}
}
}
def selectors = [
new ConsumerVersionSelector('master', true)
]

when:
def result = pactBrokerLoader().load('test')

then:
result == []
1 * brokerClient.fetchConsumersWithSelectors('test', selectors, [], false, '') >> new Ok([])
}

def 'Loads pacts with consumer version selectors when consumer version selectors and tags are both present'() {
given:
tags = ['master', 'prod']
consumerVersionSelectors = [createVersionSelector('demo', 'true')]
def expected = [
new PactBrokerResult('a', '', '', [], [], false, 'demo', false),
new PactBrokerResult('b', '', '', [], [], false, 'demo', false),
new PactBrokerResult('c', '', '', [], [], false, 'demo', false),
new PactBrokerResult('d', '', '', [], [], false, 'demo', false)
]
def selectors = [ new ConsumerVersionSelector('demo', true) ]

when:
def result = pactBrokerLoader().load('test')

then:
brokerClient.getOptions() >> [:]
1 * brokerClient.fetchConsumersWithSelectors('test', selectors, [], false, '') >> new Ok(expected)
0 * brokerClient._
result.size() == 4
}

def 'Loads pacts with no selectors when none are specified'() {
given:
pactBrokerLoader = {
new PactBrokerLoader(FullPactBrokerAnnotation.getAnnotation(PactBroker)) {
@Override
IPactBrokerClient newPactBrokerClient(URI url, ValueResolver resolver) {
assert url.host == 'pactbroker.host'
assert url.port == 1000
brokerClient
}
}
}

when:
def result = pactBrokerLoader().load('test')

then:
result == []
1 * brokerClient.fetchConsumersWithSelectors('test', [], [], false, '') >> new Ok([])
}

def 'Does not loads wip pacts when pending is false'() {
given:
consumerVersionSelectors = [
Expand Down Expand Up @@ -653,16 +720,13 @@ class PactBrokerLoaderSpec extends Specification {
loader.pactReader = mockReader
loader
}
def selectors = [
new ConsumerVersionSelector('latest', true)
]

when:
def result = pactBrokerLoader().load('test')

then:
result == []
1 * brokerClient.fetchConsumersWithSelectors('test', selectors, [], false, '') >> new Ok([])
1 * brokerClient.fetchConsumersWithSelectors('test', [], [], false, '') >> new Ok([])
}

def 'configured from annotation with https and no port'() {
Expand All @@ -681,16 +745,13 @@ class PactBrokerLoaderSpec extends Specification {
loader.pactReader = mockReader
loader
}
def selectors = [
new ConsumerVersionSelector('latest', true)
]

when:
def result = pactBrokerLoader().load('test')

then:
result == []
1 * brokerClient.fetchConsumersWithSelectors('test', selectors, [], false, '') >> new Ok([])
1 * brokerClient.fetchConsumersWithSelectors('test', [], [], false, '') >> new Ok([])
}

def 'Auth: Uses no auth if no auth is provided'() {
Expand Down Expand Up @@ -850,4 +911,9 @@ class PactBrokerLoaderSpec extends Specification {

}

@PactBroker(host = 'pactbroker.host', port = '1000', tags = 'master')
static class PactBrokerAnnotationWithTags {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ open class PactBrokerLoader(
}

private fun buildConsumerVersionSelectors(resolver: ValueResolver): List<ConsumerVersionSelector> {
return if (pactBrokerConsumerVersionSelectors.isEmpty()) {

return if (shouldFallBackToTags()) {
pactBrokerTags.orEmpty().flatMap { parseListExpression(it, resolver) }.map { ConsumerVersionSelector(it) }
} else {
pactBrokerConsumerVersionSelectors.flatMap {
val parsedTags = parseListExpression(it.tag, resolver)
val tags = parseListExpression(it.tag, resolver)
val parsedLatest = parseListExpression(it.latest, resolver)
val tags = if (parsedTags.isEmpty()) listOf("latest") else parsedTags
val latest = if (parsedLatest.isEmpty()) List(tags.size) { true.toString() } else parsedLatest
if (tags.size != latest.size) {
throw IllegalArgumentException("Invalid Consumer version selectors. Each version selector must have a tag " +
Expand All @@ -128,6 +128,11 @@ open class PactBrokerLoader(
}
}

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

private fun setupValueResolver(): ValueResolver {
var valueResolver: ValueResolver = SystemPropertyResolver()
if (resolver != null) {
Expand Down

0 comments on commit a5765fd

Please sign in to comment.