Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. public class BillingCalloutService {
  2. //Implement business and callout logic methods here
  3. //
  4.  
  5. public List<Project__c> lstToUpdateProj = new List<Project__c>();
  6.  
  7. @future(callout=true)
  8. public static void callBillingService(List<Id> projsId){
  9. ServiceCredentials__c sc = ServiceCredentials__c.getValues('BillingServiceCredential');
  10. String username = '';
  11. String password = '';
  12. if(sc!=null){
  13. username = sc.Username__c;
  14. password = sc.Password__c;
  15. }
  16.  
  17. List<Project__c> lstAllProj = new List<Project__c>([Select Id,Status__c,ProjectRef__c,Billable_Amount__c from Project__c where Id=:projsId]);
  18.  
  19. for(Project__c p:lstAllProj){
  20. if(p.Status__c == 'Billable'){
  21. BillingServiceProxy.project wrapProj = new BillingServiceProxy.project();
  22. wrapProj.projectRef = p.ProjectRef__c;
  23. wrapProj.billAmount = p.Billable_Amount__c;
  24. wrapProj.username = username;
  25. wrapProj.password = password;
  26.  
  27. BillingServiceProxy.InvoicesPortSoap11 objBill = new BillingServiceProxy.InvoicesPortSoap11();
  28. objBill.billProject(wrapProj);
  29.  
  30. p.Status__c ='Billed';
  31. lstToUpdateProj.add(p);
  32. }
  33. }
  34. }
  35.  
  36. if(lstToUpdateProj.size()>0 && lstToUpdateProj!=null){
  37. update lstToUpdateProj;
  38. }
  39. }
  40.  
  41. trigger ProjectTrigger on Project__c (after update) {
  42. //Call the Billing Service callout logic here
  43. //
  44. List<Id> lstProjIds = new List<Id>();
  45. for(Project__c p:Trigger.New){
  46. lstProjIds.add(p.Id);
  47. }
  48. BillingCalloutService.callBillingService(lstProjIds);
  49. }
  50.  
  51. @isTest
  52. private class BillingCalloutServiceTest {
  53. //Implement mock callout tests here
  54. static testMethod void testSuccess(){
  55. ServiceCredentials__c sc = new ServiceCredentials__c();
  56. sc.Name = 'BillingServiceCredential';
  57. sc.Username__c = 'bsUser1';
  58. sc.Password__c = 'bsPass1';
  59. insert sc;
  60.  
  61. Opportunity opp = new Opportunity(Name='New Opp Test',StageName='Prospecting',Type='New Project',CloseDate=date.today().addDays(20));
  62. insert opp;
  63.  
  64. Project__c proj= new Project__c(Opportunity__c=opp.Id);
  65. proj.Status__c = 'Running';
  66. insert proj;
  67.  
  68. BillingCalloutServiceMock mock = new BillingCalloutServiceMock();
  69.  
  70. Test.startTest();
  71. Test.setMock(WebServiceMock.class, mock);
  72. proj.Status__c = 'Billable';
  73. update proj;
  74. Test.stopTest();
  75.  
  76. System.assertEquals('Billed', proj.Status__c);
  77. }
  78.  
  79. static testMethod void testFailure(){
  80.  
  81. ServiceCredentials__c sc = new ServiceCredentials__c();
  82. sc.Name = 'BillingServiceCredential';
  83. sc.Username__c = 'bsUser1';
  84. sc.Password__c = 'bsPass1';
  85. insert sc;
  86.  
  87. Opportunity opp = new Opportunity(Name='New Opp Test',StageName='Prospecting',Type='New Project',CloseDate=date.today().addDays(20));
  88. insert opp;
  89.  
  90. Project__c proj= new Project__c(Opportunity__c=opp.Id);
  91. proj.Status__c = 'Running';
  92. insert proj;
  93.  
  94. BillingCalloutServiceMockFailure mock = new BillingCalloutServiceMockFailure();
  95.  
  96. Test.startTest();
  97. Test.setMock(WebServiceMock.class, mock);
  98. proj.Status__c = 'Billable';
  99. update proj;
  100. Test.stopTest();
  101.  
  102. System.assertEquals('Billed', proj.Status__c);
  103. }
  104. }
  105.  
  106. for(Project__c p:Trigger.New){
  107. //add your filter logic here, so specific changes to be passed for callout
  108. lstProjIds.add(p.Id);
  109. }
  110.  
  111. List<Project__c> lstAllProj = [Select Id,Status__c,ProjectRef__c,Billable_Amount__c from Project__c where Id=:projsId];
  112.  
  113. if(lstToUpdateProj.size()>0 && lstToUpdateProj!=null){
  114. update lstToUpdateProj;
  115. }
  116.  
  117. Test.startTest();
  118. Test.setMock(WebServiceMock.class, mock);
  119. proj.Status__c = 'Billable';
  120. update proj;
  121. Test.stopTest();
  122.  
  123. //perform your DML before callout
  124. Test.startTest();
  125. Test.setMock(WebServiceMock.class, mock);
  126. // check results with assets
  127. Test.stopTest();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement