Skip to content

Commit

Permalink
refactor(Gradle): use delegation instead of inheritance to allow supp…
Browse files Browse the repository at this point in the history
…orting Gradle 8 changes
  • Loading branch information
uglyog committed Jun 24, 2022
1 parent 905ed65 commit 5acf7d2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,58 @@ import au.com.dius.pact.core.pactbroker.ConsumerVersionSelector
import au.com.dius.pact.provider.ConsumerInfo
import au.com.dius.pact.provider.ConsumersGroup
import au.com.dius.pact.provider.IConsumerInfo
import au.com.dius.pact.provider.IProviderInfo
import au.com.dius.pact.provider.PactVerification
import au.com.dius.pact.provider.ProviderInfo
import au.com.dius.pact.provider.gradle.PactPluginBase.Companion.PACT_VERIFY
import groovy.lang.Closure
import mu.KLogging
import org.gradle.api.GradleScriptException
import org.gradle.api.Project
import org.gradle.util.ConfigureUtil
import java.io.File
import java.net.URL

/**
* Extends the provider info to be setup in a gradle build
*/
open class GradleProviderInfo(name: String, val project: Project) : ProviderInfo(name) {
open class GradleProviderInfo(override var name: String, val project: Project) : IProviderInfo {
var providerVersion: Any? = null
var providerTags: Any? = null
var brokerConfig: PactBrokerConsumerConfig? = null
val provider = ProviderInfo(name)

override var protocol: String by provider::protocol
override var host: Any? by provider::host
override var port: Any? by provider::port
override var path: String by provider::path
override var requestFilter: Any? by provider::requestFilter
override var stateChangeRequestFilter: Any? by provider::stateChangeRequestFilter
override var stateChangeUrl: URL? by provider::stateChangeUrl
override var stateChangeUsesBody: Boolean by provider::stateChangeUsesBody
override var stateChangeTeardown: Boolean by provider::stateChangeTeardown
override var packagesToScan: List<String> by provider::packagesToScan
override var verificationType: PactVerification? by provider::verificationType
override var createClient: Any? by provider::createClient
override var insecure: Boolean by provider::insecure
override var trustStore: File? by provider::trustStore
override var trustStorePassword: String? by provider::trustStorePassword
override var consumers: MutableList<IConsumerInfo> by provider::consumers
var startProviderTask: Any? by provider::startProviderTask
var terminateProviderTask: Any? by provider::terminateProviderTask
var isDependencyForPactVerify: Boolean by provider::isDependencyForPactVerify

open fun hasPactWith(consumer: String, closure: Closure<*>): IConsumerInfo {
val consumerInfo = ConsumerInfo(consumer, null, true, listOf(), this.verificationType)
consumers.add(consumerInfo)
provider.consumers.add(consumerInfo)
ConfigureUtil.configure(closure, consumerInfo)
return consumerInfo
}

open fun hasPactsWith(consumersGroupName: String, closure: Closure<*>): List<IConsumerInfo> {
val consumersGroup = ConsumersGroup(consumersGroupName)
ConfigureUtil.configure(closure, consumersGroup)
return setupConsumerListFromPactFiles(consumersGroup)
return provider.setupConsumerListFromPactFiles(consumersGroup)
}

@JvmOverloads
Expand All @@ -47,9 +71,9 @@ open class GradleProviderInfo(name: String, val project: Project) : ProviderInfo
return fromPactBroker
}

override fun hasPactsFromPactBroker(options: Map<String, Any>, pactBrokerUrl: String): List<ConsumerInfo> {
fun hasPactsFromPactBroker(options: Map<String, Any>, pactBrokerUrl: String): List<ConsumerInfo> {
return try {
super.hasPactsFromPactBroker(options, pactBrokerUrl)
provider.hasPactsFromPactBroker(options, pactBrokerUrl)
} catch (e: Exception) {
val verifyTaskName = PACT_VERIFY.toLowerCase()
if (project.gradle.startParameter.taskNames.any { it.toLowerCase().contains(verifyTaskName) }) {
Expand All @@ -76,13 +100,13 @@ open class GradleProviderInfo(name: String, val project: Project) : ProviderInfo
return fromPactBroker
}

override fun hasPactsFromPactBrokerWithSelectors(
fun hasPactsFromPactBrokerWithSelectors(
options: Map<String, Any?>,
pactBrokerUrl: String,
selectors: List<ConsumerVersionSelector>
): List<ConsumerInfo> {
return try {
super.hasPactsFromPactBrokerWithSelectors(options, pactBrokerUrl, selectors)
provider.hasPactsFromPactBrokerWithSelectors(options, pactBrokerUrl, selectors)
} catch (e: Exception) {
val verifyTaskName = PACT_VERIFY.toLowerCase()
if (project.gradle.startParameter.taskNames.any { it.toLowerCase().contains(verifyTaskName) }) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package au.com.dius.pact.provider.gradle

import au.com.dius.pact.provider.IProviderInfo
import au.com.dius.pact.provider.IProviderVerifier
import au.com.dius.pact.provider.ProviderInfo
import au.com.dius.pact.provider.VerificationResult
import org.gradle.api.DefaultTask
import org.gradle.api.GradleScriptException

open class PactVerificationBaseTask : DefaultTask() {
fun runVerification(verifier: IProviderVerifier, providerToVerify: ProviderInfo) {
fun runVerification(verifier: IProviderVerifier, providerToVerify: IProviderInfo) {
val failures = verifier.verifyProvider(providerToVerify).filterIsInstance<VerificationResult.Failed>()
try {
if (failures.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ interface IProviderInfo {
var insecure: Boolean
var trustStore: File?
var trustStorePassword: String?

var consumers: MutableList<IConsumerInfo>
}

interface IConsumerInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ open class ProviderInfo @JvmOverloads constructor (
open var isDependencyForPactVerify: Boolean = true,
override var verificationType: PactVerification? = PactVerification.REQUEST_RESPONSE,
override var packagesToScan: List<String> = emptyList(),
open var consumers: MutableList<IConsumerInfo> = mutableListOf()
override var consumers: MutableList<IConsumerInfo> = mutableListOf()
) : IProviderInfo {

override fun hashCode() = HashCodeBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ interface IProviderVerifier {
/**
* Run the verification for the given provider and return any failures
*/
fun verifyProvider(provider: ProviderInfo): List<VerificationResult>
fun verifyProvider(provider: IProviderInfo): List<VerificationResult>

/**
* Reports the state of the interaction to all the registered reporters
Expand Down Expand Up @@ -797,7 +797,7 @@ open class ProviderVerifier @JvmOverloads constructor (
}
}

override fun verifyProvider(provider: ProviderInfo): List<VerificationResult> {
override fun verifyProvider(provider: IProviderInfo): List<VerificationResult> {
initialiseReporters(provider)

val consumers = provider.consumers.filter(::filterConsumers)
Expand Down

0 comments on commit 5acf7d2

Please sign in to comment.