0

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();
            }
    }
Suryaneel Varshney
  • 85
  • 1
  • 2
  • 13
  • 1
    Curl is a command line http client. You can do the same thing with a java http client. You will have to translate the curl command to the http concepts and then figure out how the java http client uses those in its api. – M.P. Korstanje Mar 20 '21 at 13:53
  • @M.P.Korstanje can u check Sérgio answer below and suggest how to do – Suryaneel Varshney Mar 23 '21 at 12:07

2 Answers2

0

You can see a concrete example in the following code, taken from this repo which implements a Java client library for REST API. Perhaps you can use it (see docs here) and thus avoid having to implement it in your end.

package net.azae.xray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpClient {
    private final static Logger logger = LoggerFactory.getLogger(HttpClient.class);

    // TODO : get base URL from conf
    String baseUrl = "https://xray.cloud.xpand-it.com";

    public static String jsonPost(String toURL, String data, String token) {
        URL url;
        String response = "";
        try {
            url = new URL(toURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            if (token != null)
                conn.setRequestProperty("Authorization", "Bearer " + token);

            conn.setDoInput(true);
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(data);
            writer.flush();
            writer.close();
            os.close();
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                String line;
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line = br.readLine()) != null) {
                    response += line;
                }
            } else {
                response = "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    public static String jsonPost(String toURL, String data) {
        return jsonPost(toURL, data, null);
    }

    String getToken(String json) {
        String response = jsonPost(baseUrl + "/api/v1/authenticate", json);
        return response.replace("\"", "");
    }

    void publishToXray(String token, String inputJson) {
        logger.debug("Publish input: " + inputJson);
        String response = jsonPost(baseUrl + "/api/v1/import/execution", inputJson, token);
        logger.debug("Publish response: " + response);
    }
}

https://gitlab.com/azae/outils/junit-xray/-/blob/master/src/main/java/net/azae/xray/HttpClient.java

Sérgio
  • 1,777
  • 2
  • 10
  • 12
  • which method to call and what prameters need to be passed so that it will import the execution result. public static void main(String[] args) { jsonPost("toURL", "data", "token"); } – Suryaneel Varshney Mar 22 '21 at 18:03
0

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);
Thomas
  • 26
  • 1
  • i had gone through this post but if i use that specific library it doesnot have option to pass client id and client secret value to get the token. so its not much clear – Suryaneel Varshney Mar 24 '21 at 04:38
  • @After public void jiraReport() throws IOException { //JiraReporting.importExecutionToJira(); HttpClient httpClient = new HttpClient(); String response = httpClient.getToken("empty json"); System.out.println("response ----"+response); } } – Suryaneel Varshney Mar 24 '21 at 04:52
  • @SuryaneelVarshney I add some usage example. – Thomas Apr 01 '21 at 15:19