Skip to content

Commit aa11791

Browse files
committed
fix: Update matching rule loading code to support correct + incorrect formatted date/time matchers #1617
1 parent d079d45 commit aa11791

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ interface MatchingRule {
116116
else TimestampMatcher()
117117
TIME ->
118118
if (values.has("format")) TimeMatcher(values["format"].toString())
119-
else if (values.has("time")) TimestampMatcher(values["time"].toString())
119+
else if (values.has("time")) TimeMatcher(values["time"].toString())
120120
else TimeMatcher()
121121
DATE ->
122122
if (values.has("format")) DateMatcher(values["format"].toString())
123-
else if (values.has("date")) TimestampMatcher(values["date"].toString())
123+
else if (values.has("date")) DateMatcher(values["date"].toString())
124124
else DateMatcher()
125125
"values" -> ValuesMatcher
126126
"ignore-order" -> ruleForIgnoreOrder(values)

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,21 @@ class MatchingRulesSpec extends Specification {
344344
new StatusCodeMatcher(HttpStatus.StatusCodes, [100, 200])
345345
])
346346
}
347+
@Unroll
348+
def 'Loading Date/Time matchers'() {
349+
expect:
350+
MatchingRule.fromJson(Json.INSTANCE.toJson(map)) == matcher
351+
352+
where:
353+
map | matcher
354+
[match: 'timestamp'] | new TimestampMatcher()
355+
[match: 'timestamp', timestamp: 'yyyy-MM-dd'] | new TimestampMatcher('yyyy-MM-dd')
356+
[match: 'timestamp', format: 'yyyy-MM-dd'] | new TimestampMatcher('yyyy-MM-dd')
357+
[match: 'date'] | new DateMatcher()
358+
[match: 'date', date: 'yyyy-MM-dd'] | new DateMatcher('yyyy-MM-dd')
359+
[match: 'date', format: 'yyyy-MM-dd'] | new DateMatcher('yyyy-MM-dd')
360+
[match: 'time'] | new TimeMatcher()
361+
[match: 'time', time: 'HH:mm'] | new TimeMatcher('HH:mm')
362+
[match: 'time', format: 'HH:mm'] | new TimeMatcher('HH:mm')
363+
}
347364
}

core/support/src/main/kotlin/au/com/dius/pact/core/support/Utils.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlin.reflect.full.declaredMemberProperties
1515
/**
1616
* Common utility functions
1717
*/
18+
@Suppress("TooManyFunctions")
1819
object Utils : KLogging() {
1920
/**
2021
* Recursively extracts a sequence of keys from a recursive Map structure
@@ -70,7 +71,8 @@ object Utils : KLogging() {
7071
}
7172

7273
/**
73-
* Determines if the given port number is available. Does this by trying to open a socket and then immediately closing it.
74+
* Determines if the given port number is available. Does this by trying to open a socket and then
75+
* immediately closing it.
7476
*/
7577
fun portAvailable(p: Int): Boolean {
7678
var socket: ServerSocket? = null
@@ -208,4 +210,20 @@ object Utils : KLogging() {
208210
* Convert a value to snake-case form (a.b.c -> A_B_C)
209211
*/
210212
private fun snakeCase(key: String) = key.split('.').joinToString("_") { it.toUpperCase() }
213+
214+
/**
215+
* Try to convert an any to an Int, throwing an exception if the conversion can't happen
216+
*/
217+
@Suppress("TooGenericExceptionThrown")
218+
fun toInt(any: Any?): Int {
219+
if (any == null) {
220+
throw RuntimeException("Required an integer value, but got a NULL")
221+
} else {
222+
return when (any) {
223+
is Number -> any.toInt()
224+
is String -> any.toInt()
225+
else -> throw RuntimeException("Required an integer value, but got a $any")
226+
}
227+
}
228+
}
211229
}

0 commit comments

Comments
 (0)