function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
developvoismartdevelopvoismart 

Write test class for this class?

public class ClickAndDial {
    public String Phone {get; set;}
    public Id Id { get; set; }
    public Contact contact { get; set; }

    public ClickAndDial(ApexPages.StandardController controller) {
        contact =  (Contact) controller.getRecord();
        Id = contact.Id;
        Phone = contact.Phone;  
    }
    
    public PageReference clickAndDial() {
		PageReference pageRef = new PageReference('/' + Id);
                
       	String token = this.login();
        if (token == null || String.isEmpty(token)) {
            return pageRef;
        }
		Id userId = UserInfo.getUserId();

        System.debug(LoggingLevel.DEBUG,'User ID' +userId);
        List<User> prof = [SELECT Extension FROM User WHERE Id=:userId LIMIT 1];
        if (prof.isEmpty() || String.isEmpty(prof[0].Extension)) {
            return pageRef;
        }
		String myExt = prof[0].Extension;
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
  
        request.setMethod('POST');
        request.setEndpoint('callout:Voismart/jsondata/extjs/dialerHandler/click_and_dial');
        request.setHeader('Content-Type', 'application/x-www-form-urlencoded'); 
        String payload = 
            'token=' + EncodingUtil.urlEncode(token,'UTF-8') 
            + '&data='+ EncodingUtil.urlEncode(
                '[{"number_to_call":"' + Phone + 
                '", "number_to_connect":"' + myExt + 
                '", "auto_answer": true}]'
                ,'UTF-8');
        request.setBody(payload);
        HttpResponse res = http.send(request);
        
        return pageRef;
    }
    
    public String login() {
        Http http = new Http();
        HTTPRequest loginRequest = new HTTPRequest();
        loginRequest.setMethod('POST');
		loginRequest.setEndpoint('callout:Voismart');
		loginRequest.setHeader('Content-Type', 'application/x-www-form-urlencoded');

        
        String loginPayload = 
            'username=' + '{!$Credential.UserName}' + 
            '&password={!$Credential.Password}' +
            '&api=true' + '&browser_tz=' + EncodingUtil.urlEncode('Europe/Rome','UTF-8')+
            '&request_date='+ EncodingUtil.urlEncode(string.valueOfGmt(System.now()),'UTF-8');
        	
		loginRequest.setBody(loginPayload);
        HTTPResponse loginRes = http.send(loginRequest);
        System.debug('========>loginRes'+loginRes);

        Map<String, Object> values = (Map<String, Object>)JSON.deserializeUntyped(loginRes.getBody());
		String token = (String)values.get('token');
        return token;
    }
}

 
Soniya RautSoniya Raut
Please refer this link (https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_rest_callouts) to write test class for REST API call out. See under Test Callouts section. Also refer this video (https://www.youtube.com/watch?v=tXzY2tQQkzY) for better understanding of Test classes for callouts