1. While
working as a manual or automation tester we need to interact with JIRA many
time, for bug reporting, for task creation etc, it take much time to do this
again and again, to minimize this time, we can automate this process by interacting with JIRA using its REST API
1 1. Create a java project in eclipse
1.
As a Prerequisite, we need some client libraries
to access JIRA Rest API
Javax.ws.rs.jar
Jersey-bundle-1.6.jar
Jason-lib.jar
1.
Create Package & Class, Add following code
package testing;
import javax.naming.AuthenticationException;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.Base64;
import core.ValueSettings;
class Case1 {
// Create a string
for authorization(Passing username and password)
static String auth
= new String(Base64.encode(ValueSettings.userAuthentication()));
// Url for
creating an issue using REST API ----
static String url
= ValueSettings.getIssueURL()+"/TES"; // Project ID
static String
url11 = "http://localhost:8080/rest/api/2/issue";
//JSON for
creating an issue using REST API
static
String data = "{\"fields\"" +
":{\"project\"" +
":{\"key\":\"TES\"}," +
"\"description\": \"description|TESTING|B\" ,"+
// Description
"\"priority\": {\"name\":
\"Major\"},"+
"\"reporter\": {\"name\":
\"testingworldnoida\"},"+
"\"assignee\": {\"name\":
\"testingworldnoida\"},"+
"\"summary\":\"REST Test
\",\"issuetype\":{\"name\":\"Task\"}}}";
static
String data2 = "{\"fields\"" +
":{\"project\""
+
":{\"key\":\"TES\"},"
+
"\"summary\": \"something's wrong\","+
"\"issuetype\": {\"name\":
\"Task\"},"+
// "\"assignee\":
{\"name\": \"testingworldnoida\"},"+
// "\"reporter\":
{\"name\": \"testingworldnoida\"},"+
// "\"priority\":
{\"name\": \"Major\"},"+
"\"description\": \"description\"}" ;
//url for
retrieving an issue using REST API ---- R of CRUD
static
String url1 = "http://localhost:8080/rest/api/2/issue/TJ-1";
//url for
updating an issue using REST API ---- U of CRUD
static
String url2 = "http://localhost:8080/rest/api/2/issue/TJ-1";
/*
* JSON
data to be updated for an issue using RESP API
*
* */
static
String data1 =
"{\"fields\":{\"assignee\":{\"name\":\"vinodh\"}}}";
// String data2 =
"{fields:{summary:Demo Test}}"
/*
* url
for deleting an issue using RESP API ---- D of CRUD
*
* */
static
String url3 = "http://localhost:8080/rest/api/2/issue/TJ-1";
/*
* HTTP
POST method is used to create the issue
*
* */
public
static String invokePostMethod(String auth, String url, String data) throws
AuthenticationException, ClientHandlerException {
Client client = Client.create();
System.out.println(url);
WebResource webResource = client.resource(url);
ClientResponse response = webResource.header("Authorization",
"Basic " + auth).type("application/json").accept("application/json").post(ClientResponse.class,
data);
int
statusCode = response.getStatus();
System.out.println(statusCode);
if
(statusCode == 401) {
throw new AuthenticationException("Invalid Username or
Password");
}
return "issue successfully created";
}
/*
* HTTP
GET method is used to get the issue
*
* */
private
static String invokeGetMethod(String auth, String url) throws
AuthenticationException, ClientHandlerException {
Client client = Client.create();
WebResource webResource = client.resource(url);
ClientResponse response = webResource.header("Authorization",
"Basic " +
auth).type("application/json").accept("application/json").get(ClientResponse.class);
int statusCode = response.getStatus();
System.out.println(statusCode);
if
(statusCode == 401) {
throw new AuthenticationException("Invalid Username or
Password");
}
return
response.getEntity(String.class);
}
/*
* HTTP
PUT method is used to update the issue
*
* */
private
static String invokePutMethod(String auth, String url, String data1) throws
AuthenticationException, ClientHandlerException {
Client
client = Client.create();
WebResource
webResource = client.resource(url);
ClientResponse
response = webResource.header("Authorization", "Basic " +
auth).type("application/json").accept("application/json").put(ClientResponse.class,
data1);
int
statusCode = response.getStatus();
if
(statusCode == 401) {
throw
new AuthenticationException("Invalid Username or Password");
}
return
"success";
}
/*
* HTTP
DELETE method is used to delete the issue
*
* */
private
static String invokeDeleteMethod(String auth, String url) throws
AuthenticationException, ClientHandlerException {
Client client = Client.create();
WebResource webResource = client.resource(url);
ClientResponse response = webResource.header("Authorization",
"Basic " +
auth).type("application/json").accept("application/json").delete(ClientResponse.class);
int
statusCode = response.getStatus();
if (statusCode == 401) {
throw new AuthenticationException("Invalid Username or
Password");
}
return "successfully deleted";
}
public
static void main(String[] args) throws Exception {
/*
* 1. Creating
an JIRA issue in project with key TJ using REST API
*
*/
System.out.println(invokePostMethod(auth, url11, data));
/*
* 2. Getting
details of an JIRA issue in project with issue no TJ-1 using REST API
*/
String resp=invokeGetMethod(auth, url1);
JSONObject
jo=(JSONObject)JSONSerializer.toJSON(resp);
JSONObject
sum=jo.getJSONObject("fields");
System.out.println(sum.getString("summary"));
/*
* 3. Updating
details of an JIRA issue in project with issue no TJ-1 using REST API
*/
System.out.println(invokePutMethod(auth, url2, data1));
/*
* 4. Deleting
an JIRA issue in project with issue no TJ-1 using REST API
*/
System.out.println(invokeDeleteMethod(auth,
url3));
}
}
Run this code, go
to JIRA and Verify
Here we will find Task is created



