From b44e8d4be512fa645c592c6c0f438609b349c419 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Sat, 21 Aug 2021 15:43:50 +1000 Subject: [PATCH] feat: add method to ProviderInfo with better type safety #1415 --- .../pact/core/pactbroker/PactBrokerClient.kt | 8 ++--- .../dius/pact/provider/PactBrokerOptions.kt | 18 ++++++++++++ .../au/com/dius/pact/provider/ProviderInfo.kt | 29 ++++++++++++------- .../groovysupport/ProviderClientSpec.groovy | 5 +--- 4 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 provider/src/main/kotlin/au/com/dius/pact/provider/PactBrokerOptions.kt diff --git a/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt b/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt index 68237a12e4..b1562e94a0 100644 --- a/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt +++ b/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt @@ -121,7 +121,7 @@ interface IPactBrokerClient { selectors: List, providerTags: List = emptyList(), enablePending: Boolean = false, - includeWipPactsSince: String + includeWipPactsSince: String? ): Result, Exception> fun getUrlForProvider(providerName: String, tag: String): String? @@ -236,7 +236,7 @@ open class PactBrokerClient( selectors: List, providerTags: List, enablePending: Boolean, - includeWipPactsSince: String + includeWipPactsSince: String? ): Result, Exception> { val halClient = when (val navigateResult = handleWith { newHalClient().navigate() }) { is Err -> return navigateResult @@ -272,7 +272,7 @@ open class PactBrokerClient( selectors: List, enablePending: Boolean, providerTags: List, - includeWipPactsSince: String, + includeWipPactsSince: String?, halClient: IHalClient, pactsForVerification: String, providerName: String @@ -284,7 +284,7 @@ open class PactBrokerClient( if (enablePending) { body["providerVersionTags"] = jsonArray(providerTags) body["includePendingStatus"] = true - if (!includeWipPactsSince.isBlank()) { + if (includeWipPactsSince.isNotEmpty()) { body["includeWipPactsSince"] = includeWipPactsSince } } diff --git a/provider/src/main/kotlin/au/com/dius/pact/provider/PactBrokerOptions.kt b/provider/src/main/kotlin/au/com/dius/pact/provider/PactBrokerOptions.kt new file mode 100644 index 0000000000..f1fa8881c5 --- /dev/null +++ b/provider/src/main/kotlin/au/com/dius/pact/provider/PactBrokerOptions.kt @@ -0,0 +1,18 @@ +package au.com.dius.pact.provider + +data class PactBrokerOptions @JvmOverloads constructor( + /** + * Enable pending pacts + */ + val enablePending: Boolean = false, + + /** + * Provider tags. Required if pending pacts are enabled + */ + val providerTags: List = listOf(), + + /** + * Only include WIP pacts since the provided date + */ + val includeWipPactsSince: String? = null +) diff --git a/provider/src/main/kotlin/au/com/dius/pact/provider/ProviderInfo.kt b/provider/src/main/kotlin/au/com/dius/pact/provider/ProviderInfo.kt index 544da14f96..848b1a5045 100644 --- a/provider/src/main/kotlin/au/com/dius/pact/provider/ProviderInfo.kt +++ b/provider/src/main/kotlin/au/com/dius/pact/provider/ProviderInfo.kt @@ -65,28 +65,37 @@ open class ProviderInfo @JvmOverloads constructor ( hasPactsFromPactBrokerWithSelectors(options, pactBrokerUrl, emptyList()) @JvmOverloads - @Suppress("TooGenericExceptionThrown") open fun hasPactsFromPactBrokerWithSelectors( options: Map = mapOf(), pactBrokerUrl: String, selectors: List ): List { val enablePending = Utils.lookupInMap(options, "enablePending", Boolean::class.java, false) - if (enablePending && (!options.containsKey("providerTags") || options["providerTags"] !is List<*>)) { - throw RuntimeException("No providerTags: To use the pending pacts feature, you need to provide the list of " + - "provider names for the provider application version that will be published with the verification results") - } val providerTags = if (enablePending) { - options["providerTags"] as List + options["providerTags"] as List? } else { emptyList() } val includePactsSince = Utils.lookupInMap(options, "includeWipPactsSince", String::class.java, "") - val client = pactBrokerClient(pactBrokerUrl, options) - val consumersFromBroker = client.fetchConsumersWithSelectors(name, selectors, providerTags, enablePending, - includePactsSince) - .map { results -> results.map { ConsumerInfo.from(it) } + return hasPactsFromPactBrokerWithSelectors(pactBrokerUrl, selectors, + PactBrokerOptions(enablePending, providerTags.orEmpty(), includePactsSince)) + } + + @Suppress("TooGenericExceptionThrown") + open fun hasPactsFromPactBrokerWithSelectors( + pactBrokerUrl: String, + selectors: List, + options: PactBrokerOptions + ): List { + if (options.enablePending && options.providerTags.isEmpty()) { + throw RuntimeException("No providerTags: To use the pending pacts feature, you need to provide the list of " + + "provider names for the provider application version that will be published with the verification results") } + val client = pactBrokerClient(pactBrokerUrl, mapOf()) + val consumersFromBroker = client.fetchConsumersWithSelectors(name, selectors, options.providerTags, + options.enablePending, options.includeWipPactsSince) + .map { results -> results.map { ConsumerInfo.from(it) } + } return when (consumersFromBroker) { is Ok> -> { val list = consumersFromBroker.value diff --git a/provider/src/test/groovy/au/com/dius/pact/provider/groovysupport/ProviderClientSpec.groovy b/provider/src/test/groovy/au/com/dius/pact/provider/groovysupport/ProviderClientSpec.groovy index cf6f14e423..28a8c8522d 100644 --- a/provider/src/test/groovy/au/com/dius/pact/provider/groovysupport/ProviderClientSpec.groovy +++ b/provider/src/test/groovy/au/com/dius/pact/provider/groovysupport/ProviderClientSpec.groovy @@ -5,8 +5,6 @@ import au.com.dius.pact.core.model.ProviderState import au.com.dius.pact.core.model.Request import au.com.dius.pact.core.support.Json import au.com.dius.pact.core.model.ContentType as PactContentType -@SuppressWarnings('UnusedImport') -import au.com.dius.pact.provider.GroovyScalaUtils$ import au.com.dius.pact.provider.IHttpClientFactory import au.com.dius.pact.provider.IProviderInfo import au.com.dius.pact.provider.ProviderClient @@ -288,7 +286,7 @@ class ProviderClientSpec extends Specification { def 'execute request filter executes any scala closure'() { given: - provider.requestFilter = GroovyScalaUtils$.MODULE$.testRequestFilter() + provider.requestFilter = au.com.dius.pact.provider.GroovyScalaUtils$.MODULE$.testRequestFilter() when: client.executeRequestFilter(httpRequest) @@ -703,5 +701,4 @@ class ProviderClientSpec extends Specification { then: result.contentType.toString() == 'text/plain; charset=ISO-8859-1' } - }