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
Shruti NigamShruti Nigam 

need help in writing test class for API calling

Hi all need help in writing test class.

Not able to get coverage for both @aura enabled methods.
public class CallPageApexLightning {
	
    public Settings settings;
    public Data data;
    
    @auraEnabled
    public static Map<String, Object> checkuser()
    {
			String email = UserInfo.getUserEmail();

        	Http http = new Http();
			HttpRequest request = new HttpRequest();
			request.setEndpoint('https://xxxxxxxxxxxxx');
			request.setMethod('GET');

			HttpResponse response = http.send(request);
        	Map<String, Object> records;
			if (response.getStatusCode() == 200) 
        	{	
            		Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                    records = (Map<String, Object>) results.get('data');
   			}

        	String token = String.valueof(records.get('ws_token'));

            Http http1 = new Http();
            HttpRequest request1 = new HttpRequest();

            request1.setEndpoint('https://xxxxxxxxxxxxxxxxxxxxxxx');
            request1.setMethod('POST');
         	request1.setHeader( 'Content-Type', 'application/json' );
        	String JSONContent = '{"email":"'+email+'","ws_token":"'+token+'"}';
        	request1.setBody(JSONContent);

            HttpResponse response1 = http1.send(request1);
        	Map<String, Object> urlrecorddata;
        	Map<String, Object> expertdetail;
            if (response1.getStatusCode() == 200) 
            {
                Map<String, Object> results1 = (Map<String, Object>) JSON.deserializeUntyped(response1.getBody());
                urlrecorddata = (Map<String, Object>) results1.get('data');
            }
        	urlrecorddata.put('ws_token', token);

			return urlrecorddata;        
    }

    @auraEnabled
    public static void closecallapex(String caseid, String experttoken, String token)
    {
        	Http http = new Http();
            HttpRequest request = new HttpRequest();

            request.setEndpoint('https://xxxxxxxxxxxxx');
            request.setMethod('POST');
         	request.setHeader( 'Content-Type', 'application/json' );
        	String JSONContent = '{"cr_id":"'+caseid+'","expert":"'+experttoken+'","ws_token":"'+token+'"}';
        	request.setBody(JSONContent);
        	
            HttpResponse response = http.send(request);
            if (response.getStatusCode() == 200) 
            {
             	CallPageApexLightning ab = CallPageApexLightning.parse(response.getBody());

                afRecord__c cobject = new afRecord__c();
                for(CallPageApexLightning.Call_detail pol:  ab.data.c_detail)
                {
                    	
                        cobject.Case_Related_To__c = caseid;
                        cobject.Expert_Chat__c = pol.expert_chat;
                        cobject.Expert_Notes__c = pol.expert_notes;
                }
                insert cobject;
            }
    }


	public class c_detail {
		public String expert_notes;
		public String expert_chat;
	}

	public class Data {
		public List<Call_detail> c_detail;

	}

	public class Settings {
		public String success;
		public String message;
		public List<String> fields;
	}

    public static CallPageApexLightning parse(String json) 
    {
        return (CallPageApexLightning) System.JSON.deserialize(json, CallPageApexLightning.class);
    }
}

Thanks in advance.
 
ShirishaShirisha (Salesforce Developers) 
Hi Shruti,

Can you please try the sample code given in the below documentation to cover the Http callouts:

https://salesforce.stackexchange.com/questions/23597/test-class-for-rest-api

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri