You need to sign in to do that
Don't have an account?
Phuc Nguyen 18
Test Class coverage for containsKey
Having an issue getting coverage for mapping
Test Class
I am having an issue with the mapping/containsKey
Never goes past:
Thanks
P
global class Clarke_AttachmentTriggerHandler { 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(); global override void bulkBeforeExtended() { if( Trigger.isUpdate ){ List<Id> docIds = new List<Id>(); List<String> parentIds = new List<String>(); List<String> categoryvals = new List<String>(); for( Attachment__c record : ( List<Attachment__c> ) Trigger.new ){ Id docId = record.ContentDocumentId__c; String parentId = record.Parent_ID__c ; Id recId = Id.valueOf(record.Parent_ID__c ); String parentObjectType = String.valueOf( recId.getSObjectType() ); if( parentObjectType == 'Activity__c' ){ parentId = record.Proj__c; } if( docId != null ){ docIds.add( docId ); } if( parentId != null ){ parentIds.add( parentId ); } } 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; 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 'Site__c' { newAttachment.Site__c = link.LinkedEntityId; } when 'Programme__c' { newAttachment.Programme__c = link.LinkedEntityId; } when 'Activity__c' { newAttachment.Activity__c= link.LinkedEntityId; } when 'Project__c' { newAttachment.Proj__c = link.LinkedEntityId; } } } } } } } global override void beforeUpdateExtended(SObject oldso, SObject newso) { Attachment__c newAttachment = ( Attachment__c ) newSo; String folderparentId = newAttachment.Parent_Id__c; Id recId = Id.valueOf(newAttachment.Parent_ID__c ); String parentObjectType = String.valueOf( recId.getSObjectType() ); if( parentObjectType == 'Activity__c' ){ folderparentId = newAttachment.Proj__c; } if( directoryMap.containsKey( folderparentId)){ Directory__c dir = this.directoryMap.get( folderparentId ); List<File_Folder__c> folders = 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()); } } } } }
Test Class
@isTest private class Clarke_AttachmentTriggerHandlerTest { @TestSetup static void CreateTestData() { Site__c site = new Site__c(Name = 'Test Site'); insert site; System.assert(site.Id != null); Project_Template__c newTemplate = new Project_Template__c( Name = 'test template' ); insert newTemplate; Project__c proj = new Project__c( Site__c = site.id ); insert proj; Activity__c activity = new Activity__c( Project__c = proj.Id ); insert activity; Programme__c programme = new Programme__c(Name = 'Test Program'); insert programme; List<Attachment__c> attachments = new List<Attachment__c>{ new Attachment__c(Parent_ID__c = site.Id), new Attachment__c(Parent_ID__c = proj.Id), new Attachment__c(Parent_ID__c = programme.Id), new Attachment__c() }; insert attachments; attachments = [SELECT Id,Document_Type__c,Name FROM Attachment__c WHERE Site__r.Name = 'Test Site']; System.assertEquals(1, attachments.size(), 'Site attachment records'); Directory_Template__c dt = new Directory_Template__c( Name = 'Test Directory Template' ); insert dt; //dt.Active__c = true; //update dt; File_Folder_Template__c ft = new File_Folder_Template__c( Name = 'Test Folder Template', Directory_Template__c = dt.Id ); insert ft; File_Folder_Template__c ftc = new File_Folder_Template__c( Name = 'Test Folder Template Child', Directory_Template__c = dt.Id, Parent_Folder_Template__c = ft.id ); insert ftc; } @isTest private static void TestFunction(){ Test.startTest(); List<Project_Template__c> templates = [ SELECT Id FROM Project_Template__c ]; List<Site__c> sites = [SELECT Id FROM Site__c]; System.assertEquals( 1, sites.size(), '1 site found' ); Project__c proj = new Project__c( Site__c = sites[ 0 ].Id ); insert proj; ContentVersion ContentDoc = new ContentVersion(); ContentDoc.Title = 'Test Title'; ContentDoc.ContentUrl= 'test.com'; Insert ContentDoc; ContentVersion contentVersion = new ContentVersion( Title = 'a picture', PathOnClient = 'Pic.jpg', VersionData = Blob.valueOf('Test Content'), IsMajorVersion = true); insert contentVersion; List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument]; ContentDocumentLink cdl = new ContentDocumentLink(); cdl.LinkedEntityId = proj.Id; cdl.ContentDocumentId = documents[0].Id; cdl.ShareType = 'V'; cdl.Visibility = 'AllUsers'; insert cdl; List<ContentDocument> cdList = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument]; List<ContentDocumentLink> files=[SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersionId, LinkedEntityId, ContentDocument.Title from ContentDocumentLink where ContentDocumentId =: documents[0].Id]; Test.stopTest(); } }
I am having an issue with the mapping/containsKey
Never goes past:
if( this.linkMap.containsKey( newAttachment.ContentDocumentId__c ) ){or
if( directoryMap.containsKey( folderparentId)){Any suggestion is appreciated.
Thanks
P
Thanks
P