How to write an Apex Test Class for Importing a CSV File?

2.7k views Asked by At

Hello! I am unable to test the written apex class.. Could you help me with this task?

Through the component, we transfer the ID of the CSV file sent to the server to the apex class, after which our apex class creates stations if there were none before, and also updates the list of sensors based on the records of the CSV file (insert/ update).

Controller Class

public inherited sharing class lwnReadCsvFileController {

    @AuraEnabled
    public static list<Sensor__c> readCSVFile(Id idContentDocument){
        list<Sensor__c> lstSensToInsert = new list<Sensor__c>();
        if(idContentDocument != null) {
            
            // getting File Data based on document id 
            ContentVersion objVersion = [SELECT Id, VersionData FROM ContentVersion WHERE ContentDocumentId =:idContentDocument];
            // split the file data
            list<String> lstCSVLines = objVersion.VersionData.toString().split('\n');

            //creating Basic Stations records
            List<Base_Station__c> createdBSRec = [SELECT Name, Id  From Base_Station__c];
            List<Base_Station__c> newBSRec = new List<Base_Station__c>();
            Set<String> uniqueSet = new Set<String>();
            for ( Integer i = 1; i < lstCSVLines.size(); i++ ) {
                integer coincidences = 0; 
                list<String> csvRowData = lstCSVLines[i].split(',');
                for ( Base_Station__c rec : createdBSRec ) {
                    if (csvRowData[0] == rec.Name) {
                        coincidences++;
                    }
                }
                if ( coincidences == 0 ) {
                    uniqueSet.add(csvRowData[0]);
                }
            }
            List<String> uniquelist = new List<String>(uniqueSet);
            for (integer i = 0; i < uniquelist.size() ; i++) {
                Base_Station__c newBS = new Base_Station__c(Name = uniquelist[i]);
                NewBSRec.add(newBS);
            }
            upsert newBSRec;
            

            //creating Sensor records
            for(Integer i = 1; i < lstCSVLines.size(); i++){
                Sensor__c objRec = new Sensor__c();
                list<String> csvRowData = lstCSVLines[i].split(',');
                string tempBase = csvRowData[0];
                objRec.Name = csvRowData[1];
                objRec.Base_Station__c = [SELECT Id From Base_Station__c Where Name = :tempBase ].id;
                objRec.Sensor_ID__c = integer.valueof(csvRowData[1]);
                objRec.Status__c = csvRowData[2];
                objRec.Sensor_Model__c = csvRowData[3];
                lstSensToInsert.add(objRec);
            }
                if(!lstSensToInsert.isEmpty()) {
                    List<Sensor__c> createdSenRec = [SELECT Name, Sensor_ID__c  From Sensor__c];
                    for (Sensor__c sens : lstSensToInsert ) {
                        integer coincidences = 0;
                        for (Sensor__c rec : createdSenRec ) {
                            if (sens.Sensor_ID__c == rec.Sensor_ID__c) {
                                sens.id = rec.id;
                                update sens;
                                coincidences++;
                            }
                        }
                        if ( coincidences == 0 ) {
                            insert sens;
                        }
                    }
                }
        }
        return lstSensToInsert;    
    }
}

Test Class 100% coverage

@isTest
public class IwnReadCsvFileControllerTest {    
    public static String str = 'BASE STATION,SENSOR ID,STATUS,SENSOR MODEL \n' +
        'Leeds,1,Enabled ,R8 \n' +
        'Glasgow Central,2,Enabled,R8';
@isTest
    public static void testReadCSVFile(){
    Base_Station__c newBS = new Base_Station__c(Name = 'Leeds');
    upsert newBS;
    Sensor__c newSensor = new Sensor__c (Name = '1', Sensor_Id__c = 1, Status__c = 'Enabled', Base_Station__c = newBs.id);
    insert newSensor;
    ContentVersion contentVersionInsert = new ContentVersion(
            Title = 'Test',
            PathOnClient = 'Test.csv',
            VersionData = Blob.valueOf(str),
            IsMajorVersion = true
    );
    insert contentVersionInsert;
    Id getId = [Select ContentDocumentId From ContentVersion Where Id =:contentVersionInsert.id and isLatest=true].ContentDocumentId;
    List<Sensor__c> result = lwnReadCsvFileController.readCSVFile(getId);
}   
}
1

There are 1 answers

1
David Reed On BEST ANSWER

Your unit test class is passing a ContentVersion Id:

List<Sensor__c> result = lwnReadCsvFileController.readCSVFile(contentVersionInsert.Id);

but your class is treating this Id as a ContentDocument Id:

public static list<Sensor__c> readCSVFile(Id idContentDocument){
    if(idContentDocument != null) {
        ContentVersion objVersion = [
            SELECT Id, VersionData 
            FROM ContentVersion 
            WHERE ContentDocumentId =:idContentDocument
        ];

Your test class needs to query for the ContentDocumentId of the newly-inserted ContentVersion and pass that Id into your class under test.