0

I have to increase the code coverage of the class. In order to do that, I have written code coverage for the constructors. This is the class:

public class LendingTIDOutboundIntegrationHelper {
    IntegrationBase iBase;
    public Map<String, Integration_Credential__mdt> creds;
    public LendingTIDOutboundIntegrationHelper() {
        iBase = new IntegrationBase();
        creds = new Map<String, Integration_Credential__mdt>();
        for(Integration_Credential__mdt cred : [SELECT ID, Http_Method__c, DeveloperName, Endpoint__c, HeaderMap__c, Password__c, Integration_Type__c, Username__c FROM Integration_Credential__mdt WHERE Integration_Type__c = 'Lending System']) {
            creds.put(cred.DeveloperName, cred);
        }
    }

    public LendingTIDOutboundIntegrationHelper(IntegrationBase iBase, Map<String, Integration_Credential__mdt> creds) {
        this.iBase = iBase;
        this.creds = creds;
    }

    public HttpResponse authenticateLendingSystem(Integration_Message__c im, Access_Tokens__c tokens) {
        AccessTokenWrapper wrapper = getAccessToken(im);
        if(wrapper.accessToken != null) {
            tokens.Lending_System_Access_Token__c = wrapper.accessToken;
            // call the actual service again
            im.Error_Description__c = null;
            im.Error_Code__c = null;
            Integration_Credential__mdt cred = creds.get(im.Parent_Template_Name__c);
            Map<String, Object> header = (Map<String, Object>) JSON.deserializeUntyped(cred.HeaderMap__c);
            header.put('access-token', tokens.Lending_System_Access_Token__c); //changed lending as per doc 1.2
            HttpResponse res = iBase.makeCalloutWithHeaderV2(im.Request_Envelope__c, cred.Http_Method__c, cred.Endpoint__c, header);
            return res;
        }
        return wrapper.resp;
    }

    @TestVisible private AccessTokenWrapper getAccessToken(Integration_Message__c im) {
        AccessTokenWrapper toReturn = new AccessTokenWrapper();
        // 1. get the credentials
        Integration_Credential__mdt cred = creds.get('Lending_Get_Access_Token');
        Map<String, Object> header = (Map<String, Object>) JSON.deserializeUntyped(cred.HeaderMap__c);
        
        // 2. call the access token api
        HttpResponse res = iBase.makeCalloutWithHeaderV2(null, cred.Http_Method__c, cred.Endpoint__c, header);
        **toReturn.resp = res;
        System.debug(Logginglevel.DEBUG, 'response : ' + res.getBody() + ' Code : ' + res.getStatusCode());
        im.Login_Response_Envelope__c = res.getBody();
        
        // 3. parse the response and return access token
        Map<String, Object> response = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
        if(res.getStatusCode() == 200) {
            toReturn.accessToken = (String)response.get('token'); //changed lending as per doc 1.2
            return toReturn;
        } else {
            im.Error_Description__c = (String)response.get('error_description');
            im.Error_Code__c = res.getStatusCode() + ' ' + (String)response.get('error');
        }
        return toReturn;
    }**

    public class AccessTokenWrapper {
        public String accessToken;
        public HttpResponse resp;
    }

    public class TIDStatusResponseWrapper {
        public List<TIDStatus> tIdStatus;
    }

    public class TIDStatus {
        public String tId, posId, status, remark, processingStatus;
    }
    
    public class Stage2KYCResponseWrapper {
        public List<DocStatus> docStatus;
    }

    public class DocStatus {
        public String docType, docId, status, remark, processingStatus;
    }
    
    public class BankReviewResponseWrapper{
        public String  processingRemarks, processingStatus, reqId;
    }
}

And this is the test class method:

    @isTest
    static void testLendingSystemStage2KYCAPI() {
        List<Account> accList = TestDataFactory.createMerchantAccounts();
        insert accList;
        List<Opportunity> opptylist = TestDataFactory.createOpportunity(accList[0].Id);
        opptylist[0].Type = 'PQR';
        insert opptylist;
        
        Integration_Message__c IM = new Integration_Message__c();
        IM.Opportunity__c = opptylist[0].id;
        IM.Parent_Template_Name__c='Stage_2_KYC_Lending_System_Template';
        IM.Integration_Status__c = 'Success';
        insert IM;
        
        Integration_Message__c IM2 = new Integration_Message__c();
        IM2.Opportunity__c = opptylist[0].id;
        IM2.Parent_Template_Name__c='Stage_2_KYC_Lending_System_Template';
        insert IM2;
        Test.startTest();
        IntegrationBaseHelper ibh = new IntegrationBaseHelper();
        String resp1 = '{\"reqId\":\"101000007\",\"opportunityId\":\"1000018886726768\",\"OppurnuityKycStatus\":\"0\",\"kycStage\":\"2\",\"batchNo\":\"98001010111\",\"docStatus\":[{\"docType\":\"10000001\",\"docId\":\"XXXXXXXXXXXX\",\"status\":\"0\",\"remark\":\"Test Remarks\",\"processingStatus\":\"0\"},{\"docType\":\"10000001\",\"docId\":\"XXXXXXXXXXXX\",\"status\":\"0\",\"remark\":\"Test Remarks\",\"processingStatus\":\"0\"}]}';
        ibh.parseLendingSystemStage2KYCResponse(resp1,IM,IM2);
        
        IntegrationBaseHelper ibh2 = new IntegrationBaseHelper();
        String resp2 = '{\"reqId\":\"101000007\",\"opportunityId\":\"1000018886726768\",\"OppurnuityKycStatus\":\"0\",\"kycStage\":\"2\",\"batchNo\":\"98001010111\",\"docStatus\":[{\"docType\":\"10000001\",\"docId\":\"XXXXXXXXXXXX\",\"status\":\"0\",\"remark\":\"Test Remarks\",\"processingStatus\":\"1\"},{\"docType\":\"10000001\",\"docId\":\"XXXXXXXXXXXX\",\"status\":\"0\",\"remark\":\"Test Remarks\",\"processingStatus\":\"0\"}]}';
        ibh2.parseLendingSystemStage2KYCResponse(resp2,IM,IM2);
        Test.stopTest();
        
    }
    
    @isTest
    static void testLendingTID(){
        
        Test.setMock(HttpCalloutMock.class, new MockHttpResponse_lending());
        Test.startTest();
        LendingTIDOutboundIntegrationHelper t1= new LendingTIDOutboundIntegrationHelper();
        IntegrationBase ibase1;
        Map<String, Integration_Credential__mdt> creds1;
        LendingTIDOutboundIntegrationHelper test1= new LendingTIDOutboundIntegrationHelper();
        LendingTIDOutboundIntegrationHelper test2= new LendingTIDOutboundIntegrationHelper(ibase1,creds1);
        
        
        City_Master__c cm = TestDataFactory.createCityMaster('0','RJ');
        insert cm;
        
        List<Account> accList = TestDataFactory.createBillingEntityAccounts();
        accList[0].State__c = 'RJ';
        accList[0].City__c = cm.id;
        accList[0].Shop_Establishment_Number__c = 'B7/2014/0016739';
        //accList[0].State_Name__c = 'Rajasthan';
        insert accList;
        List<Case> caseList = TestDataFactory.createCase(accList[0].Id, null, null);
        insert caseList;
        
        Integration_Message__c IM = TestDataFactory.createIM(caseList[0].id);
        IM.Parent_Template_Name__c='Signzy_GST_Search';
        IM.Related_Record_Id__c = caseList[0].id;
        IM.Account__c = accList[0].Id;
        IM.Credential_Template_Name__c = 'Signzy_SNE_Validation_Credentials';
        
        insert IM;
        
        Access_Tokens__c at =  new Access_Tokens__c();
        at.Lending_System_Access_Token__c = 'abc';
        insert at;
        String accessToken= 'Access TOKEN 1';
        
        t1.authenticateLendingSystem(IM, at);
        
        LendingTIDOutboundIntegrationHelper atw = LendingTIDOutboundIntegrationHelper.AccessTokenWrapper;
        atw.getAccessToken(IM);
                
        Test.stopTest();
    }
    

I need help to cover the Classes.

2
  • Hello Ashok, and welcome to SFSE, I suggest you rewrite your post with what have you attempted and where you are struggling. Review the How to Ask & search on our community, you can find answers like this one that can help you to get there. Commented Oct 12, 2021 at 9:52
  • which lines do you struggle to cover?
    – Patlatus
    Commented Oct 12, 2021 at 10:34

0

Browse other questions tagged .