Guest User

Untitled

a guest
Oct 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.92 KB | None | 0 0
  1. public class BillingServiceProxy {
  2. public class billProjectRequest_element {
  3. public BillingServiceProxy.project project;
  4. private String[] project_type_info = new String[]{'project','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
  5. private String[] apex_schema_type_info = new String[]{'http://salesforce.com/th/invoice-web-service','true','false'};
  6. private String[] field_order_type_info = new String[]{'project'};
  7. }
  8. public class project {
  9. public String username;
  10. public String password;
  11. public String projectid;
  12. public String projectRef;
  13. public Double billAmount;
  14. private String[] username_type_info = new String[]{'username','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
  15. private String[] password_type_info = new String[]{'password','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
  16. private String[] projectRef_type_info = new String[]{'projectRef','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
  17. private String[] projectid_type_info = new String[]{'projectid','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
  18. private String[] billAmount_type_info = new String[]{'billAmount','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
  19. private String[] apex_schema_type_info = new String[]{'http://salesforce.com/th/invoice-web-service','true','false'};
  20. private String[] field_order_type_info = new String[]{'username','password','projectid','projectRef','billAmount'};
  21. }
  22. public class billProjectResponse_element {
  23. public String status;
  24. private String[] status_type_info = new String[]{'status','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
  25. private String[] apex_schema_type_info = new String[]{'http://salesforce.com/th/invoice-web-service','true','false'};
  26. private String[] field_order_type_info = new String[]{'status'};
  27. }
  28. public class InvoicesPortSoap11 {
  29. public String endpoint_x = 'http://sb-integration-bs.herokuapp.com:80/ws';
  30. public Map<String,String> inputHttpHeaders_x;
  31. public Map<String,String> outputHttpHeaders_x;
  32. public String clientCertName_x;
  33. public String clientCert_x;
  34. public String clientCertPasswd_x;
  35. public Integer timeout_x;
  36. private String[] ns_map_type_info = new String[]{'http://salesforce.com/th/invoice-web-service', 'BillingServiceProxy'};
  37. public String billProject(BillingServiceProxy.project project) {
  38. BillingServiceProxy.billProjectRequest_element request_x = new BillingServiceProxy.billProjectRequest_element();
  39. request_x.project = project;
  40. BillingServiceProxy.billProjectResponse_element response_x;
  41. Map<String, BillingServiceProxy.billProjectResponse_element> response_map_x = new Map<String, BillingServiceProxy.billProjectResponse_element>();
  42. response_map_x.put('response_x', response_x);
  43. WebServiceCallout.invoke(
  44. this,
  45. request_x,
  46. response_map_x,
  47. new String[]{endpoint_x,
  48. '',
  49. 'http://salesforce.com/th/invoice-web-service',
  50. 'billProjectRequest',
  51. 'http://salesforce.com/th/invoice-web-service',
  52. 'billProjectResponse',
  53. 'BillingServiceProxy.billProjectResponse_element'}
  54. );
  55. response_x = response_map_x.get('response_x');
  56. return response_x.status;
  57. }
  58. }
  59. }
  60.  
  61.  
  62.  
  63. APex CLASS:
  64.  
  65. public class BillingCalloutService {
  66.  
  67. //method to run asynchronously or in future, invoked from trigger.
  68. @future(callout=true)
  69. public static void callBillingService(String projectRef, Decimal billingAmount){
  70. //gets the custom settings values.
  71. ServiceCredentials__c servCred = ServiceCredentials__c.getValues('BillingServiceCredential');
  72. //creating the project object to be passed in argument below.
  73. BillingServiceProxy.project project = new BillingServiceProxy.project();
  74. project.username = servCred.Username__c;
  75. project.password = servCred.Password__c;
  76. project.projectid = projectRef;
  77. project.projectRef = projectRef;
  78. project.billAmount = billingAmount;
  79. //synchronous call to the Billing Invoice system.
  80. BillingServiceProxy.InvoicesPortSoap11 invoiceCall = new BillingServiceProxy.InvoicesPortSoap11();
  81. //getting the response back from billing system.
  82. String response = invoiceCall.billProject(project);
  83. //if the response is ok, need to change the billing status to 'Billed'
  84. List<Project__c> proj;
  85. system.debug('response: '+response);
  86. if (response != null && response.equalsIgnoreCase('OK')){
  87. proj = [SELECT Status__c FROM Project__c WHERE ProjectRef__c =: projectRef];
  88. if(proj.size() > 0){
  89. proj[0].Status__c = 'Billed';
  90. }
  91. update proj;
  92. }
  93. }
  94. }
  95.  
  96.  
  97. APEX CLASS:
  98.  
  99. @isTest
  100. global class BillingCalloutServiceMock implements WebServiceMock {
  101. global void doInvoke(
  102. Object stub,
  103. Object request,
  104. Map<String, Object> response,
  105. String endpoint,
  106. String soapAction,
  107. String requestName,
  108. String responseNS,
  109. String responseName,
  110. String responseType) {
  111. BillingServiceProxy.billProjectResponse_element respElement =
  112. new BillingServiceProxy.billProjectResponse_element();
  113. respElement.status = 'OK';
  114. response.put('response_x', respElement);
  115. }
  116. }
  117.  
  118. APEX CLASS:
  119.  
  120. @isTest
  121. global class BillingCalloutServiceMockFailure implements WebServiceMock {
  122. global void doInvoke(
  123. Object stub,
  124. Object request,
  125. Map<String, Object> response,
  126. String endpoint,
  127. String soapAction,
  128. String requestName,
  129. String responseNS,
  130. String responseName,
  131. String responseType) {
  132. BillingServiceProxy.billProjectResponse_element respElement =
  133. new BillingServiceProxy.billProjectResponse_element();
  134. respElement.status = 'UNAUTHORIZED';
  135. response.put('response_x', respElement);
  136. }
  137. }
  138.  
  139. APEX CLASS:
  140.  
  141. @isTest
  142. private class BillingCalloutServiceTest {
  143.  
  144. @testSetup static void setupProject(){
  145.  
  146. Opportunity oppo = new Opportunity();
  147. oppo.Name = 'TestOpp';
  148. oppo.CloseDate = Date.today();
  149. oppo.StageName = 'Prospecting';
  150. insert oppo;
  151.  
  152. Project__c proj = new Project__c();
  153. proj.Name = 'TestProj';
  154. proj.Billable_Amount__c = 1000;
  155. proj.ProjectRef__c = 'TestRef';
  156. proj.Status__c = 'Running';
  157. proj.Opportunity__c = oppo.Id;
  158. insert proj;
  159.  
  160. ServiceCredentials__c servCred = new ServiceCredentials__c();
  161. servCred.Name = 'BillingServiceCredential';
  162. servCred.Username__c = 'user1';
  163. servCred.Password__c = 'pass1';
  164. insert servCred;
  165.  
  166. }
  167.  
  168. @isTest static void testCalloutSuccess(){
  169. Test.setMock(WebServiceMock.class, new BillingCalloutServiceMock());
  170. List<Project__c> prof = [SELECT Status__C FROM Project__c WHERE ProjectRef__c = 'TestRef'];
  171. System.assertEquals(1, prof.size());
  172. Test.startTest();
  173. prof[0].Status__c = 'Billable';
  174. update prof;
  175. Test.stopTest();
  176. prof = [SELECT Status__C FROM Project__c WHERE ProjectRef__c = 'TestRef'];
  177. System.assertEquals(1, prof.size());
  178. System.assertEquals('Billed', prof[0].Status__C);
  179. }
  180.  
  181. @isTest static void testCalloutFailure(){
  182. Test.setMock(WebServiceMock.class, new BillingCalloutServiceMockFailure());
  183. List<Project__c> prof = [SELECT Status__C FROM Project__c WHERE ProjectRef__c = 'TestRef'];
  184. System.assertEquals(1, prof.size());
  185. Test.startTest();
  186. prof[0].Status__c = 'Running';
  187. update prof;
  188. Test.stopTest();
  189. prof = [SELECT Status__C FROM Project__c WHERE ProjectRef__c = 'TestRef'];
  190. System.assertEquals(1, prof.size());
  191. System.assertEquals('Running', prof[0].Status__C);
  192. }
  193.  
  194. }
  195.  
  196.  
  197. APEX TRIGGER:
  198.  
  199. trigger ProjectTrigger on Project__c (after update) {
  200. //after trigger to fire if the Status is set to Billable.
  201. If (Trigger.isAfter && Trigger.isUpdate){
  202. for(Project__c proj : Trigger.new){
  203. if(proj.Status__c.equals('Billable')){
  204. BillingCalloutService.callBillingService(proj.ProjectRef__c, proj.Billable_Amount__c);
  205. }
  206. }
  207. }
  208. }
Add Comment
Please, Sign In to add comment