Guest User

Untitled

a guest
Oct 13th, 2018
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. public class Token{
  2. public String accessToken{get;set;}
  3. }
  4.  
  5. public static String accessTokenBody(){ //our web token (data is in fields)
  6. Settings__c settings = [SELECT ConsumerKey__c, ClientSecret__c, Username__c, Password__c, SecurityToken__c
  7. FROM Settings__c
  8. WHERE Name = 'OurSettings'];
  9. String consumerKey = settings.ConsumerKey__c;
  10. String consumerSecret = settings.ClientSecret__c;
  11. String username = settings.Username__c;
  12. String password = settings.Password__c + settings.SecurityToken__c;
  13. String request = 'grant_type=password&client_id=' + consumerKey +'&client_secret=' + consumerSecret +
  14. '&username=' + username + '&password='+password;
  15. return request;
  16. }
  17.  
  18. public static String GenerateJSON(Type__c t){
  19. //I will send and post it like a record in another org:
  20. Map<String, String> fieldMap = new Map<String, String>{
  21. 'Name' => t.Name,
  22. 'Desc__c' => t.Type_Description__c};
  23. String serialized = JSON.serialize(fieldMap);
  24. return serialized;
  25. }
  26.  
  27. public static HttpRequest httpRequest(String service){
  28. String requestBody = accessTokenBody();
  29. HttpRequest ourRequest = new HttpRequest();
  30. ourRequest.setBody(requestBody);
  31. ourRequest.setMethod(service);
  32. ourRequest.setEndpoint('https://p21.lightning.force.com/services/oauth2/token');
  33. return ourRequest;
  34. }
  35.  
  36. public static HttpRequest finalHttpRequest(String token, String method, String endpointUrl){
  37. HttpRequest finalRequest = new HttpRequest();
  38. finalRequest.setHeader('Authorization','Bearer ' + token);
  39. finalRequest.setHeader('Content-Type','application/json');
  40. finalRequest.setHeader('accept','application/json');
  41. finalRequest.setMethod(method);
  42. finalRequest.setEndpoint('https://p21.lightning.force.com/services/oauth2/token' + endpointUrl);
  43. return finalRequest;
  44. }
  45.  
  46. public static HttpResponse postCallout(String positionId) {
  47. Http ourHttp = new Http();
  48. HttpRequest request = httpRequest('POST');
  49. //Error is here:
  50. HttpResponse response = ourHttp.send(request);
  51. Token objAuthenticationInfo = (Token)JSON.deserialize(response.getbody(), Token.class);
  52.  
  53. if(objAuthenticationInfo.ACCESS_TOKEN != null){
  54. Type__c typ = [SELECT Id, Name FROM Type__c WHERE Id =: TypeID];
  55. HttpRequest finalRequest = finalHttpRequest(objAuthenticationInfo.ACCESS_TOKEN, 'POST', '');
  56. finalRequest.setBody(GenerateJSON(typ));
  57. HttpResponse finalResponse = ourHttp.send(finalRequest);
  58. if(finalResponse.getStatusCode() == 200) {
  59. System.debug('CREATED: ' + finalResponse.getBody());
  60. return finalResponse;
  61. }else {
  62. return null;
  63. }
  64. }
  65. return null;
  66. }
  67.  
  68. @isTest
  69. global class AnimalsHttpCalloutMock implements HttpCalloutMock {
  70. global HTTPResponse respond(HTTPRequest request) {
  71. HttpResponse response = new HttpResponse();
  72. response.setHeader('Content-Type', 'application/json');
  73. response.setBody('{"Name":"ttt"}');
  74. response.setStatusCode(200);
  75. return response;
  76. }
  77.  
  78. @isTest
  79. private class CalloutTest {
  80.  
  81. @isTest
  82. static void testPostCallout() {
  83.  
  84. public class Token{
  85. public String token{get;set;}
  86. }
  87.  
  88. Settings__c settings = new Settings__c(Name = 'OurSettings',
  89. ConsumerKey__c = '3Mv78JilhgTvnlkjlkjydGLbGpb8xcTB.2lhtT
  90.  
  91. 16LdER9QVx_AmoaOb6gN_3OEtHPlvBDzBYWieC189Cp',
  92. ClientSecret__c = '3453453453419024129',
  93. Username__c = 'mail@mail.com',
  94. SecurityToken__c = '6qdfgcdY6ndyURd32J7Ls2VN',
  95. Password__c = 'mypassword1');
  96. insert settings;
  97. String consumerKey = settings.ConsumerKey__c;
  98. String consumerSecret = settings.ClientSecret__c;
  99. String username = settings.Username__c;
  100. String password = settings.Password__c + settings.SecurityToken__c;
  101. String request = 'grant_type=password&client_id=' + consumerKey +'&client_secret=' + consumerSecret +
  102. '&username=' + username + '&password='+password;
  103.  
  104. Http ourHttp = new Http();
  105. String requestBody = CalloutClass.accessTokenBody();
  106. System.debug('request: ' + request);
  107. System.debug('request2: ' + requestBody);
  108. HttpRequest ourRequest = new HttpRequest();
  109. ourRequest.setBody(request);
  110. ourRequest.setMethod('POST');
  111. ourRequest.setEndpoint('https://p21.salesforce.com/services/oauth2/token');
  112. Type__c typ = new Type__c(Name = 'ttt');
  113. insert typ;
  114.  
  115. Test.startTest();
  116. Test.setMock(HttpCalloutMock.class, new AnimalsHttpCalloutMock());
  117. HttpResponse response = CalloutClass.postCallout(typ.Id);
  118. //System.NullPointerException: Attempt to de-reference a null object:
  119. String contentType = response.getHeader('Content-Type');
  120. System.assert(contentType == 'application/json');
  121. String actualValue = response.getBody();
  122. System.debug(response.getBody());
  123. String expectedValue = '{"Name":"ttt"}';
  124. System.assertEquals(actualValue, expectedValue);
  125. System.assertEquals(200, response.getStatusCode());
  126. Test.stopTest();
  127. }
  128. }
Add Comment
Please, Sign In to add comment