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
Sai Ram 118Sai Ram 118 

Help in test Class?

Can you please help me with this

public with sharing class PicklistDescriber {
    static final Pattern OPTION_PATTERN = Pattern.compile('<option.+?>(.+?)</option>');

    /**
        Desribe a picklist field for an sobject id. RecordType is automatically picked
        based on the record's RecordTypeId field value.
        example usage :
        List<String> options = PicklistDescriber.describe(accountId, 'Industry');
    */
    public static List<String> describe(Id sobjectId, String pickListFieldAPIName) {
        return parseOptions(
                            new Map<String, String> {
                                                     'id' => sobjectId,
                                                     'pickListFieldName'=> pickListFieldAPIName
                                                    }
                            );
    }

    /**
        Describe a picklist field for a SobjectType, its given record type developer name and the picklist field
        example usage :
        List<String> options = PicklistDescriber.describe('Account', 'Record_Type_1', 'Industry'));
    */
    public static List<String> describe(String sobjectType, String recordTypeName, String pickListFieldAPIName) {
        return parseOptions(
                            new Map<String, String> {
                                                     'sobjectType' => sobjectType,
                                                     'recordTypeName' => recordTypeName,
                                                     'pickListFieldName'=> pickListFieldAPIName
                                                    }
                            );
    }

    /**
        Describe a picklist field for a SobjectType, its given record type ID and the picklist field
        example usage :
        Id recType1Id = [Select Id from RecordType Where SobjectType = 'Account'
                                            AND DeveloperName like 'Record_Type_2'].Id;
        System.assertEquals(REC_TYPE_1_OPTIONS, PicklistDescriber.describe('Account', recType2Id, 'Industry'));
    */
    public static List<String> describe(String sobjectType, Id recordTypeId, String pickListFieldAPIName) {
        return parseOptions(
                            new Map<String, String> {
                                                     'sobjectType' => sobjectType,
                                                     'recordTypeId' => recordTypeId,
                                                     'pickListFieldName'=> pickListFieldAPIName
                                                    }
                            );
    }

    public static List<String> describe(String sobjectType, String recordTypeName, String pickListFieldAPIName, String contrfieldName) {
        return parseOptions(
                            new Map<String, String> {
                                                     'sobjectType' => sobjectType,
                                                     'recordTypeName' => recordTypeName,
                                                     'pickListFieldName'=> pickListFieldAPIName,
                                                     'contrfieldName' => contrfieldName
                                                    }
                            );
    }

    public static Map<String,List<String>> describeWithDependency(String sobjectType, String recordTypeName, String pickListFieldAPIName, String contrfieldName) {
        Map<String, List<String>> result = new Map<String,List<String>>();
        Map<String, List<String>> depentOptions = PicklistFieldController.getDependentOptionsImpl(sobjectType, contrfieldName, pickListFieldAPIName);
        for(String pickval : PicklistDescriber.describe(sobjectType, recordTypeName, pickListFieldAPIName, contrfieldName)) {
            for(String dep : depentOptions.keySet()) {
                result.put(dep, new List<String>());
                for(String val : depentOptions.get(dep)) {
                    if(val == pickval) {
                        result.get(dep).add(val);
                    }
                }
            }
        }

        return result;
    }

    public static Map<String,List<String>> describeWithDependency(String sobjectType, Id recordTypeId, String pickListFieldAPIName, String contrfieldName) {
        Map<String, List<String>> result = new Map<String,List<String>>();
        Map<String, List<String>> depentOptions = PicklistFieldController.getDependentOptionsImpl(sobjectType, contrfieldName, pickListFieldAPIName);
        for(String pickval : PicklistDescriber.describe(sobjectType, recordTypeId, pickListFieldAPIName)) {
            for(String dep : depentOptions.keySet()) {
                result.put(dep, new List<String>());
                for(String val : depentOptions.get(dep)) {
                        System.debug(val);
                        System.debug(pickval);
                    if(val == pickval) {
                        result.get(dep).add(val);
                    }
                }
            }
        }

        return result;
    }

    /*
        Internal method to parse the OPTIONS
    */
    static List<String> parseOptions(Map<String, String> params) {
        PageReference pr = Page.PicklistDesc;
        // to handle development mode, if ON
        pr.getParameters().put('core.apexpages.devmode.url', '1');

        for (String key : params.keySet()) {
            pr.getParameters().put(key, params.get(key));
        }

        String xmlContent = pr.getContent().toString();

        Matcher mchr = OPTION_PATTERN.matcher(xmlContent);
        List<String> options = new List<String>();
        while(mchr.find()) {
            System.debug(mchr.group(1));
            options.add(mchr.group(1));
        }
        // remove the --None-- element
        if (!options.isEmpty()) options.remove(0);
        return options;
    }
}

Thanks
Steven NsubugaSteven Nsubuga
Try this
@isTest
private class PicklistDescriberTest {

    @isTest static void test() {
        
        // Reason__c is a dependent picklist, Rating is controlling picklist
        // If Stage is Hot, the valid values for Reason are Many deals or Great staff
        // If Stage is Cold, the valid values for Reason are Unresponsive staff or No deals
        // If Stage is Warm, the valid value for Reason is Responsive staff
        
        // There must be at least 1 record type exists for Account
        Map<String, Schema.RecordTypeInfo> recordTypes = Schema.SObjectType.Account.getRecordTypeInfosByName();
        
        Account testAccount = new Account(Name='Test Account', Rating = 'Hot', Reason__c = 'Many deals', RecordTypeId = recordTypes.get(recordTypes.keyset()[0]).getRecordTypeId());
        insert testAccount;
        
        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
            AccountId = testAccount.Id,
            CloseDate = date.today().addDays(45)
        );
        insert testOpportunity;
        
        System.assert(PicklistDescriber.describe(testAccount.Id, 'Rating').size()!=0);
        
        
        System.assert(PicklistDescriber.describe('Account', recordTypes.keyset()[0], 'Rating').size() > 0);
        
        System.assert(PicklistDescriber.describe('Account', recordTypes.get(recordTypes.keyset()[0]).getRecordTypeId(), 'Rating').size() > 0);
        
        System.assert(PicklistDescriber.describe('Account', recordTypes.keyset()[0], 'Reason__c', 'Rating').size() > 0);
        
        System.assert(PicklistDescriber.describeWithDependency('Account', recordTypes.keyset()[0], 'Reason__c', 'Rating').size() > 0);
        
        System.assert(PicklistDescriber.describeWithDependency('Account', recordTypes.get(recordTypes.keyset()[0]).getRecordTypeId(), 'Reason__c', 'Rating').size() > 0);
    }
    
}
Steven NsubugaSteven Nsubuga
Try this
@isTest
private class PicklistDescriberTest {

    @isTest static void test() {
        
        // Reason__c is a dependent picklist, Rating is controlling picklist
        // If Stage is Hot, the valid values for Reason are Many deals or Great staff
        // If Stage is Cold, the valid values for Reason are Unresponsive staff or No deals
        // If Stage is Warm, the valid value for Reason is Responsive staff
        
        // There must be at least 1 record type exists for Account
        Map<String, Schema.RecordTypeInfo> recordTypes = Schema.SObjectType.Account.getRecordTypeInfosByName();
		List<String> recordTypeNames = new List<String>(recordTypes.keyset());
		
        
        Account testAccount = new Account(Name='Test Account', Rating = 'Hot', Reason__c = 'Many deals', RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId());
        insert testAccount;
        
        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
            AccountId = testAccount.Id,
            CloseDate = date.today().addDays(45)
        );
        insert testOpportunity;
        
        System.assert(PicklistDescriber.describe(testAccount.Id, 'Rating').size()!=0);
        
        
        System.assert(PicklistDescriber.describe('Account', recordTypeNames[0], 'Rating').size() > 0);
        
        System.assert(PicklistDescriber.describe('Account', recordTypes.get(recordTypeNames[0]).getRecordTypeId(), 'Rating').size() > 0);
        
        System.assert(PicklistDescriber.describe('Account', recordTypeNames[0], 'Reason__c', 'Rating').size() > 0);
        
        System.assert(PicklistDescriber.describeWithDependency('Account', recordTypeNames[0], 'Reason__c', 'Rating').size() > 0);
        
        System.assert(PicklistDescriber.describeWithDependency('Account', recordTypes.get(recordTypeNames[0]).getRecordTypeId(), 'Reason__c', 'Rating').size() > 0);
    }
    
}