In my visualforce page I have a search field (inputText) with an id of 'query'. Query is passed into my controller and the runSearch() method starts a process which initiates a web service call. In my test class I am trying to initiate the API call by passing in a search value.
Extension
public with sharing class ClinicalStudySetupExtension {
private String studyJson;
private String result;
private String protocolNumber;
public String query {get; set;}
public Boolean searched {get; set;}
public Clinical_Study__c cStudy {get; set;}
private ApexPages.StandardController std {get; set;}
//controller
public ClinicalStudySetupExtension(ApexPages.StandardController std) {
this.std = std;
this.studyJson = callRestEndPoint();
query = '';
searched=false;
String queryStr=ApexPages.currentPage().getParameters().get('query');
if (queryStr!=null)
{
protocolNumber=queryStr;
runSearch();
}
}
public pageReference runSearch() {//runSearch takes the input 'query' and passes it to the REST api call
searched=true;
String searchStr = ''+query+'';
system.debug('Study name: ' + query);
protocolNumber = searchStr.deleteWhitespace();
this.studyJson = callRestEndPoint();
return null;
}
private string callRestEndPoint() {//the rest api call generates a json string and passes it to the json parser
Http h = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
String username = 'ex';
String password = 'ex';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setClientCertificateName('CN');
req.setHeader('Authorization', authorizationHeader);
req.setHeader('Api-Key', '777..');
req.setTimeout(120000);
req.setMethod('GET');
req.setEndpoint('https://example.com/study/'+protocolNumber);
String result = '';
try{
res = h.send(req);
result = res.getBody();
}
catch(System.CalloutException e){
result = res.toString();
}
return result;
}
List<StudyWrapper> Study = new List<StudyWrapper>();
public List<StudyWrapper> getStudy(){//the json parser parses the json string and puts it in a list, study
JSONParser parser = JSON.createParser(this.studyJson);
while(parser.nextToken() != null){
while(parser.nextToken()!= null){
if(parser.getCurrentToken()==JSONToken.START_OBJECT){
StudyWrapper s = (StudyWrapper)parser.readValueAs(StudyWrapper.class);
Study.add(s);
}
}
}
return Study;
}
I'm trying to set up my test class, so far it looks like this:
Test class
static testMethod void testRunSearch(){
//create a test user
Profile p1 = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u1 = new User(Alias = 'George', UserName='JFFGeo@testorg#.com', Email='[email protected]', LastName='George', ProfileId = p1.Id, EmailEncodingKey='UTF-8', LanguageLocaleKey='en_US', LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles' );
insert u1;
//Arrange the components
PageReference pageRef = Page.ClinicalStudySetup;
Test.setCurrentPage(pageRef);
ApexPages.StandardController sc = new ApexPages.StandardController(new Clinical_Study__c());
ClinicalStudySetupExtension ext = new ClinicalStudySetupExtension(sc);
pageRef.getParameters().put('query','GA00806');
Test.startTest();
System.runAs(u1){
ext.runSearch();
}
Test.stopTest();
}
Unfortunately, the way that I'm defining query and executing runSearch() doesn't seem to be working in my test class. The method isn't even being hit when I run my test class. Can someone tell me, what am I doing wrong? Or what could be the cause of this?