You need to sign in to do that
Don't have an account?
Derek Davis 7
Create a Test Class for Bitly Integration
Hello Everyone!
I was able to create a Bitly Integration using the awesome instructions provided by Doug Ayers shown here: https://douglascayers.wordpress.com/2015/10/21/salesforce-create-short-urls-with-bitly-process-builder-and-apex/
This is working well in the Sandbox, but I am not a new to APEX and attempting to create a Test Class so I can move to Production.
There are two different APEX Classes:
BitlyService Apex Class:
BitlyShortenURLInvocable APEX Class:
I was able to create a Bitly Integration using the awesome instructions provided by Doug Ayers shown here: https://douglascayers.wordpress.com/2015/10/21/salesforce-create-short-urls-with-bitly-process-builder-and-apex/
This is working well in the Sandbox, but I am not a new to APEX and attempting to create a Test Class so I can move to Production.
There are two different APEX Classes:
- BitlyService
- BitlyShortenURLInvocable
BitlyService Apex Class:
/** * Simple service to make http callout to * Bitly url shortener service. */ public class BitlyService { // reusable access token for oauth, // required when making API requests private String accessToken; public BitlyService() { this.accessToken = getAccessToken(); } /** * Given a long URL will return the shortened version. * http://dev.bitly.com/links.html#v3_shorten */ public String shorten( String url ) { HttpRequest req = new HttpRequest(); req.setEndpoint( 'callout:Bitly/v3/shorten' + '?access_token=' + this.accessToken + '&longUrl=' + EncodingUtil.urlEncode( url, 'UTF-8' ) + '&format=txt' ); req.setMethod('GET'); Http http = new Http(); HttpResponse res = http.send(req); return res.getBody(); } /** * Get the access token to make authenticated oauth calls. * The actual username/password credentials are stored in * Named Credentials so that the password is stored securely. * * This does require an extra http request when instantiating * the service which adds to latency. Alternatively, you could * store the generated access token in a custom setting and simply * reference it from your code, but then anyone who can view * custom settings can view the access token and use the API. * Trade-offs. */ private String getAccessToken() { HttpRequest req = new HttpRequest(); req.setEndpoint('callout:Bitly/oauth/access_token'); req.setMethod('POST'); Http http = new Http(); HttpResponse res = http.send(req); return res.getBody(); } }
BitlyShortenURLInvocable APEX Class:
public class BitlyShortenURLInvocable { @InvocableMethod( label = 'shorten' description = 'Given service request IDs then generates a bitly short url for them' ) public static List<String> shorten( List<ID> srIds ) { // You can't invoke http callouts from Process Builder or Flow // because the database transaction has not committed yet, you will get error: // "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out" // To get around this then we'll actually make the call in a @Future method asynchronously. shortenAsync( srIds ); return new List<String>(); } @Future( callout = true ) private static void shortenAsync( List<ID> srIds ) { // Fetch your data and a field where you want to store the generated short url List<Service_Request__c> requests = new List<Service_Request__c>([ SELECT id, assigned_to_text__c, short_url__c FROM service_request__c WHERE id IN :srIds ]); // Service to actually call out to bitly and get a shortened url BitlyService service = new BitlyService(); // Bitly does not support bulk url shortening // so you must make each call individually. // Do be aware of Bitly's rate limiting if you try // to mass create a bunch of records in short succession // http://dev.bitly.com/rate_limiting.html for ( Service_Request__c ServiceRequestObj : requests ) { // create Service Request URL to be shortened (with the variables needed by the Service Request Status Flow) ServiceRequestObj.short_url__c = service.shorten( 'https://MyServerName.salesforce.com/My_Flow_Name_Here?varSRID=' + ServiceRequestObj.id + '&varOriginalAssignedTo=' + ServiceRequestObj.assigned_to_text__c ); } // update the records with their short urls // use workflow or trigger to fire off the short url field being populated // to then send email alerts, etc. including the short url if ( requests.size() > 0 ) { update requests; } } }
All Answers
In BitlyService you ar making http callout . You will have to create a test class with HttpCalloutMock .
Kindly follow the below link:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_dml.htm
Regards
Ankit Gupta
I agree with what Ankit has said, for testing HTTP callouts its always better to have your Mock Callout Classes.
However, there is a way to cover part of this class,
for example:
public static testmethod void negativeTest1() {
Test.startTest();
String string123 = '123123123123';
MDTBitlyConnection bitlyObject= new MDTBitlyConnection();
MDTBitlyConnection.accessToken = null;
String shortenURL = bitlyObject.getShortenURL(string123);
Test.stopTest();
}
PLEASE VOTE THIS AS THE RIGHT ANSWER, IF YOU LIKE IT.
Thanks,
Rohit Alladi
Thanks!