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
Akshay MhetreAkshay Mhetre 

test class having wrapper class...please Help

Hello Everyone,I need help. I have a class who has wrapper too. Confused.

Main Class:::

public without sharing class DownloadAllFilesController {
    public static Per_ResponseWrapper response = new Per_ResponseWrapper();
    @AuraEnabled
    public static Per_ResponseWrapper getAllContentDocumentIds(String recordId){
        system.debug('recordId is :::: ' + recordId);
        try{
            Set<Id> setDocumentChecklistIds = new Set<Id>();
            Set<Id> setCaseIds = new Set<Id>();
             for(Case subCase: [select Id,Document_Checklist__c from Case 
                               where ParentId =:recordId and RecordType.Name in ('RCU Document Check','RCU Profile Check Off')]){
                                   setDocumentChecklistIds.add(subCase.Document_Checklist__c);
                                   setDocumentChecklistIds.add(subCase.Id);
                               }
            String ContentdocumentIds = '';
            if(setDocumentChecklistIds.size() >0){
                List<ContentDocumentLink> contentDocumentLinkDatafrmCase = [select id, ContentdocumentId, LinkedEntityId from ContentDocumentLink 
                                                                            where LinkedEntityId IN : setDocumentChecklistIds WITH SECURITY_ENFORCED];
                System.debug('contentDocumentLinkDatafrmCase: '+contentDocumentLinkDatafrmCase.size());
                if(contentDocumentLinkDatafrmCase.size() > 0){  
                    for(integer i=0; i < contentDocumentLinkDatafrmCase.size(); i++){                    
                        ContentdocumentIds += '/' + contentDocumentLinkDatafrmCase[i].ContentdocumentId;  
                    }
                }
            }
            response.success = true;
            response.data = ContentdocumentIds;                   
        }
        catch(Exception e)
        {
            system.debug('exception:' + e.getMessage());
            response.success = false;
            response.message = e.getMessage();
        }
        system.debug('Content Document ID is:::: ' + response);
        return response;
    }
}

Wrapper Class:::

public with sharing class Per_ResponseWrapper {
    
    @AuraEnabled public String message {get; set;}
    @AuraEnabled public Boolean success {get; set;}
    @AuraEnabled public object data {get; set;}    
    @AuraEnabled public Boolean hasEditAccess {get; set;}
    @AuraEnabled public String lastModifiedBy {get; set;}    

    public class LabelValueWrapper{
        @AuraEnabled public Object label;
        @AuraEnabled public Object value;
        @AuraEnabled public Object optionKey;
        @AuraEnabled public Object subQuestionID;

        public LabelValueWrapper(Object newLabel, Object newValue){
            label = newLabel;
            value = newValue;
        }
        public LabelValueWrapper(Object newLabel, Object newValue, Object newKey){
            label = newLabel;
            value = newValue;
            optionKey = newKey;
        }
        public LabelValueWrapper(Object newLabel, Object newValue, Object newKey, Object newSubQuestionID){
            label = newLabel;
            value = newValue;
            optionKey = newKey;
            subQuestionID = newSubQuestionID;
        }
    }

    
    public static List<FieldWrapper> getFieldSet(String fieldSetName, String objectName) {
        List<FieldWrapper> lstfieldWrapper = new List<FieldWrapper>();
        if (String.isNotBlank(fieldSetName) && String.isNotBlank(objectName)) {
            Schema.DescribeSObjectResult describeSObjectResult = Schema.getGlobalDescribe().get(objectName).getDescribe();
            Map<String,Schema.SObjectField> objFieldMap = describeSObjectResult.fields.getMap();
            Schema.FieldSet fieldSetObj = describeSObjectResult.FieldSets.getMap().get(fieldSetName);
            if (fieldSetObj != null) {
                for(Schema.FieldSetMember fieldSet : fieldSetObj.getFields() ) {
                    System.debug(fieldSet);
                    lstfieldWrapper.add( new FieldWrapper(
                        String.ValueOf(fieldSet.getLabel()+'$$'+String.valueOf(fieldSet.getType())),
                        String.ValueOf(fieldSet.getFieldPath()), 
                        (fieldSet.getDBRequired() || fieldSet.getRequired())
                    ));
                }
            }
        }
        return lstfieldWrapper; 
    }

    @AuraEnabled
    public static Per_ResponseWrapper getPicklistValues(Id recordId, List<String> fieldApiNames){
        Per_ResponseWrapper response = new Per_ResponseWrapper();
        List<Per_ResponseWrapper.LabelValueWrapper> fieldPicklistValueList = new List<LabelValueWrapper>();
        Schema.DescribeSObjectResult describeSObjectResult = recordId.getSobjectType().getDescribe();
        Map<String,Schema.SObjectField> objFieldMap = describeSObjectResult.fields.getMap();
        for(String fieldName : objFieldMap.keySet()){
            if(fieldApiNames.contains(fieldName)){
                List<Schema.PicklistEntry> picklistValues = objFieldMap.get(fieldName).getDescribe().getPicklistValues();
                fieldPicklistValueList.add(new Per_ResponseWrapper.LabelValueWrapper(fieldName, picklistValues));
            }   
        }
        response.success = true;
        response.data = fieldPicklistValueList;
        return response;
    }
    
    public class FieldWrapper {
        @AuraEnabled public String fieldName {get;set;}
        @AuraEnabled public String fieldLabel {get;set;}
        @AuraEnabled public String dataType {get;set;}
        @AuraEnabled public Boolean isRequired {get;set;}
        
        public FieldWrapper(String fieldLabel,String fieldName,Boolean isRequired ) {
            this.fieldLabel  = fieldLabel;
            this.fieldName   = fieldName;
            this.isRequired  = isRequired;
        }
    }
}

Best Answer chosen by Akshay Mhetre
CharuDuttCharuDutt
Hii Akshay Mhetre
Try Below Code
@isTest
public class DownloadAllFilesControllerTest {
@isTest
    public Static Void unittTest(){
        String base64Data = EncodingUtil.urlDecode('Test base64Data For testing', 'UTF-8');
        
        Case c = new case();
        c.Status = 'New';
        c.Origin = 'Phone';
        c.Document_Checklist__c = true;
        c.RecordType = 'RCU Document Check';
        Insert c;
        Case c1 = new case();
        c1.Status = 'New';
        c1.Origin = 'Phone';
        c1.ParentId = c.Id;
        c.Document_Checklist__c = true;
        c.RecordType = 'RCU Document Check';
        Insert c1;
        
        ContentVersion cv = new ContentVersion();
        cv.Title = 'fileName';
        cv.PathOnClient = 'fileName.pdf';
        cv.VersionData =   EncodingUtil.base64Decode(base64Data); 
        cv.IsMajorVersion = true; 
        
        insert cv;
        system.debug('cv  : ' + cv );
        
        Id conDocId = [SELECT id,ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
        system.debug('conDocId : ' + conDocId);
        
        
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = c.Id;
        cdl.ContentDocumentId = conDocId;
        cdl.shareType = 'V';
        
        insert cdl; 
        Per_ResponseWrapper prw = new Per_ResponseWrapper();
        prw.hasEditAccess = true;
        prw.lastModifiedBy = '19/06/2021';

        DownloadAllFilesController.getAllContentDocumentIds(c.Id);
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 

All Answers

CharuDuttCharuDutt
Hii Akshay Mhetre
Try Below Code
@isTest
public class DownloadAllFilesControllerTest {
@isTest
    public Static Void unittTest(){
        String base64Data = EncodingUtil.urlDecode('Test base64Data For testing', 'UTF-8');
        
        Case c = new case();
        c.Status = 'New';
        c.Origin = 'Phone';
        c.Document_Checklist__c = true;
        c.RecordType = 'RCU Document Check';
        Insert c;
        Case c1 = new case();
        c1.Status = 'New';
        c1.Origin = 'Phone';
        c1.ParentId = c.Id;
        c.Document_Checklist__c = true;
        c.RecordType = 'RCU Document Check';
        Insert c1;
        
        ContentVersion cv = new ContentVersion();
        cv.Title = 'fileName';
        cv.PathOnClient = 'fileName.pdf';
        cv.VersionData =   EncodingUtil.base64Decode(base64Data); 
        cv.IsMajorVersion = true; 
        
        insert cv;
        system.debug('cv  : ' + cv );
        
        Id conDocId = [SELECT id,ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
        system.debug('conDocId : ' + conDocId);
        
        
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = c.Id;
        cdl.ContentDocumentId = conDocId;
        cdl.shareType = 'V';
        
        insert cdl; 
        Per_ResponseWrapper prw = new Per_ResponseWrapper();
        prw.hasEditAccess = true;
        prw.lastModifiedBy = '19/06/2021';

        DownloadAllFilesController.getAllContentDocumentIds(c.Id);
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
This was selected as the best answer
BharatPM YojanaBharatPM Yojana
You have shared a very good PHP Code. PM Modi Yojana (https://bharatpmyojna.in/)