How to update JIRA X-RAY test execution automatically using REST API Java?

Yosuva ArulanthuRanorexLeave a Comment

JAR Files used:

  1. gson-2.6.2.jar
  2. commons.io-2.6.jar
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.Base64;

import javax.net.ssl.HttpsURLConnection;

import org.apache.commons.io.FileUtils;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class JIRAUpdate {
	
	public static void main(String args[]) throws IOException
	{
		String evidencePath="<screenshotpath>,<html/pdf path>";
		String evidences[]=evidencePath.split(",");
		JsonArray evidencesJson=new JsonArray();
		
		for (int i=0;i<evidences.length;i++)
		{
			File evidenceFile=new File(evidences[i]);
			byte[] filecontent=FileUtils.readFileToByteArray(evidenceFile);
			String encoded_FileContent=Base64.getEncoder().encodeToString(filecontent);
			JsonObject evidence=new JsonObject();
			evidence.addProperty("data", encoded_FileContent);
			evidence.addProperty("filename", evidenceFile.getName());
			evidence.addProperty("contentType", "application/html");
			evidencesJson.add(evidence);
		}
		
		JsonObject json=new JsonObject();
		json.addProperty("testExecutionKey", "TEST-123");
		
		JsonArray testArray=new JsonArray();
		JsonObject test=new JsonObject();
		test.addProperty("testKey", "TEST123");
		test.addProperty("start", "2020-04-30T11:47:35+01:00");
		test.addProperty("finish", "2020-04-30T11:47:35+01:00");
		test.addProperty("comment", "Executed through automation.Evidences attached");
		test.addProperty("status", "PASS");
		test.add("evidences", evidencesJson);
		testArray.add(test);
		json.add("test", testArray);
		
		HttpsURLConnection con=null;
		InputStreamReader inputStream=null;
		try {
			URL jira_API_URL=new URL("https://jira.example.com/rest/raven/1.0/import/execution");
			String encodeCredentials=Base64.getEncoder().encodeToString("UserName:Password".getBytes("UTF-8"));
			con=(HttpsURLConnection)jira_API_URL.openConnection();
			con.setRequestMethod("POST");
			con.setDoOutput(true);
			con.setRequestProperty("Autherization", "Basic "+encodeCredentials);
			con.setRequestProperty("Content-Type", "application/json");
			con.setRequestProperty("X-Atlassian-Token", "nocheck");
			try(OutputStream os=con.getOutputStream()){
				byte[] input=json.toString().getBytes("UTF-8");
				os.write(input,0,input.length);
			}
			inputStream=new InputStreamReader(con.getInputStream(),"UTF-8");
			try(BufferedReader br=new BufferedReader(inputStream))
			{
				StringBuilder response=new StringBuilder();
				String responseline=null;
				while((responseline=br.readLine())!=null) {
					response.append(responseline.trim());
				}
			}
			if(con.getResponseCode()==200)
			{
				//jira Test execution updated successfully for the specified test
			}
			else
			{
				//some other connection issue
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if(inputStream!=null) {
				try {
					inputStream.close();
				}
				catch (IOException e) {
					// TODO: handle exception
				}
			}
			if(con!=null) {
				con.disconnect();
			}
		}
	}

}


Leave a Reply

Your email address will not be published. Required fields are marked *