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 f825173 commit 00ce375
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 28 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}']
'timestamp' | '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']
'timestamp' | '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 @@ -153,9 +153,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 @@ -179,9 +179,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
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,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 @@ -224,9 +224,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 @@ -364,9 +364,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 @@ -40,16 +40,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 @@ -111,7 +111,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)
}

/**
Expand Down Expand Up @@ -179,14 +179,14 @@ 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)
}

/**
* 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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,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
@@ -1,5 +1,6 @@
package au.com.dius.pact.core.model.matchingrules

import au.com.dius.pact.core.model.PactSpecVersion
import au.com.dius.pact.core.support.Json
import au.com.dius.pact.core.support.json.JsonValue
import spock.lang.Issue
Expand Down Expand Up @@ -236,4 +237,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 00ce375

Please sign in to comment.