Check if field exists in JSON regardless of the value

9,480 views
Skip to first unread message

Steve Rodriguez

unread,
Mar 3, 2014, 11:10:14 AM3/3/14
to rest-a...@googlegroups.com
Here is my Test for the JSON below:

expect().statusCode(200)
.body("status", equalTo("success"), 
 "models.size", greaterThanOrEqualTo(1),
 "models.model_id", hasItems(639506,639507))   
.given().pathParam("firm",   "4797")
       .pathParam("branch", "143")
       .pathParam("rep_ae", "126742")
   .when()
   .get("/pb-client-wrapper/models/{firm}/{branch}/{rep_ae}");

Here is my JSON response ( basically some models and each model has some allocations ).  

Questions:

  1. How do I check that the property "fund_code" exists in the response regardless of the value?
  2. How do I check the the property "number_of_clients" exists in the model object regardless of the value?

{
"status": "success"
"fund_code":"00200",
"fund_name":"My Fund Name",
"models": [
  {
    "model_id": 639506,
    "model_name": "New Validated Model",
    "model_type": null,
    "portfolios": null,
    "created_date": 1390978800000,
    "display_create_date": "01/29/2014",
    "updated_date": 1392274800000,
    "display_update_date": "02/13/2014",
    "number_of_clients": 1,
    "risk": "N/A",
    "time_frame": "N/A"
  },
  {
    "model_id": 639507,
    "model_name": "My Validated Model",
    "model_type": null,
    "portfolios": null,
    "created_date": 1390978800000,
    "display_create_date": "01/29/2014",
    "updated_date": 1392274800000,
    "display_update_date": "02/13/2014",
    "number_of_clients": 1,
    "risk": "N/A",
    "time_frame": "N/A"
  }
]
}

Johan Haleby

unread,
Mar 4, 2014, 1:30:04 AM3/4/14
to rest-a...@googlegroups.com
You can do something like this:

given().
pathParam("firm",   "4797").
pathParam("branch", "143").
pathParam("rep_ae", "126742").
when().
   get("/pb-client-wrapper/models/{firm}/{branch}/{rep_ae}")
then().
body("any { it.key == 'fund_code' }"), is(true)).
body("models.any { it.containsKey('number_of_clients') }"), is(true));


See FAQ #2.

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

Steve Rodriguez

unread,
Mar 4, 2014, 4:04:27 PM3/4/14
to rest-a...@googlegroups.com
OK, that problem is solved :-)    Next Question:  How do I do that same kind of check for an element in an array within an array ( see JSON below )?

For example, I want to check that fund_code element exists in every allocation node in the JSON below.   How do I do that?   I tried the following but it failed:

body("models.allocations.any {it.containsKey('fund_code') }", is(true)).   // This did not work and generated the following error:

java.lang.IllegalArgumentException: No signature of method: java.util.ArrayList.containsKey() is applicable for argument types: (java.lang.String) values: [fund_code]
Possible solutions: contains(java.lang.Object), containsAll(java.util.Collection), contains(java.lang.Object), containsAll([Ljava.lang.Object;)

{
"status": "success"
"fund_code":"00200",
"fund_name":"My Fund Name",
"models": [
  {
    "model_id": 639506,
    "model_name": "New Validated Model",
    "model_type": null,
    "portfolios": null,
    "created_date": 1390978800000,
    "display_create_date": "01/29/2014",
    "updated_date": 1392274800000,
    "display_update_date": "02/13/2014",
    "number_of_clients": 1,
    "allocations": [
        {
            "fund_code": "00225",
            "fund_name": "Rising Dividends Fund",
            "fund_class": "A"
        },
        {
            "fund_code": "00230",
            "fund_name": "Global Strategic Income Fund",
            "fund_class": "A"
         }
     ],
    "risk": "N/A",
    "time_frame": "N/A"
  },
  {
    "model_id": 639507,
    "model_name": "My Validated Model",
    "model_type": null,
    "portfolios": null,
    "created_date": 1390978800000,
    "display_create_date": "01/29/2014",
    "updated_date": 1392274800000,
    "display_update_date": "02/13/2014",
    "number_of_clients": 1,
    "allocations": [
        {
            "fund_code": "00225",
            "fund_name": "Rising Dividends Fund",
            "fund_class": "A"
        },
        {
            "fund_code": "00230",
            "fund_name": "Global Strategic Income Fund",
            "fund_class": "A"
         }
     ],
    "risk": "N/A",
    "time_frame": "N/A"
  }
]

Johan Haleby

unread,
Mar 5, 2014, 1:55:56 AM3/5/14
to rest-a...@googlegroups.com
You need to read up on Groovy GPath syntax if you want to do these kinds of operations. In this case you'd probably have to flatten the lists since now you get lists in lists, so probably something like: body("models.allocations.flatten().any {it.containsKey('fund_code') }", is(true)). 

Regards,
/Johan


--

Steve Rodriguez

unread,
Mar 5, 2014, 10:43:58 AM3/5/14
to rest-a...@googlegroups.com
Thanks.   Will look into the Groovy GPath syntax...  Would help others to see these kinds of examples posted somewhere.   Very cool framework though!


On Monday, March 3, 2014 9:10:14 AM UTC-7, Steve Rodriguez wrote:

Johan Haleby

unread,
Mar 6, 2014, 1:49:05 AM3/6/14
to rest-a...@googlegroups.com
Yes an introduction of some sort would probably be a good idea. However that would be like teaching Groovy, its collection framework and GPath and I hope that there are enough Groovy resources out there to get going. If you find any good resources out there that we should link to then send the link(s) to the mailing list and I'll include them in the documentation. Also you're of course very welcome to contribute to the documentation if you want to to help out improving it.

Regards,
/Johan


--

Ranjit kumar Kundu

unread,
Jul 18, 2017, 4:58:51 AM7/18/17
to REST assured
What if i have to right this in Kotlin language ?

Ranjit kumar Kundu

unread,
Jul 18, 2017, 4:58:51 AM7/18/17
to REST assured
What if i have to right it in Kotlin Language ?

Johan Haleby

unread,
Jul 21, 2017, 4:08:47 AM7/21/17
to rest-a...@googlegroups.com
REST Assured uses GPath (groovy) and not Kotlin so you cannot use Kotlin expressions.

To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages