+ Start a Discussion
Sagar104Sagar104 

Test class for apex class for webservice callouts

Hi All

Kindly help me here, i have an apex class for webservice, i require the test class to be completed.

The APEX CLASS is as follows;

global class Sendxxx {
    private static String workOrderId ;
    private static String workOrderStatus ;
    webService static void postToSAP(String orderStatus, String woId) { 
   system.debug('orderStatus'+orderStatus);
      If(orderStatus =='Complete' || orderStatus =='Partially Completed')
       { 
       system.debug('orderStatus'+orderStatus);
        workOrderId  = woId;
          workOrderStatus = orderStatus.tolowercase();
          //workOrderStatus = orderStatus;
        getAndParse();
        
    }
    else
    {
      system.debug('orderStatus'+orderStatus);
     }
      }
    
  global static void getAndParse() {
    
        HttpRequest req = new HttpRequest();
  
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        //String strname = 'Username';
       // String strpwd= 'pwd';
        //String SessionId = Userinfo.getSessionId();
        String strURL = System.URL.getSalesforceBaseURL().toExternalForm();
        req.setEndpoint('https://elastic.snaplogic.com:443/api/1/rest/slsched/feed/xxxx/xx/xxxrviceMax/SVMXC_SumEstimatedTime_Task?work_order_id='+workOrderId+'&state='+workOrderStatus);
        req.setHeader('authorization','Bearer owhBgvxxxxxxz');
        req.setTimeout(120000);
        req.setHeader('cache-control', 'no-cache');
        req.setMethod('GET');
        
        JSONGenerator gen = JSON.createGenerator(true);
            gen.writeStartArray();
            gen.writeStartObject();
            gen.writeStringField('SVMXC__Service_Order__c', workOrderId );
           gen.writeStringField('SVMXC__Order_Status__c',workOrderStatus );
            gen.writeEndObject();              
          gen.writeEndArray();
          String jsonOrders = gen.getAsString();
           System.debug('jsonOrders: ' + jsonOrders);
          req.setHeader('Content-Type', 'application/json');
          req.setHeader('Accept-Encoding', 'gzip');
           req.setBody(jsonOrders);
            req.setCompressed(true); // otherwise we hit a limit of 32000 
        try {
            res = http.send(req);
            
            if (res.getStatusCode() == 200 ) {
            
             System.debug('CALLOUOT SUCCESSFUL');
             System.debug('RESPONSE FROM CPQ 1111--->'+res.toString());
             System.debug('RESPONSE BODY--->'+res.getBody());
            }
        }             
        catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug('RESPONSE FROM CPQ'+res.toString());
        }
        
 }
 }
JeffreyStevensJeffreyStevens
See if this helps....
https://developer.salesforce.com/blogs/developer-relations/2013/03/testing-apex-callouts-using-httpcalloutmock.html
 
Sagar104Sagar104
Hi Jeffrey,

I tried to go through your link, but its hard for me to get those, As im new to development .. i Kindly request you to give me a test class for that , so that i can present it to business..
Thank you.