+ Start a Discussion
Holly Havelka 10Holly Havelka 10 

Help with Sharing Trigger Test Class

Hi all, 
I have the below apex sharing trigger, but am struggling to get code coverage (currently at 44%):
trigger AffiliationMakePublicContactTrigger on npe5__Affiliation__c (after insert, after update, before delete) {
 
    // Get the Account Name Details
    Set<Id> AcctId = new Set<Id>();
    List<Account> AccountLists = new List<Account>();
    Map<Id,String> AccountNameMap = new Map<Id,String>();
	
    if (!Trigger.isDelete) {
		List<npe5__Affiliation__c> affiliations = [select Id, npe5__Organization__c, npe5__Organization__r.Id, Make_Public__c, npe5__Contact__r.Id from npe5__Affiliation__c where Id IN: Trigger.newMap.keyset()];
		for(npe5__Affiliation__c aff : affiliations)
		{
		 
		 if(aff.npe5__Organization__r != null)
		 {
			AcctId.add(aff.npe5__Organization__r.Id);
		 }

		}
		 
		 if(AcctId.size() > 0)
		 {
			 AccountLists  = [select Id,name from Account where Id IN: AcctId];
		 }

		for(Account acc :AccountLists  )
		{
			AccountNameMap.put(acc.id,acc.Name);
		}
		
		// Get the Group Details
		List<Group> groups = [SELECT Email,Id,Name FROM Group];

		Map<String,Id> GroupMap = new MAp<String,Id>();
		for( Group grp:groups)
		{
			if(grp.Name != null && grp.Name != '')
			{
				GroupMap.put(grp.Name,grp.Id);
			}
		}
	  
	  // inserting new records
	  if (Trigger.isInsert) {
				 
		List<ContactShare> sharesToCreate = new List<ContactShare>();

		
		for (npe5__Affiliation__c affiliation : affiliations) {
			
		  if (affiliation.Make_Public__c == true) 
		  {

			// create the new share for group
			ContactShare cs = new ContactShare();
			cs.ContactAccessLevel = 'Edit';
			cs.ContactId = affiliation.npe5__Contact__r.Id;        
			system.debug(cs.ContactId);
			if(GroupMap.containsKey(AccountNameMap.get(affiliation.npe5__Organization__r.Id)))
					cs.UserOrGroupId = GroupMap.get(AccountNameMap.get(affiliation.npe5__Organization__r.Id));   
			sharesToCreate.add(cs);

		  }
		}

		// do the DML to create shares
		if (!sharesToCreate.isEmpty())
		  insert sharesToCreate;

	  // updating existing records
	  } else if (Trigger.isUpdate) {

		List<ContactShare> sharesToCreate = new List<ContactShare>();
		List<ID> shareIdsToDelete = new List<ID>();

		for (npe5__Affiliation__c affiliation : affiliations) {

		  // if the record was public but is now private -- delete the existing share
		  if (Trigger.oldMap.get(affiliation.Id).Make_Public__c == true && affiliation.Make_Public__c == false) {
			shareIdsToDelete.add(affiliation.npe5__Contact__r.Id);

		  // if the record was private but now is public -- create the new share for the group
		  } else if (Trigger.oldMap.get(affiliation.Id).Make_Public__c == false && affiliation.Make_Public__c == true) {
			
			// create the new share with read/write access
			ContactShare cs = new ContactShare();
			cs.ContactAccessLevel = 'Edit';
			cs.ContactId = affiliation.npe5__Contact__r.Id;
			if(GroupMap.containsKey(AccountNameMap.get(affiliation.npe5__Organization__r.Id)))
					cs.UserOrGroupId =  GroupMap.get(AccountNameMap.get(affiliation.npe5__Organization__r.Id));   
			sharesToCreate.add(cs);

		  }

		}

		// do the DML to delete shares
		if (!shareIdsToDelete.isEmpty())
		  delete [select id from ContactShare where ContactId IN :shareIdsToDelete and RowCause = 'Manual'];

		// do the DML to create shares
		if (!sharesToCreate.isEmpty())
		  insert sharesToCreate;

	  }
  }
  else if (Trigger.isDelete) {

    List<ID> shareIdsToDelete = new List<ID>();

	List<npe5__Affiliation__c> affiliations = [select Id, npe5__Organization__c, npe5__Organization__r.Id, Make_Public__c, npe5__Contact__r.Id from npe5__Affiliation__c where Id IN: Trigger.oldMap.keyset()];
    for (npe5__Affiliation__c affiliation : affiliations) {
        shareIdsToDelete.add(affiliation.npe5__Contact__r.Id);
        system.debug(shareIdsToDelete);
    }

    // do the DML to delete shares
    if (!shareIdsToDelete.isEmpty())
      delete [select id from ContactShare where ContactId IN :shareIdsToDelete and RowCause = 'Manual'];
  }

}
Here is my test class:
@isTest
private class TestAffiliationMakePublicContactTrigger {

    // test that newly inserted records marked as pubic=true have corresponding shares created
    static testMethod void testAddShares() {
    
    Account acct = new Account(name='Breakthrough Birmingham');
        insert acct;
    Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex');
        insert c;
    
    //Create Public Group
    Group gp            = new Group();
    gp.Name             = 'Breakthrough Birmingham';
    gp.DeveloperName    = 'Breakthrough_Birmingham';
    gp.Type             = 'Regular';
    insert gp;
    
    ContactShare testShare = new ContactShare();
    testShare.ContactAccessLevel = 'Edit';
    testShare.ContactId = c.Id;        
	testShare.UserOrGroupId = gp.Id;
    insert testShare;
    
    Set<ID> affiliationIds = new Set<ID>();
    List<npe5__Affiliation__c> affiliations = new List<npe5__Affiliation__c>();
    
    for (npe5__Affiliation__c aff : affiliations)
        affiliations.add(new npe5__Affiliation__c(npe5__Organization__c=acct.Id,Make_Public__c=true,npe5__Contact__c=c.Id));
		insert affiliations;
        
    for (npe5__Affiliation__c a : affiliations)
      affiliationIds.add(a.npe5__Contact__r.id);
        
    // assert that 1 share was created
    List<ContactShare> shares = [select id from ContactShare where 
      ContactId IN :affiliationIds and RowCause = 'Manual'];
    System.assertEquals(shares.size(),1);
    }

    // insert records and switch them from public = true to public = false
    static testMethod void testUpdateAffiliations() {

    Account acct = new Account(name='Breakthrough Birmingham');
        insert acct;
    Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex');
        insert c;
        
    Set<ID> affiliationIds = new Set<ID>();
    List<npe5__Affiliation__c> affiliations = new List<npe5__Affiliation__c>();
    
    for (npe5__Affiliation__c aff : affiliations)
        affiliations.add(new npe5__Affiliation__c(npe5__Organization__c=acct.Id,Make_Public__c=false,npe5__Contact__c=c.Id));
		insert affiliations;
        
    for (npe5__Affiliation__c a : affiliations)
      affiliationIds.add(a.npe5__Contact__r.id);
        
    // assert that 0 shares exist
    List<ContactShare> shares = [select id from ContactShare where 
      ContactId IN :affiliationIds and RowCause = 'Manual'];
    System.assertEquals(shares.size(),0);
    
    for (npe5__Affiliation__c aff : affiliations)
      aff.Make_Public__c = true;

    update affiliations;

    // assert that 1 share was created
    shares = [select id from ContactShare where ContactId IN :affiliationIds and RowCause = 'Manual'];
    System.assertEquals(shares.size(),1);

    for (npe5__Affiliation__c aff : affiliations)
      aff.Make_Public__c = false;

    update affiliations;

    // assert that 0 shares exist
    shares = [select id from ContactShare where ContactId IN :affiliationIds and RowCause = 'Manual'];
    System.assertEquals(shares.size(),0);

    }
}

 
Best Answer chosen by Holly Havelka 10
Steven NsubugaSteven Nsubuga
The good news is that the minimum coverage required for a trigger is 1%. So you're covered.
That said, I have added an additional test delete test method.
@isTest
private class TestAffiliationMakePublicContactTrigger {

    // test that newly inserted records marked as pubic=true have corresponding shares created
    static testMethod void testAddShares() {
    
		Account acct = new Account(name='Breakthrough Birmingham');
			insert acct;
		Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex');
			insert c;
		
		//Create Public Group
		Group gp            = new Group();
		gp.Name             = 'Breakthrough Birmingham';
		gp.DeveloperName    = 'Breakthrough_Birmingham';
		gp.Type             = 'Regular';
		insert gp;
		
		ContactShare testShare = new ContactShare();
		testShare.ContactAccessLevel = 'Edit';
		testShare.ContactId = c.Id;        
		testShare.UserOrGroupId = gp.Id;
		insert testShare;
		
		Set<ID> affiliationIds = new Set<ID>();
		List<npe5__Affiliation__c> affiliations = new List<npe5__Affiliation__c>();
		
		for (npe5__Affiliation__c aff : affiliations)
			affiliations.add(new npe5__Affiliation__c(npe5__Organization__c=acct.Id,Make_Public__c=true,npe5__Contact__c=c.Id));
			insert affiliations;
			
		for (npe5__Affiliation__c a : affiliations)
		  affiliationIds.add(a.npe5__Contact__r.id);
			
		// assert that 1 share was created
		List<ContactShare> shares = [select id from ContactShare where 
		  ContactId IN :affiliationIds and RowCause = 'Manual'];
		System.assertEquals(shares.size(),1);
    }

    // insert records and switch them from public = true to public = false
    static testMethod void testUpdateAffiliations() {

		Account acct = new Account(name='Breakthrough Birmingham');
			insert acct;
		Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex');
			insert c;
			
		Set<ID> affiliationIds = new Set<ID>();
		List<npe5__Affiliation__c> affiliations = new List<npe5__Affiliation__c>();
		
		for (npe5__Affiliation__c aff : affiliations)
			affiliations.add(new npe5__Affiliation__c(npe5__Organization__c=acct.Id,Make_Public__c=false,npe5__Contact__c=c.Id));
			insert affiliations;
			
		for (npe5__Affiliation__c a : affiliations)
		  affiliationIds.add(a.npe5__Contact__r.id);
			
		// assert that 0 shares exist
		List<ContactShare> shares = [select id from ContactShare where 
		  ContactId IN :affiliationIds and RowCause = 'Manual'];
		System.assertEquals(shares.size(),0);
		
		for (npe5__Affiliation__c aff : affiliations)
		  aff.Make_Public__c = true;

		update affiliations;

		// assert that 1 share was created
		shares = [select id from ContactShare where ContactId IN :affiliationIds and RowCause = 'Manual'];
		System.assertEquals(shares.size(),1);

		for (npe5__Affiliation__c aff : affiliations)
		  aff.Make_Public__c = false;

		update affiliations;

		// assert that 0 shares exist
		shares = [select id from ContactShare where ContactId IN :affiliationIds and RowCause = 'Manual'];
		System.assertEquals(shares.size(), 0);

    }
	
    static testMethod void testDeleteAffiliations() {
    
		Account acct = new Account(name='Breakthrough Birmingham');
			insert acct;
		Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex');
			insert c;
		
		//Create Public Group
		Group gp            = new Group();
		gp.Name             = 'Breakthrough Birmingham';
		gp.DeveloperName    = 'Breakthrough_Birmingham';
		gp.Type             = 'Regular';
		insert gp;
		
		ContactShare testShare = new ContactShare();
		testShare.ContactAccessLevel = 'Edit';
		testShare.ContactId = c.Id;        
		testShare.UserOrGroupId = gp.Id;
		insert testShare;
		
		Set<ID> affiliationIds = new Set<ID>();
		List<npe5__Affiliation__c> affiliations = new List<npe5__Affiliation__c>();
		
		affiliations.add(new npe5__Affiliation__c(npe5__Organization__c=acct.Id,Make_Public__c=true,npe5__Contact__c=c.Id));
		insert affiliations;
			
		for (npe5__Affiliation__c a : affiliations)
		  affiliationIds.add(a.npe5__Contact__r.id);
			
		// assert that 1 share was created
		List<ContactShare> shares = [select id from ContactShare where 
		  ContactId IN :affiliationIds and RowCause = 'Manual'];
		System.assertEquals(shares.size(),1);
		
		delete affiliations;
		List<ContactShare> shares2 = [select id from ContactShare where 
		  ContactId IN :affiliationIds and RowCause = 'Manual'];
		System.assertEquals(shares2.size(), 0);
		
    }
}

 

All Answers

Steven NsubugaSteven Nsubuga
The good news is that the minimum coverage required for a trigger is 1%. So you're covered.
That said, I have added an additional test delete test method.
@isTest
private class TestAffiliationMakePublicContactTrigger {

    // test that newly inserted records marked as pubic=true have corresponding shares created
    static testMethod void testAddShares() {
    
		Account acct = new Account(name='Breakthrough Birmingham');
			insert acct;
		Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex');
			insert c;
		
		//Create Public Group
		Group gp            = new Group();
		gp.Name             = 'Breakthrough Birmingham';
		gp.DeveloperName    = 'Breakthrough_Birmingham';
		gp.Type             = 'Regular';
		insert gp;
		
		ContactShare testShare = new ContactShare();
		testShare.ContactAccessLevel = 'Edit';
		testShare.ContactId = c.Id;        
		testShare.UserOrGroupId = gp.Id;
		insert testShare;
		
		Set<ID> affiliationIds = new Set<ID>();
		List<npe5__Affiliation__c> affiliations = new List<npe5__Affiliation__c>();
		
		for (npe5__Affiliation__c aff : affiliations)
			affiliations.add(new npe5__Affiliation__c(npe5__Organization__c=acct.Id,Make_Public__c=true,npe5__Contact__c=c.Id));
			insert affiliations;
			
		for (npe5__Affiliation__c a : affiliations)
		  affiliationIds.add(a.npe5__Contact__r.id);
			
		// assert that 1 share was created
		List<ContactShare> shares = [select id from ContactShare where 
		  ContactId IN :affiliationIds and RowCause = 'Manual'];
		System.assertEquals(shares.size(),1);
    }

    // insert records and switch them from public = true to public = false
    static testMethod void testUpdateAffiliations() {

		Account acct = new Account(name='Breakthrough Birmingham');
			insert acct;
		Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex');
			insert c;
			
		Set<ID> affiliationIds = new Set<ID>();
		List<npe5__Affiliation__c> affiliations = new List<npe5__Affiliation__c>();
		
		for (npe5__Affiliation__c aff : affiliations)
			affiliations.add(new npe5__Affiliation__c(npe5__Organization__c=acct.Id,Make_Public__c=false,npe5__Contact__c=c.Id));
			insert affiliations;
			
		for (npe5__Affiliation__c a : affiliations)
		  affiliationIds.add(a.npe5__Contact__r.id);
			
		// assert that 0 shares exist
		List<ContactShare> shares = [select id from ContactShare where 
		  ContactId IN :affiliationIds and RowCause = 'Manual'];
		System.assertEquals(shares.size(),0);
		
		for (npe5__Affiliation__c aff : affiliations)
		  aff.Make_Public__c = true;

		update affiliations;

		// assert that 1 share was created
		shares = [select id from ContactShare where ContactId IN :affiliationIds and RowCause = 'Manual'];
		System.assertEquals(shares.size(),1);

		for (npe5__Affiliation__c aff : affiliations)
		  aff.Make_Public__c = false;

		update affiliations;

		// assert that 0 shares exist
		shares = [select id from ContactShare where ContactId IN :affiliationIds and RowCause = 'Manual'];
		System.assertEquals(shares.size(), 0);

    }
	
    static testMethod void testDeleteAffiliations() {
    
		Account acct = new Account(name='Breakthrough Birmingham');
			insert acct;
		Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex');
			insert c;
		
		//Create Public Group
		Group gp            = new Group();
		gp.Name             = 'Breakthrough Birmingham';
		gp.DeveloperName    = 'Breakthrough_Birmingham';
		gp.Type             = 'Regular';
		insert gp;
		
		ContactShare testShare = new ContactShare();
		testShare.ContactAccessLevel = 'Edit';
		testShare.ContactId = c.Id;        
		testShare.UserOrGroupId = gp.Id;
		insert testShare;
		
		Set<ID> affiliationIds = new Set<ID>();
		List<npe5__Affiliation__c> affiliations = new List<npe5__Affiliation__c>();
		
		affiliations.add(new npe5__Affiliation__c(npe5__Organization__c=acct.Id,Make_Public__c=true,npe5__Contact__c=c.Id));
		insert affiliations;
			
		for (npe5__Affiliation__c a : affiliations)
		  affiliationIds.add(a.npe5__Contact__r.id);
			
		// assert that 1 share was created
		List<ContactShare> shares = [select id from ContactShare where 
		  ContactId IN :affiliationIds and RowCause = 'Manual'];
		System.assertEquals(shares.size(),1);
		
		delete affiliations;
		List<ContactShare> shares2 = [select id from ContactShare where 
		  ContactId IN :affiliationIds and RowCause = 'Manual'];
		System.assertEquals(shares2.size(), 0);
		
    }
}

 
This was selected as the best answer
Holly Havelka 10Holly Havelka 10
Steven, your code is still getting the 44%.  I am still learning (obviously), and see that there is still a lot of red showing in the developers console.  Specifically, lines 85 to 90 of the trigger.