Skip to content

Commit

Permalink
fix: support specifying both tags and consumers as system property li…
Browse files Browse the repository at this point in the history
…st expressions #1447
  • Loading branch information
Ronald Holshausen committed Sep 24, 2021
1 parent ed6e82c commit 7c556ea
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ class PactBrokerLoaderSpec extends Specification {
createVersionSelector(tag: 'c', latest: 'true')
]
def selectors = [
new ConsumerVersionSelector('a', true, '', null),
new ConsumerVersionSelector('b', false, '', null),
new ConsumerVersionSelector('c', true, '', null)
new ConsumerVersionSelector('a', true, null, null),
new ConsumerVersionSelector('b', false, null, null),
new ConsumerVersionSelector('c', true, null, null)
]
def expected = [
new PactBrokerResult('test', 'a', '', [], [], false, null, false, true),
Expand Down Expand Up @@ -360,8 +360,8 @@ class PactBrokerLoaderSpec extends Specification {
System.setProperty('compositeLatest', "true${VALUES_SEPARATOR}false")
consumerVersionSelectors = [createVersionSelector(tag: '${compositeTag}', latest: '${compositeLatest}')]
def selectors = [
new ConsumerVersionSelector('one', true, '', null),
new ConsumerVersionSelector('two', false, '', null)
new ConsumerVersionSelector('one', true, null, null),
new ConsumerVersionSelector('two', false, null, null)
]
def expected = [
new PactBrokerResult('test', 'one', '', [], [], false, null, false, true),
Expand Down Expand Up @@ -425,9 +425,9 @@ class PactBrokerLoaderSpec extends Specification {
newLoader.valueResolver = [resolveValue: { val -> val == 'd' ? 'D' : 'X' }] as ValueResolver
def expected = [ new PactBrokerResult('test', 'a', '', [], [], false, null, false, true) ]
def selectors = [
new ConsumerVersionSelector('X', true, '', null),
new ConsumerVersionSelector('X', false, '', null),
new ConsumerVersionSelector('X', true, '', 'D')
new ConsumerVersionSelector('X', true, null, null),
new ConsumerVersionSelector('X', false, null, null),
new ConsumerVersionSelector('X', true, null, 'D')
]

when:
Expand All @@ -447,9 +447,9 @@ class PactBrokerLoaderSpec extends Specification {
System.setProperty('compositeTag', "one${VALUES_SEPARATOR}two${VALUES_SEPARATOR}three")
consumerVersionSelectors = [createVersionSelector(tag: '${compositeTag}')]
def selectors = [
new ConsumerVersionSelector('one', true, '', null),
new ConsumerVersionSelector('two', true, '', null),
new ConsumerVersionSelector('three', true, '', null)
new ConsumerVersionSelector('one', true, null, null),
new ConsumerVersionSelector('two', true, null, null),
new ConsumerVersionSelector('three', true, null, null)
]
def expected = [
new PactBrokerResult('test', 'one', '', [], [], false, null, false, true),
Expand Down Expand Up @@ -639,7 +639,7 @@ class PactBrokerLoaderSpec extends Specification {
new PactBrokerResult('c', '', '', [], [], false, 'demo', false, false),
new PactBrokerResult('d', '', '', [], [], false, 'demo', false, false)
]
def selectors = [ new ConsumerVersionSelector('demo', true, '', null) ]
def selectors = [ new ConsumerVersionSelector('demo', true, null, null) ]

when:
def result = pactBrokerLoader().load('test')
Expand Down Expand Up @@ -682,9 +682,9 @@ class PactBrokerLoaderSpec extends Specification {
valueResolver = Mock(ValueResolver)
valueResolver.propertyDefined(_) >> false
def selectors = [
new ConsumerVersionSelector('one', true, '', null),
new ConsumerVersionSelector('two', true, '', null),
new ConsumerVersionSelector('three', true, '', null)
new ConsumerVersionSelector('one', true, null, null),
new ConsumerVersionSelector('two', true, null, null),
new ConsumerVersionSelector('three', true, null, null)
]
def expected = [
new PactBrokerResult('d', '', '', [], [], false, 'one', false, false)
Expand Down Expand Up @@ -712,7 +712,7 @@ class PactBrokerLoaderSpec extends Specification {
new PactBrokerResult('c', '', '', [], [], false, 'demo', false, false),
new PactBrokerResult('d', '', '', [], [], false, 'demo', false, false)
]
def selectors = [ new ConsumerVersionSelector('demo', true, '', null) ]
def selectors = [ new ConsumerVersionSelector('demo', true, null, null) ]

when:
def result = pactBrokerLoader().load('test')
Expand Down Expand Up @@ -753,8 +753,8 @@ class PactBrokerLoaderSpec extends Specification {
]
includeWipPactsSince = '2020-06-25'
def selectors = [
new ConsumerVersionSelector('a', true, '', null),
new ConsumerVersionSelector('b', false, '', null),
new ConsumerVersionSelector('a', true, null, null),
new ConsumerVersionSelector('b', false, null, null),
]
def expected = [
new PactBrokerResult('test', 'a', '', [], [], false, null, false, false),
Expand Down Expand Up @@ -782,8 +782,8 @@ class PactBrokerLoaderSpec extends Specification {
providerTags = ['dev']
includeWipPactsSince = '2020-06-25'
def selectors = [
new ConsumerVersionSelector('a', true, '', null),
new ConsumerVersionSelector('b', false, '', null),
new ConsumerVersionSelector('a', true, null, null),
new ConsumerVersionSelector('b', false, null, null),
]
def expected = [
new PactBrokerResult('test', 'a', '', [], [], false, null, false, false),
Expand Down Expand Up @@ -1068,9 +1068,9 @@ class PactBrokerLoaderSpec extends Specification {

then:
result == [
new ConsumerVersionSelector('one', true, '', null),
new ConsumerVersionSelector('two', true, '', null),
new ConsumerVersionSelector('three', true, '', 'four')
new ConsumerVersionSelector('one', true, null, null),
new ConsumerVersionSelector('two', true, null, null),
new ConsumerVersionSelector('three', true, null, 'four')
]
}

Expand Down Expand Up @@ -1099,10 +1099,10 @@ class PactBrokerLoaderSpec extends Specification {

then:
result == [
new ConsumerVersionSelector('one', true, '', null),
new ConsumerVersionSelector('2', true, '', null),
new ConsumerVersionSelector('3', true, '', null),
new ConsumerVersionSelector('three', true, '', 'Y')
new ConsumerVersionSelector('one', true, null, null),
new ConsumerVersionSelector('2', true, null, null),
new ConsumerVersionSelector('3', true, null, null),
new ConsumerVersionSelector('three', true, null, 'Y')
]
}

Expand Down Expand Up @@ -1131,10 +1131,10 @@ class PactBrokerLoaderSpec extends Specification {

then:
result == [
new ConsumerVersionSelector('one', true, '', null),
new ConsumerVersionSelector('2', true, '', null),
new ConsumerVersionSelector('3', false, '', null),
new ConsumerVersionSelector('three', true, '', null)
new ConsumerVersionSelector('one', true, null, null),
new ConsumerVersionSelector('2', true, null, null),
new ConsumerVersionSelector('3', false, null, null),
new ConsumerVersionSelector('three', true, null, null)
]
}

Expand Down Expand Up @@ -1163,10 +1163,10 @@ class PactBrokerLoaderSpec extends Specification {

then:
result == [
new ConsumerVersionSelector('one', true, '', null),
new ConsumerVersionSelector('2', false, '', null),
new ConsumerVersionSelector('3', false, '', null),
new ConsumerVersionSelector('three', true, '', null)
new ConsumerVersionSelector('one', true, null, null),
new ConsumerVersionSelector('2', false, null, null),
new ConsumerVersionSelector('3', false, null, null),
new ConsumerVersionSelector('three', true, null, null)
]
}

Expand All @@ -1184,8 +1184,8 @@ class PactBrokerLoaderSpec extends Specification {

then:
result == [
new ConsumerVersionSelector('one', true, '', null),
new ConsumerVersionSelector('two', false, '', null),
new ConsumerVersionSelector('one', true, null, null),
new ConsumerVersionSelector('two', false, null, null),
new ConsumerVersionSelector('three', true, 'test', null)
]
}
Expand All @@ -1206,6 +1206,96 @@ class PactBrokerLoaderSpec extends Specification {
]
}

def 'building the list of selectors expands any consumer name expressions'() {
given:
valueResolver = Mock(ValueResolver) {
it.propertyDefined(_) >> { it[0] == 'two' || it[0] == 'X' }
it.resolveValue(_) >> {
if (it[0] == 'two') {
'2,3'
} else if (it[0] == 'X') {
'Y'
} else {
null
}
}
}
consumerVersionSelectors = [
createVersionSelector(consumer: '${two}')
]

when:
def result = pactBrokerLoader.call().buildConsumerVersionSelectors(valueResolver)

then:
result == [
new ConsumerVersionSelector(null, true, '2', null),
new ConsumerVersionSelector(null, true, '3', null)
]
}

def 'building the list of selectors with both tag and consumer name expressions'() {
given:
valueResolver = Mock(ValueResolver) {
it.propertyDefined(_) >> { it[0] == 'two' || it[0] == 'X' }
it.resolveValue(_) >> {
if (it[0] == 'two') {
'2,3,4'
} else if (it[0] == 'X') {
'Y,Z'
} else {
null
}
}
}
consumerVersionSelectors = [
createVersionSelector(tag: '${X}', consumer: '${two}')
]

when:
def result = pactBrokerLoader.call().buildConsumerVersionSelectors(valueResolver)

then:
result == [
new ConsumerVersionSelector('Y', true, '2', null),
new ConsumerVersionSelector('Y', true, '3', null),
new ConsumerVersionSelector('Y', true, '4', null),
new ConsumerVersionSelector('Z', true, '2', null),
new ConsumerVersionSelector('Z', true, '3', null),
new ConsumerVersionSelector('Z', true, '4', null)
]
}

@RestoreSystemProperties
void 'test annotation with system properties with both tag and consumer name expressions'() {
given:
System.setProperty('pactbroker.consumerversionselectors.tags', '1,2,3')
System.setProperty('pactbroker.consumers', 'A,B')
pactBrokerLoader = {
new PactBrokerLoader(PactBrokerAnnotationNoPort.getAnnotation(PactBroker)) {
@Override
IPactBrokerClient newPactBrokerClient(URI url, ValueResolver resolver) {
brokerClient
}
}
}
def selectors = [
new ConsumerVersionSelector('1', true, 'A', ''),
new ConsumerVersionSelector('1', true, 'B', ''),
new ConsumerVersionSelector('2', true, 'A', ''),
new ConsumerVersionSelector('2', true, 'B', ''),
new ConsumerVersionSelector('3', true, 'A', ''),
new ConsumerVersionSelector('3', true, 'B', '')
]

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

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

@Unroll
@SuppressWarnings('LineLength')
def 'getPactBrokerSource uses the URL if it is set'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ open class PactBrokerLoader(
} else {
pactBrokerConsumerVersionSelectors.flatMap {
val tags = ep.parseListExpression(it.tag, resolver)
val consumer = ep.parseExpression(it.consumer, DataType.STRING, resolver) as String?
val consumers = ep.parseListExpression(it.consumer, resolver)
val fallbackTag = ep.parseExpression(it.fallbackTag, DataType.STRING, resolver) as String?
val parsedLatest = ep.parseListExpression(it.latest, resolver)
val latest = when {
Expand All @@ -147,13 +147,22 @@ open class PactBrokerLoader(
}

when {
tags.isNotEmpty() && consumers.isNotEmpty() -> {
permutations(tags.mapIndexed { index, tag -> tag to index }, consumers).map { (tag, consumer) ->
ConsumerVersionSelector(tag!!.first, latest[tag.second].toBoolean(), fallbackTag = fallbackTag,
consumer = consumer)
}
}
tags.isNotEmpty() -> {
tags.mapIndexed { index, tag ->
ConsumerVersionSelector(tag, latest[index].toBoolean(), fallbackTag = fallbackTag, consumer = consumer)
ConsumerVersionSelector(tag, latest[index].toBoolean(), fallbackTag = fallbackTag,
consumer = consumers.firstOrNull())
}
}
consumer.isNotEmpty() -> {
listOf(ConsumerVersionSelector(null, true, fallbackTag = fallbackTag, consumer = consumer))
consumers.isNotEmpty() -> {
consumers.map { name ->
ConsumerVersionSelector(null, true, fallbackTag = fallbackTag, consumer = name)
}
}
else -> listOf()
}
Expand Down

0 comments on commit 7c556ea

Please sign in to comment.