Skip to content

Commit

Permalink
fix: Update matching rule loading code to support correct + incorrect…
Browse files Browse the repository at this point in the history
… formatted date/time matchers #1617
  • Loading branch information
rholshausen committed Dec 23, 2022
1 parent d079d45 commit aa11791
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ interface MatchingRule {
else TimestampMatcher()
TIME ->
if (values.has("format")) TimeMatcher(values["format"].toString())
else if (values.has("time")) TimestampMatcher(values["time"].toString())
else if (values.has("time")) TimeMatcher(values["time"].toString())
else TimeMatcher()
DATE ->
if (values.has("format")) DateMatcher(values["format"].toString())
else if (values.has("date")) TimestampMatcher(values["date"].toString())
else if (values.has("date")) DateMatcher(values["date"].toString())
else DateMatcher()
"values" -> ValuesMatcher
"ignore-order" -> ruleForIgnoreOrder(values)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,21 @@ class MatchingRulesSpec extends Specification {
new StatusCodeMatcher(HttpStatus.StatusCodes, [100, 200])
])
}
@Unroll
def 'Loading Date/Time matchers'() {
expect:
MatchingRule.fromJson(Json.INSTANCE.toJson(map)) == matcher

where:
map | matcher
[match: 'timestamp'] | new TimestampMatcher()
[match: 'timestamp', timestamp: 'yyyy-MM-dd'] | new TimestampMatcher('yyyy-MM-dd')
[match: 'timestamp', format: 'yyyy-MM-dd'] | new TimestampMatcher('yyyy-MM-dd')
[match: 'date'] | new DateMatcher()
[match: 'date', date: 'yyyy-MM-dd'] | new DateMatcher('yyyy-MM-dd')
[match: 'date', format: 'yyyy-MM-dd'] | new DateMatcher('yyyy-MM-dd')
[match: 'time'] | new TimeMatcher()
[match: 'time', time: 'HH:mm'] | new TimeMatcher('HH:mm')
[match: 'time', format: 'HH:mm'] | new TimeMatcher('HH:mm')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlin.reflect.full.declaredMemberProperties
/**
* Common utility functions
*/
@Suppress("TooManyFunctions")
object Utils : KLogging() {
/**
* Recursively extracts a sequence of keys from a recursive Map structure
Expand Down Expand Up @@ -70,7 +71,8 @@ object Utils : KLogging() {
}

/**
* Determines if the given port number is available. Does this by trying to open a socket and then immediately closing it.
* Determines if the given port number is available. Does this by trying to open a socket and then
* immediately closing it.
*/
fun portAvailable(p: Int): Boolean {
var socket: ServerSocket? = null
Expand Down Expand Up @@ -208,4 +210,20 @@ object Utils : KLogging() {
* Convert a value to snake-case form (a.b.c -> A_B_C)
*/
private fun snakeCase(key: String) = key.split('.').joinToString("_") { it.toUpperCase() }

/**
* Try to convert an any to an Int, throwing an exception if the conversion can't happen
*/
@Suppress("TooGenericExceptionThrown")
fun toInt(any: Any?): Int {
if (any == null) {
throw RuntimeException("Required an integer value, but got a NULL")
} else {
return when (any) {
is Number -> any.toInt()
is String -> any.toInt()
else -> throw RuntimeException("Required an integer value, but got a $any")
}
}
}
}

0 comments on commit aa11791

Please sign in to comment.