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
Mahmoud Coudsi 1Mahmoud Coudsi 1 

Apex Test Class for method calling in onclick javascript & call bunch of CSV and JSON data from the static resources.

Hi all,
 
Can anyone help me in solving the scenario below .
 
I have written an onclick javascript button and called a method and passed one arguement into it. Now I want to write a test method for the class which is declared as webservice and load bunch of CSV and JSON data into the class.
 
/////JAVASCRIPT BUTTON CODE////////
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var msg = new sforce.SObject("Lead"); 
msg.Id = '{!Lead.Id}'; 	

var result = sforce.connection.update([msg]); 

var patientId='{!Lead.Name}'; 
var result = sforce.apex.execute("Qualification","PreQualificationCheck",{patientID: patientId}); 

alert(result); 


// } 

window.location.reload(); 
sforce.debug.trace=true;
 
/////APEX CLASS/////
global with sharing class Qulification {

// API secret key and password


Webservice static void PreQualificationCheck(String patientID) {
             
 // Find the patient in question using the passed in Patient ID through the check Qulification button
     Lead l = [select Id, Name, Leads_DOB__c, FirstName, LastName,
                              From Lead
                              Where Name = :patientID];  

// a series of if-else statements to determine lead qualification (More than 40 scenario
if(bla bla  == bla) {
  ..etc
  ..etc
  ..etc
 }

update l;

}

Private static String getCoverageInformation(String response, String patientID) {

// Parase JSON responses into variables --> then Salesforce fields

// A series of if-else statements to further determine lead qualification

}


Test Class/Method:

To trigger all the possible API reponses in my scenerio, I have 40+ different input possiblities (combinations) that should fire back 40+ responses combinations. Afterwards, the responses go through a series of if-else statements to results in one of the three possible answers. 

Since there are numerous of inputs and outputs in this case are, I thought it would be best to utilize the static resources; load my input data through a CSV (1000+ records) and load another JSON file of all the different possible outputs (API responses). 

I haven't got very far with my test class, as I need more direction here: 

1. How can I call the PreQualificationCheck method and pass my CSV lead data through it?
2. How can I tie in my input data that I'm entering in the class through CSV to the response data that being returned as a JSON from the API response call? Do I actually have rewrite my if-else statements logic in the @testclass? 

Example:

If (State__c == "Texas" &&  Lead_Type__c == "Remote memeber" && Lead_Memeber_Policy_Id == 1234111) {Memeber_Group_type__c = "Commerical" && Active__c = "True"..etc.}

CSV (Input): State__c, Lead_Type__c, Lead_Memeber_Policy_Id
JSON (Output/API response): Memeber_Group_type__c, Active__c
 

@isTest 
global class DataUtil {
    static testmethod void testLoadData() {
        // Load the test lead from the static resource
        List<sObject> ls = Test.loadData(Lead.sObjectType, 'TestCSV');
        
        // Verify that all my 1000 test lead were created
        System.assert(ls.size() == 1000);

        // Get first test lead
        Lead l1 = (Lead)ls[0];
        String LeadFirstName = l1.FirstName;
        String LeadLastName = l1.LastName;
        String PolicyID = l1.Leads_Policy_ID__c;
        Decimal Weight = l1.LEADS_WEIGHT__c;
        Decimal Feets = l1.FEET__C;
        Decimal Inches = l1.INCHES__c;
        
// Now not sure what do I do next? Do I loop through all the 1000 leads and entering as params in the method? 

// How can load my JSON - static resource to load my respones to this test class? (I have a rough idea but detailed description/examplification would be apperciated)

// How can I tie in my input data that I'm entering in the class through CSV to the response data that being returned as a JSON from the API response call? Do I actually have rewrite my if-else statements logic in the @testclass?            

       }
}

Please help me in writting the test method for the above class.

P.S:

My code runs perfectly fine on the sandbox so no need to review the Qulification Class or the Button's code.