Search code examples
pojorest-assuredjsonschema2pojo

RestAssured, POJO and dealing with lists


I'm writing a test for an API and at one endpoint I get JOSN in response with couple arrays into one array. Can I make POJO class which mapping those array/arrays into a class object?

for example, JSON in response looks like this:

{
"total": 55,
"limit": 500,
"skip": 0,
"data": [
    {
        "id": 13,
        "email": "probe@gmail.com",
        "phone": "121-121-123",
        "firstName": "Prob",
        "lastName": "Buraz",
        "platforms": [
            {
                "id": 3,
                "name": "Dowe Assis",
                "url": "ins.probe.com",
                "user_platforms": {
                    "id": 496,
                    "userId": 13,
                    "platformId": 100,
                    "user_id": 82
                }
            }
        ], .....

And so on. I suppose that I can use this class for mapping response into POJO:

 @JsonIgnoreProperties(ignoreUnknown = true) 
public class GetUsers {

public  Integer total;

public Integer limit;

public List<DataReference> data;   
@JsonIgnoreProperties(ignoreUnknown = true)
public static class DataReference{              
    public int id;
    public String email;
    public String phone;
    public String firstName;
    public String lastName;


public List<Platform> platforms;                    
@JsonIgnoreProperties(ignoreUnknown = true)             
public static class Platform{
    public Integer id;
    public String name;
    public String url;
}   

}

}

But in the test code i can get nothing more then data elements. platforms elements are untouchable:

 GetUsers users = given()
          .header("Authorization",              
                  "Bearer " + adminToken)        //
          .when()
          .get()
          .then()//.log().all()
          .statusCode(200)//.log().all()
          .extract().as(GetUsers.class);

i can checks values of array data like those:

 assertThat(users.data).extracting(user -> user.email).contains("probe1@gmail.com", probe+2@gmail.com, ... );

But i can't access to any value of users.data.platforms.name or users.data.platforms.id and so on.

How I can access to platform element for one of users?

users.data.platforms.name

How i can do that (access platforms data) for one or for all listed users in data array?


Solution

  • Which library you are using for JSON POJO mapping? If there is no error with mapping then platform variable can be accessed using following.

    Add getter setter in POJO

    users.getData.getindex(0).getplatforms.getindex(0).getid();
    

    users is object of GetUsers Class which is used to map the JSON response.