BaymaxSky / postman-api

0 stars 0 forks source link

Xray Rest API - Create Test Execution #4

Open Jacobvu84 opened 1 year ago

Jacobvu84 commented 1 year ago
        static String CREATE_ISSUE = JIRA_BASE + "/rest/api/latest/issue";

        String environment = EnvironmentSpecificConfiguration.from(environmentVariables)
                .getProperty("report.customfields.environment");
        String version = EnvironmentSpecificConfiguration.from(environmentVariables)
                .getProperty("report.customfields.ApplicationVersion");
        String testPlan = EnvironmentSpecificConfiguration.from(environmentVariables)
                .getProperty("xray.testplan");

        JSONArray testPlanId = new JSONArray();
        testPlanId.add(testPlan);

        JSONObject project = new JSONObject().element("key", "OSW");

        JSONArray env = new JSONArray();
        env.add(environment);

        JSONObject issuetype = new JSONObject()
                .element("name", IssueType.TEST_EXECUTION);
        JSONObject fields = new JSONObject()
                .element("customfield_12928", testPlanId)
                .element("customfield_12926", env)
                .element("project", project)
                .element("summary", "[TE][Automation] Execution of automated tests for " + environment+"-"+version)
                .element("issuetype", issuetype)
                .element("description", "This execution is automatically created when importing execution results from an external source");
        createIssue(new JSONObject().element("fields", fields));
    }

    private static void createIssue(JSONObject testexec) {
        if(!Files.exists(Paths.get("testExecId.txt"))) {
            SerenityRest.given()
                    .header("Authorization", JIRA_TOKEN)
                    .contentType(ContentType.JSON)
                    .body(testexec)
                    .post(CREATE_ISSUE);

            String key = SerenityRest.lastResponse().getBody().jsonPath().get("key");

            BufferedWriter writer = null;
            try {
                writer = new BufferedWriter(new FileWriter("testExecId.txt"));
                writer.write(key);

            } catch (IOException e) {
                throw new RuntimeException(e);

            } finally {
                try {
                    writer.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }