Home > Software design >  How to set the request payload for the below example in Rest assured?
How to set the request payload for the below example in Rest assured?

Time:01-03

I need to set a request Payload as (attached image) in rest assured, the images tag has to contain a array of name and job value pair and followed by url string array. I tried using POJO, but I could not replicate the payload exactly.

sample payload

{
   "id":1,
   "title":"iPhone 9",
   "description":"An apple mobile which is nothing like apple",
   "price":549,
   "images":[
      {
         "name":"aaa",
         "job":"dev"
      },
      "https://i.dummyjson.com/data/products/1/1.jpg",
      "https://i.dummyjson.com/data/products/1/2.jpg"
   ]
}

The code I tried.

Main class,

public class PostDataWithoutSerial {
    
    pojoimage p1 = new pojoimage();
    Images i1=new Images();
    List<Images> img = new ArrayList<Images>();
    ArrayList<String> url1 = new ArrayList<String>();
    
    @Test
    public void postRequestWithoutSerial()
    {

        p1.setTitle("google");
        p1.setDescription("google phone");
        p1.setPrice("800");
        i1.setName("james");
        i1.setJob("watt");
        url1.add("aaa");
        url1.add("bbb");
        i1.setUrl(url1);
        img.add(i1);
        p1.setImages(img);


        given().log().all()
        .contentType("application/json")
        .body(p1)
        .when()
        .post("http://localhost:3000/products")
        .then()
        .statusCode(201);
        //validating single value in response

    }
}

pojoimage.java class (Getters and setters class)

import java.util.ArrayList;
import java.util.List;
import java.util.List;
public class pojoimage {
    public  String id;
    public String title;
    public String description;
    public String price;
    public List< Images> images;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public List<Images> getImages() {
        return images;
    }
    public void setImages(List<Images> images) {
        this.images = images;
    }
}

Images. java class

import java.util.ArrayList;

public class Images {
    public String name;
    public String job;
    ArrayList<String> url = new ArrayList<String>();
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public ArrayList<String> getUrl() {
        return url;
    }
    
    public void setUrl(ArrayList<String> url1) {
        TODO Auto-generated method stub
        this.url = url1;
    }
}

CodePudding user response:

Since images in your json contains json object and string, then the correct data type to hold both of them is List<Object>. In this solution, I use Map<> to simplify setting data for json object, you can replace by POJO.

import lombok.Data;

@Data
static class PojoImage {
    public  int id;
    public String title;
    public String description;
    public int price;
    public List<Object> images;
}
@Test
public void postRequestWithoutSerial() {
    PojoImage pojoImage = new PojoImage();
    pojoImage.setId(1);
    pojoImage.setTitle("iPhone 9");
    pojoImage.setDescription("An apple mobile which is nothing like apple");
    pojoImage.setPrice(549);

    List<Object> images = new ArrayList<>();
    images.add(Map.of("name", "aaa", "job", "dev"));
    images.add("https://i.dummyjson.com/data/products/1/1.jpg");
    images.add("https://i.dummyjson.com/data/products/1/2.jpg");

    pojoImage.setImages(images);

    given().log().all()
            .contentType("application/json")
            .body(pojoImage)
            .when()
            .post("https://postman-echo.com/post");
}
  • Related