Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BillingServiceProxy {
- public class billProjectRequest_element {
- public BillingServiceProxy.project project;
- private String[] project_type_info = new String[]{'project','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
- private String[] apex_schema_type_info = new String[]{'http://salesforce.com/th/invoice-web-service','true','false'};
- private String[] field_order_type_info = new String[]{'project'};
- }
- public class project {
- public String username;
- public String password;
- public String projectid;
- public String projectRef;
- public Double billAmount;
- private String[] username_type_info = new String[]{'username','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
- private String[] password_type_info = new String[]{'password','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
- private String[] projectRef_type_info = new String[]{'projectRef','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
- private String[] projectid_type_info = new String[]{'projectid','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
- private String[] billAmount_type_info = new String[]{'billAmount','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
- private String[] apex_schema_type_info = new String[]{'http://salesforce.com/th/invoice-web-service','true','false'};
- private String[] field_order_type_info = new String[]{'username','password','projectid','projectRef','billAmount'};
- }
- public class billProjectResponse_element {
- public String status;
- private String[] status_type_info = new String[]{'status','http://salesforce.com/th/invoice-web-service',null,'1','1','false'};
- private String[] apex_schema_type_info = new String[]{'http://salesforce.com/th/invoice-web-service','true','false'};
- private String[] field_order_type_info = new String[]{'status'};
- }
- public class InvoicesPortSoap11 {
- public String endpoint_x = 'http://sb-integration-bs.herokuapp.com:80/ws';
- public Map<String,String> inputHttpHeaders_x;
- public Map<String,String> outputHttpHeaders_x;
- public String clientCertName_x;
- public String clientCert_x;
- public String clientCertPasswd_x;
- public Integer timeout_x;
- private String[] ns_map_type_info = new String[]{'http://salesforce.com/th/invoice-web-service', 'BillingServiceProxy'};
- public String billProject(BillingServiceProxy.project project) {
- BillingServiceProxy.billProjectRequest_element request_x = new BillingServiceProxy.billProjectRequest_element();
- request_x.project = project;
- BillingServiceProxy.billProjectResponse_element response_x;
- Map<String, BillingServiceProxy.billProjectResponse_element> response_map_x = new Map<String, BillingServiceProxy.billProjectResponse_element>();
- response_map_x.put('response_x', response_x);
- WebServiceCallout.invoke(
- this,
- request_x,
- response_map_x,
- new String[]{endpoint_x,
- '',
- 'http://salesforce.com/th/invoice-web-service',
- 'billProjectRequest',
- 'http://salesforce.com/th/invoice-web-service',
- 'billProjectResponse',
- 'BillingServiceProxy.billProjectResponse_element'}
- );
- response_x = response_map_x.get('response_x');
- return response_x.status;
- }
- }
- }
- APex CLASS:
- public class BillingCalloutService {
- //method to run asynchronously or in future, invoked from trigger.
- @future(callout=true)
- public static void callBillingService(String projectRef, Decimal billingAmount){
- //gets the custom settings values.
- ServiceCredentials__c servCred = ServiceCredentials__c.getValues('BillingServiceCredential');
- //creating the project object to be passed in argument below.
- BillingServiceProxy.project project = new BillingServiceProxy.project();
- project.username = servCred.Username__c;
- project.password = servCred.Password__c;
- project.projectid = projectRef;
- project.projectRef = projectRef;
- project.billAmount = billingAmount;
- //synchronous call to the Billing Invoice system.
- BillingServiceProxy.InvoicesPortSoap11 invoiceCall = new BillingServiceProxy.InvoicesPortSoap11();
- //getting the response back from billing system.
- String response = invoiceCall.billProject(project);
- //if the response is ok, need to change the billing status to 'Billed'
- List<Project__c> proj;
- system.debug('response: '+response);
- if (response != null && response.equalsIgnoreCase('OK')){
- proj = [SELECT Status__c FROM Project__c WHERE ProjectRef__c =: projectRef];
- if(proj.size() > 0){
- proj[0].Status__c = 'Billed';
- }
- update proj;
- }
- }
- }
- APEX CLASS:
- @isTest
- global class BillingCalloutServiceMock implements WebServiceMock {
- global void doInvoke(
- Object stub,
- Object request,
- Map<String, Object> response,
- String endpoint,
- String soapAction,
- String requestName,
- String responseNS,
- String responseName,
- String responseType) {
- BillingServiceProxy.billProjectResponse_element respElement =
- new BillingServiceProxy.billProjectResponse_element();
- respElement.status = 'OK';
- response.put('response_x', respElement);
- }
- }
- APEX CLASS:
- @isTest
- global class BillingCalloutServiceMockFailure implements WebServiceMock {
- global void doInvoke(
- Object stub,
- Object request,
- Map<String, Object> response,
- String endpoint,
- String soapAction,
- String requestName,
- String responseNS,
- String responseName,
- String responseType) {
- BillingServiceProxy.billProjectResponse_element respElement =
- new BillingServiceProxy.billProjectResponse_element();
- respElement.status = 'UNAUTHORIZED';
- response.put('response_x', respElement);
- }
- }
- APEX CLASS:
- @isTest
- private class BillingCalloutServiceTest {
- @testSetup static void setupProject(){
- Opportunity oppo = new Opportunity();
- oppo.Name = 'TestOpp';
- oppo.CloseDate = Date.today();
- oppo.StageName = 'Prospecting';
- insert oppo;
- Project__c proj = new Project__c();
- proj.Name = 'TestProj';
- proj.Billable_Amount__c = 1000;
- proj.ProjectRef__c = 'TestRef';
- proj.Status__c = 'Running';
- proj.Opportunity__c = oppo.Id;
- insert proj;
- ServiceCredentials__c servCred = new ServiceCredentials__c();
- servCred.Name = 'BillingServiceCredential';
- servCred.Username__c = 'user1';
- servCred.Password__c = 'pass1';
- insert servCred;
- }
- @isTest static void testCalloutSuccess(){
- Test.setMock(WebServiceMock.class, new BillingCalloutServiceMock());
- List<Project__c> prof = [SELECT Status__C FROM Project__c WHERE ProjectRef__c = 'TestRef'];
- System.assertEquals(1, prof.size());
- Test.startTest();
- prof[0].Status__c = 'Billable';
- update prof;
- Test.stopTest();
- prof = [SELECT Status__C FROM Project__c WHERE ProjectRef__c = 'TestRef'];
- System.assertEquals(1, prof.size());
- System.assertEquals('Billed', prof[0].Status__C);
- }
- @isTest static void testCalloutFailure(){
- Test.setMock(WebServiceMock.class, new BillingCalloutServiceMockFailure());
- List<Project__c> prof = [SELECT Status__C FROM Project__c WHERE ProjectRef__c = 'TestRef'];
- System.assertEquals(1, prof.size());
- Test.startTest();
- prof[0].Status__c = 'Running';
- update prof;
- Test.stopTest();
- prof = [SELECT Status__C FROM Project__c WHERE ProjectRef__c = 'TestRef'];
- System.assertEquals(1, prof.size());
- System.assertEquals('Running', prof[0].Status__C);
- }
- }
- APEX TRIGGER:
- trigger ProjectTrigger on Project__c (after update) {
- //after trigger to fire if the Status is set to Billable.
- If (Trigger.isAfter && Trigger.isUpdate){
- for(Project__c proj : Trigger.new){
- if(proj.Status__c.equals('Billable')){
- BillingCalloutService.callBillingService(proj.ProjectRef__c, proj.Billable_Amount__c);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment