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 write a test method content document

@AuraEnabled
    public static FileInfoWrapper createPublicUrl(String fileData) {
        try {
            FileInfoWrapper file = (FileInfoWrapper)JSON.deserialize(fileData, FileInfoWrapper.class);
            if (String.isNotBlank(file.base64Data)) {
                ContentVersion cv = new ContentVersion();
                cv.ContentLocation = 'S';
                cv.PathOnClient = file.fileName;
                cv.VersionData = EncodingUtil.base64Decode(file.base64Data);
                cv.Title = file.fileName;
                insert cv;
                ContentDistribution cd = new ContentDistribution();
                cd.Name = file.fileName;
                cd.ContentVersionId = cv.Id;
                cd.PreferencesAllowViewInBrowser = true;
                cd.PreferencesLinkLatestVersion = true;
                cd.PreferencesNotifyOnVisit = false;
                cd.PreferencesPasswordRequired = false;
                cd.PreferencesAllowOriginalDownload = true;
                insert cd;
                cd = [SELECT Id, ContentVersionId, ContentDownloadUrl FROM ContentDistribution WHERE Id = :cd.Id];
                file.publicUrl = cd.ContentDownloadUrl;
                file.contentDistributionId = cd.Id;
                file.contentVersionId = cd.ContentVersionId;
                return file;
            }
            else {
                return file;
            }
        }
Best Answer chosen by Raffus
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you share the line where you are getting the error. Can you try this and check.
 
Test.startTest();
       try
      {
        
ClassName.createPublicUrl('sample String');
      }
      catch(exception e)
         {
}     
        Test.stopTest();
    }


Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Did you try just calling the method with string data.
 
ClassName.createPublicUrl('sample String')

Let me know if you face any issues.

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

Thanks,

 
RaffusRaffus
It is a giving error- System.AuraHandledException: Script-thrown exception
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you share the line where you are getting the error. Can you try this and check.
 
Test.startTest();
       try
      {
        
ClassName.createPublicUrl('sample String');
      }
      catch(exception e)
         {
}     
        Test.stopTest();
    }


Thanks,
 
This was selected as the best answer
RaffusRaffus
It Worked, Thanks