Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. webservice static void RESTApiToAccount(id AccountId)
  2. {
  3. String clientId = '3MVG9YDQS5WtC11p0izGLTFFxnJa365Xd9WpNGN1oZPe1ArfQML4Q8EKN8ye3qF4m7iCVPWas1LV1OVCFd9oO';
  4. String clientSecret = '4032474715821060338';
  5. String username = '';
  6. String password = '';
  7. String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
  8. Http h = new Http();
  9. HttpRequest req = new HttpRequest();
  10. req.setBody(reqbody);
  11. req.setMethod('POST');
  12. req.setEndpoint('https://ap4.salesforce.com/services/oauth2/token');
  13. HttpResponse res = h.send(req);
  14. SendAccountUsingRESTAPI resp1 = (SendAccountUsingRESTAPI)JSON.deserialize(res.getbody(),SendAccountUsingRESTAPI.class);
  15. system.debug('@@@@access_token@@'+res.getbody() );
  16. String accessToken;
  17. accessToken = resp1.access_token;
  18. if(accessToken != null)
  19. {
  20. String endPoint = 'https://ap4.salesforce.com/services/apexrest/Account';
  21. JSONGenerator gen = JSON.createGenerator(true);
  22. Account a=[select id,name,website from account where id=:AccountId];
  23. gen.writeStartObject();
  24. gen.writeStringField('name', a.name);
  25. gen.writeStringField('id', a.id);
  26. gen.writeStringField('website', a.website);
  27. String s=gen.getasString();
  28. Http h2 = new Http();
  29. HttpRequest req1 = new HttpRequest();
  30. req1.setHeader('Authorization','Bearer ' + accessToken);
  31. req1.setHeader('Content-Type','application/json');
  32. req1.setHeader('accept','application/json');
  33. req1.setBody(s);
  34. system.debug('sumaiyaaaaaa'+req1.getbody());
  35. req1.setMethod('POST');
  36. req1.setEndpoint(endPoint);
  37. HttpResponse res1 = h2.send(req1);
  38. String trimmedResponse = res1.getBody();
  39. system.debug('sasasasa'+res1);
  40. system.debug('@@@RESPONSE@@'+trimmedResponse);
  41. JSONParser parser = JSON.createParser(res1.getBody());
  42. }
  43.  
  44. @isTest
  45. static void myTest() { // Set mock callout class
  46. Test.startTest();
  47.  
  48. Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
  49.  
  50. Account a=new account();
  51. a.name='test';
  52. a.website='www.test.com';
  53.  
  54. SendAccountUsingRESTAPI.RESTApiToAccount(a.id);
  55.  
  56. Test.StopTest();
  57. }
  58.  
  59. @isTest
  60. public class MockHttpResponseGenerator implements HttpCalloutMock {
  61. // Implement this interface method
  62. public HTTPResponse respond(HTTPRequest req) {
  63. String endpoint = req.getEndpoint();
  64. if ( endpoint.contains('oauth2/token') ) {
  65. return buildOAuthResponse( req );
  66. } else if ( endpoint.contains('/services/apexrest/Account') ) {
  67. return buildShortenResponse( req );
  68. }
  69. return null;
  70. }
  71. private HttpResponse buildOAuthResponse( HttpRequest req ) {
  72. HttpResponse res = new HttpResponse();
  73. res.setBody('123');
  74. res.setStatusCode(200);
  75. return res;
  76. }
  77. private HttpResponse buildShortenResponse( HttpRequest req ) {
  78.  
  79. // Create a fake response
  80. HttpResponse res = new HttpResponse();
  81. res.setHeader('Content-Type', 'application/json');
  82. res.setBody('{"foo":"bar"}');
  83. res.setStatusCode(200);
  84. return res;
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement