How to raise a Defect in JIRA using Java Program


We always wonder that we have Test scripts to send an Email report to all the stakeholder with extensive report with count of Passed and failed after test suite execution but no logical test script to log the defect for the failed test cases in the Test management Tool.

It might not affect much if we are working in conventional methodology like waterfall, protype etc. But we are in the level of deploying the products by adhering to the Continuous Delivery and Continuous Deployment process.

So, to cope up the automation activities with the product deployment, the Product management tool – Jira provides an additional feature of updating/defect logging using test scripts by providing REST Api. Now we will look the implementation of Defect logging script,


Pre-requisites
Need below mentioned two Jars files in the build class path.
Apache HttpClient 4.5.3 API - https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.5.3
jersey-client.jar - http://www.java2s.com/Code/Jar/j/Downloadjerseyclientjar.htm

//intiate connection with JIRA tool
Client oClient = Client.create();
oClient.addFilter(new HTTPBasicAuthFilter(JIRA_USERNAME,JIRA_PASSWORD));
WebResource oWebResource = oClient.resource(JIRA_URL+ "/rest/api/2/issue");

//frame the message which we want to post to JIRA defect board
String sInput = "{'fields':"+
                                +"{'project':"
                                +"{'id':'" + JIRA_PROJECT_ID + "'},"+
                                +"'summary':'" + summary + "',"+
                                +"'description':'" + description + "',"+
                                +"'reporter': {'name': '" + JIRA_ISSUE_REPORTER + "'},"+
                                +"'issuetype':{'name':'" + JIRA_ISSUE_TYPE + "'}"+
                                +"}"+
                +"}";

//post the framed value and store the output to extract the issueKey. So that, we can attach the screenshot of the failed test case along with the defect
ClientResponse oResponse = oWebResource.type("application/json").post(ClientResponse.class, sInput);
String sOutput = oResponse.getEntity(String.class);
String sIssueKey = sOutput.split(":")[2].split(",")[0].replace("\"", "");

//create connection to attach the screenshot to the defect
String sAuthenticationConnString = new String(org.apache.commons.codec.binary.Base64.encodeBase64((JIRA_USERNAME + ":" +JIRA_PASSWORD).getBytes()));

CloseableHttpClient oHttpclient = HttpClients.createSystem();
HttpPost oHttppost = new HttpPost(JIRA_URL + "/rest/api/2/issue/"+ sIssueKey + "/attachments");
oHttppost.setHeader("X-Atlassian-Token", "no-check");
oHttppost.setHeader("Authorization", "Basic "+ sAuthenticationConnString);

//screenshot attached to the defect
File oToUpload = new File(SCREENSHOTPATH);
FileBody oFileBody = new FileBody(fileToUpload);
HttpEntity oEntity = MultipartEntityBuilder.create().addPart("file", oFileBody).build();
oHttppost.setEntity(oEntity);
CloseableHttpResponse oResponse;
oResponse= oHttpclient.execute(oHttppost);
oHttpclient.close();

if (oResponse.getStatusLine().getStatusCode() == 200) {
       System.Out.Println(“attached successfully”);
} else {
      System.Out.Println(“Error while attaching the file” + oResponse.getStatusLine().getStatusCode() );
}


Voila, Done!!!!!

Popular posts from this blog

Selenium Webdriver Interview Questions with Answer

Selenium IDE Interview Questions with Answer

DLL to Jar file conversion