+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Code coverage for metadata

I am wondering how do I cover code that is related to cusotm metadata.
class:
public with sharing class ApplicationServices {

    public static final String APPLICATION_STATUS_DRAFT = 'Draft';
    public string coloTemplateName;

    public static List<ApplicationProjectTemplateConfig__mdt> templateConfigs = [SELECT Id,ApplicationCriteria__c,ProjectTemplateName__c from ApplicationProjectTemplateConfig__mdt];
     
    public static Map<String, SObject> templatesByNameMap {
        get{
            if(templatesByNameMap == null){
                list<Project_Template__c> templates = [Select Id, Name FROM Project_Template__c WHERE 
Active__c =: true];
                templatesByNameMap = PGE_EDSUtils.createMapByStringFieldSingleObject(templates, 'Name');
            }

            return templatesByNameMap;
        }
        private set;
    }

    public static void handleSubmit(Map<Id, Application__c> applications) {
        // Only before update
        if( !Trigger.isBefore || !Trigger.isUpdate ) {
            return;
        }
      
        // Determine the projects to create, if any
        Map<Application__c, Project__c> projectsToCreate = new Map<Application__c, Project__c>();
        for(Application__c application : applications.values()) {
            boolean docreate = shouldCreateColoProject(application);
            if( docreate ) {
                Project__c project = createColoProject(application);
                   if(project != null){
                        projectsToCreate.put(application, project);
                    }
            }
        }


///no code coverage
        if(projectsToCreate.size() > 0) {
            insert projectsToCreate.values();
            for(Application__c application : projectsToCreate.keySet()) {
                Project__c project = projectsToCreate.get(application);
                application.Project__c = project.Id;
                application.Date_Submitted__c = application.Date_Submitted__c == null
                                                ? System.today()
                                                : application.Date_Submitted__c ;
            }
        }
    }

///no code coverage
     public static String coloTemplateName {
        get {
            if(coloTemplateName == null) {
                coloTemplateName = AppConfig.getAppConfigValue('COL_PROJECT_TEMPLATE');
            }
            return coloTemplateName;
        }
        private set;
    }

///no code coverage
    public static Id coloTemplateId {
        get {
            if(coloTemplateId == null) {
                if(String.isNotEmpty(coloTemplateName)) {
                    Project_Template__c template = [SELECT Id 
                                                                 FROM Project_Template__c
                                                                 WHERE Name =: coloTemplateName LIMIT 1];
                    coloTemplateId = template.Id;
                }
            }
            return coloTemplateId;
        }
        private set;
    }


    public static Boolean shouldCreateColoProject(Application__c application) {
        return !isDraft(application) && !hasProject(application);
    }

    private static Boolean isDraft(Application__c application) {
        return application.Application_Status__c == APPLICATION_STATUS_DRAFT;
    }

    private static Boolean hasProject(Application__c application) {
        return application.Project__c != null;
    }

  
    public static Project__c createColoProject(Application__c application) {
        Project__c project = null;
        string projectTemplate;
   
        for( ApplicationProjectTemplateConfig__mdt  config :templateConfigs){
            boolean templatematch = BooleanLogicEvaluation.evaluate(application , config.ApplicationCriteria__c);
     
            if(templatematch){
                projectTemplate = config.ProjectTemplateName__c;
                break;
            }
         } 
      
         if (projectTemplate!=null && templatesByNameMap.containsKey(projectTemplate)){
           // no code coverage
            project = new Project__c(
                AccountSite__c =  application.AccountSite__c 
                OwnerId = application.ownerId,
                application__c = application.Id,
                ProjectTemplate__c = templatesByNameMap.get(projectTemplate).id,
                Project_Start_Date_A__c = System.today()
            );
         }

        return project;
    }

}
Test Class
static testMethod void testApplicationBasicCRUD() {
    	AccountSite__c  as = [SELECT Id FROM AccountSite__c LIMIT 1];
        Test.startTest();

        Application__c application = new Application__c(
            Application_Type__c = 'NewAgreement',
            Scope_of_Work__c = 'Test Setup',
            AccountSite__c = as.Id
        );
        insert application;
        System.assert(application.Id != null, 'Successfully created application');

        // Update
        application.Tenant_AccountSite_Name__c = ' Mountain';
        application.Tenant_AccountSite_Number__c = '123';
        update application;
        System.assert(application.Id != null, 'Successfully updates application');

        // Delete
        delete application;
        System.assert(true, 'Successfully deleted application');

        Test.stopTest();
    }

    static testMethod void testAutomaticProjectCreation() {
    	AccountSite__c  as= [SELECT Id FROM AccountSite__c  LIMIT 1];

        Test.startTest();
        Application__c application = new Application__c(
            Application_Type__c = 'NewAgreement',
            Scope_of_Work__c = 'Test Setup',
            Date_Submitted__c = System.today(),
            AccountSite__c = as.Id
        );
        insert application;

        application.Application_Status__c = 'Feasibility';
        application.Approval_Status__c = 'Pending';
        update application;
        
        application = [SELECT id, Project__c,  Date_Submitted__c FROM Application__c where id=: application.Id];
        System.assertEquals(System.today(), application.Date_Submitted__c, 'Date Submitted Set');
     
        Test.stopTest();
    }


    @testSetup
    private static void createTestData() {

       
        AccountSite__c as = new AccountSite__c (
            Name = 'Casterly Rock'
        );
        insert as;

      
        Project_Template__c template = new Project_Template__c (
            Name = 'Collocation',
            Active__c = true,
            Object__c  = 'Project__c',
            Project_Extension_Objects__c = 'Coll_Project__c'
        );
        insert template;

        Collocation__c collocation = new Collocation__c (
            Name = '100ft',
            AccountSite__c = as.Id
        );
        insert collocation;

        Application__c application = new Application__c (
            Application_Type__c = 'NewAgreement',
            Scope_of_Work__c = 'Test Setup',
            AccountSite__c = as.Id
        );
        insert application;

    }


}


 
Best Answer chosen by Phuc Nguyen 18
AnudeepAnudeep (Salesforce Developers) 

All Answers

AnudeepAnudeep (Salesforce Developers) 
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
thanks for the reply Anudeep, read the examples but stil lhave not been able to get the coverage yet.