I am very new to HTTP Callouts and now I am in the testing phase. My HTTP Callout returns XML
This is the link. I input a company already, soulcycle, in case anybody wanted to see what was being returned from the HTTP Request.
https://news.google.com/rss/search?q=soulcycle&hl=en-US&gl=US&ceid=US:en
Anyways, here is my Apex Class:
public with sharing class getRSSFeed {
@AuraEnabled
public static List<Map<String,String>> getCompanyRSSFeed(String url, String recordID) {
String recordName = getRecordName(recordID);
List<Map<String,String>> newsArticles = search(url, recordName);
return newsArticles;
}
@AuraEnabled
public static String getRecordName(String recordID)
{
String accountRecordID = recordID;
return [SELECT Name From Account WHERE ID = : accountRecordID limit 1].Name;
}
@AuraEnabled
public static List<Map<String,String>> search(String remoteSiteURL, String query)
{
String url = remoteSiteURL + EncodingUtil.urlEncode(query, 'UTF-8');
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
HttpResponse res = h.send(req);
while (res.getStatusCode() == 302) {
req.setEndpoint(res.getHeader('Location'));
System.debug(res.getHeader('Location'));
res = new Http().send(req);
}
Dom.Document doc = res.getBodyDocument();
Dom.XMLNode root = doc.getRootElement();
Dom.XMLNode bodyNode = root.getChildElement('channel', null);
//Dom.XMLNode bodyNode = bodyNode.getChildElement('');
List<Map<String,String>> newsStories = new List<Map<String,String>>();
Integer sizeOfList = 20;
Integer i = 0;
for(DOM.XmlNode x : bodyNode.getChildren())
{
//System.debug(x.getName());
//System.debug(x.getText());
if(x.getName() == 'item' &&
i != sizeOfList)
{
Map<String,String> story = new Map<String, String>();
story.put('publisher', '');
story.put('date', '');
story.put('link', '');
story.put('image', '');
story.put('title', '');
story.put('description', '');
for(DOM.XmlNode itemChildren : x.getChildElements())
{
System.debug(itemChildren.getName());
//System.debug(itemChildren.getName());
//System.debug(itemChildren.getText());
if(itemChildren.getName() == 'link')
{
story.put('link', itemChildren.getText());
}
else if(itemChildren.getName() == 'title')
{
story.put('title', itemChildren.getText());
}
else if(itemChildren.getName() == 'pubDate')
{
story.put('date', itemChildren.getText());
}
else if(itemChildren.getName() == 'description')
{
String stripTags = itemChildren.getText();
List<String> description = stripTags.split('(<.*?>)');
story.put('description', description[5]);
}
else if(itemChildren.getName() == 'content')
{
story.put('image', itemChildren.getAttributeValue(itemChildren.getAttributeKeyAt(0), itemChildren.getAttributeKeyNsAt(0)));
}
else if(itemChildren.getName() == 'source')
{
story.put('publisher', itemChildren.getText());
}
}
newsStories.add(story);
i += 1;
}
}
return newsStories;
}
}
Here is my Test Class (Which is honestly embarrassing right now, because I have no clue what I am doing.)
public with sharing class getRSSFeed {
@AuraEnabled
public static List<Map<String,String>> getCompanyRSSFeed(String url, String recordID) {
String recordName = getRecordName(recordID);
List<Map<String,String>> newsArticles = search(url, recordName);
return newsArticles;
}
@AuraEnabled
public static String getRecordName(String recordID)
{
String accountRecordID = recordID;
return [SELECT Name From Account WHERE ID = : accountRecordID limit 1].Name;
}
@AuraEnabled
public static List<Map<String,String>> search(String remoteSiteURL, String query)
{
String url = remoteSiteURL + EncodingUtil.urlEncode(query, 'UTF-8');
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
HttpResponse res = h.send(req);
while (res.getStatusCode() == 302) {
req.setEndpoint(res.getHeader('Location'));
System.debug(res.getHeader('Location'));
res = new Http().send(req);
}
Dom.Document doc = res.getBodyDocument();
Dom.XMLNode root = doc.getRootElement();
Dom.XMLNode bodyNode = root.getChildElement('channel', null);
//Dom.XMLNode bodyNode = bodyNode.getChildElement('');
List<Map<String,String>> newsStories = new List<Map<String,String>>();
Integer sizeOfList = 20;
Integer i = 0;
for(DOM.XmlNode x : bodyNode.getChildren())
{
//System.debug(x.getName());
//System.debug(x.getText());
if(x.getName() == 'item' &&
i != sizeOfList)
{
Map<String,String> story = new Map<String, String>();
story.put('publisher', '');
story.put('date', '');
story.put('link', '');
story.put('image', '');
story.put('title', '');
story.put('description', '');
for(DOM.XmlNode itemChildren : x.getChildElements())
{
System.debug(itemChildren.getName());
//System.debug(itemChildren.getName());
//System.debug(itemChildren.getText());
if(itemChildren.getName() == 'link')
{
story.put('link', itemChildren.getText());
}
else if(itemChildren.getName() == 'title')
{
story.put('title', itemChildren.getText());
}
else if(itemChildren.getName() == 'pubDate')
{
story.put('date', itemChildren.getText());
}
else if(itemChildren.getName() == 'description')
{
String stripTags = itemChildren.getText();
List<String> description = stripTags.split('(<.*?>)');
story.put('description', description[5]);
}
else if(itemChildren.getName() == 'content')
{
story.put('image', itemChildren.getAttributeValue(itemChildren.getAttributeKeyAt(0), itemChildren.getAttributeKeyNsAt(0)));
}
else if(itemChildren.getName() == 'source')
{
story.put('publisher', itemChildren.getText());
}
}
newsStories.add(story);
i += 1;
}
}
return newsStories;
}
}
I know this test class is really bad. I am researching a lot on how to achieve 100% code coverage. If you have any tips, please send them over. I appreciate all the help I can get!
Thanks in advance.
{}
button (or pressctrl
+k
on windows,cmd
+k
on mac)