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
Sandesh Vishwakarma 9Sandesh Vishwakarma 9 

Please help me get this apex code's test class.

My apex code works first for the authentication with the external api and then sending a record it on update of the opportunity record in salesforce , here is apex class 
 
public class DepreciationSWGetIdUpdatedOppRecord_th {
    @future (callout=true)
    public static void makePostCalloutForAuthentication(String recId) {
        String jwtTokenAccessToken; 
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        // endpoint for establishing authentication with application to get the response of TOKEN 
        request.setEndpoint('callout:BasicAuthintegration/api/Accounts/authenticate');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        // Set the body as a JSON object
        request.setBody('{ "email" : "{!$Credential.BasicAuthforDuoTaxintegration.username}", "password" : "{!$Credential.BasicAuthforDuoTaxintegration.password}"}');
        System.debug('request'+ request);
        HttpResponse response = http.send(request);
        System.debug('response'+ response);
        // Parse the JSON response
        if (response.getStatusCode() != 200) {
            System.debug('The status code returned was not expected: ' +
                         response.getStatusCode() + ' ' + response.getStatus());
        } else {
            // System.debug(response.getBody());
            Map<String , Object> responseResult = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
           // System.debug('responseResult' + responseResult);
            Map<String, Object> getDataValuesFromResponse = (Map<String, Object>) responseResult.get('data');
            //System.debug('results>>>>>>>'+ getDataValuesFromResponse.get('jwtToken'));
            jwtTokenAccessToken = (string)getDataValuesFromResponse.get('jwtToken');
            //System.debug('jwtTokenAccessToken>>>>>>>> '+ jwtTokenAccessToken);
        }
        makePostCalloutToSendRecordId(recId , jwtTokenAccessToken);
        
    }
    
    
    public static void makePostCalloutToSendRecordId(String recId , String accessToken) {
        System.debug('accessToken >>>'+ accessToken);
        String body = recId;
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        // setting the endpoint for and body inculdes the recordID 
        
        request.setEndpoint('callout:BasicAuthintegration/api/Salesforce/UpdateOppotunityMySql?id='+ body);
        request.setMethod('POST');
        request.setHeader('Content-Type', 'text');
        request.setHeader( 'token', 'Bearer ' + accessToken );
        // Set the body as a JSON object
        request.setBody(body);
        System.debug('request'+ request);
        HttpResponse response = http.send(request);
        System.debug('response'+ response);
        // Parse the JSON response 
        if (response.getStatusCode() != 200) {
            System.debug('The status code returned was not expected: ' +
                         response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());
        } 
    } 
    
}
h i am getting 88% code coverage , but pass test are not passing saying , 

System.JSONException: Unexpected character ('S' (code 83)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]

here is my test class 
 
@isTest
public class DepreciationSWGetIdUpdatedOppRecord_test {
    
    // Define a mock HTTP response class for the first callout (authentication).
    public class MockHttpResponseAuthentication implements HttpCalloutMock {
        public HTTPResponse respond(HTTPRequest req) {
            // Create a fake response to simulate a successful authentication callout.
            HttpResponse res = new HttpResponse();
            res.setHeader('Content-Type', 'application/json');
            res.setBody('{"data":{"jwtToken":"fdsbjhifewonaldj"}}');
            res.setStatusCode(200);
            //System.debug('>>> response ' + HTTPResponse.getBody());
            return res;
        }
    }
    
    // Define a mock HTTP response class for the second callout.
    public class MockHttpResponseRecordId implements HttpCalloutMock {
        public HTTPResponse respond(HTTPRequest req) {
            // Create a fake response to simulate a successful callout for sending the record ID.
            HttpResponse res = new HttpResponse();
            res.setHeader('Content-Type', 'application/json;charset=UTF-8');
            res.setBody('Success');
            res.setStatusCode(200);
            return res;
        }
    }
    
    @isTest
    static void testOpportunityTrigger() {
        // Create a test Opportunity record (you can add more fields as needed).
        Opportunity opp = new Opportunity(Name = 'Test Opportunity', StageName = 'Prospecting' , CloseDate = System.today());
        
        // Insert the Opportunity record to trigger the callout.
        insert opp;
        
        
        // Check if the trigger logic works as expected.
        // Add assertions here to validate the trigger's behavior, if any.
        
        // Set up mock responses for the callout methods.
        
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseAuthentication());
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseRecordId());
        
        Test.startTest();
        
        // Call the future method for authentication.
        DepreciationSWGetIdUpdatedOppRecord_th.makePostCalloutForAuthentication(opp.Id);
           // Map<String , Object> responseResult = (Map<String, Object>) JSON.deserializeUntyped('{"data":{"jwtToken":"fdsbjhifewonaldj"}}');
           // Map<String, Object> getDataValuesFromResponse = (Map<String, Object>) responseResult.get('data');

        // Call the method for sending the record ID.
        DepreciationSWGetIdUpdatedOppRecord_th.makePostCalloutToSendRecordId(opp.Id, 'mockAccessToken');
        
        Test.stopTest();
        
        // Add assertions to verify the behavior of the callout methods, if any.
    }
}


 
SwethaSwetha (Salesforce Developers) 
HI ,
Please include which lines of code are not covered in the DepreciationSWGetIdUpdatedOppRecord_th class

The error System.JSONException: Unexpected character ('S' (code 83)): expected a valid value,  occurs when there's an issue with parsing the JSON response body.


Thanks
Sandesh Vishwakarma 9Sandesh Vishwakarma 9
Hi , you are correct here JSON response might be having issue while parsing , In particular this lines are not getting covered
 
Map<String , Object> responseResult = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
           // System.debug('responseResult' + responseResult);
            Map<String, Object> getDataValuesFromResponse = (Map<String, Object>) responseResult.get('data');
            //System.debug('results>>>>>>>'+ getDataValuesFromResponse.get('jwtToken'));
            jwtTokenAccessToken = (string)getDataValuesFromResponse.get('jwtToken');
            //System.debug('jwtTokenAccessToken>>>>>>>> '+ jwtTokenAccessToken);
        }
        makePostCalloutToSendRecordId(recId , jwtTokenAccessToken);