2

I have a response from the api using karate framework which is some thing like this..

{
"name": "xyz.json",
"fileSize": "391 B",
"timestamp": "2020-06-22 12:03:00 GMT",
"tag": "abc1"
},
{
"name": "abc.json",
"fileSize": "391 B",
"timestamp": "2020-06-22 12:03:01 GMT",
"tag": "abc2"
},

now from the above response how do i validate the "timestamp" field i.e. of the 2 timestamp values which is the latest so that i can pick up the corresponding json file from the name field.

krishnadhar
  • 73
  • 1
  • 6

1 Answers1

1

You can convert the dates to numbers this way:

* def response =
"""
[
   {
      "name":"xyz.json",
      "fileSize":"391 B",
      "timestamp":"2020-06-22 12:03:00 GMT",
      "tag":"abc1"
   },
   {
      "name":"abc.json",
      "fileSize":"391 B",
      "timestamp":"2020-06-22 12:03:01 GMT",
      "tag":"abc2"
   }
]
"""
* def dateToLong =
"""
function(s) {
  var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
  var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
  return sdf.parse(s).time;
} 
"""
* def fun = function(x){ return dateToLong(x.timestamp) }
* def dates = karate.map(response, fun)
* print dates

I leave it to you to figure how to get the latest. Karate is not a general purpose language, but you can mix JavaScript or I recommend writing a Java utility, refer to the documentation for how. And also look at JSON transforms: https://github.com/intuit/karate#json-transforms

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248