How to set post request payload from json file restassured

2.9k views Asked by At

Need help on below scenario:

I have below pojo class and when i make post call using restassured i dont want to set each field in my java class.To acheive these want to maintain one createissue.json file. when making post call, i want to read each field from createissue.json file.

Below is my pojo class CreateIssuepayload.java

public class CreateIssuepayload {

@JsonProperty("summary")
private String summary;

@JsonProperty("description")
private String description;

@JsonProperty("issuetype")
private IssueType issuetype;

@JsonProperty("project")
private Project project;

public CreateIssuepayload(Project project, IssueType issuetype,String description,  String summary) {

    this.summary = summary;
    this.description = description;
    this.issuetype = issuetype;
    this.project = project;
}

public CreateIssuepayload(Project project,IssueType issuetype,String description) {

    this.description = description;
    this.issuetype = issuetype;
    this.project = project;
    
}

public String getSummary() {
    return summary;
}

public void setSummary(String summary) {
    this.summary = summary;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public IssueType getIssuetype() {
    return issuetype;
}

public void setIssuetype(IssueType issuetype) {
    this.issuetype = issuetype;
}

public Project getProject() {
    return project;
}

public void setProject(Project project) {
    this.project = project;
}

}

My createissue.json file

{
   "fields":{
      "summary":"Please look into issue",
      "description":"Unable to create my JIRA ticket 3",
      "issuetype":{
         "name":"Bug"
      },
      "project":{
         "key":"BP"
      }
   }
}

And my testcase to make post request

 @Test(enabled = false)
        public static void test1() throws JsonProcessingException {
            IssueType issuetype = new IssueType("**Bug**");
            Project project = new Project("**BP**");
            CreateIssuepayload mypojo = new CreateIssuepayload(project, issuetype, "**Unable to create my JIRA ticket 3**",
                    "**Please look into issue.....**");
            Fields f = new Fields(mypojo);
RestAssured.baseURI = "http://localhost:8080";
        Response res = given().header("Content-Type", "application/json")
                .header("cookie", "JSESSIONID=" + Basic.sessionGen() + "").body(f).expect()
                .body(containsString("greeting")).when().post("/rest/api/2/issue").then().extract().response();
        } 

Here i dont want to set my testdata like Bug,BP etc from my testcase in java class.I want to read it dynamically from json file

Note: I also dont want to post the whole json file as my body.

Any help is Appriciated. Thankyou.

1

There are 1 answers

2
kaweesha On BEST ANSWER

You can use json-simple library in Java to read the json file. Maven repository

Then retrieve values as Strings and create the CreateIssuepayload object and Fields objects.

@Test(enabled = false)
public static void test1() throws JsonProcessingException {

    // Read the json file
    org.json.simple.JSONObject jsonObject = new org.json.simple.JSONObject();
    JSONParser parser = new JSONParser();
    try {
        Object obj = parser.parse(new FileReader("createissue.json"));

        jsonObject = (org.json.simple.JSONObject) obj;

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    JSONObject fieldsObject = (JSONObject)  jsonObject.get("fields");
    JSONObject issueTypeObject = (JSONObject) fieldsObject.get("issuetype");
    JSONObject projectObject = (JSONObject) fieldsObject.get("project");

    IssueType issueType = new IssueType(issueTypeObject.get("name").toString());
    Project project = new Project(projectObject.get("key").toString());
    String summary = fieldsObject.get("summary").toString();
    String description = fieldsObject.get("description").toString();

    CreateIssuepayload mypojo = new CreateIssuepayload(project, issuetype, description, summary);

    Fields f = new Fields(mypojo);

    RestAssured.baseURI = "http://localhost:8080";
    Response res =
            given()
            .header("Content-Type", "application/json")
            .header("cookie", "JSESSIONID=" + Basic.sessionGen() + "").body(f).expect()
            .body(containsString("greeting"))
            .when().post("/rest/api/2/issue")
            .then().extract().response();
}



If you change the json file as follows, you can get this done easily using Gson

{
  "summary": "Please look into issue",
  "description": "Unable to create my JIRA ticket 3",
  "issuetype": {
    "name": "Bug"
  },
  "project": {
    "key": "BP"
  }
}

And you don't need to add the @JsonProperty() annotations as well.

Then use Gson to de-serialize the json object to Java object

@Test(enabled = false)
public static void test1() throws JsonProcessingException {

    Gson gson = new Gson();
    CreateIssuepayload mypojo = null;
    
    try {
        BufferedReader bf = new BufferedReader(new FileReader("createissue.json"));
        mypojo = gson.fromJson(bf, CreateIssuepayload.class);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    
    RestAssured.baseURI = "http://localhost:8080";
    Response res = given()
            .header("Content-Type", "application/json")
            .header("cookie", "JSESSIONID=" + Basic.sessionGen() + "")
            .body(mypojo).expect()
            .body(containsString("greeting")).when().post("/rest/api/2/issue").then().extract().response();

}