diff --git a/core/model/src/main/kotlin/au/com/dius/pact/core/model/matchingrules/MatchingRuleCategory.kt b/core/model/src/main/kotlin/au/com/dius/pact/core/model/matchingrules/MatchingRuleCategory.kt index 478bc3ec4..7eee5705e 100644 --- a/core/model/src/main/kotlin/au/com/dius/pact/core/model/matchingrules/MatchingRuleCategory.kt +++ b/core/model/src/main/kotlin/au/com/dius/pact/core/model/matchingrules/MatchingRuleCategory.kt @@ -238,4 +238,9 @@ data class MatchingRuleCategory @JvmOverloads constructor( it.key.replace(prefix, newRoot) }.toMutableMap()) } + + /** + * If this MatchingRuleCategory is not empty, return it, otherwise, return the other set of rules + */ + fun orElse(otherRules: MatchingRuleCategory): MatchingRuleCategory = if (isEmpty()) otherRules else this } diff --git a/core/model/src/test/groovy/au/com/dius/pact/core/model/matchingrules/MatchingRuleCategorySpec.groovy b/core/model/src/test/groovy/au/com/dius/pact/core/model/matchingrules/MatchingRuleCategorySpec.groovy index c1fd05152..59593861a 100644 --- a/core/model/src/test/groovy/au/com/dius/pact/core/model/matchingrules/MatchingRuleCategorySpec.groovy +++ b/core/model/src/test/groovy/au/com/dius/pact/core/model/matchingrules/MatchingRuleCategorySpec.groovy @@ -113,4 +113,29 @@ class MatchingRuleCategorySpec extends Specification { category.matchingRules[''].rules == [ new RegexMatcher('/api/test/\\d{1,8}', null) ] category.matchingRules[''].ruleLogic == RuleLogic.OR } + + @Issue('#1509') + def 'orElse can default to another rule set if empty'() { + given: + def categoryA = new MatchingRuleCategory('A') + def categoryB = new MatchingRuleCategory('B', [ + a: new MatchingRuleGroup(), + b: new MatchingRuleGroup() + ]) + def categoryC = new MatchingRuleCategory('C', [ + a: new MatchingRuleGroup([ TypeMatcher.INSTANCE ]), + b: new MatchingRuleGroup() + ]) + def categoryD = new MatchingRuleCategory('D', [ + a: new MatchingRuleGroup([ TypeMatcher.INSTANCE ]), + b: new MatchingRuleGroup([ TypeMatcher.INSTANCE ]) + ]) + + expect: + categoryA.orElse(categoryA) == categoryA + categoryA.orElse(categoryB) == categoryB + categoryA.orElse(categoryC) == categoryC + categoryC.orElse(categoryA) == categoryC + categoryC.orElse(categoryD) == categoryC + } } diff --git a/provider/src/main/kotlin/au/com/dius/pact/provider/ResponseComparison.kt b/provider/src/main/kotlin/au/com/dius/pact/provider/ResponseComparison.kt index caf21e016..a049d4233 100755 --- a/provider/src/main/kotlin/au/com/dius/pact/provider/ResponseComparison.kt +++ b/provider/src/main/kotlin/au/com/dius/pact/provider/ResponseComparison.kt @@ -241,7 +241,9 @@ class ResponseComparison( ): ComparisonResult { val (bodyMismatches, metadataMismatches) = when (message) { is V4Interaction.AsynchronousMessage -> { - val bodyContext = MatchingContext(message.contents.matchingRules.rulesForCategory("body"), + val bodyContext = MatchingContext( + message.contents.matchingRules.rulesForCategory("content") + .orElse(message.contents.matchingRules.rulesForCategory("body")), true, pluginConfiguration) val metadataContext = MatchingContext(message.contents.matchingRules.rulesForCategory("metadata"), true, pluginConfiguration) @@ -258,7 +260,8 @@ class ResponseComparison( } is Message -> { - val bodyContext = MatchingContext(message.matchingRules.rulesForCategory("body"), + val bodyContext = MatchingContext(message.matchingRules.rulesForCategory("content") + .orElse(message.matchingRules.rulesForCategory("body")), true, pluginConfiguration) val metadataContext = MatchingContext(message.matchingRules.rulesForCategory("metadata"), true, pluginConfiguration) @@ -282,7 +285,7 @@ class ResponseComparison( override fun compareSynchronousMessage( interaction: V4Interaction.SynchronousMessages, - actual: OptionalBody, + body: OptionalBody, messageMetadata: Map?, pluginConfiguration: Map ): ComparisonResult { @@ -296,7 +299,7 @@ class ResponseComparison( true, pluginConfiguration) val metadataContext = MatchingContext(messageContents.matchingRules.rulesForCategory("metadata"), true, pluginConfiguration) - val bodyMismatches = compareMessageBody(interaction, actual, bodyContext) + val bodyMismatches = compareMessageBody(interaction, body, bodyContext) val metadataMismatches = when (messageMetadata) { null -> emptyList() else -> Matching.compareMessageMetadata(messageContents.metadata, messageMetadata, metadataContext) @@ -304,7 +307,7 @@ class ResponseComparison( val messageContentType = messageContents.getContentType().or(ContentType.TEXT_PLAIN) val responseComparison = ResponseComparison( mapOf("Content-Type" to listOf(messageContentType.toString())), messageContents.contents, - messageContentType.isJson(), messageContentType, actual) + messageContentType.isJson(), messageContentType, body) val bodyResult = responseComparison.bodyResult(bodyMismatches, SystemPropertyResolver) return ComparisonResult(bodyMismatches = bodyResult, metadataMismatches = metadataMismatches.groupBy { it.key })