Skip to content

Commit

Permalink
fix: write date/time matchers in the correct format as per the spec #…
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Dec 23, 2022
1 parent aa11791 commit b1d6c01
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class MatchersSpec extends Specification {
'regexp' | '[0-9]+' | [match: 'regex', regex: '[0-9]+']
'hexValue' | '1234' | [match: 'regex', regex: '[0-9a-fA-F]+']
'ipAddress' | '1.2.3.4' | [match: 'regex', regex: '(\\d{1,3}\\.)+\\d{1,3}']
'datetime' | 'yyyy-mm-dd' | [match: 'timestamp', timestamp: 'yyyy-mm-dd']
'datetime' | 'yyyy-mm-dd' | [match: 'timestamp', timestamp: 'yyyy-mm-dd']
'date' | 'yyyy-mm-dd' | [match: 'date', date: 'yyyy-mm-dd']
'time' | 'yyyy-mm-dd' | [match: 'time', time: 'yyyy-mm-dd']
'datetime' | 'yyyy-mm-dd' | [match: 'timestamp', format: 'yyyy-mm-dd']
'datetime' | 'yyyy-mm-dd' | [match: 'timestamp', format: 'yyyy-mm-dd']
'date' | 'yyyy-mm-dd' | [match: 'date', format: 'yyyy-mm-dd']
'time' | 'yyyy-mm-dd' | [match: 'time', format: 'yyyy-mm-dd']
'uuid' | '12345678-1234-1234-1234-123456789012' | [match: 'regex', regex: '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}']
'equalTo' | 'value' | [match: 'equality']
'includesStr' | 'value' | [match: 'include', value: 'value']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ class DslPartSpec extends Specification {

matcherMethod | param | matcherDefinition
'regexp' | '[0-9]+' | [match: 'regex', regex: '[0-9]+']
'matchTimestamp' | 'yyyy-mm-dd' | [match: 'timestamp', timestamp: 'yyyy-mm-dd']
'matchDate' | 'yyyy-mm-dd' | [match: 'date', date: 'yyyy-mm-dd']
'matchTime' | 'yyyy-mm-dd' | [match: 'time', time: 'yyyy-mm-dd']
'matchTimestamp' | 'yyyy-mm-dd' | [match: 'timestamp', format: 'yyyy-mm-dd']
'matchDate' | 'yyyy-mm-dd' | [match: 'date', format: 'yyyy-mm-dd']
'matchTime' | 'yyyy-mm-dd' | [match: 'time', format: 'yyyy-mm-dd']
'matchMin' | 1 | [match: 'type', min: 1]
'matchMax' | 1 | [match: 'type', max: 1]
'includesMatcher' | 1 | [match: 'include', value: '1']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ class LambdaDslSpec extends Specification {

then:
result.matchers.toMap(PactSpecVersion.V3) == [
'$.dateExp': [matchers: [[match: 'date', date: 'yyyy-MM-dd']], combine: 'AND'],
'$.timeExp': [matchers: [[match: 'time', time: 'HH:mm:ss']], combine: 'AND'],
'$.datetimeExp': [matchers: [[match: 'timestamp', timestamp: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']
'$.dateExp': [matchers: [[match: 'date', format: 'yyyy-MM-dd']], combine: 'AND'],
'$.timeExp': [matchers: [[match: 'time', format: 'HH:mm:ss']], combine: 'AND'],
'$.datetimeExp': [matchers: [[match: 'timestamp', format: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']
]
result.generators.toMap(PactSpecVersion.V3) == [
body: [
Expand All @@ -187,9 +187,9 @@ class LambdaDslSpec extends Specification {

then:
result.matchers.toMap(PactSpecVersion.V3) == [
'[0]': [matchers: [[match: 'date', date: 'yyyy-MM-dd']], combine: 'AND'],
'[1]': [matchers: [[match: 'time', time: 'HH:mm:ss']], combine: 'AND'],
'[2]': [matchers: [[match: 'timestamp', timestamp: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']
'[0]': [matchers: [[match: 'date', format: 'yyyy-MM-dd']], combine: 'AND'],
'[1]': [matchers: [[match: 'time', format: 'HH:mm:ss']], combine: 'AND'],
'[2]': [matchers: [[match: 'timestamp', format: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']
]

result.generators.toMap(PactSpecVersion.V3) == [
Expand Down Expand Up @@ -314,7 +314,7 @@ class LambdaDslSpec extends Specification {
variants: [
[
index: 0,
rules: ['$': [matchers: [[match: 'date', date: 'yyyy-MM-dd']], combine: 'AND']],
rules: ['$': [matchers: [[match: 'date', format: 'yyyy-MM-dd']], combine: 'AND']],
generators: ['$': [type: 'DateTime', format: 'yyyy-MM-dd']]
],
[index: 1, rules: [:], generators: [:]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ class PactDslJsonArraySpec extends Specification {

expect:
body.matchers.toMap(PactSpecVersion.V3) == [
'$[0]': [matchers: [[match: 'date', date: 'yyyy-MM-dd']], combine: 'AND'],
'$[1]': [matchers: [[match: 'time', time: 'HH:mm:ss']], combine: 'AND'],
'$[2]': [matchers: [[match: 'timestamp', timestamp: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']]
'$[0]': [matchers: [[match: 'date', format: 'yyyy-MM-dd']], combine: 'AND'],
'$[1]': [matchers: [[match: 'time', format: 'HH:mm:ss']], combine: 'AND'],
'$[2]': [matchers: [[match: 'timestamp', format: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']]

body.generators.toMap(PactSpecVersion.V3) == [body: [
'$[0]': [type: 'Date', format: 'yyyy-MM-dd', expression: 'today + 1 day'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ class PactDslJsonBodySpec extends Specification {

expect:
body.matchers.toMap(PactSpecVersion.V3) == [
'$.dateExp': [matchers: [[match: 'date', date: 'yyyy-MM-dd']], combine: 'AND'],
'$.timeExp': [matchers: [[match: 'time', time: 'HH:mm:ss']], combine: 'AND'],
'$.datetimeExp': [matchers: [[match: 'timestamp', timestamp: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']]
'$.dateExp': [matchers: [[match: 'date', format: 'yyyy-MM-dd']], combine: 'AND'],
'$.timeExp': [matchers: [[match: 'time', format: 'HH:mm:ss']], combine: 'AND'],
'$.datetimeExp': [matchers: [[match: 'timestamp', format: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']]

body.generators.toMap(PactSpecVersion.V3) == [body: [
'$.dateExp': [type: 'Date', format: 'yyyy-MM-dd', expression: 'today + 1 day'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ class PactDslJsonRootValueSpec extends Specification {
def datetime = PactDslJsonRootValue.datetimeExpression('today + 1 hour')

expect:
date.matchers.toMap(PactSpecVersion.V3) == [matchers: [[match: 'date', date: 'yyyy-MM-dd']], combine: 'AND']
date.matchers.toMap(PactSpecVersion.V3) == [matchers: [[match: 'date', format: 'yyyy-MM-dd']], combine: 'AND']
date.generators.toMap(PactSpecVersion.V3) == [body: [
'': [type: 'Date', format: 'yyyy-MM-dd', expression: 'today + 1 day']]]

time.matchers.toMap(PactSpecVersion.V3) == [matchers: [[match: 'time', time: 'HH:mm:ss']], combine: 'AND']
time.matchers.toMap(PactSpecVersion.V3) == [matchers: [[match: 'time', format: 'HH:mm:ss']], combine: 'AND']
time.generators.toMap(PactSpecVersion.V3) == [body: [
'': [type: 'Time', format: 'HH:mm:ss', expression: 'now + 1 hour']]]

datetime.matchers.toMap(PactSpecVersion.V3) == [matchers: [[
match: 'timestamp', timestamp: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']
match: 'timestamp', format: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']
datetime.generators.toMap(PactSpecVersion.V3) == [body: [
'': [type: 'DateTime', format: 'yyyy-MM-dd\'T\'HH:mm:ss', expression: 'today + 1 hour']]]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ interface MatchingRule {
* Matching Rule for dates
*/
data class DateMatcher @JvmOverloads constructor(val format: String = "yyyy-MM-dd") : MatchingRule {
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "date", "date" to format)
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "date", "format" to format)
override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
return if (pactVersion < PactSpecVersion.V3) {
listOf("Date matchers can only be used with Pact specification versions >= V3")
Expand Down Expand Up @@ -387,7 +387,7 @@ data class RegexMatcher @JvmOverloads constructor (val regex: String, val exampl
* Matcher for time values
*/
data class TimeMatcher @JvmOverloads constructor(val format: String = "HH:mm:ss") : MatchingRule {
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "time", "time" to format)
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "time", "format" to format)
override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
return if (pactVersion < PactSpecVersion.V3) {
listOf("Time matchers can only be used with Pact specification versions >= V3")
Expand All @@ -406,7 +406,7 @@ data class TimeMatcher @JvmOverloads constructor(val format: String = "HH:mm:ss"
* Matcher for time values
*/
data class TimestampMatcher @JvmOverloads constructor(val format: String = "yyyy-MM-dd HH:mm:ssZZZZZ") : MatchingRule {
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "timestamp", "timestamp" to format)
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "timestamp", "format" to format)
override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
return if (pactVersion < PactSpecVersion.V3) {
listOf("DateTime matchers can only be used with Pact specification versions >= V3")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class MatchingRuleGroupSpec extends Specification {
new MinTypeMatcher(1) | [match: 'type', min: 1]
new MaxTypeMatcher(1) | [match: 'type', max: 1]
new MinMaxTypeMatcher(2, 3) | [match: 'type', max: 3, min: 2]
new TimestampMatcher() | [match: 'timestamp', timestamp: 'yyyy-MM-dd HH:mm:ssZZZZZ']
new TimeMatcher() | [match: 'time', time: 'HH:mm:ss']
new DateMatcher() | [match: 'date', date: 'yyyy-MM-dd']
new TimestampMatcher() | [match: 'timestamp', format: 'yyyy-MM-dd HH:mm:ssZZZZZ']
new TimeMatcher() | [match: 'time', format: 'HH:mm:ss']
new DateMatcher() | [match: 'date', format: 'yyyy-MM-dd']
new IncludeMatcher('A') | [match: 'include', value: 'A']
ValuesMatcher.INSTANCE | [match: 'values']
NullMatcher.INSTANCE | [match: 'null']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,14 @@ class MatchingRulesSpec extends Specification {
[match: 'time', time: 'HH:mm'] | new TimeMatcher('HH:mm')
[match: 'time', format: 'HH:mm'] | new TimeMatcher('HH:mm')
}

def 'date/time matcher to json'() {
expect:
new TimestampMatcher().toMap(PactSpecVersion.V3) == [match: 'timestamp', format: 'yyyy-MM-dd HH:mm:ssZZZZZ']
new TimestampMatcher('yyyy').toMap(PactSpecVersion.V3) == [match: 'timestamp', format: 'yyyy']
new DateMatcher().toMap(PactSpecVersion.V3) == [match: 'date', format: 'yyyy-MM-dd']
new DateMatcher('yyyy').toMap(PactSpecVersion.V3) == [match: 'date', format: 'yyyy']
new TimeMatcher().toMap(PactSpecVersion.V3) == [match: 'time', format: 'HH:mm:ss']
new TimeMatcher('hh').toMap(PactSpecVersion.V3) == [match: 'time', format: 'hh']
}
}

0 comments on commit b1d6c01

Please sign in to comment.