Search code examples
javacurlautomationjirajira-xray

Run multiple curl commands sequentially using java


i am able to run both the commands in terminal sequentialy and its working fine.same thing i want to achieve through java

token=$(curl -H "Content-Type: application/json" -X POST --data @"/Users/surya/KarateUIAutomation/target/surefire-reports/cloud_auth.json" https://xray.cloud.xpand-it.com/api/v2/authenticate| tr -d '"')

In 2nd curl command the 1st token to be passed as parameter

curl -H "Content-Type: application/json" -X POST -H "Authorization: Bearer $token"  --data @"/Users/surya/KarateUIAutomation/target/surefire-reports/testcase.firstUITest.json" https://xray.cloud.xpand-it.com/api/v2/import/execution/cucumber

I have written the below java code but not sure how to pass the above 2 commands and run sequentially

String[] command = {" "};
           
           
            ProcessBuilder process = new ProcessBuilder(command); 
            Process p;
            try
            {
                p = process.start();
                 BufferedReader reader =  new BufferedReader(new InputStreamReader(p.getInputStream()));
                    StringBuilder builder = new StringBuilder();
                    String line = null;
                    while ( (line = reader.readLine()) != null) {
                            builder.append(line);
                            builder.append(System.getProperty("line.separator"));
                    }
                    String result = builder.toString();
                    System.out.print(result);

            }
            catch (IOException e)
            {   System.out.print("error");
                e.printStackTrace();
            }
    }

Solution

  • I'm agree with M.P. Korstanje, launching curl command line from java it's a poor solution in front of calling URLs from pure java HTTP library.

    HttpClient.getToken replace your first curl command (https://gitlab.com/azae/outils/junit-xray/-/blob/master/src/main/java/net/azae/xray/HttpClient.java#L58)

    You can use it with this java code :

    String token = new HttpClient().getToken("{ " +
                    "\"client_id\": \"" + xray_client_id + "\", " +
                    "\"client_secret\": \"" + xray_client_secret + "\"" +
                    " }";);
    

    and HttpClient.publishToXray could be update to push cucumber result (https://gitlab.com/azae/outils/junit-xray/-/blob/master/src/main/java/net/azae/xray/HttpClient.java#L63)

    And you can use it with this java code :

    String json = "Your json"; //See https://docs.getxray.app/display/XRAYCLOUD/Import+Execution+Results+-+REST
    new HttpClient().publishToXray(token, json);