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
Rahul Gupta 166Rahul Gupta 166 

Test class for Metadataservice class

Hi Can some one help me with test class of the following code :
 
public with sharing class MetadataServiceExamples{
   
    
 public static map<String,map<String,map<string,string>>> profiledata(set<String> prfset){
 map<String,map<String,map<string,string>>> fillprofiledetail=new map<String,map<String,map<string,string>>>();
   MetadataService.MetadataPort service = createService();
   
  // String [] arrayOfProfileName = new List<String>();
    //   arrayOfProfileName.addAll(prfset);
       
   for(String strprf:prfset){
   String StrTmpProfile= strprf;
    if(strprf == 'System Administrator')
     {
        strprf= 'Admin';
     }
    if(strprf == 'Solution Manager')
     {
        strprf= 'SolutionManager';
     } 
     if(strprf == 'Contract Manager')
     {
        strprf= 'ContractManager';
     } 
     if(strprf == 'Standrad Marketing User')
     {
        strprf= 'StandradAul';
     } 
     if(strprf == 'Standard User')
     {
        strprf= 'Standard';
     }
     if(strprf == 'Marketing User')
     {
        strprf= 'MarketingProfile';
     } 
     if(strprf=='Standard Platform User')
      {
        strprf= 'StandradAul';
      }
     if(strprf=='High Volume Customer Portal')
     {
       strprf= 'HighVolumePortal';
     }
     
     
      String [] arrayOfProfileName = new List<String>();
      arrayOfProfileName.add(strprf); 
   map<String,map<string,string>>  innermap=new map<String,map<string,string>>();
   //MetadataService.Profile admin = (MetadataService.Profile) service.readMetadata('Profile', new String[] {strprf}).getRecords()[0];
   MetadataService.Profile admin = (MetadataService.Profile) service.readMetadata('Profile', arrayOfProfileName).getRecords()[0];
   system.debug(admin.fullName); 
   
   //User Permissions
   
   map<string,string> innerininnermap=new map<string,string>();
   if(admin.userPermissions.size()>0){
   for(MetadataService.ProfileUserPermission ptv : admin.userPermissions){
   
      innerininnermap.put(''+ptv.name,''+ptv.enabled);
     //system.debug('User Permission = ' + ptv.name + ' & ' + 'visibility = ' + ptv.enabled);
     }
     innermap.put('userPermissions', innerininnermap); 
    }
   
  //Tab visiblity
  if(admin.tabVisibilities.size()>0){
   innerininnermap=new map<string,string>();
   for(MetadataService.ProfileTabVisibility ptv : admin.tabVisibilities){
      innerininnermap.put(''+ptv.tab,''+ptv.visibility);
     //system.debug('tab name = ' + ptv.tab + ' & ' + 'visibility = ' + ptv.visibility);
     }
    innermap.put('tabVisibilities', innerininnermap); 
    }
    
  //Application Visiblity
  if(admin.applicationVisibilities.size()>0){
   innerininnermap=new map<string,string>();      
    for(MetadataService.ProfileApplicationVisibility pav : admin.applicationVisibilities){
     innerininnermap.put(''+pav.application,''+pav.visible);
     //  system.debug('app name = ' + pav.application+ ' & ' + 'visibility = ' + pav.visible);
    }   
  innermap.put('applicationVisibilities', innerininnermap);  
  }
  
   //Apex Class
  if(admin.classAccesses.size()>0){  
  innerininnermap=new map<string,string>();  
  for(MetadataService.ProfileApexClassAccess pac : admin.classAccesses){
       innerininnermap.put(''+pac.apexClass,''+pac.enabled);
//     system.debug('Apex class = ' + pac.apexClass+ ' & ' + 'visibility = ' + pac.enabled);
    }   
  innermap.put('classAccesses', innerininnermap); 
  }
 
  //Apex Page
  if(admin.pageAccesses.size()>0){  
  innerininnermap=new map<string,string>();  
  for(MetadataService.ProfileApexPageAccess pa : admin.pageAccesses){
    innerininnermap.put(''+pa.apexPage,''+pa.enabled);
    //system.debug('Apex Page= ' + pa.apexPage+ ' & ' + 'visibility = ' + pa.enabled);
  }   
  innermap.put('pageAccesses', innerininnermap);
 }   
   
   
   
     //recordtype     
  innerininnermap=new map<string,string>();  
  if(admin.recordTypeVisibilities.size()>0){ 
  for(MetadataService.ProfileRecordTypeVisibility pa : admin.recordTypeVisibilities)
    {
            innerininnermap.put(''+pa.recordType,''+pa.visible);

    }   
  innermap.put('recordTypeVisibilities', innerininnermap);   
    } 
     
   
    
    
    innerininnermap=new map<string,string>();  
  if(admin.recordTypeVisibilities.size()>0){ 
  for(MetadataService.ProfileRecordTypeVisibility pa : admin.recordTypeVisibilities)
    {
            innerininnermap.put(''+pa.recordType,''+pa.visible);

    }   
  innermap.put('recordTypeVisibilities', innerininnermap);   
    } 
     
     
   fillprofiledetail.put(StrTmpProfile,innermap);  
  }
   return   fillprofiledetail;
   }
 
   public static map<String,map<String,map<string,List<string>>>> profiledataObject (set<String> prfset)
    {
    map<String,map<String,map<string,List<string>>>> fillprofiledetail1=new map<String,map<String,map<string,List<string>>>>();
    MetadataService.MetadataPort service = createService();
    for(String strprf:prfset){
     String StrTmpProfile= strprf;
    if(strprf == 'System Administrator')
     {
        strprf= 'Admin';
     }
    if(strprf == 'Solution Manager')
     {
        strprf= 'SolutionManager';
     } 
     if(strprf == 'Contract Manager')
     {
        strprf= 'ContractManager';
     } 
     if(strprf == 'Standrad Marketing User')
     {
        strprf= 'StandradAul';
     } 
     if(strprf == 'Standard User')
     {
        strprf= 'Standard';
     }
     if(strprf == 'Marketing User')
     {
        strprf= 'MarketingProfile';
     } 
     if(strprf=='Standard Platform User')
      {
        strprf= 'StandradAul';
      }
     if(strprf=='High Volume Customer Portal')
     {
       strprf= 'HighVolumePortal';
     }
     
     
      String [] arrayOfProfileName = new List<String>();
      arrayOfProfileName.add(strprf);
      map<String,map<string,List<string>>>  innermap=new map<String,map<string,List<string>>>();      
      MetadataService.Profile admin = (MetadataService.Profile) service.readMetadata('Profile', arrayOfProfileName).getRecords()[0];
      system.debug(admin.fullName); 
      map<string,List<string>>innerininnermap=new map<string,List<string>>();  
       if(admin.objectPermissions.size()>0){ 
        for(MetadataService.ProfileObjectPermissions pa : admin.objectPermissions)
          {
           
           List<String>Temp = new List<String>();
           Temp.add(''+pa.allowCreate);
           Temp.add(''+pa.allowDelete);
           Temp.add(''+pa.allowEdit);
           Temp.add(''+pa.allowRead);
           Temp.add(''+pa.modifyAllRecords);
           Temp.add(''+pa.viewAllRecords);
           innerininnermap.put(''+pa.object_x,Temp);
            

        }   
      innermap.put('objectPermissions', innerininnermap);   
    } 
    fillprofiledetail1.put(StrTmpProfile,innermap);
    }
    return fillprofiledetail1;
    }
    
    
    public static map<String,map<String,map<string,List<string>>>> profiledataField (set<String> prfset)
    {
    map<String,map<String,map<string,List<string>>>> fillprofiledetail2 =new map<String,map<String,map<string,List<string>>>>();
    MetadataService.MetadataPort service = createService();
    for(String strprf:prfset){
     String StrTmpProfile= strprf;
   if(strprf == 'System Administrator')
     {
        strprf= 'Admin';
     }
    if(strprf == 'Solution Manager')
     {
        strprf= 'SolutionManager';
     } 
     if(strprf == 'Contract Manager')
     {
        strprf= 'ContractManager';
     } 
     if(strprf == 'Standrad Marketing User')
     {
        strprf= 'StandradAul';
     } 
     if(strprf == 'Standard User')
     {
        strprf= 'Standard';
     }
     if(strprf == 'Marketing User')
     {
        strprf= 'MarketingProfile';
     } 
     if(strprf=='Standard Platform User')
      {
        strprf= 'StandradAul';
      }
     if(strprf=='High Volume Customer Portal')
     {
       strprf= 'HighVolumePortal';
     }
     
     
      String [] arrayOfProfileName = new List<String>();
      arrayOfProfileName.add(strprf);
      map<String,map<string,List<string>>>  innermap=new map<String,map<string,List<string>>>();      
      MetadataService.Profile admin = (MetadataService.Profile) service.readMetadata('Profile', arrayOfProfileName).getRecords()[0];
      system.debug(admin.fullName); 
      map<string,List<string>>innerininnermap=new map<string,List<string>>();  
       if(admin.fieldPermissions.size()>0){ 
        for(MetadataService.ProfileFieldLevelSecurity pa : admin.fieldPermissions)
          {
           
           List<String>Temp = new List<String>();
           Temp.add(''+pa.editable);
           Temp.add(''+pa.readable);
           
           innerininnermap.put(''+pa.field,Temp);
            

        }   
      innermap.put('fieldPermissions', innerininnermap);   
    } 
    fillprofiledetail2.put(StrTmpProfile,innermap);
    }
    return fillprofiledetail2;
    }
   
   
   
    
    
    public class MetadataServiceExamplesException extends Exception { }

    public static MetadataService.MetadataPort createService()
    {
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        return service;
    }

    /**
     * Example helper method to interpret a SaveResult, throws an exception if errors are found
     **/
    public static void handleSaveResults(MetadataService.SaveResult saveResult)
    {
        // Nothing to see?
        if(saveResult==null || saveResult.success)
            return;
        // Construct error message and throw an exception
        if(saveResult.errors!=null)
        {
            List<String> messages = new List<String>();
            messages.add(
                (saveResult.errors.size()==1 ? 'Error ' : 'Errors ') +
                    'occured processing component ' + saveResult.fullName + '.');
            for(MetadataService.Error error : saveResult.errors)
                messages.add(
                    error.message + ' (' + error.statusCode + ').' +
                    ( error.fields!=null && error.fields.size()>0 ?
                        ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );
            if(messages.size()>0)
                throw new MetadataServiceExamplesException(String.join(messages, ' '));
        }
        if(!saveResult.success)
            throw new MetadataServiceExamplesException('Request failed with no specified error.');
    }

    /**
     * Example helper method to interpret a SaveResult, throws an exception if errors are found
     **/
    public static void handleDeleteResults(MetadataService.DeleteResult deleteResult)
    {
        // Nothing to see?
        if(deleteResult==null || deleteResult.success)
            return;
        // Construct error message and throw an exception
        if(deleteResult.errors!=null)
        {
            List<String> messages = new List<String>();
            messages.add(
                (deleteResult.errors.size()==1 ? 'Error ' : 'Errors ') +
                    'occured processing component ' + deleteResult.fullName + '.');
            for(MetadataService.Error error : deleteResult.errors)
                messages.add(
                    error.message + ' (' + error.statusCode + ').' +
                    ( error.fields!=null && error.fields.size()>0 ?
                        ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );
            if(messages.size()>0)
                throw new MetadataServiceExamplesException(String.join(messages, ' '));
        }
        if(!deleteResult.success)
            throw new MetadataServiceExamplesException('Request failed with no specified error.');
    }
    
     public static void handleUpsertResults(MetadataService.UpsertResult upsertResult)
    {
        // Nothing to see?
        if(upsertResult==null || upsertResult.success)
            return;
        // Construct error message and throw an exception
        if(upsertResult.errors!=null)
        {
            List<String> messages = new List<String>();
            messages.add(
                (upsertResult.errors.size()==1 ? 'Error ' : 'Errors ') +
                    'occured processing component ' + upsertResult.fullName + '.');
            for(MetadataService.Error error : upsertResult.errors)
                messages.add(
                    error.message + ' (' + error.statusCode + ').' +
                    ( error.fields!=null && error.fields.size()>0 ?
                        ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );
            if(messages.size()>0)
                throw new MetadataServiceExamplesException(String.join(messages, ' '));
        }
        if(!upsertResult.success)
            throw new MetadataServiceExamplesException('Request failed with no specified error.');
    }
}

 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Rahul Gupta,

May I suggest you please check "Test Class Generator for Salesforce
which will be helpful.
hope it helps.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar