This is my apex class, please help me in writing a test class for it.Thanks
global clas LoginClass{
public string Cookie_M;
public string Session_M;
public string Token_M;
public string Username_M;
public string Password_M;
public string URL_M;
public string Name_M;
public String StatusResp;
Http httpToken = new Http();
HttpRequest reqToken = new HttpRequest();
public HttpResponse resToken = new HttpResponse();
Http http = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
public Dom.Document doc;
// Login Access
public String getLoginAccess(String Name, String username, String password, String url){
Name_M = Name;
Username_M = username;
Password_M = password;
URL_M = url;
system.debug(username);system.debug(url);
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' +EncodingUtil.base64Encode(headerValue);
//Token Request
reqToken.setHeader('Authorization', authorizationHeader);
reqToken.setEndpoint(url+'/api/xml?action=login&login='+username+'&password='+password);
reqToken.setMethod('GET');
reqToken.setHeader('Content-Type', 'application/json');
reqToken.setHeader('Accept','application/json');
try{
resToken = httpToken.send(reqToken);
if(resToken !=null){
System.debug('Result :: '+resToken.getBody());
System.debug('*Resp :: ' + String.ValueOF(resToken.getBody()));
System.debug('RESPONSE STRING :: ' + resToken.toString());
System.debug('RESPONSE STATUS: ' + resToken.getStatus());
System.debug('STATUS_CODE: ' + resToken.getStatusCode());
System.debug('Cookie :: '+resToken.getHeader('Set-Cookie'));
System.debug('Session :: '+resToken.getHeader('Set-Cookie').substringBetween('BREEZESESSION=', ';'));
//get token from XML response
doc = resToken.getBodyDocument();
Dom.XMLNode results = doc.getRootElement();
Dom.XMLNode common = results.getChildElement('OWASP_CSRF_TOKEN', null);
Dom.XMLNode Status = results.getChildElement('status', null);
if(resToken!=null){
Session_M = resToken.getHeader('Set-Cookie').substringBetween('BREEZESESSION=', ';'); //This is not cookie this is session
System.debug('session :: '+Session_M);
}
if(common != Null) {
Token_M = common.getChildElement('token', null).getText();
System.debug('*** Access Token *** :: ' +Token_M);
System.debug('*** Access Token length *** :: ' +Token_M.length() + ' string :: ' +String.valueof(Token_M.length()));
}
StatusResp = Status.getAttributeValue('code',null);
System.debug('StatusResp :: ' +StatusResp);
}
}catch(System.CalloutException e) {
System.debug('Server Callout Exception in response for Token :: ' + e.getMessage());
StatusResp = e.getMessage() ;
System.debug('StatusResp :: ' +StatusResp);
}
catch(Exception e) {
System.debug('Exception in response for API call :: ' + e.getMessage());
StatusResp = e.getMessage() ;
System.debug('StatusResp :: ' +StatusResp);
}
System.debug('StatusResponse :: ' +StatusResp);
//Cookie Request
HttpRequest requestCookie = new HttpRequest();
requestCookie.setHeader('Authorization', authorizationHeader);
requestCookie.setEndpoint(url+'/api/xml?action=login&login='+username+'&password='+password+'&domain=apac1cps.adobeconnect.com&session='+Session_M);
requestCookie.setHeader('Content-Type', 'application/json');
requestCookie.setHeader('Accept','application/json');
requestCookie.setMethod('POST');
Http httpCookie = new Http();
HttpResponse responseCookie = new HttpResponse();
try {
responseCookie = httpCookie.send(requestCookie);
System.debug('*Resp Cookie:' + String.ValueOF(responseCookie.getBody()));
System.debug('RESPONSECookie STRING: ' + responseCookie.toString());
System.debug('RESPONSECookie STATUS: ' + responseCookie.getStatus());
System.debug('STATUS_CODE: ' + responseCookie.getStatusCode());
System.debug('*** Cookie *** :: ' +responseCookie.getBody());
/* string location = responseCookie.getHeader('Location');
System.debug('*** Cookie Location *** :: ' + location); */
Cookie_M = responseCookie.getHeader('Set-Cookie');
System.debug('cookie is : ' + Cookie_M );
if((!StatusResp.contains('Unauthorized endpoint,'))){
if((!StatusResp.contains('no-data'))){
if((!StatusResp.contains('invalid'))){
GetEventList();
XMLLoginAccess loginaccess = new XMLLoginAccess(Name_M,Username_M,Password_M,URL_M,Session_M,Token_M);
}
}
}
}
catch(System.CalloutException e) {
system.debug('Server Callout Exception in responseCookie :: ' + e.getMessage());
} catch(Exception e){
system.debug('Other exception ::'+e.getMessage());
}
System.debug('StatusResponsesss :: ' +StatusResp);
return StatusResp;
}
//
public void GetEventList(){
Blob headerValue = Blob.valueOf(Username_M + ':' + Password_M);
String authorizationHeader = 'BASIC ' +EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setEndpoint(URL_M+'/api/xml?action=report-my-events&session='+Session_M);
req.setMethod('GET');
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept','application/json');
req.setHeader('Cookie', Cookie_M);
try {
res = http.send(req);
System.debug('MyResult :'+res.getBody());
// Saves these webinar records in SF
XMLParserCopy s = new XMLParserCopy(res.getBody());
System.debug('Response value'+s);
}catch(System.CalloutException e){
System.debug('Callout error: '+ e);
}
}