Skip to content

Commit

Permalink
fix: Need to pass any provider state data through to the plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Aug 9, 2024
1 parent aa5770a commit ea14386
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.pact.plugins.jvm.core.CatalogueEntry
import io.pact.plugins.jvm.core.CatalogueManager
import io.pact.plugins.jvm.core.DefaultPluginManager
import io.pact.plugins.jvm.core.PactPluginNotFoundException
import io.pact.plugins.jvm.core.PluginManager
import java.io.File
import java.net.URL

Expand Down Expand Up @@ -79,6 +80,7 @@ data class PluginProvider(
*/
class PluginTestTarget(private val config: MutableMap<String, Any?> = mutableMapOf()) : TestTarget {
private lateinit var transportEntry: CatalogueEntry
private var pluginManager: PluginManager = DefaultPluginManager

override val userConfig: Map<String, Any?>
get() = config
Expand All @@ -89,10 +91,16 @@ class PluginTestTarget(private val config: MutableMap<String, Any?> = mutableMap

override fun prepareRequest(pact: Pact, interaction: Interaction, context: MutableMap<String, Any>): Pair<Any, Any?>? {
return when (val v4pact = pact.asV4Pact()) {
is Ok -> when (val result = DefaultPluginManager.prepareValidationForInteraction(transportEntry, v4pact.value,
interaction.asV4Interaction(), config)) {
is Ok -> RequestDataToBeVerified(result.value) to transportEntry
is Err -> throw RuntimeException("Failed to configure the interaction for verification - ${result.error}")
is Ok -> {
val testContext = config.toMutableMap()
if (context.containsKey("providerState")) {
testContext["providerState"] = context["providerState"]
}
when (val result = pluginManager.prepareValidationForInteraction(transportEntry, v4pact.value,
interaction.asV4Interaction(), testContext)) {
is Ok -> RequestDataToBeVerified(result.value) to transportEntry
is Err -> throw RuntimeException("Failed to configure the interaction for verification - ${result.error}")
}
}
is Err -> throw RuntimeException("PluginTestTarget can only be used with V4 Pacts")
}
Expand All @@ -113,7 +121,7 @@ class PluginTestTarget(private val config: MutableMap<String, Any?> = mutableMap
when (val v4pact = pact.asV4Pact()) {
is Ok -> {
for (plugin in v4pact.value.pluginData()) {
when (DefaultPluginManager.loadPlugin(plugin.name, plugin.version)) {
when (pluginManager.loadPlugin(plugin.name, plugin.version)) {
is Ok -> {}
is Err -> throw PactPluginNotFoundException(plugin.name, plugin.version)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package au.com.dius.pact.provider.junit5

import au.com.dius.pact.core.model.V4Interaction
import spock.lang.Specification
import au.com.dius.pact.core.model.RequestResponseInteraction
import au.com.dius.pact.core.model.messaging.Message
import au.com.dius.pact.core.matchers.generators.ArrayContainsJsonGenerator
import au.com.dius.pact.core.model.Consumer
import au.com.dius.pact.core.model.OptionalBody
import au.com.dius.pact.core.model.Provider
import au.com.dius.pact.core.model.V4Interaction
import au.com.dius.pact.core.model.V4Pact
import au.com.dius.pact.core.support.Result
import io.pact.plugins.jvm.core.CatalogueEntry
import io.pact.plugins.jvm.core.CatalogueEntryProviderType
import io.pact.plugins.jvm.core.CatalogueEntryType
import io.pact.plugins.jvm.core.InteractionVerificationData
import io.pact.plugins.jvm.core.PluginManager

class PluginTestTargetSpec extends Specification {
def 'supports any V4 interaction'() {
Expand All @@ -18,4 +29,38 @@ class PluginTestTargetSpec extends Specification {
new V4Interaction.SynchronousMessages('test') | true
new V4Interaction.SynchronousHttp('test') | true
}

def 'when calling a plugin, prepareRequest must merge the provider state test context config'() {
given:
def config = [
transport: 'grpc',
host: 'localhost',
port: 38525
]
def target = new PluginTestTarget(config)
target.transportEntry = new CatalogueEntry(CatalogueEntryType.CONTENT_MATCHER, CatalogueEntryProviderType.PLUGIN,
'null', 'null')
def interaction = new V4Interaction.SynchronousHttp(null, 'test interaction')
def pact = new V4Pact(new Consumer(), new Provider(), [ interaction ])
def context = [
providerState: [a: 100, b: 200],
ArrayContainsJsonGenerator: ArrayContainsJsonGenerator.INSTANCE
]
def expectedContext = [
transport: 'grpc',
host: 'localhost',
port: 38525,
providerState: [a: 100, b: 200]
]
def pluginManager = Mock(PluginManager)
target.pluginManager = pluginManager

when:
target.prepareRequest(pact, interaction, context)

then:
noExceptionThrown()
1 * pluginManager.prepareValidationForInteraction(_, _, _, expectedContext) >> new Result.Ok(
new InteractionVerificationData(OptionalBody.missing(), [:]))
}
}

0 comments on commit ea14386

Please sign in to comment.