diff --git a/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PluginTestTarget.kt b/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PluginTestTarget.kt index 4b14fda4b..942b075d2 100644 --- a/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PluginTestTarget.kt +++ b/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PluginTestTarget.kt @@ -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 @@ -79,6 +80,7 @@ data class PluginProvider( */ class PluginTestTarget(private val config: MutableMap = mutableMapOf()) : TestTarget { private lateinit var transportEntry: CatalogueEntry + private var pluginManager: PluginManager = DefaultPluginManager override val userConfig: Map get() = config @@ -89,10 +91,16 @@ class PluginTestTarget(private val config: MutableMap = mutableMap override fun prepareRequest(pact: Pact, interaction: Interaction, context: MutableMap): Pair? { 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") } @@ -113,7 +121,7 @@ class PluginTestTarget(private val config: MutableMap = 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) } diff --git a/provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/PluginTestTargetSpec.groovy b/provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/PluginTestTargetSpec.groovy index 637d98909..988bc207d 100644 --- a/provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/PluginTestTargetSpec.groovy +++ b/provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/PluginTestTargetSpec.groovy @@ -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'() { @@ -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(), [:])) + } }