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