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
RaffusRaffus 

How to get for loop covered in test method

How to cover a for loop in this below test method
@AuraEnabled
    public static string getCasesData(String caseId) {
        try {
            String query = 'SELECT Id,(SELECT Id, ContentDocumentId FROM ContentDocumentLinks), CaseNumber,Issue_category__c, Issue_Sub_Category__c, rand_App_Customer__r.Party_Id__c,rand_App_Customer__r.Customer_Mobile__c, Current_Location__Latitude__s, Current_Location__Longitude__s, LocationName__c, Owner.Name, Description,CreatedDate FROM Case WHERE Id =:caseId';
            query += ' ORDER BY CreatedDate DESC LIMIT 1';
            List<Case> cases = Database.query(query);
            CaseInfoWrapper caseInfo = new CaseInfoWrapper();
            set<Id> contentDocId = new set<Id>();
            for (Case caseRecord : cases) {
                caseInfo.description = caseRecord.Description;
                caseInfo.longitude = caseRecord.Current_Location__Latitude__s;
                caseInfo.latitude = caseRecord.Current_Location__Longitude__s;
                caseInfo.locationName = caseRecord.LocationName__c;
                caseInfo.category = caseRecord.Issue_category__c;
                caseInfo.subCategory = caseRecord.Issue_Sub_Category__c;
                caseInfo.partyId = caseRecord.rand_App_Customer__r.Party_Id__c;
                caseInfo.mobile = caseRecord.rand_App_Customer__r.Customer_Mobile__c;
                if (caseRecord.ContentDocumentLinks.size() > 0) {
                    for (ContentDocumentLink cdl : caseRecord.ContentDocumentLinks) {
                        contentDocId.add(cdl.ContentDocumentId);
                    }
                }
            }
            list<id> contectVersionID = new list<id>();
            map<Id, ContentDistribution> contectDMap = new map<Id, ContentDistribution>();
            for (ContentDistribution conDistribution : [SELECT Id, ContentDocumentId, ContentDownloadUrl FROM ContentDistribution WHERE ContentDocumentId IN:contentDocId ORDER BY CreatedDate ASC]) {
                contectVersionID.add(conDistribution.ContentDocumentId);
                contectDMap.put(conDistribution.ContentDocumentId, conDistribution);
            }
            list<FileInfoWrapper> fileInfo = new list<FileInfoWrapper>();
            caseInfo.contentDocumentIds = contectVersionID;
            for (ContentVersion cv : [SELECT Id, ContentDocumentId, FileExtension, FileType, PathOnClient, VersionData FROM ContentVersion WHERE ContentDocumentId IN:contectVersionID ORDER BY CreatedDate ASC]) {
                FileInfoWrapper fileData = new FileInfoWrapper();
                fileData.base64Data = String.valueOf(cv.VersionData);
                fileData.contentVersionId = cv.Id;
                fileData.fileName = cv.PathOnClient;
                if (contectDMap.containsKey(cv.ContentDocumentId)) {
                    fileData.publicUrl = contectDMap.get(cv.ContentDocumentId).ContentDownloadUrl;
                    fileData.contentDistributionId = contectDMap.get(cv.ContentDocumentId).Id;
                }
                fileInfo.add(fileData);
            }
            caseInfo.files = fileInfo;
            System.debug('caseInfo ##' + JSON.serialize(caseInfo));
            return JSON.serialize(caseInfo);
        }
        catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }
I have written the below test class
@IsTest
    public static void getCasesDataTest(){


        Case caseInfo = new Case();

        caseInfo.description = 'test';
        caseInfo.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('rand_App').getRecordTypeId();
        insert  caseInfo;

        ContentVersion contentVersion = new ContentVersion(
        Title = 'Penguins',
        PathOnClient = 'Penguins.jpg',
        VersionData = Blob.valueOf('Test Content'),
        IsMajorVersion = true
        );
        insert contentVersion;    
        List<ContentDocument> documents = [
        SELECT Id, Title, LatestPublishedVersionId 
        FROM ContentDocument
        ];

        //create ContentDocumentLink  record 
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = caseInfo.id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.shareType = 'V';
        insert cdl;
           
        randAppController.getCasesData(caseInfo.id);

    }
But not covering the for loop that is
 
for (ContentVersion cv : [SELECT Id, ContentDocumentId, FileExtension, FileType, PathOnClient, VersionData FROM ContentVersion WHERE ContentDocumentId IN:contectVersionID ORDER BY CreatedDate ASC]) {
                FileInfoWrapper fileData = new FileInfoWrapper();
                fileData.base64Data = String.valueOf(cv.VersionData);
                fileData.contentVersionId = cv.Id;
                fileData.fileName = cv.PathOnClient;
                if (contectDMap.containsKey(cv.ContentDocumentId)) {
                    fileData.publicUrl = contectDMap.get(cv.ContentDocumentId).ContentDownloadUrl;
                    fileData.contentDistributionId = contectDMap.get(cv.ContentDocumentId).Id;
                }
                fileInfo.add(fileData);
            }

 
Best Answer chosen by Raffus
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

I hope in the test class contentdistribution record is not inserted. Can you check with below test class.
 
@IsTest
    public static void getCasesDataTest(){


        Case caseInfo = new Case();

        caseInfo.description = 'test';
        caseInfo.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('rand_App').getRecordTypeId();
        insert  caseInfo;

        ContentVersion contentVersion = new ContentVersion(
        Title = 'Penguins',
        PathOnClient = 'Penguins.jpg',
        VersionData = Blob.valueOf('Test Content'),
        IsMajorVersion = true
        );
        insert contentVersion;    
        List<ContentDocument> documents = [
        SELECT Id, Title, LatestPublishedVersionId 
        FROM ContentDocument
        ];
        //create ContentDocumentLink  record 
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = caseInfo.id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.shareType = 'V';
        insert cdl;
        ContentDistribution cd = new ContentDistribution();
        cd.Name = 'Test';
        cd.ContentVersionId = contentVersion.id;
        cd.PreferencesAllowViewInBrowser= true;
        cd.PreferencesLinkLatestVersion=true;
        cd.PreferencesNotifyOnVisit=false;
        cd.PreferencesPasswordRequired=false;
        cd.PreferencesAllowOriginalDownload= true;
        insert cd;
           
        randAppController.getCasesData(caseInfo.id);

    }

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

I hope in the test class contentdistribution record is not inserted. Can you check with below test class.
 
@IsTest
    public static void getCasesDataTest(){


        Case caseInfo = new Case();

        caseInfo.description = 'test';
        caseInfo.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('rand_App').getRecordTypeId();
        insert  caseInfo;

        ContentVersion contentVersion = new ContentVersion(
        Title = 'Penguins',
        PathOnClient = 'Penguins.jpg',
        VersionData = Blob.valueOf('Test Content'),
        IsMajorVersion = true
        );
        insert contentVersion;    
        List<ContentDocument> documents = [
        SELECT Id, Title, LatestPublishedVersionId 
        FROM ContentDocument
        ];
        //create ContentDocumentLink  record 
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = caseInfo.id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.shareType = 'V';
        insert cdl;
        ContentDistribution cd = new ContentDistribution();
        cd.Name = 'Test';
        cd.ContentVersionId = contentVersion.id;
        cd.PreferencesAllowViewInBrowser= true;
        cd.PreferencesLinkLatestVersion=true;
        cd.PreferencesNotifyOnVisit=false;
        cd.PreferencesPasswordRequired=false;
        cd.PreferencesAllowOriginalDownload= true;
        insert cd;
           
        randAppController.getCasesData(caseInfo.id);

    }

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
RaffusRaffus
Hi @Sai Praveen 
Can you help me with this, please - https://developer.salesforce.com/forums/ForumsMain?id=9062I000000UjUCQA0
 
Vivek Garg 10Vivek Garg 10
I hope you get the answer. Microsoft Power BI Training (https://www.igmguru.com/data-science-bi/power-bi-certification-training/)