• Vivek Lath
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Trying to upload the sandbox classes to production but its gettinf error of code coverage , just know to create tet class of my apex class.my class code coverage show only 29%

public class DomDocument {
    public list<GoogleNewsItem> actualList { get; set; }
    // public GoogleNewsItem googleNewsItem { get; set; }
    public Account aa { get; set; }
    public String endPoint { get; set; }
    public String accountName { get; set; }
    public String url { get; set; }
    // Pass in the URL for the request
    // For the purposes of this sample,assume that the URL
    // returns the XML shown above in the response body
    public DomDocument() {
        
    }
    public DomDocument(ApexPages.StandardController controller) {
        Account aa= [SELECT Name FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
                accountName = aa.name;
        endPoint = 'https://news.google.com/rss/search?cf=all&hl=en-IN&pz=1&q=' + accountName + '&gl=IN&ceid=IN:en';
        // url = endPoint.deleteWhitespace();
        url = endPoint.replaceAll(' ','%20');
        actualList = parseResponseDom(url);
    }
    
    // Method to parse the element value in page block and Return to display the List Element value in page block.
    public list<GoogleNewsItem> parseResponseDom(String url){
    
    // For Developer Console and Debugging.
    // public void parseResponseDom(String url){
        
        // For individual list only for Single Single Tabs of XML.
        // list<String> titles = new list<String>();
        // list<String> links = new list<String>();
        
        // For List of Objects of Google News Items.
        //List of wrapper class.
        list<GoogleNewsItem> googleNewsItemsList = new list<GoogleNewsItem>();
        
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        // url that returns the XML in the response body
        req.setEndpoint(url);
        req.setMethod('GET');
        HttpResponse res = h.send(req);
        Dom.Document doc = res.getBodyDocument();
        
        //Retrieve the root element for this document.
        Dom.XMLNode rss = doc.getRootElement();
        //System.debug(rss);
        Dom.XmlNode channelNode = rss.getChildElement('channel', null);
        Dom.XmlNode itemNode = channelNode.getChildElement('item', null);
        String title = itemNode.getChildElement('title', null).getText();
        //System.debug('Title:  ' + title);
        System.debug(channelNode.getChildElement('link', null).getText());
        //String state = address.getChildElement('state', null).getText();
        // print out specific elements
        //System.debug('State: ' + state);
        
        //System.debug('Name: ' + channelNode);
        
        // Alternatively, loop through the child elements.
        // This prints out all the elements of the address
        for(Dom.XMLNode child : channelNode.getChildElements()) {
           //System.debug(child.getText());
        }
        
        // Traverse through the list of child element inside the root element.
        for(Dom.XMLNode channelForLoop: doc.getRootElement().getChildElements()) {
            for(Dom.XmlNode channelChildForLoop: channelForLoop.getChildElements()) {
                if(channelChildForLoop.getName() == 'item') {
                    // titles.add(channelChildForLoop.getChildElement('title', null).getText());
                    // links.add(channelChildForLoop.getChildElement('link', null).getText());
                    
                    //Instance of wrapper class to add the value in Wrapper property and then adding to the above list.
                    GoogleNewsItem googleNewsItem = new GoogleNewsItem(); // Dynamically Object creation.

                    googleNewsItem.title = channelChildForLoop.getChildElement('title', null).getText();
                    googleNewsItem.link = channelChildForLoop.getChildElement('link', null).getText();
                    googleNewsItem.publishDate = channelChildForLoop.getChildElement('pubDate', null).getText();
                    googleNewsItem.description = channelChildForLoop.getChildElement('description', null).getText();
                    googleNewsItem.source = channelChildForLoop.getChildElement('source', null).getText();
                    googleNewsItem.sourceUrl = channelChildForLoop.getChildElement('source', null).getAttribute('url', null);

                    googleNewsItemsList.add(googleNewsItem); // Object added to List of NewsItem.
                }
            }
        }
        
        // Using List of individual Single Single Tabs of XML.
        // System.debug(titles); // All the Titles.
        // System.debug(links); // All the Links.
        // System.debug(titles[10]); // Only 1st Title.
        
        // Using List Object.
        System.debug('**********');
        System.debug('Full Object:  ' + googleNewsItemsList);// Full Objects.
        System.debug('First Object:  ' + googleNewsItemsList[0]);// 1st Object.
        System.debug('Last Object:  ' + googleNewsItemsList[99]);// 103rd Object.
        System.debug('Last Object Source:  ' + googleNewsItemsList[99].source);// 103rd Object's Source Content.
        System.debug('Last Object Source URL Attribute:  ' + googleNewsItemsList[99].sourceUrl);// 103rd Object's Source Url Attribute Content.
        
        
        // Return to display the element value in page block.
        return googleNewsItemsList;
        // System.debug('Last Object:  ' + googleNewsItemsList);// Full Object.
    }
}

and created test class for this block of code as follows

@isTest
public class DomDocumentTest {
    @IsTest(SeeAllData=true) public static void testDomDocument() {
        
        DomDocument dom = new DomDocument();
        
        Account aa = new Account(name='Test');
        insert aa;
        dom.aa = aa;
        // Contact myContact = new Contact(firstname='Goquest', lastname='Media');
        // insert myContact;
 
        dom.accountName = 'Goquest';
        dom.endPoint = 'https://news.google.com/rss/search?cf=all&hl=en-IN&pz=1&q=Goquest&gl=IN&ceid=IN:en';
        dom.url = 'https://news.google.com/rss/search?cf=all&hl=en-IN&pz=1&q=Goquest&gl=IN&ceid=IN:en';
        dom.actualList = dom.parseResponseDom('https://news.google.com/rss/search?cf=all&hl=en-IN&pz=1&q=Goquest&gl=IN&ceid=IN:en');
        
        Apexpages.StandardController testSC = new Apexpages.StandardController(aa);
        DomDocument dom1 = new DomDocument(testSC);
        dom1.aa = aa;
        dom1.accountName = 'Goquest';
        dom1.endPoint = 'https://news.google.com/rss/search?cf=all&hl=en-IN&pz=1&q=Goquest&gl=IN&ceid=IN:en';
        dom1.url = 'https://news.google.com/rss/search?cf=all&hl=en-IN&pz=1&q=Goquest&gl=IN&ceid=IN:en';
        dom1.actualList = dom.parseResponseDom('https://news.google.com/rss/search?cf=all&hl=en-IN&pz=1&q=Goquest&gl=IN&ceid=IN:en');
   
        System.assertEquals(dom.parseResponseDom('https://news.google.com/rss/search?cf=all&hl=en-IN&pz=1&q=Goquest&gl=IN&ceid=IN:en'),dom.actualList); 
        
    }
    

}