Skip to content

Commit

Permalink
feat: update the general verifier to support verification via plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Mar 16, 2023
1 parent 9a7d4d0 commit 83a17c9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 14 deletions.
6 changes: 3 additions & 3 deletions provider/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ dependencies {
api project(':core:pactbroker')
api 'org.apache.httpcomponents.client5:httpclient5'
api 'io.github.classgraph:classgraph:4.8.129'
api('io.pact.plugin.driver:core') {
exclude group: 'au.com.dius.pact.core'
}

implementation 'commons-io:commons-io:2.11.0'
implementation 'org.slf4j:slf4j-api'
implementation('io.pact.plugin.driver:core') {
exclude group: 'au.com.dius.pact.core'
}
implementation 'org.apache.commons:commons-lang3'
implementation 'org.apache.commons:commons-collections4'
implementation 'com.github.ajalt:mordant:1.2.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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 io.pact.plugins.jvm.core.CatalogueEntry
import io.pact.plugins.jvm.core.CatalogueManager
import mu.KLogging
import org.gradle.api.GradleScriptException
import org.gradle.api.Project
Expand Down Expand Up @@ -51,6 +53,9 @@ open class GradleProviderInfo @Inject constructor(
var terminateProviderTask: Any? by provider::terminateProviderTask
var isDependencyForPactVerify: Boolean by provider::isDependencyForPactVerify

override val transportEntry: CatalogueEntry?
get() = CatalogueManager.lookupEntry("transport/$protocol")

open fun hasPactWith(consumer: String, closure: Closure<GradleConsumerInfo>): IConsumerInfo {
val consumerInfo = objectFactory.newInstance(GradleConsumerInfo::class.java, consumer)
consumerInfo.name = consumer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ data class PluginProvider(
config["port"] = value
}

override val transportEntry: CatalogueEntry?
get() = CatalogueManager.lookupEntry("transport/${config.getOrDefault("transport", "http")}")

override var verificationType: PactVerification? = PactVerification.PLUGIN

override var path: String = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import au.com.dius.pact.core.support.json.JsonValue
import groovy.lang.Binding
import groovy.lang.Closure
import groovy.lang.GroovyShell
import io.pact.plugins.jvm.core.CatalogueEntry
import mu.KLogging
import org.apache.hc.client5.http.classic.methods.HttpDelete
import org.apache.hc.client5.http.classic.methods.HttpGet
Expand Down Expand Up @@ -56,6 +57,7 @@ interface IProviderInfo {
var port: Any?
var path: String
var name: String
val transportEntry: CatalogueEntry?

val requestFilter: Any?
val stateChangeRequestFilter: Any?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import au.com.dius.pact.core.pactbroker.PactBrokerClientConfig
import au.com.dius.pact.core.support.Result
import au.com.dius.pact.core.support.Utils
import au.com.dius.pact.core.support.mapOk
import io.pact.plugins.jvm.core.CatalogueEntry
import io.pact.plugins.jvm.core.CatalogueManager
import mu.KLogging
import org.apache.commons.lang3.builder.HashCodeBuilder
import org.apache.commons.lang3.builder.ToStringBuilder
Expand Down Expand Up @@ -172,6 +174,9 @@ open class ProviderInfo @JvmOverloads constructor (
return consumers
}

override val transportEntry: CatalogueEntry?
get() = CatalogueManager.lookupEntry("transport/$protocol")

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import groovy.lang.Closure
import io.github.classgraph.ClassGraph
import io.pact.plugins.jvm.core.CatalogueEntry
import io.pact.plugins.jvm.core.DefaultPluginManager
import io.pact.plugins.jvm.core.InteractionVerificationData
import io.pact.plugins.jvm.core.InteractionVerificationDetails
import mu.KLogging
import java.io.File
Expand Down Expand Up @@ -227,6 +226,7 @@ interface IProviderVerifier {
/**
* Verifies the response from the provider against the interaction
*/
@Suppress("LongParameterList")
fun verifyResponseFromProvider(
provider: IProviderInfo,
interaction: SynchronousRequestResponse,
Expand All @@ -240,6 +240,7 @@ interface IProviderVerifier {
/**
* Verifies the interaction by invoking a method on a provider test class
*/
@Suppress("LongParameterList")
fun verifyResponseByInvokingProviderMethods(
providerInfo: IProviderInfo,
consumer: IConsumerInfo,
Expand All @@ -249,6 +250,7 @@ interface IProviderVerifier {
pending: Boolean
): VerificationResult

@Suppress("LongParameterList")
fun verifyResponseByFactory(
providerInfo: IProviderInfo,
consumer: IConsumerInfo,
Expand All @@ -261,6 +263,7 @@ interface IProviderVerifier {
/**
* Compares the expected and actual responses
*/
@Suppress("LongParameterList")
fun verifyRequestResponsePact(
expectedResponse: IResponse,
actualResponse: ProviderResponse,
Expand Down Expand Up @@ -326,6 +329,7 @@ open class ProviderVerifier @JvmOverloads constructor (
override var providerVersion: Supplier<String> = ProviderVersion {
SystemPropertyResolver.resolveValue(PACT_PROVIDER_VERSION, "")
},
@Deprecated("Use version that returns multiple tags", replaceWith = ReplaceWith("providerTags"))
override var providerTag: Supplier<String?>? = Supplier {
SystemPropertyResolver.resolveValue(PACT_PROVIDER_TAG, "")
},
Expand Down Expand Up @@ -357,7 +361,7 @@ open class ProviderVerifier @JvmOverloads constructor (
return when {
!projectHasProperty.apply(PACT_VERIFIER_PUBLISH_RESULTS) ->
verificationReporter.publishingResultsDisabled(SystemPropertyResolver)
else -> projectGetProperty.apply(PACT_VERIFIER_PUBLISH_RESULTS)?.toLowerCase() != "true"
else -> projectGetProperty.apply(PACT_VERIFIER_PUBLISH_RESULTS)?.lowercase() != "true"
}
}

Expand Down Expand Up @@ -653,12 +657,26 @@ open class ProviderVerifier @JvmOverloads constructor (
reporters.forEach { it.finaliseReport() }
}

@JvmOverloads
@Deprecated("Use the version of verifyInteraction that passes in the full Pact and transport entry",
ReplaceWith("verifyInteraction(provider, consumer, failures, interaction, pact, transportEntry, providerClient)")
)
fun verifyInteraction(
provider: IProviderInfo,
consumer: IConsumerInfo,
failures: MutableMap<String, Any>,
interaction: Interaction,
providerClient: ProviderClient = ProviderClient(provider, HttpClientFactory())
): VerificationResult = verifyInteraction(provider, consumer, failures, interaction, null, null, providerClient)

@JvmOverloads
fun verifyInteraction(
provider: IProviderInfo,
consumer: IConsumerInfo,
failures: MutableMap<String, Any>,
interaction: Interaction,
pact: Pact?,
transportEntry: CatalogueEntry?,
providerClient: ProviderClient = ProviderClient(provider, HttpClientFactory())
): VerificationResult {
Metrics.sendMetrics(MetricEvent.ProviderVerificationRan(1, verificationSource.ifNullOrEmpty { "unknown" }!!))
Expand Down Expand Up @@ -701,13 +719,28 @@ open class ProviderVerifier @JvmOverloads constructor (
}
PactVerification.PLUGIN -> {
logger.debug { "Verifying via plugin" }
// when (val result = DefaultPluginManager.prepareValidationForInteraction(transportEntry, v4pact.value,
// interaction.asV4Interaction(), config)) {
// is Ok -> result.value to null
// is Err -> throw RuntimeException("Failed to configure the interaction for verification - ${result.error}")
// }
// verifyInteractionViaPlugin(provider, consumer, interaction, providerClient, )
TODO("Not yet implemented")
if (pact != null && pact.isV4Pact() && transportEntry != null) {
val v4pact = pact.asV4Pact().unwrap()
val v4Interaction = interaction.asV4Interaction()
val config = mutableMapOf(
"host" to provider.host.toString(),
"port" to provider.port
)

for ((k, v) in stateChangeResult.stateChangeResult.value) {
config[k] = v
}

val request = when (val result = DefaultPluginManager.prepareValidationForInteraction(transportEntry,
v4pact, v4Interaction, config)) {
is Ok -> result.value
is Err -> throw RuntimeException("Failed to configure the interaction for verification - ${result.error}")
}
verifyInteractionViaPlugin(provider, consumer, v4pact, v4Interaction, providerClient, request, context)
} else {
throw RuntimeException("INTERNAL ERROR: Verification via a plugin requires the version of " +
"verifyInteraction to be called with the full V4 Pact model")
}
}
else -> {
logger.debug { "Verifying via provider methods" }
Expand Down Expand Up @@ -827,7 +860,7 @@ open class ProviderVerifier @JvmOverloads constructor (
client: ProviderClient
) = verifyResponseFromProvider(provider, interaction, interactionMessage, failures, client, mutableMapOf(), false)

@Suppress("TooGenericExceptionCaught")
@Suppress("TooGenericExceptionCaught", "LongParameterList")
override fun verifyResponseFromProvider(
provider: IProviderInfo,
interaction: SynchronousRequestResponse,
Expand Down Expand Up @@ -893,7 +926,7 @@ open class ProviderVerifier @JvmOverloads constructor (
VerificationResult.Ok()
} else {
val result = pact.interactions.map {
verifyInteraction(provider, consumer, failures, it)
verifyInteraction(provider, consumer, failures, it, pact, provider.transportEntry)
}.reduce { acc, result -> acc.merge(result) }
result.merge(when {
pact.isFiltered() -> {
Expand Down

0 comments on commit 83a17c9

Please sign in to comment.