How to verify responsebody with ignoring some timestamp fields?

1,152 views
Skip to first unread message

chang...@gmail.com

unread,
Apr 2, 2015, 6:00:25 AM4/2/15
to rest-a...@googlegroups.com
In my test script, I want to verify responsebody with ignoring some timestamp fields.
 
Below is the sample json responsebody, I want to verify all the cotents except the   "value": "1395365082000" of      "name": "_lastModifiedDate" and   "value": "2968387199000" of  "_endDate",
{
    "country": "US",
    "depth": 2,
    "id": 0,
    "language": "en",
    "modelDepth": 0,
    "name": "Bundle1",
    "path": "Bundle1",
    "type": "tree",
    "properties": [
        {
            "name": "UI: PAGE BACKGROUND",
            "path": "Bundle1",
            "type": "String",
            "value": ""
        },
{
            "name": "_lastModifiedDate",
            "path": "Bundle1",
            "type": "Long",
            "value": "1395365082000"
        }, {
            "name": "_endDate",
            "path": "Bundle1",
            "type": "String",
            "value": "2968387199000"
        }
}
 
How to implement this?
 
I thought to get the "value": "1395365082000" of      "name": "_lastModifiedDate"  from responsebody, and update it to null. But, how Can I return the null value to responsebody? If this is reasonable, I can use
equalTo(ot.getMsgBodyfromJson(xmloutputpath)) to do the entire match.

 

 

 
 

Johan Haleby

unread,
Apr 2, 2015, 6:07:32 AM4/2/15
to rest-a...@googlegroups.com
Hi, 

If I understand your question correctly you want to validate some attributes and extract others to use them in a subsequent request?

To validate attributes you typically do something like this:

when().
get("/resource").
then().
body("country", equalTo("US")).
body("properties.type", hasItems("String", "Long")).
...

Note that you may not want to validate too many things (behavior tests can reside somewhere else, for example in a unit test). Also have a look at the json schema module.

To extract a single value from the response you can do:

String country =
when().
get("/resource").
then().
body("country", equalTo("US")).
body("properties.type", hasItems("String", "Long")).
extract().
        body("country");


To extract multiple values you can either map the response to a DTO or use JsonPath.

/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.

chang...@gmail.com

unread,
Apr 2, 2015, 8:56:22 PM4/2/15
to rest-a...@googlegroups.com
Johan, many thanks for your quick response.
 
As responsebody is large, actually I want to compare the whole content of ResponseBody except the timestamp with expected output saved in the file.
 
My original code is like this:
 

given().header("Accept", "application/xml").get("/Bundle1").then()

.log().all().assertThat().statusCode(200)

.body(equalTo(ot.getMsgBodyfromJson(xmloutputpath)))

Is there any way to exclude the timestamp before using equalTo? Like the method equalToExceptTimestamp?
 
 
在 2015年4月2日星期四 UTC+8下午6:00:25,chang...@gmail.com写道:

Johan Haleby

unread,
Apr 3, 2015, 1:07:24 AM4/3/15
to rest-a...@googlegroups.com
Oh I see. If it's XML perhaps you can make use of XmlUnit (write a hamcrest matcher for it) and is it Json perhaps you can make use of Hamcrest-JSON. Not sure if you can exclude certain node/properties. If not perhaps you could create your own JSON schema (you can generate it).

When looking at your code I'm not sure if you're talking about JSON or XML? Seems like you expect xml (Accept header) but then you compare the body to a JSON document?!

/Johan

--

chang...@gmail.com

unread,
Apr 8, 2015, 3:55:10 AM4/8/15
to rest-a...@googlegroups.com
JsonPath has below method to check whether there are such node:
new JsonPath((response.getBody().asString())).get("properties.findAll { properties -> properties.name =='_lastModifiedDate' }");
 
Can JsonPath support the customized remove method to delete unexpected node? If it's, I can easily to ignore some node verification.
 
new JsonPath((response.getBody().asString())).remove("properties.findAll { properties -> properties.name =='_lastModifiedDate' }");
 
Additionally where can I find such methods like "properties.findAll { properties -> properties.name =='_lastModifiedDate' "? Is this groovy.

Johan Haleby

unread,
Apr 8, 2015, 4:39:00 AM4/8/15
to rest-a...@googlegroups.com
JsonPath is only for reading, not modifying the document.

--
Reply all
Reply to author
Forward
0 new messages