Here i am tried to write a test class for importing csv file.THere is no code coverage.Plese help me on this.
public class adminPriorSale {
public list<Distributor_Prior_Sales__c> accon{get;set;}
public Distributor_Prior_Sales__c agnt{get;set;}
public Integer PriorSale{set;get;}
public id tobeEdited{get; set;}
public string deleteid{get;set;}
public boolean editSection {get;set;}
public ApexPages.StandardController stand;
Public String retURL{get;set;}
//To import file
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public adminPriorSale(apexpages.standardcontroller controller )
{
retURL = ApexPages.currentPage().getParameters().get('retURL');
this.stand=controller;
tobeEdited = controller.getId();
agnt = new Distributor_Prior_Sales__c();
accon = New List<Distributor_Prior_Sales__c>();
accon= [select id,Name,SMS_sales_amount__c,SMS_Year__c,Customer__c,Calculation_Type__c from Distributor_Prior_Sales__c];
csvFileLines = new String[]{};
}
public PageReference EdittheSection()
{
PageReference ref=new PageReference('/apex/adminPriorSale1?retURL=/apex/Admin');
ref.setRedirect(true);
update accon;
return ref;
}
private void LoadData()
{
accon= [select id,Name,SMS_sales_amount__c,SMS_Year__c,Customer__c,Calculation_Type__c from Distributor_Prior_Sales__c WHERE (owner.id =: userinfo.getuserid())];
}
// Import CSV file to list of record
public void importCSVFile(){
try{
csvAsString = csvFileBody.toString();
csvFileLines = csvAsString.split('\n');
for(Integer i=0;i<csvFileLines.size();i++){
Distributor_Prior_Sales__c prisale = new Distributor_Prior_Sales__c () ;
string[] csvRecordData = csvFileLines[i].split(',');
prisale.SMS_Year__c= csvRecordData[0] ;
prisale.SMS_sales_amount__c= Decimal.valueof(csvRecordData[1]);
prisale.Customer__c= csvRecordData[2];
prisale.Calculation_Type__c = csvRecordData[3];
accon.add(prisale);
}
upsert accon;
}
catch (Exception e)
{
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,e.getMessage());
ApexPages.addMessage(errorMessage);
}
}
public void save1()
{
update accon;
}
public void DeleteSale()
{
Distributor_Prior_Sales__c del_task=new Distributor_Prior_Sales__c(id=deleteid);
delete del_task;
accon=[select id,Name,SMS_sales_amount__c,SMS_Year__c,Customer__c,Calculation_Type__c from Distributor_Prior_Sales__c];
}
public pagereference saveRecord()
{
List<Distributor_Prior_Sales__c> discLocal=accon.clone() ;
List<Distributor_Prior_Sales__c> discLocal2=new List<Distributor_Prior_Sales__c>(accon);
integer i=0;
for(Distributor_Prior_Sales__c sale:accon){
if(sale.SMS_sales_amount__c==null && sale.SMS_Year__c ==null && sale.Customer__c ==null && sale.Calculation_Type__c==null )
{ discLocal2.remove(i); }
else if(sale.SMS_sales_amount__c==null || sale.SMS_Year__c ==null || sale.Customer__c ==null || sale.Calculation_Type__c==null )
{
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'You must enter all required fields');
ApexPages.addMessage(msg);
return null;
}
i++;
}
upsert discLocal2;
PageReference pageRef = new PageReference('/apex/adminPriorSale?retURL=/apex/Admin');
pageref.setRedirect(true);
return pageref;
}
public PageReference cancel()
{
PageReference pg1=new PageReference('/apex/adminPriorSale?retURL=/apexAdmin');
pg1.setRedirect(true);
return pg1;
}
public void addrow() {
Distributor_Prior_Sales__c proSale = new Distributor_Prior_Sales__c();
proSale.SMS_sales_amount__c= (agnt.SMS_sales_amount__c);
proSale.SMS_Year__c =(agnt.SMS_Year__c );
proSale.Customer__c=(agnt.Customer__c);
proSale.Calculation_Type__c=(agnt.Calculation_Type__c);
accon.add(proSale );
}
}
And it test class is
@isTest(seeAllData=true)
private class test_adminPriorSale {
static testMethod void test_adminPriorSale()
{
Distributor_Prior_Sales__c t1= new Distributor_Prior_Sales__c ();
t1.Name='test1';
t1.SMS_sales_amount__c =123;
t1.SMS_Year__c='2012';
insert t1;
String csvContent = 'test1',123,'2012','I Architects','Estimated Prior sales';
PageReference pageRef = Page.adminPriorSale; // Adding VF page Name here
pageRef.getParameters().put('id', String.valueOf(t1.id));//for page reference
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('RecordType','adminPriorSale');
ApexPages.StandardController sc = new ApexPages.standardController(t1);
adminPriorSale controller = new adminPriorSale(sc);
controller.save1();
controller.saveRecord();
controller.addrow();
controller.DeleteSale();
controller.editSection = true;
controller.EdittheSection();
//controller.LoadData();
controller.cancel();
controller.importCSVFile();
}
static testMethod void test_adminPriorSale2()
{
Distributor_Prior_Sales__c t1= new Distributor_Prior_Sales__c ();
t1.Name='test1';
//t1.SMS_sales_amount__c =123;
//t1.SMS_Year__c='2014';
insert t1;
Distributor_Prior_Sales__c t2= new Distributor_Prior_Sales__c ();
t2.Name='test1';
t2.SMS_sales_amount__c =123;
t2.SMS_Year__c='2014';
insert t2;
PageReference pageRef = Page.adminPriorSale; // Adding VF page Name here
pageRef.getParameters().put('id', String.valueOf(t1.id));//for page reference
pageRef.getParameters().put('retURL', String.valueOf(t1.id));//for page reference
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('RecordType','adminPriorSale');
ApexPages.StandardController sc = new ApexPages.standardController(t1);
adminPriorSale controller = new adminPriorSale(sc);
controller.saveRecord();
controller.cancel();
}
static testMethod void test_adminPriorSale3()
{
Distributor_Prior_Sales__c t1= new Distributor_Prior_Sales__c ();
t1.Name='test1';
//t1.SMS_sales_amount__c =123;
//t1.SMS_Year__c='2014';
insert t1;
PageReference pageRef = Page.adminPriorSale; // Adding VF page Name here
pageRef.getParameters().put('id', String.valueOf(t1.id));//for page reference
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('RecordType','adminPriorSale');
ApexPages.StandardController sc = new ApexPages.standardController(t1);
adminPriorSale controller = new adminPriorSale(sc);
controller.cancel();
controller.editSection = true;
controller.EdittheSection();
}
}
It is giving 64% code coverage.not covering the importing csv file part.someone please help me on this.