简体   繁体   中英

Jira API Automation - REST Assured

Am trying to automate Jira Issue Creation via REST API with Java Rest Assured. The following are my code snippets.

I need to reframe the following JSON in Java and pass it to the Body.

{
"fields": {
    "project": {
        "id": "13204"
    },
    "summary": "welcome to testing1",
    "issuetype": {
        "id": "3"
    },
    "reporter": {
        "name": "parthiban.selvaraj"
    },
    "priority": {
        "id": "3"
    },
    "description": "description",
     "customfield_10201": {
       "id": "10608"
    }
}

}

Below is my Java code with getter and setter:

package jira;
import com.google.gson.Gson;
import io.restassured.RestAssured;
import io.restassured.authentication.PreemptiveBasicAuthScheme;
import io.restassured.http.ContentType;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import java.util.List;
public class home {
    public static void main (String args[]){
        System.out.println("Welcome");
        RestAssured.baseURI = "http://##.###.##.##:####/jira/rest/api/2/issue/";
        PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
        authScheme.setUserName("########");
        authScheme.setPassword("#######");
        RestAssured.authentication = authScheme;
        getterSetter values=new getterSetter();
        values.setProject(13204);
        values.setSummary("Checking via REST Assured");
        values.setIssueType(3);
        values.setReporter("#######");
        values.setPriority(3);
        values.setDescription("Welcome to JAVA World");
        //Updating sprint name custom field value
        values.setCustomfield_10201(10608);
        Gson gson=new Gson();
        String json= gson.toJson(values);
        System.out.println("JSON Values " + json);
        RequestSpecification httpRequest = RestAssured.given().header("Content-Type", 
        "application/json").body(json);
        System.out.println(httpRequest + " Request ");
        Response response = httpRequest.request(Method.POST, "");
        System.out.println(response.getStatusCode());
        String responseBody = response.getBody().asString();
        System.out.println("response " + responseBody);
        JsonPath jsonPath = new JsonPath(responseBody);
    }
}

Getter and Setter File:

    package jira;
    import javax.xml.bind.annotation.XmlRootElement;
    //@XmlRootElement
    public class getterSetter {
        private int project;
        private int issueType;
        private String reporter;
        private String summary;
        private int priority;
        private String description;
        private int customfield_10201;
        public String getSummary() {
            return summary;
        }

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

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

        public int getProject() {
            return project;
        }

        public int getIssueType() {
            return issueType;
        }

        public void setIssueType(int issueType) {
            this.issueType = issueType;
        }

        public String getReporter() {
            return reporter;
        }

        public void setReporter(String reporter) {
            this.reporter = reporter;
        }

        public int getPriority() {
            return priority;
        }
        public void setPriority(int  priority) {
            this.priority = priority;
        }

        public String getDescription() {
            return description;
        }

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

        public int getCustomfield_10201() {
            return customfield_10201;
        }

        public void setCustomfield_10201(int customfield_10201) {
            this.customfield_10201 = customfield_10201;
        }
      }

I understand JSON format which I am passing through request Body is wrong. Can anyone help me to pass correct JSON format in Request Body. I tested in Postman with the above JSON format Issue created successfully in my Instance.

For the above code am hitting with response code 500 and Internal Server Error.

According to the JSON string as the payload to create Jira issue, you can directly create several corresponding classes as follows:

class IssueInfo {
    private Field fields;
    //general getters and setters
}

class Field {
    private Project project;
    private String summary;
    private IssueType issuetype;
    private Reporter reporter;
    private Priority priority;
    private String description;
    private Customfield10201 customfield_10201;
    //general getters and setters
}

class Project {
    private String id;
    //general getters and setters
}

class IssueType {
    private String id;
    //general getters and setters
}

class Reporter {
    private String name;
    //general getters and setters
}

class Priority {
    private String id;
    //general getters and setters
}

class Customfield10201 {
    private String id;
    //general getters and setters
}

After assigning value of each required field, you can pass the instance of IssueInfo as request body.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM