0

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)
Aimee Jones
  • 881
  • 2
  • 18
  • 38

2 Answers2

1

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

e.g.

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.

Christopher Schneider
  • 3,745
  • 2
  • 24
  • 38
  • I should have said, "is there an easy, one liner way to do it?" Thanks for this though. It's very helpful, just also long winded. – Aimee Jones Mar 09 '20 at 16:33
0

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}]]

Arun Nair
  • 425
  • 3
  • 11