You need to sign in to do that
Don't have an account?
Priya Babu 15
Few Lines in Apex Class is not getting covered in Test Class. Need to increase code coverage for few lines,
Apex Class that is not getting covered in Test Class:
Test Class for the above class:
public with sharing class AskHR_PurgeCaseFiles implements Schedulable{ public Set<Id> CaseId = new Set<Id>(); public Map<Id, Case> caseDetails = new Map<Id, Case>(); public static string groupName = 'AskHR_Purging_Case_Attachment_Group'; public List<ContentDocumentLink> attachmentList = new List<ContentDocumentLink>(); public Map<Id, ContentDocumentLink> contentMap = new Map<Id, ContentDocumentLink>(); public List<Id> CDID = new List<Id>(); public void execute(SchedulableContext sc) { performOperation(); } public void performOperation() { if(!Test.isRunningTest()){ attachmentList = [SELECT ContentDocumentId, ContentDocument.Title, ContentDocument.FileType, LinkedEntityId, ContentDocument.isDeleted FROM ContentDocumentLink where isDeleted=false and LinkedEntityId in (Select Id from Case where Status = 'Closed'and ClosedDate < Last_N_Days:14 and Type = 'HR Reporting') limit 1000]; /*attachmentList = [SELECT ContentDocumentId, ContentDocument.Title, ContentDocument.FileType, LinkedEntityId FROM ContentDocumentLink where LinkedEntityId in (Select Id from Case where Status = 'Closed' and Type = 'HR Reporting') limit 1000]; attachmentList = [SELECT ContentDocumentId, ContentDocument.Title, ContentDocument.FileType, LinkedEntityId FROM ContentDocumentLink where LinkedEntityId in (Select Id from Case where status != 'closed')]; System.debug('AttachmentList: '+attachmentList); */ } else{ attachmentList = [SELECT ContentDocumentId, ContentDocument.Title, ContentDocument.FileType, LinkedEntityId, ContentDocument.isDeleted FROM ContentDocumentLink where isDeleted=false and LinkedEntityId in (Select Id from Case where Status = 'Closed'and Type = 'HR Reporting' ) limit 1000]; } system.debug('attachmentList count ='+attachmentList.size()); System.debug('Debug CDID Size: '+ CDID.size()); System.debug('Debug CaseId Size'+ CaseId.size()); if (!attachmentList.isEmpty()) { //Get the Case detail System.debug('Debug Initial CDID Size: '+ CDID.size()); System.debug('Debug Initial CDID Data'+ CDID); CDID.clear(); System.debug('Debug CDID set to Zero:'+CDID.size()); System.debug('Debug Initial CaseId Size'+ CaseId.size()); System.debug('Debug Initial CaseId Data'+ CaseId); CaseId.clear(); System.debug('Debug CaseId set to Zero:'+CaseId.size()); for(ContentDocumentLink cdl : attachmentList){ System.debug('ContentDocumentId: '+cdl.ContentDocumentId); CaseId.add(cdl.LinkedEntityId); contentMap.put(cdl.ContentDocumentId, cdl);//cdl.id CDID.add(cdl.ContentDocumentId);//cdl.id } caseDetails = getCaseDetails(CaseId); System.debug('Debug CaseId Size after adding caseids:'+ CaseId.size()); System.debug('Debug CaseId data after adding caseids:'+ CaseId); System.debug('Debug CDID Before Delete: '+ CDID.size()); System.debug('Debug CDID Data Before Delete'+ CDID); //List<ContentDocument> CDIDList = select title,fileType, isDeleted from ContentDocument Where isDeleted=false and id in :=cdid; /* List<ContentDocument> docsList = [select id, Title, FileType, isDeleted from ContentDocument where id in:CDID]; for(ContentDocument doc: docsList){ system.debug('Document: '+doc); } */ /*List<contentDocument> deletedDocuments = new List<contentDocument>([SELECT Id from contentDocument where isDeleted = true]); Database.emptyRecycleBin(deletedDocuments);*/ Database.DeleteResult[] results = Database.delete(CDID, false); Database.emptyRecycleBin(CDID); System.debug('Debug results After Delete: '+ results.size()); List<String> operationResults = new List<String> (); operationResults.add(createHTMLTable()); Integer loopIndex = 0; Integer countFailedOperation = 0; for (loopIndex = 0; loopIndex < results.size(); loopIndex++) { Database.DeleteResult dr = results.get(loopIndex); Id CID = CDID.get(loopIndex); String operationResult; operationResult = '<tr><td>' + caseDetails.get(contentMap.get(CID).LinkedEntityId).CaseNumber + '</td>' + '<td>' + caseDetails.get(contentMap.get(CID).LinkedEntityId).Owner.Name + '</td>' + '<td>' + caseDetails.get(contentMap.get(CID).LinkedEntityId).Type + '</td>' + '<td>' + caseDetails.get(contentMap.get(CID).LinkedEntityId).ClosedDate + '</td>' + '<td>' + contentMap.get(CID).ContentDocument.Title + '</td>' + '<td>' + contentMap.get(CID).ContentDocument.FileType + '</td>'; if (!dr.isSuccess()) { operationResult += '<td>Failed-'; for (Database.Error err : dr.getErrors()) { operationResult += 'Details: ' + err.getStatusCode() + '<br/>' + err.getMessage(); } operationResult += '</td>'; countFailedOperation++; } else { operationResult += '<td>Success</td>'; } operationResult += '</tr>'; operationResults.add(operationResult); } //End for======== operationResults.add('</table>'); system.debug('operationResult'+operationResults); if (!operationResults.isEmpty()) { Integer successOperation = results.size() - countFailedOperation; operationResults.add(0, '<b>ASKHR- Purging Case Attachment Job Result: </b><br/>' + 'Success: ' + successOperation + '<br/>' + 'Fail: ' + countFailedOperation + '<br/><br/>' + 'Please see below Files deleted from HR reporting cases closed 14 days ago,<br/><br/>'); system.debug('Sending Email---'); sendHTMLEmailNotification('ASKHR- Case File Purging Job ', string.join(operationResults, '\n')); system.debug('Email sent---'); } } } public string createHTMLTable() { return '<table border="1"><tr><th>CaseNumber</th><th>CaseOwner</th><th>CaseType</th><th>ClosedDate</th><th>FileName</th><th>FileType</th><th>Result</th></tr>'; } public Map<Id, Case> getCaseDetails(Set<Id> ids){ Map<Id, Case> caseMap = new Map<Id, Case>([Select Id, Type, CaseNumber,Owner.Name, ClosedDate from Case where Id in :ids and isDeleted=false]); return caseMap; } public static void sendHTMLEmailNotification(String action, String htmlMessage) { try { if(String.isEmpty(action) || String.isEmpty(htmlMessage)){ throw new NotificationUtilException('Action or Email message is empty'); } emailOperation(getAddresses(), action, htmlMessage, true); } catch(Exception e) { System.debug('Error occur when attempting to notify html error:' + e.getMessage()); } } public static List<String> getAddresses() { List<Group> groups = [select(select UserOrGroupId from GroupMembers) from Group where DeveloperName = :groupName]; if(groups.isEmpty()){ throw new NotificationUtilException('Could not find the public group ' + groupName + ' when attempting to notify error.'); }else{ List<String> userIds = new List<String> (); List<String> addresses = new List<String> (); Group g = groups.get(groups.size()-1); for (GroupMember gm : g.GroupMembers) { userIds.add(gm.UserOrGroupId); } User[] users = [select Email from User where Id in :userIds]; for (User u : users) { addresses.add(u.Email); } return addresses; } } public static void emailOperation(List<String> addresses, String action, String message, Boolean isHTML) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(addresses); mail.setSubject(getSandboxName() + action); if (isHTML) { mail.setHTMLBody(message); } else { mail.setPlainTextBody(message); } //avoid sending emails in Unit Testing to governor limit error if (!Test.isRunningTest()) { Messaging.sendEmail(new Messaging.Email[] { mail }, false); } } public static String getSandboxName() { try { Organization org = [select id, IsSandbox from Organization]; if (org.IsSandbox) { if (UserInfo.getUserName().substringAfterLast('.com') != null) return '[' + UserInfo.getUserName().substringAfterLast('.').toUpperCase() + '] '; } } catch(Exception ex) { } return ''; } public class NotificationUtilException extends Exception{} }
Test Class for the above class:
@isTest(SeeAllData = true) public class AskHR_PurgeCaseFilesTest { public static Map<String, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Case.getRecordTypeInfosByName(); static testMethod void testmethod1() { Case testCase = new Case(External_Contact_Name__c = 'TEST EXTERNAL NAME', Secondary_Country_of_Reference__c = 'USA', Type = 'HR Reporting', Status = 'Closed', RecordTypeId=ASKHR_TestUtility.GetRecordTypeID(ASKHR_Common.CASE_RECORD_TYPE_NAME_GENERAL_HR)); insert testCase; ContentVersion contentVersion = new ContentVersion( Title = 'Test', PathOnClient = 'Test.jpg', VersionData = Blob.valueOf('Test Content'), IsMajorVersion = true ); insert contentVersion; List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument]; List<Id> CDID = new List<Id>(); //create ContentDocumentLink record ContentDocumentLink cdl = New ContentDocumentLink(); cdl.LinkedEntityId = testCase.id; cdl.ContentDocumentId = documents[0].Id; cdl.shareType = 'V'; insert cdl; CDID.add(cdl.id); CDID.clear(); Database.DeleteResult[] results = Database.delete(CDID, false); Delete cdl; String action = 'Test Action'; String htmlMessage = 'Html Messages'; List<Case> cse = [select id from case]; set<id> ids = new set<id>(); for(case c : cse){ ids.add(c.id); } Test.startTest(); AskHR_PurgeCaseFiles purgeJob = new AskHR_PurgeCaseFiles(); AskHR_PurgeCaseFiles.getAddresses(); purgeJob.createHTMLTable(); AskHR_PurgeCaseFiles.getSandboxName(); purgeJob.performOperation(); purgeJob.getCaseDetails(ids); AskHR_PurgeCaseFiles.sendHTMLEmailNotification(action, htmlMessage); purgeJob.execute(null); Test.stopTest(); } }
Can you try the test class as below.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,