Skip to content

Commit ea14386

Browse files
committed
fix: Need to pass any provider state data through to the plugins
1 parent aa5770a commit ea14386

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PluginTestTarget.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import io.pact.plugins.jvm.core.CatalogueEntry
1515
import io.pact.plugins.jvm.core.CatalogueManager
1616
import io.pact.plugins.jvm.core.DefaultPluginManager
1717
import io.pact.plugins.jvm.core.PactPluginNotFoundException
18+
import io.pact.plugins.jvm.core.PluginManager
1819
import java.io.File
1920
import java.net.URL
2021

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

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

9092
override fun prepareRequest(pact: Pact, interaction: Interaction, context: MutableMap<String, Any>): Pair<Any, Any?>? {
9193
return when (val v4pact = pact.asV4Pact()) {
92-
is Ok -> when (val result = DefaultPluginManager.prepareValidationForInteraction(transportEntry, v4pact.value,
93-
interaction.asV4Interaction(), config)) {
94-
is Ok -> RequestDataToBeVerified(result.value) to transportEntry
95-
is Err -> throw RuntimeException("Failed to configure the interaction for verification - ${result.error}")
94+
is Ok -> {
95+
val testContext = config.toMutableMap()
96+
if (context.containsKey("providerState")) {
97+
testContext["providerState"] = context["providerState"]
98+
}
99+
when (val result = pluginManager.prepareValidationForInteraction(transportEntry, v4pact.value,
100+
interaction.asV4Interaction(), testContext)) {
101+
is Ok -> RequestDataToBeVerified(result.value) to transportEntry
102+
is Err -> throw RuntimeException("Failed to configure the interaction for verification - ${result.error}")
103+
}
96104
}
97105
is Err -> throw RuntimeException("PluginTestTarget can only be used with V4 Pacts")
98106
}
@@ -113,7 +121,7 @@ class PluginTestTarget(private val config: MutableMap<String, Any?> = mutableMap
113121
when (val v4pact = pact.asV4Pact()) {
114122
is Ok -> {
115123
for (plugin in v4pact.value.pluginData()) {
116-
when (DefaultPluginManager.loadPlugin(plugin.name, plugin.version)) {
124+
when (pluginManager.loadPlugin(plugin.name, plugin.version)) {
117125
is Ok -> {}
118126
is Err -> throw PactPluginNotFoundException(plugin.name, plugin.version)
119127
}

provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/PluginTestTargetSpec.groovy

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
package au.com.dius.pact.provider.junit5
22

3-
import au.com.dius.pact.core.model.V4Interaction
43
import spock.lang.Specification
54
import au.com.dius.pact.core.model.RequestResponseInteraction
65
import au.com.dius.pact.core.model.messaging.Message
6+
import au.com.dius.pact.core.matchers.generators.ArrayContainsJsonGenerator
7+
import au.com.dius.pact.core.model.Consumer
8+
import au.com.dius.pact.core.model.OptionalBody
9+
import au.com.dius.pact.core.model.Provider
10+
import au.com.dius.pact.core.model.V4Interaction
11+
import au.com.dius.pact.core.model.V4Pact
12+
import au.com.dius.pact.core.support.Result
13+
import io.pact.plugins.jvm.core.CatalogueEntry
14+
import io.pact.plugins.jvm.core.CatalogueEntryProviderType
15+
import io.pact.plugins.jvm.core.CatalogueEntryType
16+
import io.pact.plugins.jvm.core.InteractionVerificationData
17+
import io.pact.plugins.jvm.core.PluginManager
718

819
class PluginTestTargetSpec extends Specification {
920
def 'supports any V4 interaction'() {
@@ -18,4 +29,38 @@ class PluginTestTargetSpec extends Specification {
1829
new V4Interaction.SynchronousMessages('test') | true
1930
new V4Interaction.SynchronousHttp('test') | true
2031
}
32+
33+
def 'when calling a plugin, prepareRequest must merge the provider state test context config'() {
34+
given:
35+
def config = [
36+
transport: 'grpc',
37+
host: 'localhost',
38+
port: 38525
39+
]
40+
def target = new PluginTestTarget(config)
41+
target.transportEntry = new CatalogueEntry(CatalogueEntryType.CONTENT_MATCHER, CatalogueEntryProviderType.PLUGIN,
42+
'null', 'null')
43+
def interaction = new V4Interaction.SynchronousHttp(null, 'test interaction')
44+
def pact = new V4Pact(new Consumer(), new Provider(), [ interaction ])
45+
def context = [
46+
providerState: [a: 100, b: 200],
47+
ArrayContainsJsonGenerator: ArrayContainsJsonGenerator.INSTANCE
48+
]
49+
def expectedContext = [
50+
transport: 'grpc',
51+
host: 'localhost',
52+
port: 38525,
53+
providerState: [a: 100, b: 200]
54+
]
55+
def pluginManager = Mock(PluginManager)
56+
target.pluginManager = pluginManager
57+
58+
when:
59+
target.prepareRequest(pact, interaction, context)
60+
61+
then:
62+
noExceptionThrown()
63+
1 * pluginManager.prepareValidationForInteraction(_, _, _, expectedContext) >> new Result.Ok(
64+
new InteractionVerificationData(OptionalBody.missing(), [:]))
65+
}
2166
}

0 commit comments

Comments
 (0)