Karate- Need help to assert a single dimension array for date range

Here you go:

* def dateToLong =
"""
function(s) {
  var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
  var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
  return sdf.parse(s).time;
} 
"""
* def min = dateToLong('2019-04-24T17:25:00.000')
* def max = dateToLong('2019-04-24T17:50:00.000')
* def isValid = function(x){ var temp = dateToLong(x); return temp >= min && temp <= max }

* def response =
"""
[
"2019-04-24T17:41:27.975",
"2019-04-24T17:41:27.954",
"2019-04-24T17:41:27.93",
"2019-04-24T17:41:27.907",
"2019-04-24T17:41:27.886",
"2019-04-24T17:41:27.862",
"2019-04-24T17:41:27.84",
"2019-04-24T17:41:27.816",
"2019-04-24T17:41:27.792"
]
"""
* match each response == '#? isValid(_)'

Please refer the docs if you have doubts about any of the keywords. I removed the first date in the list because it was not consistent, but you have enough info to handle it if needed – you may need some conditional logic somewhere.

Also see:

https://stackoverflow.com/a/54114432/143475

https://stackoverflow.com/a/52892797/143475

Leave a Comment