0

I have to test a method that return a boolean false when the attachment upload is bigger than 25 megas, I tried to test this one using the code below but there is a limit dml exception string too long, besides I tried to cut them in pieces more smaller but the problem is with cpu apex clock, any idea to resolve this? Any help would be appreciated

    public static Boolean  verifySizeAttachment(String caseId){
    Boolean validation = true;
    AggregateResult[] lstAttachment = [SELECT  SUM(ContentDocument.ContentSize)
                                        FROM ContentDocumentLink
                                        WHERE LinkedEntityId =: caseId and 
                                        ContentDocument.Description = 'Send Email'];
        
    if(lstAttachment.size() > 0){           
        Double size = (Double) lstAttachment[0].get('expr0');          
        if(size > 25000000){                
            validation = false;                
            list<ContentDocumentLink> lstAttachmentDelete = [SELECT Id
                                                            FROM ContentDocumentLink
                                                            WHERE LinkedEntityId =: caseId and 
                                                            ContentDocument.Description = 'Send Email' limit 1];              
            delete lstAttachmentDelete;    
            }         
        }
        
        return validation; 
    }
    }

//TEST CLASSES

    //STEP 1
    ContentVersion version = new ContentVersion();
    version.Title= 'File Name';
    version.PathOnClient = 'sample.pdf';
    version.VersionData=blob.valueOf('Test Content data');
    version.Description= 'Send Email';
    insert version;
    
    version = [SELECT ContentDocumentId, ContentDocument.ContentSize FROM ContentVersion WHERE Id = : version.Id];
    
    //STEP 2
    ContentDocumentLink cdl = new ContentDocumentLink();
    cdl.ContentDocumentId = version.ContentDocumentId;
    cdl.LinkedEntityId = newCase.Id;
    insert cdl;
    
    
    Test.startTest();
    // VERIFY LESS THAN 25mb - TRUE
    Boolean validation =  AttachmentController.verifySizeAttachment(newCase.Id);
    System.assertEquals(true, validation);
    
    // VERIFY MORE THEN 25 MB - FALSE
   
    version.VersionData = Blob.valueOf(String.valueOf('a').repeat(26000000));
    update version;
    validation = AttachmentController.verifySizeAttachment(newCase.Id);   
    System.assertEquals(false, validation);

    Test.stopTest();
}
2
  • 3
    Even if you managed to not get a CPU timeout, you'd be limited by the 6/12MB heap limit, which is a hard limit for the length of any one string value. Your only real choice here would be dependency injection by faking a record. Without seeing more of your code, we can't really help you. If you'd please edit in the relevant parts of the AttachmentController, that'd be helpful.
    – sfdcfox
    Commented Jan 31, 2023 at 15:20
  • Thanks for your help, i have attached the sample of code above..
    – GRC7
    Commented Jan 31, 2023 at 16:28

1 Answer 1

0

Change your code under test to:

public AttachmentController {
  @TestVisible Integer MAX_FILESIZE = 25000000;
  ...
  public static Boolean  verifySizeAttachment(String caseId){
  
    Boolean validation = true;
    AggregateResult[] lstAttachment = [SELECT  SUM(ContentDocument.ContentSize)
                                    FROM ContentDocumentLink
                                    WHERE LinkedEntityId =: caseId and 
                                    ContentDocument.Description = 'Send Email'];
    
    if(lstAttachment.size() > 0){           
    Double size = (Double) lstAttachment[0].get('expr0');          
    if(size > MAX_FILESIZE){                
        validation = false;                
    ... 
  }
}

Then dependency inject a smaller value to MAX_FILESIZE in the testmethod

//STEP 1 - create attachment bigger than max
AttachmentController.MAX_FILESIZE = 10;
ContentVersion version = new ContentVersion();
version.Title= 'File Name';
version.PathOnClient = 'sample.pdf';
version.VersionData=blob.valueOf('a'.repeat(MAX_FILESIZE); 
version.Description= 'Send Email';
insert version;

...
Boolean validation =  AttachmentController.verifySizeAttachment(newCase.Id);
System.assertEquals(true, validation,'attachment should be ok size');

//STEP 2 - create attachment bigger than max
Cversion = new ContentVersion();
version.Title= 'File Name';
version.PathOnClient = 'sample.pdf';
version.VersionData=blob.valueOf('a'.repeat(MAX_FILESIZE+1); 
version.Description= 'Send Email';
insert version;
...
Boolean validation =  AttachmentController.verifySizeAttachment(newCase.Id);
System.assertEquals(false, validation,'attachment too big');
5
  • Thanks but unfortunatelly, I've received the same exeption: System.LimitException: String is too long.
    – GRC7
    Commented Jan 31, 2023 at 19:40
  • I'd have to see how you modified your code under test as well as the testmethod; the above answer only creates strings of size 10 and 11
    – cropredy
    Commented Jan 31, 2023 at 23:51
  • Thanks for your help, i have attached the code
    – GRC7
    Commented Feb 1, 2023 at 1:02
  • Gabriel - I'm not seeing any update in the OP
    – cropredy
    Commented Feb 1, 2023 at 1:31
  • Oh, I'm sorry, I don't know how this system works and I deleted it by mistake when I was writing the message , thank a lot for your he
    – GRC7
    Commented Feb 1, 2023 at 1:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .