Apex Class parent code
The Connector Create REST API, Fetch REST API and Synchronize REST API codes all share an Apex Class Parent Code which is provided below. This code will need to be included as a class before you are able to use the codes provided in the Create REST API, Fetch REST API and Synchronize REST API pages.
We have also included a Unit Test Code for this parent which you can find below.
Each REST API will also require three additional classes, if you want to run their respective Unit Test codes. These classes are also provided below.
Apex Class parent code
The following class is necessary before implementing any Apex Classes for REST APIs:
public with sharing class JIRA {
// Change values in this class according to you JIRA/Salesforce coordinates.
public static String baseUrl = 'JIRA URL'; // Base URL of your JIRA instance
public static String systemId = '1'; // Salesforce Connector System ID in JIRA
public static String username = 'your jira username'; // JIRA username
public static String password = 'your jira password'; // JIRA password
public static String agentProfileName = 'JIRA Agent'; // Jira agent profile name in Salesforce
// Constructs Basic Http Authentication header from provided credentials
public static String authHeader(String u, String p) {
Blob headerValue = Blob.valueOf(u + ':' + p);
return 'Basic ' + EncodingUtil.base64Encode(headerValue);
}
// Sends a request and returns the response
public static HttpResponse sendRequest(HttpRequest req) {
Http http = new Http();
return http.send(req);
}
// Detects whether current user is not JIRA agent. By calling this you can make sure that
// infinite loops won't happen in triggers (for instance when synchronizing an issue with JIRA)
public static Boolean currentUserIsNotJiraAgent() {
Boolean allow = false;
List<Profile> jiraAgentProfile = [SELECT Id FROM Profile WHERE Name=:JIRA.agentProfileName];
if (!jiraAgentProfile.isEmpty()) {
String jiraProfileAgentId = String.valueOf(jiraAgentProfile[0].id);
allow = UserInfo.getProfileId() != jiraProfileAgentId;
}
return allow || Test.isRunningTest();
}
}
Remember to change the following variables accordingly:
Variables | Details |
---|---|
| Your Jira URL |
| Your system Id in Connections. You can check this in Jira under Connections |
| Jira username |
| Jira password |
| Profile Name in Salesforce Profiles |
Unit test code for this parent
To test the Apex Class parent code, you can use the following code:
@isTest private class JIRATest {
// Tests authHeader method in JIRA class.
static testMethod void authHeaderTest() {
Test.setMock(HttpCalloutMock.class, new MockHttpResponseJIRAConnector());
String authHeader = JIRA.authHeader(TestFixture.username, TestFixture.password);
System.assertEquals(authHeader, 'Basic eW91ckpJUkF1c2VybmFtZTp5b3VySklSQXBhc3N3b3Jk');
}
// Tests sendRequest method in JIRA class.
static testMethod void sendRequestTest() {
Test.setMock(HttpCalloutMock.class, new MockHttpResponseJIRAConnector());
HttpRequest req = new HttpRequest();
HttpResponse res = JIRA.sendRequest(req);
System.assertEquals(res.getStatusCode(), 200);
}
// Tests currentUserIsNotJiraAgent method in JIRA class.
static testMethod void currentUserIsNotJiraAgentTest() {
System.assert(JIRA.currentUserIsNotJiraAgent());
}
}
Fixtures and mock objects
When implementing unit test codes for any of the REST APIs, you must also include the following three classes:
@isTest public with sharing class TestFixture {
// All fields in this class are used only in unit tests.
// No need to change, but make sure to change the unit tests respectively if you change these values.
public static String baseUrl = 'http://jira.com';
public static String systemId = '1';
public static String username = 'yourJIRAusername';
public static String password = 'yourJIRApassword';
public static String objectType = 'Case';
public static String objectId = '1';
public static String projectKey = 'salesforce';
public static String issueType = 'bug';
public static String caseId = '1';
public static String jiraIssueResponseBody = '[{"summary": "Project One", "project": "project one", ' +
'"reporter": "admin@gmail.com", "key": "PO-1", "status": "open", ' +
'"resolution": "yes", "url": "www.google.com", "type": "bug", "assignee": "admin", ' +
'"description": "issue 1", "priority": "high", "due_date": "12-12-2015" },' +
'{"summary": "Project Two", "project": "project two"}]';
}