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
Mukesh Kumar 470Mukesh Kumar 470 

please help me how to write test case positive and negative scenario for below code

@istest
public class FileUpload_ctrlTest {
   @isTest
    public static void Unittest()  { 
        Account Acc = new Account ();
        Acc.Name = 'Test Account';
        //Fill All Required fields
        insert Acc;
       system.debug('account name'+Acc.name);
       Id parentId = Acc.Id; 
       String fileName = 'TestFile';
       String base64Data = EncodingUtil.urlDecode('Test base64Data For testing', 'UTF-8');
       String contentType = 'jpg'; 
       String fileDescription = 'Testing File';
       FileUpload_ctrl.saveFile(parentId,fileName,base64Data,contentType,fileDescription );
   
  }
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mukesh,

I hope you only attached the code for test class. Your question dont have main class logic. Can you add the main class so experts cann answer it.

Thanks,
 
Mukesh Kumar 470Mukesh Kumar 470
Hi Sai Praveen   
main class is below

public without sharing class FileUpload_ctrl {

    @AuraEnabled 
    public static Id saveFile(Id parentId, String fileName, String base64Data, String contentType, String fileDescription )  { 
        
        ContentVersion contentToInsert = new ContentVersion();  
        contentToInsert.Title = fileDescription + ' - ' + fileName; 
        contentToInsert.VersionData = EncodingUtil.base64Decode(EncodingUtil.urlDecode(base64Data, 'UTF-8'));
        contentToInsert.PathOnClient = '/' + fileName ;  
        contentToInsert.Description = fileDescription;
        contentToInsert.IsMajorVersion = false;
        insert contentToInsert; 
        
        contentToInsert = [select id, ContentDocumentId from ContentVersion WHERE Id =: contentToInsert.Id];
        ContentDocumentLink cl = new ContentDocumentLink();
        cl.ContentDocumentId = contentToInsert.ContentDocumentId;
        cl.LinkedEntityId = parentId; 
        cl.ShareType = 'V';
        cl.Visibility = 'AllUsers'; 
        insert cl;
        
        return contentToInsert.id;
    }
}