+ Start a Discussion
Adil_SFDCAdil_SFDC 

Increase test code coverage from 43 to 75

Here is my code and test method. 

 

It has to increase from 43 to 75.

the dosearch() and sendemailcreatetask() methods are not covered.

Any idea how to cover those

 

 

public with sharing class ContactiCenteraIntegration
{
public String documentSearchString {get; set;}
public Boolean isInDocument {get;set;}

public List<SelectOption> productList {get; set;}
public String productVal {get; set;}
public String sortField {get; set;}
public String previousSortField {get; set;}
public string docIDString{get;set;} 
public List<String> docIdsList = new List<String>();
public List<SelectOption> solutionList {get; set;}
public String solutionVal {get; set;}

public String apiServerURL {get; set;}
public String apiServerSession {get; set;}
public String description {get; set;} 
//public list<HelpText__c> custom {get; set;}

public List<Document> documentList {get; set;}
public string selecttitle {get; set;}
public Boolean showPrevious {get; set;}
public Boolean showNext {get; set;}
public Integer firstRecPosition {get; set;}


private String email {get; set;}
private String resBody {get; set;}

private Contact c;
public Integer pos {get; set;}
public Integer rowLimit {get; set;}
public String isDynamic {get; set;}
public String iscustomized {get; set;}
private String tokenVal = '';
private String testVar = '';

public ContactiCenteraIntegration(ApexPages.StandardController sc)
{

c = (Contact)sc.getRecord();
documentList = null;
c = [select id, name, email from Contact where id = :c.id];

showPrevious = showNext = false;
rowLimit = 20;
documentSearchString = ''; 

}

public void search()
{
if(documentList != null)
documentList.clear();
HttpRequest req = new HttpRequest();
//379 QTM
//364 TR 
//12 Apps
//326 
String fedId = [SELECT FederationIdentifier FROM User WHERE Id =: UserInfo.getUserId()].FederationIdentifier;
system.debug('fedid:::'+fedId);
String endPoint = 'http://api.icentera.com/v1sandbox/AuthenticationService.svc/json/authenticateSFSession?siteid=12&username=clesinski@icentera.com&sessionid='+apiServerSession+'&instanceUrl='+apiServerURL;//https://icentera-4.na2.visual.force.com/services/Soap/u/8.0/00D300000000PEv';//'http://api.icentera.com/v1/AuthenticationService.svc/json/authenticateSFSession?siteid=14&uid=10940&sessionid='+UserInfo.getSessionId()+'&instanceUrl=https://icentera-4.na2.visual.force.com/services/Soap/u/8.0/00D300000000PEv';//https://login.salesforce.com/services/Soap/u/23.0';//'http://api.icentera.com/iCenteraService/AuthenticationService.svc/json/authenticate?siteid='+UserInfo.getOrganizationId()+'&uid='+UserInfo.getUserId()+'&sessionid='+UserInfo.getSessionId()+'&instanceUrl=https://na2.salesforce.com/';//'https://api.icentera.com/iCenteraService/TestService.svc/json/AuthenticateSFSession?siteid='+UserInfo.getOrganizationId()+'&uid='+UserInfo.getUserId()+'&sessionid='+UserInfo.getSessionId(); 
req.setMethod('GET');
HTTPResponse res;

// system.debug('API URL is :::: '+apiServerURL);

req.setEndpoint(endPoint);
req.setTimeout(60000);

Http http = new Http();
if(testVar == '')
{
res = http.send(req);
resBody = res.getBody();
}
else
resBody = '{"d":"c577d54db33f416ab2e28b2ae00098b8"}';

JSONParser parser = JSON.createParser(resBody);
tokenVal = '';
while ( parser != null && parser.nextToken() != null) 
{
system.debug('PRINT'+parser.getText());
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'd')) 

{

if (parser.nextToken() != null ) {
tokenVal = parser.getText();

// break;
}

}
} 
if (tokenVal != '') {
req = new HttpRequest();
endPoint = 'http://api.icentera.com/v1sandbox/TestService.svc/json/searchDocuments?token='+tokenVal+'&search='+documentSearchString+'&sensitivity=1&page=1&productIds=&solutionIds=';
system.debug('XXXX'+documentSearchString);
req.setMethod('GET');
req.setEndpoint(endPoint);
req.setTimeout(60000);

http = new Http();
if(testVar == '')
{
res = http.send(req);
resBody = res.getBody();
}
else
resBody = '{"d":{"__type":"DocumentSearchResult:#iCenteraServices","Documents":[{"__type":"SimpleDocument:#iCenteraServices","CustomerReady":true,"Description":null,"FileType":"xls","Id":1042,"InternalOnly":false,"NDARequired":false,"PartnerReady":true,"Rating":4,"Title":"ROI Worksheet - REVENUE VERSION","URL":""}]}}';

parser = JSON.createParser(resBody);

system.debug('callingcreate');
createDocuments(parser);
sortField = previousSortField = null;
} 
else 
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Unable to create a Session ')); 

}
public List<Document> defaultSortdocIds(List<Document> OdocList ){
List<String> docId = new List<String>();
List<String> finaldocId = new List<String>();
List<Document> tempdoc = new List<Document>(); 
for(Document d : OdocList){

docId.add(d.id);
}
docId.sort();

for(Integer i=docId.size()-1;i>=0;i--){

finaldocId.add(docId.get(i));
}



for(String s : finaldocId){
for(Document d : OdocList){

if(d.id==s){
tempdoc.add(d);
}

}

}
return tempdoc;
}

public void createDocuments(JSONParser parser)
{
system.debug('1. Printing debug log'); 

documentList = new List<Document>();

// system.debug('rrrr'+documentList.size());
Document doc = new Document();
Integer position = 0;
while (parser.nextToken() != null) 
{
system.debug('1.1 Printing debug log inside while loop'); 
if (parser.getCurrentToken() == JSONToken.START_OBJECT || parser.getCurrentToken() ==JSONToken.END_OBJECT) 
{ 
if(doc.title != null)// && doc.url != null && doc.rating != null && doc.filetype != null && doc.id != null && doc.description != null)
{
doc.position = position;
position++;
documentList.add(doc);

doc = new Document();
} 

}
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) 
{
system.debug('1.2 Printing debug log'); 
if(parser.getText() == 'title')
{
parser.nextToken();
doc.title = parser.getText();
docIdsList.add(doc.title);
}
if(parser.getText() == 'id')
{
parser.nextToken();
doc.id = parser.getText();
// docIdsList.add(doc.id);
}
if(parser.getText() == 'description')
{
parser.nextToken();
doc.description = parser.getText();
}
if(parser.getText() == 'url')
{
parser.nextToken();
doc.url = parser.getText();
// system.debug('URL is '+doc.URL);
}
if(parser.getText() == 'filetype')
{
parser.nextToken();
doc.fileType = parser.getText();
}
if(parser.getText() == 'rating')
{
parser.nextToken();
doc.rating = parser.getText(); 
}
system.debug('3. Printing debug log'); 
if(parser.getText() == 'lastupdated')
{
parser.nextToken();
doc.lastupdated = parser.getText(); 
doc.st = ((doc.lastupdated.split('\\.',2).get(0)).split('T',2)).get(0)+' '+((doc.lastupdated.split('\\.',2).get(0)).split('T',2)).get(1); 
doc.st = doc.st.substring(0, doc.st.indexOf(' '));
system.debug('2. Printing debug log'); 
system.debug('Doc Date:'+doc.st);

}

if(parser.getText() == 'totalViews')
{
parser.nextToken();
doc.internalOnly = Boolean.valueOf(parser.getText()); 
}

/* if(doc.title != null)// && doc.url != null && doc.rating != null && doc.filetype != null && doc.id != null && doc.description != null)
{
doc.position = position;
position++;
documentList.add(doc);

doc = new Document();
}*/

}
}
if(documentList!=null){
sortField = 'st';
previousSortField='st';
dosort();
system.debug('mmmm'+documentList.size());

// documentList = defaultSortdocIds(documentList);
system.debug('DDDDD'+documentList.size());
}
 


if(documentList.isEmpty())
{

documentList = null;
showPrevious = showNext = false;

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'No results were found for the search string \''+documentSearchString+'\'.'));
}
else if(documentList.size() <= rowLimit)
{
showPrevious = showNext = false;
}
else 
{ 
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Document List '+rowLimit+' of '+documentList.size())); 
showNext = true;
showPrevious = false ;

}
firstRecPosition = 0;
}




public void populateDocString(){

String title='';
Integer numberOfSelected=0;
if(documentList!=null && documentList.size()>0)
{
for(Document d: documentList)
{


if(d.isChecked)
{
numberOfSelected++;
if( numberOfSelected==1)
title = d.title;

} 
}
}

if ( numberOfSelected==1)
{

docIDString = title;
}
else if ( numberOfSelected>0)
{

numberOfSelected=numberOfSelected-1;
docIDString = title+' '+numberOfSelected+' other(s)';
}else 
docIDString = null;


}




/*public String getdocIDString(){
String title='';
if(documentList!=null && documentList.size()>0)
{
for(Document d: documentList)
{
if(d.isChecked)
{
title = d.title;
break;

} 
}
}
return title;
}*/

public void setdocIDString(String s){
docIDString =s ;

}

public void showNextRecs()

{
if((firstRecPosition + rowLimit) <= documentList.size())
{

firstRecPosition = firstRecPosition + rowLimit;
if(firstRecPosition!=0)
showPrevious = true;


if(firstRecPosition + rowLimit >= documentList.size())
showNext = false;
}
else
{
showNext = false;
showPrevious = true;
}
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Next '+rowLimit+' of '+documentList.size())); 
}
public void showPreviousRecs()
{
if((firstRecPosition - rowLimit) <= 0)
{
firstRecPosition = firstRecPosition - rowLimit;
showNext = true;
showPrevious = false;

}
else if((firstRecPosition - rowLimit) >= 0)
{
firstRecPosition = firstRecPosition - rowLimit;
showNext = true;

}
else
{
showPrevious = false;
showNext = true;
}
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Size '+rowLimit+' of '+documentList.size())); 
}
 
public void sendMailAndCreateTask(List<Document> dList)
{
List<Task> taskList = new List<Task>();

String endPoint;
HTTPResponse res;
HttpRequest req = new HttpRequest();


Http http = new Http();
String customizedEmailText = '';
String customizedEmailSub = '';

Contact_Email__c[] ce = [select Subject__c,description__c from Contact_Email__c where user__c = :UserInfo.getUserId()];
if( ce.size() >0 &&ce[0].description__c != null)
{

}


String docIdsSelected ;
if(dList!=null &&dList.size()>0) 
docIdsSelected = dList[0].id;
if(dList!=null && dList.size()>1)
for(Integer i=1;i<dList.size();i++){
docIdsSelected = docIdsSelected + ','+dList[i].id;
system.debug('Docdocdoc'+ docIdsSelected );
}
/*List< Contact_Email__c> lstcon = new List<Contact_Email__c>();
Contact_Email__c cont = new Contact_Email__c();
//cont.subject__c= selecttitle;
ce.add(cont);*/
Integer ra=dList.size() - 1;
// docIdString=dList[0].title;
if(dList!=null &&dList.size()>1)

{
selecttitle = dList[0].title+' '+'and'+' '+ra+' '+'other(s)';
selecttitle = EncodingUtil.UrlEncode(selecttitle,'UTF-8'); 
system.debug('yyy'+selecttitle);
}
if(dList!=null &&dList.size()==1) 
selecttitle = EncodingUtil.UrlEncode(dList[0].title,'UTF-8'); 
system.debug('rrrr'+selecttitle);




// if(ce.size()>0){iscustomized
if(isDynamic == 'true')
endPoint = 'http://api.icentera.com/v1sandbox/TestService.svc/json/EmailDocuments?token='+tokenVal+'&docids='+docIdsSelected+'&recipient='+c.Email+'&subject='+selecttitle+'&sfdccontactid='+c.id+'&isDynamic=true'+'&body=';
if(isDynamic == 'true')
endPoint = 'http://api.icentera.com/v1sandbox/TestService.svc/json/EmailDocuments?token='+tokenVal+'&docids='+docIdsSelected+'&recipient='+c.Email+'&subject='+customizedEmailSub+'&sfdccontactid='+c.id+'&isDynamic=true'+'&body='+customizedEmailText;
system.debug('XXXX'+endpoint);
if(isDynamic == 'false')
endPoint = 'http://api.icentera.com/v1sandbox/TestService.svc/json/EmailDocuments?token='+tokenVal+'&docids='+docIdsSelected+'&recipient='+c.Email+'&subject='+customizedEmailSub+'&sfdccontactid='+c.id+'&body='+customizedEmailText;
if(isDynamic == 'false') 
endPoint = 'http://api.icentera.com/v1sandbox/TestService.svc/json/EmailDocuments?token='+tokenVal+'&docids='+docIdsSelected+'&recipient='+c.Email+'&subject='+selecttitle+'&sfdccontactid='+c.id+'&body='+customizedEmailText;

// }



boolean dListcountertrack=false;
for(Document d: dList)
{

if(d==dList[0])
dListcountertrack=true;
else
dListcountertrack=false;


req.setMethod('GET');
req.setTimeout(60000);

req.setEndpoint(endPoint);
system.debug('eee'+endpoint);

Task t = new Task();
t.WhoId = c.id; 
t.Subject = 'iCentera Dynamic Email sent for '+d.title;
t.status = 'Completed';
t.type = 'Email';
String comments = d.title+'\n\n';
comments += 'Please download and save the following linked file:\n';
comments += '\nTitle:'+d.title;
comments += '\nFormat:'+d.filetype;
comments += '\nDescription:'+d.description;

t.description = comments;
taskList.add(t);

if(testVar == '' && dListcountertrack)
{
res = http.send(req);
JSONParser parser = JSON.createParser(res.getBody());
while (parser.nextToken() != null) 
{
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'd')) 
{
parser.nextToken();
if(parser.getText() == 'true')
{
// taskList.add(t);

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Email sent successfully for the selected documents.'));// \''+d.title+'\'.'));
}
else
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Email could not be sent.')); 
}
}
} 

}

}
insert taskList;
}

public void doSort()
{
List<Document> docList = new List<Document>();
List<Document> tempDocList = new List<Document>();
List<Integer> positionList = new List<Integer>();
Set<Integer> positionSet = new Set<Integer>();
List<String> tempList = new List<String>();
String order = 'asc';


if(previousSortField == sortField)
{
order = 'desc';
previousSortField = null; 
}
else
previousSortField = sortField;
for(Document d: documentList)
{
if(sortField == 'Title')
tempList.add(d.title.toLowerCase());
else if(sortField == 'fileType')
tempList.add(d.fileType.toLowerCase());
else
tempList.add(d.st);
tempDocList.add(d);
}
tempList.sort();


if(order == 'desc')
{
List<String> tList = tempList.clone();
tempList.clear();
for(Integer i=tList.size() - 1; i>=0; i--)
tempList.add(tList[i]);
}
Integer z = 0;
for(String s: tempList)
{
//system.debug('\nTTTTTT :: '+s+ ' ===== '+z);
z++;
}

//Integer remPos;
for(String s: tempList)
{
for(Integer i=0; i<= tempDocList.size(); i++)
{
system.debug('qqqq'+tempdoclist);
if((tempDocList[i].title.equalsIgnoreCase(s) || tempDocList[i].fileType.equalsIgnoreCase(s)||tempDocList[i].st.equalsIgnoreCase(s)) && !positionSet.contains(tempDocList[i].position))

{
positionList.add(tempDocList[i].position);
positionSet.add(tempDocList[i].position);
break;
}
}
}

documentList.clear();
for(Integer i: positionList)
{
for(Document d: tempDocList)
{
if(d.position == i)
{
documentList.add(d);
break;
} 
}
}
firstRecPosition = 0;
showPrevious = showNext = true;
}

public class Document
{
public String type {get; set;}
public String description {get; set;}
public String fileType {get; set;}
public String id {get; set;}
public String rating {get; set;}
public String title {get; set;}
public String url {get; set;}
public Integer position {get; set;}
public Boolean internalOnly {get;set;}
public Boolean isChecked {get; set;}
public string lastupdated {get; set;}
public String st {get; set;}

public Document()
{
type = null;
description = null;
fileType = null;
id = null;
rating = null;
title = null;
url = null;
internalOnly = false;
isChecked = false;
lastupdated = null;
// st = null;
}

}

public void sendEmail()
{
List<Document> tempDocumentList = new List<Document>();
for(Document d: documentList)
{
if(d.isChecked)
{
// ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,d.title+' =========== '+d.isChecked));
//System.debug(d.title+' =========== '+d.isChecked);
tempDocumentList.add(d);
//System.debug('TTTt'+tempDocumentList );
}
} 
sendMailAndCreateTask(tempDocumentList);

contact_email__c[] ceList = [select id,Subject__c, description__c, user__c from contact_email__c where user__c = :UserInfo.getUserId()];
if(celist.size()>0)
delete ceList;
}

public void reset()
{
if(documentList != null)
documentList = null;

documentSearchString = null;
showPrevious = showNext = false;
}
public class custom{

}

public static testMethod void testICentera()
{
PageReference pageRef = Page.ContactiCenteraIntegration;
Test.setCurrentPage(pageRef);
Contact ct = new Contact(Email='imohamme@calliduscloud.com',lastname='Ahmed');
insert ct;
Contact c = [select id from Contact where id = :ct.id];
ApexPages.StandardController sc = new ApexPages.standardController(c);
ContactiCenteraIntegration controller = new ContactiCenteraIntegration(sc);
controller.pos = 0;
controller.testVar = 'in Test';
controller.documentSearchString = '';

// controller.search();
controller.documentSearchString = 'test';

//controller.search();
//controller.showNextRecs();
// controller.showPreviousRecs();
ContactiCenteraIntegration.Document d = new ContactiCenteraIntegration.Document();
controller.sortField = 'Title';
controller.previousSortField = 'lastupdated';
//controller.doSort();
controller.sortField = 'Title';
controller.previousSortField = 'Title';
// controller.doSort();
controller.documentList= new List<Document>();
d.title = 'iCentera';
d.fileType = 'xls';
d.position = 0;
d.st= '2012-07-09';

controller.documentList.add(d);
d = new ContactiCenteraIntegration.Document();
d.title = 'iCentera';
d.fileType = 'xls';
d.position = 1;
d.st= '2012-07-08';
controller.documentList.add(d);
d = new ContactiCenteraIntegration.Document();
d.title = 'iCentera';
d.fileType = 'pdf';
d.position = 2;
d.st= '2011-07-09';
controller.documentList.add(d);

//controller.sendMailAndCreateTask(controller.documentList);
controller.sortField = 'st';
controller.previousSortField = 'st';
controller.doSort();

controller.showNextRecs();
controller.showPreviousRecs();
controller.sendEmail();

}
}