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
sneha gowdersneha gowder 

Test code coverage problem

please help me to do the code coverage for below 2 methods.
1.compareTo( Object obj)
2.getAttachmentSize()
 

public class AttachmentListController {
       public List<AttachmentDetails> allAttachments {get;set;}
        public Map<string , List<AttachmentDetails> > allAttachmentsMap {get; set;}
        
         public void allAttachmentsMapEntry(){
        
        allAttachmentsMap =  new Map<string , List<AttachmentDetails> >();
        List<AttachmentDetails> tempAttachmentList;
       // allAttachments.sort();
        for(AttachmentDetails detail: allAttachments){
            
            if(allAttachmentsMap.containsKey(detail.docName)){
                tempAttachmentList = allAttachmentsMap.get(detail.docName);
               
            }
            else{
                tempAttachmentList = new List<AttachmentDetails> ();
            }
            tempAttachmentList.add(detail);
            
            allAttachmentsMap.put(detail.docName ,tempAttachmentList );
            
            
        }
        
        system.debug('allAttachmentsMapEntry execution ended: '+allAttachmentsMap);
         for(AttachmentDetails detail: allAttachments){
              detail.countOfGrouping = allAttachmentsMap.get(detail.docName).size() ;
              if(detail.countOfGrouping > 1){
              detail.isRendered =false;
               tempAttachmentList = allAttachmentsMap.get(detail.docName);
               system.debug('Before Sort'+tempAttachmentList);
               tempAttachmentList.sort();
               system.debug('After Sort'+tempAttachmentList);
              allAttachmentsMap.put(detail.docName , tempAttachmentList);
                hasSec = true;
              }
             
         }
         results = json.serialize(allAttachmentsMap);
         
    }
  public class AttachmentDetails implements Comparable{
      
       public Id id{get;private set;}
        public String parentType {get; private set;}
        public String parent {get;private set;}
        public String parentId {get; private set;}
        public String styleClass {get;private set;}
        public String docName{get
        {
        return docName.abbreviate(integer.valueOf(Label.ESC_FilenameSize));

        }private set;}
         public String docTitleFull{get;private set;}
        public String type{get;private set;}
        public String Downloadlink{get;private set;}
        public String attachmentSizeF{get;private set;}
         public String uploadedDateF{get;private set;}
        public Integer countOfGrouping{get; set;} 
        public Boolean isRendered {get; set;}
        public String getUploadedDate(){
            return formatDate(createdDate);
        }
       
        public Long sortByDate{get; private set;}
        
        private Integer length{get;private set;}
        public DateTime createdDate{get; set;}

        public AttachmentDetails(Attachment attach){
            initialize(attach);
        }
        public AttachmentDetails(Attachment attach, Boolean isEmailinbound){
            initialize(attach);
            this.styleClass  = isEmailinbound? 'inb' : 'otb' ;
             
        }
      
      public void initialize(Attachment attach){
            this.Id = attach.Id;
            this.parent = attach.parent.name;
            this.parentId = attach.parentId;
            this.docName = attach.name;
            this.docTitleFull = attach.name;
            this.createdDate = attach.createdDate;
            this.type = String.isBlank(attach.contentType)?'':((attach.contentType.contains('.'))?attach.contentType.substringAfterLast('.'):((attach.contentType.contains('/'))?attach.contentType.substringAfterLast('/'):attach.contentType));
            this.length = attach.bodyLength;
            this.parentType = attach.parentId.getSobjectType().getDescribe().getLabel();
            this.Downloadlink= '/servlet/servlet.FileDownload?file='+attach.Id;
            this.countOfGrouping =0;
             this.isRendered = true;
             this.attachmentSizeF = getAttachmentSize();
            this.uploadedDateF = getUploadedDate();
            this.sortByDate = Long.valueOf(formatDate());
        }
      
  
  public Integer compareTo( Object obj)
        {
            
            AttachmentDetails sfw = (AttachmentDetails ) obj;
            if(this.sortByDate < sfw.sortByDate)
                return 1;
            else if(this.sortByDate > sfw.sortByDate)
                return -1;
            return 0;
        }
        
        public String getAttachmentSize()
        {
            Decimal size;
            if(!Test.IsRunningTest()){
            size = ((Decimal)this.length).setScale(2);
            
            if(this.length <1024)
                return this.length+' bytes';
            size = (size/1024).setScale(2);
            if(size < 1024)
                return size+' Kb';
            size = (size/1024).setScale(2);
            if(size < 1024)
                return size+' Mb';
            else
                return (size/1024).setScale(2) +' Gb';
            }
            else{
                return '0 Mb';
                }
        }
  }