Guest User

Untitled

a guest
Oct 14th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 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. HttpResponse response = ourHttp.send(request);
  50. Token objAuthenticationInfo = (Token)JSON.deserialize(response.getbody(), Token.class);
  51.  
  52. if(objAuthenticationInfo.ACCESS_TOKEN != null){
  53. Type__c typ = [SELECT Id, Name FROM Type__c WHERE Id =: TypeID];
  54. HttpRequest finalRequest = finalHttpRequest(objAuthenticationInfo.ACCESS_TOKEN, 'POST', '');
  55. finalRequest.setBody(GenerateJSON(typ));
  56. HttpResponse finalResponse = ourHttp.send(finalRequest);
  57. if(finalResponse.getStatusCode() == 200) {
  58. System.debug('CREATED: ' + finalResponse.getBody());
  59. return finalResponse;
  60. }else {
  61. return null;
  62. }
  63. }
  64. return null;
  65. }
  66.  
  67. @isTest
  68. global class AnimalsHttpCalloutMock implements HttpCalloutMock {
  69. global HTTPResponse respond(HTTPRequest request) {
  70. HttpResponse response = new HttpResponse();
  71. response.setHeader('Content-Type', 'application/json');
  72. Settings__c settings = [SELECT ConsumerKey__c, ClientSecret__c, Username__c,
  73. Password__c, SecurityToken__c
  74. FROM Settings__c
  75. WHERE Name = 'OurSettings'];
  76. String serialized = JSON.serialize(settings);
  77. response.setBody(serialized);
  78. response.setStatusCode(200);
  79. return response;
  80. }
  81. }
  82.  
  83. @isTest
  84. private class CalloutTest {
  85.  
  86.  
  87. @isTest
  88. static void testPostCallout() {
  89. Settings__c settings = new Settings__c(Name = 'OurSettings',
  90. ConsumerKey__c = '3MVG9I1kFE5Iul2DdHpJB2OydGLbGpb8xcTB.2lhtT16LdER9QVx_AmoaOb6gN_3OEtHPlvBDzBYWieC189Cp',
  91. ClientSecret__c = '4738373237519024129',
  92. Username__c = 'nihontaisai@nihon.com',
  93. SecurityToken__c = '4qVSDcdY09ndyURd32J7Ls2VN',
  94. Password__c = 'mypassword1');
  95. insert settings;
  96.  
  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. Type__c typ = new Type__c(Name = 'ttt');
  105. insert typ;
  106.  
  107. Http ourHttp = new Http();
  108. String requestBody = CalloutJobAdvertisement.accessTokenBody();
  109. System.debug('request: ' + request);
  110. System.debug('request2: ' + requestBody);
  111. HttpRequest ourRequest = new HttpRequest();
  112. ourRequest.setBody(requestBody);
  113. ourRequest.setMethod('POST');
  114. ourRequest.setEndpoint('https://p21.salesforce.com/services/oauth2/token');
  115. Test.startTest();
  116. Test.setMock(HttpCalloutMock.class, new AnimalsHttpCalloutMock());
  117. HttpResponse response = CalloutJobAdvertisement.postCalloutResponseContents(pos.Id);
  118.  
  119. //Error is here. System.NullPointerException: Attempt to de-reference a null object
  120.  
  121. String contentType = response.getHeader('Content-Type');
  122. System.assert(contentType == 'application/json');
  123. String actualValue = response.getBody();
  124. System.debug(response.getBody());
  125. String expectedValue = '{"Name":"ttt"}';
  126. System.assertEquals(actualValue, expectedValue);
  127. System.assertEquals(200, response.getStatusCode());
  128. Test.stopTest();
  129. }
  130.  
  131. public class OAuth2{
  132. public String ACCESS_TOKEN{get;set;}
  133. }
  134. }
Add Comment
Please, Sign In to add comment