How to compare a date in json response body with a reference date?

4,314 views
Skip to first unread message

mir...@neodonis.com

unread,
Jan 12, 2016, 12:45:13 PM1/12/16
to REST assured
Hi,

I have a JUnit test using Rest-Assured.

I have a json response doc that contains following date field:

[
  {
    "transId": "21447350",
    "transDt": "2013-05-02 00:00:00"
  },
]

I'd like to assert that the date in the doc is in the past. I'm trying to use an expression like this:

body("find { it.transId == '21447350'}.transDt", before(LocalDate.now()));

I get following expected exception: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date

Note: The expression "before(LocalDate.now())" above resolves its references in the following imported libraries:

import java.time.LocalDate;
import static org.exparity.hamcrest.date.DateMatchers.before;

Any idea how to improve the Groovy expression in the body assertion above so that I could achieve my date comparison need?

Best regards,
mircea

Johan Haleby

unread,
Jan 12, 2016, 1:24:14 PM1/12/16
to rest-a...@googlegroups.com
Hi, 

Very interesting use case. There's currently no good way to map a result to another type but I would be happy to add that if we can find a nice way to do it (API wise). A workaround that might work would be something like this (I haven't tried it):

body("LocalDate.parse( find { it.transId == '21447350'}.transDt )", before(LocalDate.now())));

But I wouldn't recommend doing this. One future approach might be to apply a mapping function (similar to what is currently present when validating headers):

body("find { it.transId == '21447350'}.transDt", LocalDate::parse, before(LocalDate.now()));

I like this approach but the problem is that I have to create a lot of new overloaded methods to support it. Also it will not work (without major effort) if you define multiple expectations in the same body clause (which is generally recommended since all errors are then reported at once):

// Won't work when defining expectations like this:
body("x.y", equalTo("something"), 
     "x.z", equalTo("something else"));

But perhaps that's fine? Potentially this could work:

body("x.y", equalTo("something"), 
     "x.z", LocalDate::parse, equalTo(...));

but it'll require much more work to implement. What do you think?

/Johan


--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mir...@neodonis.com

unread,
Jan 12, 2016, 5:53:17 PM1/12/16
to REST assured
Hi Johan,

Thanks a lot for your quick response.

I tried following two variants:

body("LocalDate.parse(find { it.transId == '21447350'}.transDt)", before(LocalDate.now()));
body("LocalDate.parse(find { it.transId == '21447350'}.transDt, DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss'))", before(LocalDate.now()));

and received the error: groovy.lang.MissingPropertyException: No such property: transId for class: Script1

Btw, why you did not recommend the approach above? If it worked I would be fine with it.

I like this one that you proposed (applying a mapping function):

body("x.y", equalTo("something"), 
     "x.z", LocalDate::parse, equalTo(...));

What would be a reasonable timeline to see the above implemented?

Best,
mircea

Johan Haleby

unread,
Jan 13, 2016, 2:27:26 AM1/13/16
to rest-a...@googlegroups.com
On Tue, Jan 12, 2016 at 11:53 PM, <mir...@neodonis.com> wrote:
Hi Johan,

Thanks a lot for your quick response.

I tried following two variants:

body("LocalDate.parse(find { it.transId == '21447350'}.transDt)", before(LocalDate.now()));
body("LocalDate.parse(find { it.transId == '21447350'}.transDt, DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss'))", before(LocalDate.now()));
and received the error: groovy.lang.MissingPropertyException: No such property: transId for class: Script1

Ok then I don't think it works.
 

Btw, why you did not recommend the approach above? If it worked I would be fine with it.

I would recommend against writing arbitrary code in the GPath expressions. That's not its intension and it may lead to a lot of mess.
 

I like this one that you proposed (applying a mapping function):

body("x.y", equalTo("something"), 
     "x.z", LocalDate::parse, equalTo(...));

What would be a reasonable timeline to see the above implemented?

I don't even no if it's feasible yet. Please add an issue on the issue tracker so that I can look into it when I find some time.
 

Best,
mircea

Reply all
Reply to author
Forward
0 new messages