Skip to content

Commit

Permalink
fix: verifyMessage must pass through any plugin config to the content…
Browse files Browse the repository at this point in the history
… matcher
  • Loading branch information
rholshausen committed Mar 21, 2023
1 parent 6a7b573 commit be98ba2
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ data class PluginData(
/** Any configuration supplied by the plugin */
val configuration: Map<String, Any?>
) {
fun configAsJsonMap(): Map<String, JsonValue> {
return configuration.mapValues { Json.toJson(it.value) }
}

companion object {
fun fromJson(json: JsonValue): PluginData {
val configuration = when (val config = json["configuration"]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ interface MessageInteraction {
* Returns the message content as a String. This will convert the contents if necessary.
*/
fun contentsAsString(): String?

/**
* Any configuration provided by plugins
*/
val pluginConfiguration: Map<String, Map<String, JsonValue>>
}

/**
Expand Down Expand Up @@ -98,6 +103,9 @@ class Message @JvmOverloads constructor(
else -> contents.valueAsString()
}

override val pluginConfiguration: Map<String, MutableMap<String, JsonValue>>
get() = emptyMap()

@Suppress("NestedBlockDepth")
override fun toMap(pactSpecVersion: PactSpecVersion): Map<String, Any?> {
val map: MutableMap<String, Any?> = mutableMapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class MessageSpecificationSpec extends Specification {
@SuppressWarnings('UnnecessaryGetter')
def '#test #matchDesc'() {
expect:
ResponseComparison.compareMessage(expected, actual).bodyMismatches.value.mismatches.isEmpty() == match
ResponseComparison.Companion.newInstance().compareMessage(expected, actual, null, [:])
.bodyMismatches.value.mismatches.isEmpty() == match

where:
[test, match, matchDesc, expected, actual] << loadTestCases()
Expand All @@ -38,5 +39,4 @@ class MessageSpecificationSpec extends Specification {
}
result
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ open class MessageTarget @JvmOverloads constructor(
context: MutableMap<String, Any>,
pending: Boolean
) {
// TODO: Require the plugin config here
val result = verifier.verifyResponseByInvokingProviderMethods(provider, consumer, interaction,
interaction.description, mutableMapOf(), false)
reportTestResult(result, verifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import au.com.dius.pact.provider.IConsumerInfo
import au.com.dius.pact.provider.IProviderInfo
import au.com.dius.pact.provider.IProviderVerifier
import au.com.dius.pact.provider.PactVerification
import au.com.dius.pact.provider.ProviderUtils.pluginConfigForInteraction
import au.com.dius.pact.provider.ProviderVerifier
import au.com.dius.pact.provider.VerificationFailureType
import au.com.dius.pact.provider.VerificationResult
Expand Down Expand Up @@ -104,18 +105,12 @@ data class PactVerificationContext @JvmOverloads constructor(
val expectedResponse = DefaultResponseGenerator.generateResponse(reqResInteraction.response, context,
GeneratorTestMode.Provider, pactPluginData, pluginData)
val actualResponse = target.executeInteraction(client, request)
val pluginContext = pactPluginData.associate {
it.name to PluginConfiguration(
pluginData[it.name].orEmpty().toMutableMap(),
it.configuration.mapValues { (_, v) -> Json.toJson(v) }.toMutableMap()
)
}

listOf(
verifier!!.verifyRequestResponsePact(
expectedResponse, actualResponse, interactionMessage, mutableMapOf(),
reqResInteraction.interactionId.orEmpty(), consumer.pending,
pluginContext
pluginConfigForInteraction(pact, interaction)
)
)
} catch (e: Exception) {
Expand Down Expand Up @@ -156,7 +151,7 @@ data class PactVerificationContext @JvmOverloads constructor(
}
else -> {
return listOf(verifier!!.verifyResponseByInvokingProviderMethods(providerInfo, consumer, interaction,
interaction.description, mutableMapOf(), consumer.pending))
interaction.description, mutableMapOf(), consumer.pending, pluginConfigForInteraction(pact, interaction)))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,6 @@ class PactVerificationContextSpec extends Specification {

then:
1 * verifier.verifyResponseByInvokingProviderMethods(provider, consumer, interaction,
interaction.description, [:], true) >> new VerificationResult.Ok()
interaction.description, [:], true, _) >> new VerificationResult.Ok()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package au.com.dius.pact.provider
import au.com.dius.pact.core.model.DefaultPactReader
import au.com.dius.pact.core.model.FileSource
import au.com.dius.pact.core.model.Interaction
import au.com.dius.pact.core.model.Pact
import au.com.dius.pact.core.support.Json
import au.com.dius.pact.provider.junitsupport.loader.PactLoader
import au.com.dius.pact.provider.junitsupport.loader.PactSource
import io.pact.plugins.jvm.core.PluginConfiguration
import mu.KLogging
import org.apache.commons.io.FilenameUtils
import java.io.File
Expand Down Expand Up @@ -178,4 +181,22 @@ object ProviderUtils : KLogging() {
pactLoader.initLoader(testClass, testInstance)
return pactLoader
}

@JvmStatic
fun pluginConfigForInteraction(pact: Pact?, interaction: Interaction): Map<String, PluginConfiguration> {
return if (pact != null && pact.isV4Pact()) {
val v4Pact = pact.asV4Pact().unwrap()
val v4Interaction = interaction.asV4Interaction()
val pactPluginData = v4Pact.pluginData()
val interactionPluginData = v4Interaction.pluginConfiguration.toMap()
pactPluginData.associate {
it.name to PluginConfiguration(
interactionPluginData[it.name].orEmpty().toMutableMap(),
it.configuration.mapValues { (_, v) -> Json.toJson(v) }.toMutableMap()
)
}
} else {
emptyMap()
}
}
}
Loading

0 comments on commit be98ba2

Please sign in to comment.