+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

attachment test class help needed

Can anyone assist me with getting coverage for this test class
global class AttachmentTriggerHandler r{
    
    Map<Id, List<ContentDocumentLink>> linkMap = new Map<Id, List<ContentDocumentLink>>();
    Map<String, Directory__c> directoryMap = new Map<String, Directory__c>();
    List<ContentDocumentLink> cdl_List = new List<ContentDocumentLink>();
    public ContentDocumentLink cdl = New ContentDocumentLink();
    List<Attachment__c> categoryvals = new List<Attachment__c>();
    public String parentId;
    public String catvalue;
    public String folderparentId;
    public string parentObjectType;
    public Map<Id,Programme__c> ProgrammeMap = new Map<Id,Programme__c>();
    public Map<Id,Permit__c>  PermitMap  = new Map<Id,Permit__c>();

    global override void bulkBeforeExtended() {
        if( Trigger.isUpdate ){
            List<Id> docIds = new List<Id>();
            List<String> parentIds = new List<String>();
            List<Id> progRecordIds = new List<Id>();
            List<Id> perRecordIds = new List<Id>();
                   
            for( Attachment__c attrecord : ( List<Attachment__c> ) Trigger.new ){
                Id recId = Id.valueOf(attrecord.Parent_ID__c ); 
                parentObjectType = String.valueOf( recId.getSObjectType());

                if(parentObjectType == 'Programme__c' ){
                    progRecordIds.add(attrecord.Parent_ID__c);
                }
                if(parentObjectType == 'Permit__c' ){
                    perRecordIds.add(attrecord.Parent_ID__c);
                }
            }
            if (!progRecordIds.isEmpty()) {
                programmeMap.putall([Select Project__c from Programme__c WHERE Id =: ProgRecordIds]);  
            }
            if (!perRecordIds.isEmpty()) {
                permitMap.putall([Select Project__c from Permit__c WHERE Id =: perRecordIds]); 
            }
            if (!jobRecordIds.isEmpty()) {
                jobMap.putall([Select Project__c from Job__c WHERE Id =: JobRecordIds]);
            }
          
            for( Attachment__c record : ( List<Attachment__c> ) Trigger.new ){
                Id docId = record.ContentDocumentId__c;
                    parentId = record.Parent_ID__c ;
                    Id recId = Id.valueOf(record.Parent_ID__c ); 
                    parentObjectType = String.valueOf( recId.getSObjectType() );
       
                    if( ProgrammeMap.containsKey(record.Parent_ID__c)){ 
                        for(Programme__c prog :ProgrammeMap.values()){
                            parentId = prog.Project__c;
                        }
                    }
                    if( PermitMap.containsKey(record.Parent_ID__c)){ 
                        parentId = record.Proj__c;
                        record.sobject__c ='Project';
                    }
                    }
                 
                    if( docId != null ){    
                        docIds.add( docId );
                    }
                  
                    if( parentId != null ){    
                        parentIds.add( parentId );
                    } 
                  
                    if( record.sobject__c != null ){    
                        categoryvals.add( record );
                    }       
            }

            list<Directory__c> dirs = [
                SELECT id,Directory_Template__c,Name,ParentId__c,Sobject__c,
                    (Select Id,Name, Directory__c, Folder_Template__c,Parent_File_Folder__c FROM File_Folders__r ORDER BY Name)
                FROM Directory__c 
                WHERE ParentId__c IN: parentIds
            ];
           
            if( !dirs.isEmpty() ){
                for( Directory__c dir : dirs ){
                    this.directoryMap.put( dir.ParentId__c, dir );
                }
            }

            List<ContentDocumentLink> linkList = [ SELECT Id, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN: docIds ];
            if( !linkList.isEmpty() ){
                for( ContentDocumentLink link : linkList ){
                    List<ContentDocumentLink> links = this.linkMap.containsKey( link.ContentDocumentId )
                        ? this.linkMap.get( link.ContentDocumentId ) : new List<ContentDocumentLink>();
                    links.add( link );
                    this.linkMap.put( link.ContentDocumentId, links );
                }
            }
        }
    }
    global override void bulkAfterExtended() {
    }

    global override void beforeInsertExtended(SObject so) {
        Attachment__c newAttachment = ( Attachment__c ) so;
        this.getObjectType(newAttachment);
    }
    global override void beforeUpdateExtended(SObject oldso, SObject newso) {
      	Attachment__c newAttachment = ( Attachment__c ) newSo;
        folderparentId = newAttachment.Parent_Id__c;
        Id recId = Id.valueOf(newAttachment.Parent_ID__c );
        parentObjectType = String.valueOf( recId.getSObjectType() );
        this.getParentObjectType(parentObjectType,newAttachment);      

        if( directoryMap.containsKey( folderparentId)){           
            Directory__c dir = this.directoryMap.get( folderparentId );
            List<File_Folder__c> folders = dir.File_Folders__r;
            system.debug('dir.File_Folders__r '+ dir.File_Folders__r);
            List<ContentDocumentLink> links = this.linkMap.get( newAttachment.ContentDocumentId__c );

            for (ContentDocumentLink cont : links ){
                for (File_Folder__c folder: folders ){
                    if(newattachment.Document_Type__c == folder.name ){
                        ContentDocumentLink cdl = new ContentDocumentLink();
                        cdl.LinkedEntityId = folder.Id;
                        cdl.ContentDocumentId = cont.ContentDocumentId;
                        cdl.ShareType = 'V';
                        cdl.Visibility = 'AllUsers';
                        newAttachment.File_Folder__c = folder.Id;
                        cdl_List.add(cdl);
                        break;
                    }
                }
            }
        }
    }
    global override void afterUpdateExtended(SObject oldso, SObject newso) {}
    global override void afterInsertExtended(SObject newso) {} 
    global override void andFinallyExtended() {
    
      if (!Trigger.isDelete) {
            if (Trigger.isUpdate) {
                try {
                    if (cdl_List.size() > 0) {
                        insert cdl_List;
                    }
                } catch (Exception e) {
                    System.debug('Error: ' + e.getMessage());
                }
            }
        }

        if (!Trigger.isDelete) {
            if (Trigger.isInsert) {
                try {
                    if (categoryvals.size() > 0) {
                        update categoryvals;
                    }
                } catch (Exception e) {
                    System.debug('Error: ' + e.getMessage());
                }
            }
        }

    }

    private void getParentObjectType(String tempParentObjectType,Attachment__c newAttachment){
   
        if( tempParentObjectType == 'Programme__c'  ){
            folderparentId = parentId;
            newAttachment.sobject__c='Programme';
        }
        else{
            folderparentId = parentId;
            newAttachment.sobject__c='Project';
        }
    }

    private void getObjectType(Attachment__c newAttachment){
        if( this.linkMap.containsKey( newAttachment.ContentDocumentId__c ) ){
            List<ContentDocumentLink> links = this.linkMap.get( newAttachment.ContentDocumentId__c );
            if( !links.isEmpty() ){
                for( ContentDocumentLink link : links ){
                    String objectType = String.valueOf( link.LinkedEntityId.getSObjectType() );
                    Directory__c dir = this.directoryMap.containsKey( link.LinkedEntityId )
                        ? this.directoryMap.get( link.LinkedEntityId ) : null;
                    if( dir != null ){
                        switch on ( objectType ){
                            when 'Programme__c' {
                                newAttachment.Programme__c = link.LinkedEntityId;
                            }
         
                            when 'Permit__c' {
                               newAttachment.Permit__c = link.LinkedEntityId;
                            }
                            
                        }
                    }
                }
            }
        }
    }
}

Test Class
@IsTest
private class AttachmentTriggerHandlerTest {
    @TestSetup
    static void setupTestData() {
        Directory_Template__c dirTemplate = new Directory_Template__c(
            Name = 'Directory Template Test',
            Sobject__c = 'Project',
            Active__c = true
        );
        insert dirTemplate;
    }

    @isTest
    private static void TestTriggerFunctions(){

        List<Directory_Template__c> dirTemplates = [
            SELECT Id
            FROM Directory_Template__c
        ];
        
        System.assertEquals( 1, dirTemplates.size(), '1 template found' );

        Programme__c prog = new Programme__c(
                Name = 'Test Program',
                Operator__c = 'MBNL',
                Directory_Template__c = dirTemplates[ 0 ].Id
        );
        insert prog;

        Attachment attach = new Attachment();       
        attach.Name='Test Attachment';
        Blob bodyBlob=Blob.valueOf('Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=prog.id;
        insert attach;
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:prog.id];
        System.assertEquals(1, attachments.size());

        Blob contentData = Blob.valueOf( 'Test File' );
        String documentName = 'testfile.txt';

        ContentVersion newDocument = new ContentVersion(
            Title = documentName,
            VersionData = contentData,
            PathOnClient = documentName
        );

        insert newDocument;

        List<ContentVersion> versionVerify = [
            SELECT Id, ContentDocumentId
            FROM ContentVersion
            WHERE Id =: newDocument.Id
        ];
        System.assertEquals( 1, versionVerify.size() );

        List<ContentDocument> documents = [
            SELECT Id
            FROM ContentDocument
            WHERE Id =: versionVerify[ 0 ].ContentDocumentId
        ];

        System.assertEquals( 1, documents.size() );

        ContentDocumentLink newLink = new ContentDocumentLink(
            LinkedEntityId = projects[ 0 ].Id,
            ContentDocumentId = documents[ 0 ].Id,
            ShareType = 'V',
            Visibility = 'AllUsers'
        );
        insert newLink;
    }
}