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
Rahavan Arumugam AlameluRahavan Arumugam Alamelu 

Test class for csv upload functionality with comma included in the column value

Hi All, 

Can someone help me in writing the test class to cover the scenraio of uploading the data from the csv file. Few columns has the comma within their values.

Below is my code:
public class fileUpload
{
    public Boolean btndis
    {
        get;set;
    }
    public Blob csvFileBody
    {
        get;set;
    }
    public string csvAsString
    {
        get;set;
    }
    public list<Wrapper> wrapList
    {
        get;set;
    }
    public Map<string, List<Wrapper>> wrapmap
    {
        get;set;
    }
    public string[] FileLines = new string[]{};
    public set<string> StoreName = new  set<string>();
    public List<Store__c> StoreList = new  List<Store__c>();
    public Map<string, Id> StoreMap = new  Map<string, Id>();
    public List<Analyst_Schedule__c> AnalystList = new  List<Analyst_Schedule__c>();
    public List<Analyst_Schedule__c> FinalAnalystList = new  List<Analyst_Schedule__c>();
    Integer startIndex, endIndex;
    
    public CSVUpload(ApexPages.StandardController controller)
    {
    }
    
    public class Wrapper
    {
        public string AnalystName
        {
            get;set;
        }
        public string StoreName
        {
            get;set;
        }
        public Date StartDate
        {
            get;set;
        }
        public Date EndDate
        {
            get;set;
        }
        public string ScheduleConfirmed
        {
            get;set;
        }
        public string comments
        {
            get;set;
        }
        public string Activity
        {
            get;set;
        }
    }
    public void ImportSchedule()
    {
        try
        {
            if(csvFileBody == null)
            {
                ApexPages.Message Message1 = new  ApexPages.Message(ApexPages.severity.INFO, 'Please attach Attendees List');
                ApexPages.addMessage(Message1);
            }
            else
            {
                btndis = true;
                wrapList = new  List<Wrapper>();
                wrapmap = new  map<string, List<Wrapper>>();
                csvAsString = csvFileBody.toString();
                FileLines = csvAsString.split('\n');
                system.debug('file size is ' + FileLines.size());
                if(FileLines.size() > 1)
                {
                    system.debug('inside line 57 loop');
                    string[] csvRecordDataB = FileLines[0].split(',');
                    if(csvRecordDataB[0].contains('Chain') && csvRecordDataB[1].contains('Location') && csvRecordDataB[2].contains('Schedule Confirmed') && csvRecordDataB[3].contains('Region') && csvRecordDataB[4].contains('Analyst') && csvRecordDataB[5].contains('Activity') && csvRecordDataB[6].contains('Comments') && csvRecordDataB[7].contains('Start Date') && csvRecordDataB[8].contains('End Date'))
                    {
                        system.debug('FileLines size: ' + FileLines.size());
                        for(Integer i = 1; i < FileLines.size(); i++)
                        {
                            if(FileLines[i].replaceAll(',', '').trim().length() == 0)
                            {
                                break;
                            }
                            while (FileLines[i].indexof('"') > -1)
                            {
                                if(startIndex == null)
                                {
                                    startIndex = FileLines[i].indexof('"');
                                    FileLines[i] = FileLines[i].subString(0, startIndex) + ':quotes:' + FileLines[i].subString(startIndex + 1, FileLines[i].length());
                                }
                                else
                                {
                                    if(endIndex == null)
                                    {
                                        endIndex = FileLines[i].indexof('"');
                                        FileLines[i] = FileLines[i].subString(0, endIndex) + ':quotes:' + FileLines[i].subString(endIndex + 1, FileLines[i].length());
                                    }
                                }
                                if(startIndex != null && endIndex != null)
                                {
                                    string sub = FileLines[i].substring(startIndex, endIndex);
                                    sub = sub.replaceall(',', ':comma:');
                                    FileLines[i] = FileLines[i].substring(0, startIndex) + sub + FileLines[i].substring(endIndex, FileLines[i].length());
                                    startIndex = null;
                                    endIndex = null;
                                }
                            }
                            string[] csvRecordData = FileLines[i].split(',');
                            Wrapper wrap = new  Wrapper();
                            wrap.StoreName = csvRecordData[1].replaceAll(':quotes:', '').replaceAll(':comma:', ',');                         
                            wrap.AnalystName = csvRecordData[4];
                            wrap.Activity = csvRecordData[5].replaceAll(':quotes:', '').replaceAll(':comma:', ',');
                            wrap.Comments = csvRecordData[6].replaceAll(':quotes:', '').replaceAll(':comma:', ',');
                            string[] startDate = string.valueof(csvRecordData[7].trim()).split('/');
                            wrap.StartDate = Date.parse(startDate[0] + '/' + startDate[1] + '/' + startDate[2]);
                            string[] endDate = string.valueof(csvRecordData[8].trim()).split('/');
                            wrap.EndDate = Date.parse(endDate[0] + '/' + endDate[1] + '/' + endDate[2]);                   
                            if(wrapMap.containsKey(wrap.StoreName))
                            {
                                wrapmap.get(wrap.StoreName).add(wrap);
                            }
                            else
                            {
                                wraplist.add(wrap);
                                wrapMap.put(wrap.StoreName, new  List<Wrapper>(wraplist));
                                wraplist.clear();
                            }
                            StoreName.add(wrap.StoreName);
                            system.debug(wrapMap.size());
                            system.debug(wrapMap.values());
                        }
                        StoreList = [SELECT id,Name FROM Store__c WHERE Name = : StoreName];
                        for(Store__c s: StoreList)
                        {
                            StoreMap.put(s.Name, s.Id);
                            system.debug(StoreMap.values());
                        }
                        for(string StoreName: wrapMap.keyset())
                        {
                            for(Wrapper wr: wrapMap.get(StoreName))
                            {
                                Analyst_Schedule__c An = new  Analyst_Schedule__c();
                                An.Name = wr.AnalystName;
                                An.Store__c = StoreMap.get(wr.StoreName);
                                An.Comments__c = wr.comments;
                                An.Start_Date__c = wr.StartDate;
                                An.End_Date__c = wr.EndDate;
                                An.Activity__c = wr.Activity;
                                AnalystList.add(An);
                            }
                            FinalAnalystList.addall(AnalystList);
                            AnalystList.clear();
                        }
                        if(FinalAnalystList.size() > 0)
                        {
                            Insert FinalAnalystList;
                            ApexPages.Message Message2 = new  ApexPages.Message(ApexPages.severity.CONFIRM, 'Analysts Data Imported successfully');
                            ApexPages.addMessage(Message2);
                        }
                    }
                    else
                    {
                        btndis = true;
                        ApexPages.Message Message5 = new  ApexPages.Message(ApexPages.severity.ERROR, 'Template format mismatch. Please use this <a href="https://hon-pmt--fpsrs--c.cs7.content.force.com/sfc/servlet.shepherd/version/download/068M0000000LqrH?asPdf=false&operationContext=CHATTER" target="_blank"><font color="darkblue"><b>template</b></font></a> to upload the users.');
                        ApexPages.addMessage(Message5);
                    }
                }
                else
                {
                    btndis = true;
                    ApexPages.Message Message3 = new  ApexPages.Message(ApexPages.severity.INFO, 'Uploaded file is Empty');
                    ApexPages.addMessage(Message3);
                }
            }
        }
        catch(Exception ex)
        {
            ApexPages.Message Message7 = new  ApexPages.Message(ApexPages.severity.ERROR, 'You have encountered an error. Please );
        }
    }
}
 
cvuyyurucvuyyuru
Do you have a sample data of this CSV File?
That would be helpful in writing a test class.
Rahavan Arumugam AlameluRahavan Arumugam Alamelu
Sure.. attached the sample date..

I like the test code to cover the below lines especially:

   if(FileLines[i].replaceAll(',', '').trim().length() == 0)
                            {
                                break;
                            }
                            while (FileLines[i].indexof('"') > -1)
                            {
                                if(startIndex == null)
                                {
                                    startIndex = FileLines[i].indexof('"');
                                    FileLines[i] = FileLines[i].subString(0, startIndex) + ':quotes:' + FileLines[i].subString(startIndex + 1, FileLines[i].length());
                                }
                                else
                                {
                                    if(endIndex == null)
                                    {
                                        endIndex = FileLines[i].indexof('"');
                                        FileLines[i] = FileLines[i].subString(0, endIndex) + ':quotes:' + FileLines[i].subString(endIndex + 1, FileLines[i].length());
                                    }
                                }
                                if(startIndex != null && endIndex != null)
                                {
                                    string sub = FileLines[i].substring(startIndex, endIndex);
                                    sub = sub.replaceall(',', ':comma:');
                                    FileLines[i] = FileLines[i].substring(0, startIndex) + sub + FileLines[i].substring(endIndex, FileLines[i].length());
                                    startIndex = null;
                                    endIndex = null;
                                }
                            }
                            string[] csvRecordData = FileLines[i].split(',');
                            Wrapper wrap = new  Wrapper();
                            wrap.StoreName = csvRecordData[1].replaceAll(':quotes:', '').replaceAll(':comma:', ',');                         
                            wrap.AnalystName = csvRecordData[4];
                            wrap.Activity = csvRecordData[5].replaceAll(':quotes:', '').replaceAll(':comma:', ',');
                            wrap.Comments = csvRecordData[6].replaceAll(':quotes:', '').replaceAll(':comma:', ',');
                            string[] startDate = string.valueof(csvRecordData[7].trim()).split('/');
                            wrap.StartDate = Date.parse(startDate[0] + '/' + startDate[1] + '/' + startDate[2]);
                            string[] endDate = string.valueof(csvRecordData[8].trim()).split('/');
                            wrap.EndDate = Date.parse(endDate[0] + '/' + endDate[1] + '/' + endDate[2]);                   
                            if(wrapMap.containsKey(wrap.StoreName))
                            {
                                wrapmap.get(wrap.StoreName).add(wrap);
                            }
                            else
                            {
                                wraplist.add(wrap);
                                wrapMap.put(wrap.StoreName, new  List<Wrapper>(wraplist));
                                wraplist.clear();
 
Rahavan Arumugam AlameluRahavan Arumugam Alamelu

User-added image
Rahavan Arumugam AlameluRahavan Arumugam Alamelu
Hi cvuyyuru,

Can you please help me on this

thanks