Search code examples
javapojorest-assured

Setting data for type array- RestAssured Post Method


I am trying to create a pay load for this body and will do a POST.

{
    "location": {
        "lat": -38.383494,
        "lng": 33.427362
    },
    "accuracy": 50,
    "name": "Frontline house Seattle",
    "phone_number": "(1) 952 893 3937",
    "address": "29, side layout, cohen 09",
    "types": [
        "shoe park",
        "shop"
    ],
    "website": "http://google.com",
    "language": "French-EN"
}

These are the 3 classes I have created

@Getter
@Setter
public class Payload {
    private Location location;
    private int accuracy;
    private String name;
    private String phone_number;
    private String address;
    private List<Types> types;
    private String website;
    private String language;
}

@Getter
@Setter
public class Location {
    private double lat;
    private double lng;
}

@Setter
@Getter
public class Types {
    private String zero;
    private String one;
}

This below will be will be use to do the post

 @Test
    public void createAddress(){
        Location location = new Location(); // Location Class
        location.setLat(-38.383494);
        location.setLng(33.427362);

       Types[] type = new Types[2]; // Type Class


        Payload payload = new Payload();
        payload.setLocation(location); // This is how we will set the Location type class in Payload
        payload.setAccuracy(50);
        payload.setName("Frontline house");
        payload.setPhone_number("(1) 952 893 3937");
        payload.setAddress("29, side layout, cohen 09");
        payload.setTypes();

}

Problem: I am not sure how do I set value for Types to create this payload.I also have a feeling I have declared Types incorrectly. In that class I have declared 2 String variables.

Thanks in advance for your suggestion.


Solution

  • You don't have to create a seperate class for types, since it's a list just declare it as a list of strings

    @Setter @Getter private List<String> types;

    The getters and setters for these would be

    public List<String> getTypes() { return types; } public void setTypes(List<String> types) { this.types = types; }

    In your test

    List<String> myList =new ArrayList<String>();
        myList.add("shoe park");
        myList.add("shop");
    
        payload.setTypes(myList);