Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BillingCalloutService {
- //Implement business and callout logic methods here
- //
- public List<Project__c> lstToUpdateProj = new List<Project__c>();
- @future(callout=true)
- public static void callBillingService(List<Id> projsId){
- ServiceCredentials__c sc = ServiceCredentials__c.getValues('BillingServiceCredential');
- String username = '';
- String password = '';
- if(sc!=null){
- username = sc.Username__c;
- password = sc.Password__c;
- }
- List<Project__c> lstAllProj = new List<Project__c>([Select Id,Status__c,ProjectRef__c,Billable_Amount__c from Project__c where Id=:projsId]);
- for(Project__c p:lstAllProj){
- if(p.Status__c == 'Billable'){
- BillingServiceProxy.project wrapProj = new BillingServiceProxy.project();
- wrapProj.projectRef = p.ProjectRef__c;
- wrapProj.billAmount = p.Billable_Amount__c;
- wrapProj.username = username;
- wrapProj.password = password;
- BillingServiceProxy.InvoicesPortSoap11 objBill = new BillingServiceProxy.InvoicesPortSoap11();
- objBill.billProject(wrapProj);
- p.Status__c ='Billed';
- lstToUpdateProj.add(p);
- }
- }
- }
- if(lstToUpdateProj.size()>0 && lstToUpdateProj!=null){
- update lstToUpdateProj;
- }
- }
- trigger ProjectTrigger on Project__c (after update) {
- //Call the Billing Service callout logic here
- //
- List<Id> lstProjIds = new List<Id>();
- for(Project__c p:Trigger.New){
- lstProjIds.add(p.Id);
- }
- BillingCalloutService.callBillingService(lstProjIds);
- }
- @isTest
- private class BillingCalloutServiceTest {
- //Implement mock callout tests here
- static testMethod void testSuccess(){
- ServiceCredentials__c sc = new ServiceCredentials__c();
- sc.Name = 'BillingServiceCredential';
- sc.Username__c = 'bsUser1';
- sc.Password__c = 'bsPass1';
- insert sc;
- Opportunity opp = new Opportunity(Name='New Opp Test',StageName='Prospecting',Type='New Project',CloseDate=date.today().addDays(20));
- insert opp;
- Project__c proj= new Project__c(Opportunity__c=opp.Id);
- proj.Status__c = 'Running';
- insert proj;
- BillingCalloutServiceMock mock = new BillingCalloutServiceMock();
- Test.startTest();
- Test.setMock(WebServiceMock.class, mock);
- proj.Status__c = 'Billable';
- update proj;
- Test.stopTest();
- System.assertEquals('Billed', proj.Status__c);
- }
- static testMethod void testFailure(){
- ServiceCredentials__c sc = new ServiceCredentials__c();
- sc.Name = 'BillingServiceCredential';
- sc.Username__c = 'bsUser1';
- sc.Password__c = 'bsPass1';
- insert sc;
- Opportunity opp = new Opportunity(Name='New Opp Test',StageName='Prospecting',Type='New Project',CloseDate=date.today().addDays(20));
- insert opp;
- Project__c proj= new Project__c(Opportunity__c=opp.Id);
- proj.Status__c = 'Running';
- insert proj;
- BillingCalloutServiceMockFailure mock = new BillingCalloutServiceMockFailure();
- Test.startTest();
- Test.setMock(WebServiceMock.class, mock);
- proj.Status__c = 'Billable';
- update proj;
- Test.stopTest();
- System.assertEquals('Billed', proj.Status__c);
- }
- }
- for(Project__c p:Trigger.New){
- //add your filter logic here, so specific changes to be passed for callout
- lstProjIds.add(p.Id);
- }
- List<Project__c> lstAllProj = [Select Id,Status__c,ProjectRef__c,Billable_Amount__c from Project__c where Id=:projsId];
- if(lstToUpdateProj.size()>0 && lstToUpdateProj!=null){
- update lstToUpdateProj;
- }
- Test.startTest();
- Test.setMock(WebServiceMock.class, mock);
- proj.Status__c = 'Billable';
- update proj;
- Test.stopTest();
- //perform your DML before callout
- Test.startTest();
- Test.setMock(WebServiceMock.class, mock);
- // check results with assets
- Test.stopTest();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement