简体   繁体   中英

Iterate and validate values in a Json Array in RestAssured Response

I have a Rest Assured response that contains the following body:

{
   "content":[
      {
         "id":"7db80896",
         "secondaryId":"12F9BD",
         "version":1,
         "details":{
            "status":"VALID",
            "reason":"Passed validations"
         },
         "subscriptionStatuses":[
            {
               "subscriptionId":"b8003508",
               "subscriptionName":"Sub_1",
               "creator":"Person 1",
               "details":{
                  "status":"ACCEPTED",
                  "reason":"Passed validations"
               }
            },
            {
               "subscriptionId":"b8003509",
               "subscriptionName":"Sub_2",
               "creator":"Person 1",
               "details":{
                  "status":"ACCEPTED",
                  "reason":"Passed validations"
               }
            }
         ]
      },
      {
         "id":"7db80895",
         "secondaryId":"11F9BD",
         "version":1,
         "details":{
            "status":"VALID",
            "reason":"Passed validations"
         },
         "subscriptionStatuses":[
            {
               "subscriptionId":"b8003509",
               "subscriptionName":"Sub_2",
               "creator":"Person 2",
               "details":{
                  "status":"ACCEPTED",
                  "reason":"Passed validations"
               }
            }
         ]
      }
   ]
}

I want to verify the status "ACCEPTED" and reason "PASSED VALIDATIONS" which is nested within 2 arrays. I have tried storing the reasons as a list, outlined in this example but I get the following error:

code: var list: List<Any> = response.jsonPath().getList("reason")

error: java.lang.IllegalStateException: response.jsonPath().getList("reason") must not be null

Is there a way to do:

for obj in response:
   for obj2 in obj.subscriptionStatuses:
      assertThat(obj2.status == expected)
      assertThat(obj2.reason == expected)

There is a way: Deserialize the response into objects. Create classes that match the response.

eg

class Status {

  private String id;
  private String secondaryId;
  //etc.
  private StatusDetail details;
  private List<SubscriptionStatus> subscriptionStatuses;
}

...and so on. I don't know what this would look like in Kotlin specifically, but it would be similar.

Notice that the nested objects in the JSON response are their own classes, and the field names in each class should match the field names in your JSON.

Then, you can use ObjectMapper to deserialize. You may need to do response.jsonPath().getList("$.content") first, but I don't know what your response object is to provide more guidance than that. Preferably, you'd just get the JSON string directly:

List<Status> statuses = new ObjectMapper().readValue(jsonString, 
  new TypeReference<List<Status>>() {})

Note that you can re-use ObjectMapper and shouldn't create a new one every time, and there are options for ObjectMapper as well to allow null values .

You can get it using the following code

List<String> m1 =response.jsonPath().get("content.subscriptionStatuses.details");
System.out.println(m1);

Output will show [[{status=ACCEPTED, reason=Passed validations}, {status=ACCEPTED, reason=Passed validations}], [{status=ACCEPTED, reason=Passed validations}]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM