Skip to content

Commit c5d7342

Browse files
author
Ronald Holshausen
committedMar 22, 2020
feat: Updated Gradle plugin to use new hasPactsFromPactBrokerWithSelectors #942
1 parent 9644dd9 commit c5d7342

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed
 

‎core/pact-broker/src/main/kotlin/au/com/dius/pact/core/pactbroker/HalClient.kt

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ import au.com.dius.pact.core.support.handleWith
4747
* Interface to a HAL Client
4848
*/
4949
interface IHalClient {
50+
/**
51+
* Navigates to the Root
52+
*/
53+
fun navigate(): IHalClient
54+
5055
/**
5156
* Navigates the URL associated with the given link using the current HAL document
5257
* @param options Map of key-value pairs to use for parsing templated links
@@ -247,6 +252,11 @@ open class HalClient @JvmOverloads constructor(
247252
return httpClient!!
248253
}
249254

255+
override fun navigate(): IHalClient {
256+
pathInfo = fetch(ROOT)
257+
return this
258+
}
259+
250260
override fun navigate(options: Map<String, Any>, link: String): IHalClient {
251261
pathInfo = pathInfo ?: fetch(ROOT)
252262
pathInfo = fetchLink(link, options)

‎core/pact-broker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ open class PactBrokerClient(val pactBrokerUrl: String, val options: Map<String,
137137
* Fetches all consumers for the given provider and selectors
138138
*/
139139
open fun fetchConsumersWithSelectors(provider: String, consumerVersionSelectors: List<ConsumerVersionSelector>): Either<Exception, List<PactResult>> {
140-
val halClient = newHalClient()
140+
val halClient = newHalClient().navigate()
141141
val pactsForVerification = when {
142142
halClient.linkUrl(PROVIDER_PACTS_FOR_VERIFICATION) != null -> PROVIDER_PACTS_FOR_VERIFICATION
143143
halClient.linkUrl(BETA_PROVIDER_PACTS_FOR_VERIFICATION) != null -> BETA_PROVIDER_PACTS_FOR_VERIFICATION

‎core/pact-broker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy

+3
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ class PactBrokerClientSpec extends Specification {
352352
def result = client.fetchConsumersWithSelectors('provider', selectors)
353353

354354
then:
355+
1 * halClient.navigate() >> halClient
355356
1 * halClient.linkUrl('pb:provider-pacts-for-verification') >> 'URL'
356357
1 * halClient.postJson('pb:provider-pacts-for-verification', [provider: 'provider'], json) >> new Either.Right(jsonResult)
357358
result.right
@@ -382,6 +383,7 @@ class PactBrokerClientSpec extends Specification {
382383
def result = client.fetchConsumersWithSelectors('provider', [])
383384

384385
then:
386+
1 * halClient.navigate() >> halClient
385387
1 * halClient.linkUrl('pb:provider-pacts-for-verification') >> null
386388
1 * halClient.linkUrl('beta:provider-pacts-for-verification') >> 'URL'
387389
1 * halClient.postJson('beta:provider-pacts-for-verification', _, _) >> new Either.Right(jsonResult)
@@ -399,6 +401,7 @@ class PactBrokerClientSpec extends Specification {
399401
def result = client.fetchConsumersWithSelectors('provider', [])
400402

401403
then:
404+
1 * halClient.navigate() >> halClient
402405
1 * halClient.linkUrl('pb:provider-pacts-for-verification') >> null
403406
1 * halClient.linkUrl('beta:provider-pacts-for-verification') >> null
404407
0 * halClient.postJson(_, _, _)

‎provider/pact-jvm-provider/src/main/kotlin/au/com/dius/pact/provider/ProviderClient.kt

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import au.com.dius.pact.core.model.ProviderState
99
import au.com.dius.pact.core.model.Request
1010
import au.com.dius.pact.core.model.UrlSource
1111
import au.com.dius.pact.core.pactbroker.PactBrokerConsumer
12+
import au.com.dius.pact.core.pactbroker.PactResult
1213
import au.com.dius.pact.core.support.Json
1314
import groovy.lang.Binding
1415
import groovy.lang.Closure
@@ -166,6 +167,12 @@ open class ConsumerInfo @JvmOverloads constructor (
166167
pactSource = BrokerUrlSource(url = consumer.source, pactBrokerUrl = consumer.pactBrokerUrl, tag = consumer.tag),
167168
pactFileAuthentication = consumer.pactFileAuthentication
168169
)
170+
171+
fun from(consumer: PactResult) =
172+
ConsumerInfo(name = consumer.name,
173+
pactSource = BrokerUrlSource(url = consumer.source, pactBrokerUrl = consumer.pactBrokerUrl),
174+
pactFileAuthentication = consumer.pactFileAuthentication
175+
)
169176
}
170177
}
171178

‎provider/pact-jvm-provider/src/main/kotlin/au/com/dius/pact/provider/ProviderInfo.kt

+27-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package au.com.dius.pact.provider
22

3+
import arrow.core.Either
34
import au.com.dius.pact.core.model.DefaultPactReader
45
import au.com.dius.pact.core.model.FileSource
56
import au.com.dius.pact.core.model.Interaction
7+
import au.com.dius.pact.core.pactbroker.ConsumerVersionSelector
68
import au.com.dius.pact.core.pactbroker.PactBrokerClient
79
import org.apache.commons.lang3.builder.HashCodeBuilder
810
import org.apache.commons.lang3.builder.ToStringBuilder
@@ -55,12 +57,8 @@ open class ProviderInfo @JvmOverloads constructor (
5557
}
5658

5759
@JvmOverloads
58-
open fun hasPactsFromPactBroker(options: Map<String, Any> = mapOf(), pactBrokerUrl: String): List<ConsumerInfo> {
59-
val client = PactBrokerClient(pactBrokerUrl, options)
60-
val consumersFromBroker = client.fetchConsumers(name).map { ConsumerInfo.from(it) }
61-
consumers.addAll(consumersFromBroker)
62-
return consumersFromBroker
63-
}
60+
open fun hasPactsFromPactBroker(options: Map<String, Any> = mapOf(), pactBrokerUrl: String) =
61+
hasPactsFromPactBrokerWithSelectors(options, pactBrokerUrl, emptyList())
6462

6563
@JvmOverloads
6664
open fun hasPactsFromPactBrokerWithTag(
@@ -74,6 +72,29 @@ open class ProviderInfo @JvmOverloads constructor (
7472
return consumersFromBroker
7573
}
7674

75+
@JvmOverloads
76+
open fun hasPactsFromPactBrokerWithSelectors(
77+
options: Map<String, Any> = mapOf(),
78+
pactBrokerUrl: String,
79+
selectors: List<ConsumerVersionSelector>
80+
): List<ConsumerInfo> {
81+
val client = PactBrokerClient(pactBrokerUrl, options)
82+
val consumersFromBroker = client.fetchConsumersWithSelectors(name, selectors).map { results ->
83+
results.map { ConsumerInfo.from(it) }
84+
}
85+
return when (consumersFromBroker) {
86+
is Either.Right<*> -> {
87+
val list = (consumersFromBroker as Either.Right<List<ConsumerInfo>>).b
88+
consumers.addAll(list)
89+
list
90+
}
91+
is Either.Left<*> -> {
92+
throw RuntimeException("Call to fetch pacts from Pact Broker failed with an exception",
93+
consumersFromBroker.a as Exception)
94+
}
95+
}
96+
}
97+
7798
open fun setupConsumerListFromPactFiles(consumersGroup: ConsumersGroup): MutableList<IConsumerInfo> {
7899
val pactFileDirectory = consumersGroup.pactFileLocation ?: return mutableListOf()
79100
if (!pactFileDirectory.exists() || !pactFileDirectory.canRead()) {

0 commit comments

Comments
 (0)
Please sign in to comment.