Re: [rest-assured] Comparing JSon file to part of a response

3,290 views
Skip to first unread message

Johan Haleby

unread,
May 21, 2013, 5:03:34 AM5/21/13
to rest-a...@googlegroups.com
So is your question is essentially how to compare two JSON documents with each other? This is currently not supported by REST Assured or JsonPath. You could however write a custom Hamcrest matcher that you can use in REST Assured to verify that the HTTP response body is "JSON equal" to a file on disk.

Regards,
/Johan


On Sat, May 18, 2013 at 5:57 PM, <mdpete...@comcast.net> wrote:
I have a scenario where I have sets of static metadata that can be returned as JSon via  a /meta/{EntityType}?fields=* call or along with an entity in a call like this /entity/{EntityType}/2?meta=full&fields=*

I'd like to store each set of metadata to a file and then do an assertion in REST assured to compares the actual metadata in my responses against the files.

What is the best way to do that with REST assured?

I'm thinking that one way to do it would be to put the response data into a JSonPath object and do the same with the contents of the file. Then I'd check that contents of the two objects are the same.
Does that make sense?

All non-primitive data types in the metadata JSon includes a full URI. The only difference between the response and the file data should be the baseURI portion of these URIs. I already have that baseURI as a variable in my REST assured class. I'm thinking I would just replace the static baseURI in the metadata files with that variable.

Thanks for any suggestions!

Here's an example of the response data I want to check:
{
"entity": "EntityXXXX",
"label": "Candidate",
"fields": [
{
"name": "id",
"type": "ID",
"dataType": "Integer"
},
{
"name": "address",
"type": "COMPOSITE",
"dataType": "Address",
"label": "Address",
"fields": [
{
"name": "address1",
"type": "SCALAR",
"dataType": "String",
"maxLength": 40,
"label": "Address1"
},
...

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

mdpete...@comcast.net

unread,
May 21, 2013, 12:28:46 PM5/21/13
to rest-a...@googlegroups.com
Thanks. Here's what I ended up doing. I read in the file to a string and the replace a section of the URLs to match the actual base restURL of my response. Then I create a JsonPath object for my response and one for the Json from the file. Then I compare them.

@Test
    public void getCandidateSourceMeta() throws FileNotFoundException {
String res = expect().log().body().statusCode(200).when().given()
.header("BhRestToken", restToken)
.param("fields", "*")
         .param("meta", "full")
         .expect().get("/meta/CandidateSource").asString();
 
String baseMetaRaw = new Scanner( new File("files/basefiles/CandidateSourceMeta.json") ).useDelimiter("\\A").next();
String baseMeta=baseMetaRaw.replaceAll("restUrl/", restUrl);
System.out.println("baseMeta: " + baseMeta);

JsonPath baseJp = new JsonPath(baseMeta);

JsonPath resJp = new JsonPath(res);
System.out.println(" baseMeta fields: " + baseJp.get("fields"));

Assert.assertEquals(resJp.get("fields"), baseJp.get("fields"));

Johan Haleby

unread,
May 22, 2013, 2:28:34 AM5/22/13
to rest-a...@googlegroups.com
Thanks for sharing.
Reply all
Reply to author
Forward
0 new messages