Search code examples
javajsoncucumberjira-rest-api

Rest API post request to send Cucumber.json file to Jira in Java


I am not able to update my Cucumber Test Results stored in a .json file to Jira using REST API post request. I am able to use the same method to update my features in Cucumber but it does not work for my Json result file. I am quite new to Java and Json. Hence got some problems understanding them.

I have tried converting my Json file into a Json array as well as a Json Object but it does not work.

//Below is my method which takes the filepath from the project and the URL to update my results on Xray in Jira

public static void importCucumberResultFilesToJira(String filePath, String resultTypeUrlValue) throws IOException {
    String jiraUrl = config.getJiraLoginValue();
    log.info(String.format("Starting upload of Cucumber features to XRAY on Jira project: %s\n Using Jira user: %s ", config.getJiraProjectValue(), config.getJiraLoginValue()));
    log.info(String.format("Path to Report: %s", filePath));
    String authentication = config.getJiraLoginValue() + ':' + config.getJiraPassword();
    BASE64Encoder encoder = new BASE64Encoder();
    String encoded = null;
    try {
        encoded = encoder.encode((authentication).getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    Client client = ClientBuilder.newBuilder()
            .register(MultiPartFeature.class).property(HttpHeaders.AUTHORIZATION, encoded).build();

    //Import type is dynamic below
    StringBuffer url = new StringBuffer(resultTypeUrlValue);
    url.append("?projectKey=").append(config.getJiraProjectValue());
    WebTarget webTarget = client.target(url.toString());
    log.info(String.format("URL of the XRAY API: %s", url.toString()));

    //Read .json file
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(filePath));
    } catch (FileNotFoundException e) {
        System.out.println("File not found");
        System.exit(1);
    }
    JsonParser parser = new JsonParser();
    Object obj = parser.parse(br);
    JsonArray arr = (JsonArray) obj;

    System.out.println(arr);

    Response response = webTarget.request()
            .accept(MediaType.APPLICATION_JSON).
                    header(HttpHeaders.AUTHORIZATION, "Basic " + encoded).post(
                    Entity.json(arr));
    log.info(response.getStatus() + " "
            + response.getStatusInfo() + " " + response);
    log.info(response.getStatus() + " "
            + response.getStatusInfo() + " " + response);
    log.info("End of XRAY file upload publication");

}

// My dependencies in the POM.xml I posted it because with the research that I have done on SO and other websites, sometimes some dependencies pose a problem

<dependencies>

    <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>4.7.2</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-core</artifactId>
        <version>4.7.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apiguardian/apiguardian-api -->
    <dependency>
        <groupId>org.apiguardian</groupId>
        <artifactId>apiguardian-api</artifactId>
        <version>1.1.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>4.7.2</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.5.1</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.28.2</version>
        <scope>test</scope>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>

    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>4.5.3</version>
    </dependency>


    <!-- Web driver manager dependency -->

    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>3.6.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>3.6.2</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.13.2</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.1.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.1.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.glassfish/javax.json -->
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.json</artifactId>
        <version>1.0.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>com.github.mkolisnyk</groupId>
        <artifactId>cucumber-runner</artifactId>
        <version>1.3.5</version>
    </dependency>

    <!-- Start - Following dependencies are for XRAY export functionalities -->

    <dependency>
        <groupId>com.xpandit.xray</groupId>
        <artifactId>xray-maven-plugin</artifactId>
        <version>1.0.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.28</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.28</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.28</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>2.28</version>
    </dependency>

    <!-- End - Dependencies are for XRAY export functionalities -->

    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20190722</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.29</version>
    </dependency>


</dependencies>

The url that I am using to export the file is https://jira.mydomain.net/rest/raven/1.0/import/execution/cucumber

When I extract the Json body from the file and send it as a POST request in Postman, it works perfectly. I just want to implement the same as Java code in my Test Automation project.

Any help to get me receive a 200 status would be much appreciated.


Solution

  • Well, I found the answer to my question on my own. All I had to do is to pass the .json file as an InputStream to the Response instead of Json Array. I just replaced

    JsonParser parser = new JsonParser();
    Object obj = parser.parse(br);
    JsonArray arr = (JsonArray) obj;  
    

    with

    InputStream input = new FileInputStream(filePath);