Jira API Automation - гарантированный отдых - PullRequest
0 голосов
/ 21 октября 2019

Пытаюсь автоматизировать создание проблемы Jira через REST API с Java Rest Assured. Ниже приведены мои фрагменты кода.

Мне нужно перефразировать следующий JSON в Java и передать его в тело.

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

}

Ниже приведен мой код Javaс getter и 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;
        }
      }

Я понимаю формат JSON, который я передаю через запрос. Тело неверно. Может кто-нибудь помочь мне передать правильный формат JSON в теле запроса. Я протестировал в Postman с вышеупомянутой проблемой формата JSON, успешно созданной в моем экземпляре.

Для приведенного выше кода я получаю код ответа 500 и Внутренняя ошибка сервера.

1 Ответ

0 голосов
/ 22 октября 2019

Согласно строке JSON в качестве полезной нагрузки для создания проблемы Jira, вы можете напрямую создать несколько соответствующих классов следующим образом:

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
}

После присвоения значения каждому обязательному полю вы можете передать экземпляр IssueInfo как тело запроса.

...