Skip to content

Commit

Permalink
feat: add method to ProviderInfo with better type safety #1415
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Aug 21, 2021
1 parent 4db1f83 commit b44e8d4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ interface IPactBrokerClient {
selectors: List<ConsumerVersionSelector>,
providerTags: List<String> = emptyList(),
enablePending: Boolean = false,
includeWipPactsSince: String
includeWipPactsSince: String?
): Result<List<PactBrokerResult>, Exception>

fun getUrlForProvider(providerName: String, tag: String): String?
Expand Down Expand Up @@ -236,7 +236,7 @@ open class PactBrokerClient(
selectors: List<ConsumerVersionSelector>,
providerTags: List<String>,
enablePending: Boolean,
includeWipPactsSince: String
includeWipPactsSince: String?
): Result<List<PactBrokerResult>, Exception> {
val halClient = when (val navigateResult = handleWith<IHalClient> { newHalClient().navigate() }) {
is Err<Exception> -> return navigateResult
Expand Down Expand Up @@ -272,7 +272,7 @@ open class PactBrokerClient(
selectors: List<ConsumerVersionSelector>,
enablePending: Boolean,
providerTags: List<String>,
includeWipPactsSince: String,
includeWipPactsSince: String?,
halClient: IHalClient,
pactsForVerification: String,
providerName: String
Expand All @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> = listOf(),

/**
* Only include WIP pacts since the provided date
*/
val includeWipPactsSince: String? = null
)
29 changes: 19 additions & 10 deletions provider/src/main/kotlin/au/com/dius/pact/provider/ProviderInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,37 @@ open class ProviderInfo @JvmOverloads constructor (
hasPactsFromPactBrokerWithSelectors(options, pactBrokerUrl, emptyList())

@JvmOverloads
@Suppress("TooGenericExceptionThrown")
open fun hasPactsFromPactBrokerWithSelectors(
options: Map<String, Any> = mapOf(),
pactBrokerUrl: String,
selectors: List<ConsumerVersionSelector>
): List<ConsumerInfo> {
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<String>
options["providerTags"] as List<String>?
} 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<ConsumerVersionSelector>,
options: PactBrokerOptions
): List<ConsumerInfo> {
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())

This comment has been minimized.

Copy link
@kflorence

kflorence Aug 22, 2021

Contributor

@uglyog instead of mapOf() we need to pass in options. Currently authentication is missing, which is causing requests to the broker to fail

val consumersFromBroker = client.fetchConsumersWithSelectors(name, selectors, options.providerTags,
options.enablePending, options.includeWipPactsSince)
.map { results -> results.map { ConsumerInfo.from(it) }
}
return when (consumersFromBroker) {
is Ok<List<ConsumerInfo>> -> {
val list = consumersFromBroker.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -703,5 +701,4 @@ class ProviderClientSpec extends Specification {
then:
result.contentType.toString() == 'text/plain; charset=ISO-8859-1'
}

}

0 comments on commit b44e8d4

Please sign in to comment.