0

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.

  • 2
    Looks like you may have copy/pasted your real class twice (instead of including your test class). You can fix that by making an edit. Also, to format your code correctly, the easiest way is probably to paste your code in the editor window, highlight it, and then click the {} button (or press ctrl + k on windows, cmd + k on mac) – Derek F Apr 8 at 0:57
  • 1
    Also, if you haven't read it already, Testing HTTP Callouts by Implementing the HttpCalloutMock Interface should get you part of the way to knowing what you need to do in your test. – Derek F Apr 8 at 0:59
  • Hi Matthew, welcome to SFSE. Right now I don't see a specific question in the body of your post, which our format looks for. If you're looking for general help with unit testing, please see our canonical QA section, which has extensive resources. Otherwise, could you make an edit to clarify what you need (and, as Derek said, include the actual test class)? – David Reed Apr 8 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.