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
Major1507Major1507 

Test class for custom file upload

Hi, I have the following apex class for file upload. I'm struggling to get the code coverage more than 75%. any help is appreciated.

Apex class - 


public with sharing class customFileUploadctrl {
    @AuraEnabled(cacheable=true)
    public static List<file> getRelatedFiles(String recordId, String sectionType, String screen){
        List<File> files = new List<File>();
        system.debug('screen value is' + screen);
        if(screen == null){
            screen = 'default';
        }

        //retrieve contentDocumentLIst
        List<ContentDocumentLink> contentDocumentList = getContentDocumentList(recordId);
        system.debug('contentDocumentList' + contentDocumentList);

        //setdocumentId
        Set<Id> contentDocumentIdSet = getContentDocumentIdSet(contentDocumentList);

        //retrieve the contentversion for the specific sectionType
        Set<Id> contentDocumentForSectionIdSet = getContentDocumentForSectionIdSet(contentDocumentIdSet,sectionType, screen);
        system.debug('contentDocumentForSectionIdSet' + contentDocumentForSectionIdSet);

        for(ContentDocumentLink conLink : contentDocumentList){
            if(contentDocumentForSectionIdSet.contains(conLink.ContentDocumentId)){
                File file = new File();
                file.Title = conLink.ContentDocument.Title;
                file.Id = conLink.ContentDocument.Id;
                file.CreatedDate = conLink.ContentDocument.CreatedDate;
                file.Type = conLink.ContentDocument.FileType;
                file.Description = conLink.ContentDocument.Description;
                files.add(file);

            }
        }            
        
        return files;
    }

    @AuraEnabled
    public static boolean updateType(List<string> documentArray, string sectionType, string screen){
        system.debug('in the updatetype@@' + sectionType);
        system.debug('screen' + screen);
        if(screen == null){
            screen = 'default';
        }
        List<contentversion> contentVersionList = new List<contentversion>();
        Set<Id> contentDocumentSetId = new Set<Id>();

        /*for(string documentDetails : documentArray){
            contentversion contentVersionItem = new contentversion();
            contentVersionItem.contentDocumentId = Id.valueOf(documentDetails);
            contentVersionItem.Type_fileupload__c = sectionType;
            contentVersionList.add(contentVersionItem);            
        }*/

        for(string documentDetails : documentArray){
            contentDocumentSetId.add(Id.valueOf(documentDetails));
        }
       

        for(contentversion contentversionItem : [SELECT Id, Type__c
                                                    FROM contentversion
                                                    WHERE contentDocumentId=:contentDocumentSetId]){
            contentversion contentVersionRec = new contentversion();
            contentVersionRec.Id = contentversionItem.Id;
            contentVersionRec.Type__c = sectionType;
            contentVersionRec.screen__c = screen;
            contentVersionList.add(contentVersionRec);                                             
        }

        system.debug('contentVersionLis' + contentVersionList);
        update contentVersionList;
        return true;

    }

    private static List<ContentDocumentLink> getContentDocumentList(String recordId){
        List<ContentDocumentLink> contentDocumentList = new List<ContentDocumentLink>();

        return [SELECT ContentDocument.Id, 
                        ContentDocumentId,
                                ContentDocument.Title, 
                                ContentDocument.CreatedDate, 
                                ContentDocument.FileType,
                                ContentDocument.Description
                                    FROM ContentDocumentLink                                     
                                   WHERE LinkedEntityId =: recordId] ;
    }

    private static set<Id> getContentDocumentIdSet(List<ContentDocumentLink> listContentDocumentLinkId){
        set<Id> contentDocumentSetId = new set<Id>();
        for(ContentDocumentLink contentDocItem: listContentDocumentLinkId){
            contentDocumentSetId.add(contentDocItem.ContentDocumentID);
        }
        return contentDocumentSetId;
    }


    private static Set<Id> getContentDocumentForSectionIdSet(Set<Id> contentDocumentSet, String sectionType, String screen){
        Set<Id> contentDocumentForSectionIdSet = new Set<Id>();

        for(ContentVersion conLink : [SELECT ContentDocumentId 
                                                    FROM ContentVersion                                            
                                                    WHERE ContentDocumentId =: contentDocumentSet 
                                                    AND Type__c =: sectionType
                                                    AND screen__C =: screen] ){

            contentDocumentForSectionIdSet.add(conLink.ContentDocumentId); }

        return contentDocumentForSectionIdSet;    
    }



    public class File{
        @AuraEnabled public String Title;
        @AuraEnabled public String Type;
        @AuraEnabled public Id Id;
        @AuraEnabled public Datetime CreatedDate;
        @AuraEnabled public String Description;
    }
}
Test class - 
@isTest
public class customFileUploadctrlTest {

    public static TestMethod void testScenario1(){
       List<String> newStr = new List<String>();
       
       string before = 'Testing base 64 encode';            
         Blob beforeblob = Blob.valueOf(before); 
        
        List<contentversion> conver = new List<contentversion>();
        contentversion rec = new contentversion();
        rec.Title = 'Test';
        rec.screen__c = 'Screen';
        rec.PathOnClient = 'PathTest';
        rec.VersionData = beforeblob;
        rec.Description = 'test description';
        rec.Type__c = 'Invoices';
        conver.add(rec);
        
        insert conver;
      
        List<ContentDocumentLink> cdlink = new List<ContentDocumentLink>();
        set<Id> contentDocumentSetId = new set<Id>();
        
        Test.startTest();
        	customFileUploadctrl.getRelatedFiles('a073G000002LM7hQAG','Test','Test1');
        	customFileUploadctrl.updateType(newStr, 'test','test1');
        Test.stopTest();
    }
}


 
SubratSubrat (Salesforce Developers) 
Hello ,

To increase code coverage for your customFileUploadctrl Apex class, you can write test methods that cover different scenarios of the class functionality. Here are some suggestions for the same:

Create a test method to cover the scenario when getContentDocumentList method returns content documents linked to a specific record ID. You can create a test record with some files and associate them with a specific record ID. Then you can call the getRelatedFiles method with the same record ID as input and assert that the returned list of files contains the expected files.

Create a test method to cover the scenario when getContentDocumentForSectionIdSet method returns content documents for a specific section type and screen. You can create a test record with some files and associate them with a specific record ID. Then you can create some content versions with specific section types and associate them with the same content documents. After that, you can call the getRelatedFiles method with the record ID and the section type as input and assert that the returned list of files contains only the files that have the same section type.

Hope it helps !
Thank you.