Skip to content

Commit 0e5bdb4

Browse files
author
Ronald Holshausen
committedOct 27, 2019
feat: convert number type matchers to type matchers when spec version < 3 #958
1 parent 7d83a95 commit 0e5bdb4

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed
 

‎consumer/pact-jvm-consumer/src/test/groovy/au/com/dius/pact/consumer/dsl/PactDslJsonBodySpec.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,10 @@ class PactDslJsonBodySpec extends Specification {
305305
pactDslJsonBody.matchers.toMap(PactSpecVersion.V2) == [
306306
'$.body.contactDetails.mobile.countryCode': [match: 'type'],
307307
'$.body.contactDetails.mobile.prefix': [match: 'type'],
308-
'$.body.contactDetails.mobile.subscriberNumber': [match: 'number'],
308+
'$.body.contactDetails.mobile.subscriberNumber': [match: 'type'],
309309
'$.body.contactDetails2.mobile.countryCode': [match: 'type'],
310310
'$.body.contactDetails2.mobile.prefix': [match: 'type'],
311-
'$.body.contactDetails2.mobile.subscriberNumber': [match: 'number']
311+
'$.body.contactDetails2.mobile.subscriberNumber': [match: 'type']
312312
]
313313
pactDslJsonBody.generators.toMap(PactSpecVersion.V3) == [
314314
body: [

‎core/model/src/main/kotlin/au/com/dius/pact/core/model/matchingrules/MatchingRules.kt

+21-2
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,57 @@ enum class RuleLogic {
1515
* Matching rule
1616
*/
1717
interface MatchingRule {
18+
@Deprecated("Use version that takes a spec version")
1819
fun toMap(): Map<String, Any?>
20+
fun toMap(spec: PactSpecVersion): Map<String, Any?>
1921
}
2022

2123
/**
2224
* Matching Rule for dates
2325
*/
2426
data class DateMatcher @JvmOverloads constructor(val format: String = "yyyy-MM-dd") : MatchingRule {
2527
override fun toMap() = mapOf("match" to "date", "date" to format)
28+
override fun toMap(spec: PactSpecVersion) = toMap()
2629
}
2730

2831
/**
2932
* Matching rule for equality
3033
*/
3134
object EqualsMatcher : MatchingRule {
3235
override fun toMap() = mapOf("match" to "equality")
36+
override fun toMap(spec: PactSpecVersion) = toMap()
3337
}
3438

3539
/**
3640
* Matcher for a substring in a string
3741
*/
3842
data class IncludeMatcher(val value: String) : MatchingRule {
3943
override fun toMap() = mapOf("match" to "include", "value" to value)
44+
override fun toMap(spec: PactSpecVersion) = toMap()
4045
}
4146

4247
/**
4348
* Type matching with a maximum size
4449
*/
4550
data class MaxTypeMatcher(val max: Int) : MatchingRule {
4651
override fun toMap() = mapOf("match" to "type", "max" to max)
52+
override fun toMap(spec: PactSpecVersion) = toMap()
4753
}
4854

4955
/**
5056
* Type matcher with a minimum size and maximum size
5157
*/
5258
data class MinMaxTypeMatcher(val min: Int, val max: Int) : MatchingRule {
5359
override fun toMap() = mapOf("match" to "type", "min" to min, "max" to max)
60+
override fun toMap(spec: PactSpecVersion) = toMap()
5461
}
5562

5663
/**
5764
* Type matcher with a minimum size
5865
*/
5966
data class MinTypeMatcher(val min: Int) : MatchingRule {
6067
override fun toMap() = mapOf("match" to "type", "min" to min)
68+
override fun toMap(spec: PactSpecVersion) = toMap()
6169
}
6270

6371
/**
@@ -70,49 +78,60 @@ data class NumberTypeMatcher(val numberType: NumberType) : MatchingRule {
7078
DECIMAL
7179
}
7280

73-
override fun toMap() = mapOf("match" to numberType.name.toLowerCase())
81+
override fun toMap() = toMap(PactSpecVersion.V3)
82+
override fun toMap(spec: PactSpecVersion) = if (spec >= PactSpecVersion.V3) {
83+
mapOf("match" to numberType.name.toLowerCase())
84+
} else {
85+
TypeMatcher.toMap(spec)
86+
}
7487
}
7588

7689
/**
7790
* Regular Expression Matcher
7891
*/
7992
data class RegexMatcher @JvmOverloads constructor (val regex: String, val example: String? = null) : MatchingRule {
8093
override fun toMap() = mapOf("match" to "regex", "regex" to regex)
94+
override fun toMap(spec: PactSpecVersion) = toMap()
8195
}
8296

8397
/**
8498
* Matcher for time values
8599
*/
86100
data class TimeMatcher @JvmOverloads constructor(val format: String = "HH:mm:ss") : MatchingRule {
87101
override fun toMap() = mapOf("match" to "time", "time" to format)
102+
override fun toMap(spec: PactSpecVersion) = toMap()
88103
}
89104

90105
/**
91106
* Matcher for time values
92107
*/
93108
data class TimestampMatcher @JvmOverloads constructor(val format: String = "yyyy-MM-dd HH:mm:ssZZZ") : MatchingRule {
94109
override fun toMap() = mapOf("match" to "timestamp", "timestamp" to format)
110+
override fun toMap(spec: PactSpecVersion) = toMap()
95111
}
96112

97113
/**
98114
* Matcher for types
99115
*/
100116
object TypeMatcher : MatchingRule {
101117
override fun toMap() = mapOf("match" to "type")
118+
override fun toMap(spec: PactSpecVersion) = toMap()
102119
}
103120

104121
/**
105122
* Matcher for null values
106123
*/
107124
object NullMatcher : MatchingRule {
108125
override fun toMap() = mapOf("match" to "null")
126+
override fun toMap(spec: PactSpecVersion) = toMap()
109127
}
110128

111129
/**
112130
* Matcher for values in a map, ignoring the keys
113131
*/
114132
object ValuesMatcher : MatchingRule {
115133
override fun toMap() = mapOf("match" to "values")
134+
override fun toMap(spec: PactSpecVersion) = toMap()
116135
}
117136

118137
data class MatchingRuleGroup @JvmOverloads constructor(
@@ -121,7 +140,7 @@ data class MatchingRuleGroup @JvmOverloads constructor(
121140
) {
122141
fun toMap(pactSpecVersion: PactSpecVersion): Map<String, Any?> {
123142
return if (pactSpecVersion < PactSpecVersion.V3) {
124-
rules.first().toMap()
143+
rules.first().toMap(pactSpecVersion)
125144
} else {
126145
mapOf("matchers" to rules.map { it.toMap() }, "combine" to ruleLogic.name)
127146
}

‎core/model/src/test/groovy/au/com/dius/pact/core/model/matchingrules/MatchingRuleGroupSpec.groovy

+24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package au.com.dius.pact.core.model.matchingrules
22

3+
import au.com.dius.pact.core.model.PactSpecVersion
34
import spock.lang.Specification
45
import spock.lang.Unroll
56

7+
@SuppressWarnings('LineLength')
68
class MatchingRuleGroupSpec extends Specification {
79

810
@Unroll
@@ -44,4 +46,26 @@ class MatchingRuleGroupSpec extends Specification {
4446
expect:
4547
new MatchingRuleGroup().ruleLogic == RuleLogic.AND
4648
}
49+
50+
@Unroll
51+
def 'Converts number matchers to type matchers when spec is < V3'() {
52+
expect:
53+
new MatchingRuleGroup([matcher]).toMap(PactSpecVersion.V2) == map
54+
55+
where:
56+
matcher | map
57+
EqualsMatcher.INSTANCE | [match: 'equality']
58+
new RegexMatcher('.*') | [match: 'regex', regex: '.*']
59+
TypeMatcher.INSTANCE | [match: 'type']
60+
new NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL) | [match: 'type']
61+
new MinTypeMatcher(1) | [match: 'type', min: 1]
62+
new MaxTypeMatcher(1) | [match: 'type', max: 1]
63+
new MinMaxTypeMatcher(2, 3) | [match: 'type', max: 3, min: 2]
64+
new TimestampMatcher() | [match: 'timestamp', timestamp: 'yyyy-MM-dd HH:mm:ssZZZ']
65+
new TimeMatcher() | [match: 'time', time: 'HH:mm:ss']
66+
new DateMatcher() | [match: 'date', date: 'yyyy-MM-dd']
67+
new IncludeMatcher('A') | [match: 'include', value: 'A']
68+
ValuesMatcher.INSTANCE | [match: 'values']
69+
NullMatcher.INSTANCE | [match: 'null']
70+
}
4771
}

0 commit comments

Comments
 (0)
Please sign in to comment.