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();
}