+ Start a Discussion
externalsystem1.3882268433698855E12externalsystem1.3882268433698855E12 

Test class for XML DOM PArser

HI all,

I need to write the test class for DOM XML parser. Could anyone please help me out from this

public canvastosalesforce1_AC()
    {
        Http h=new Http();
        HttpResponse response = new HttpResponse();
        HttpRequest request= new HttpRequest();
        String responseBody;
        request.setEndpoint(URL);
        request.setMethod('GET');
        request.setHeader('Content-Type','application/xml');
        if(!Test.isRunningTest())
        {
        response = h.send(request);
       
        responseBody = response.getBody();
       
        Dom.Document doc = response.getBodyDocument();
       
        system.debug(responseBody+'****'+doc);
       
        Dom.XMLNode root = doc.getRootElement();
        system.debug('****'+root);
        ResponseContainerRec = new ResponseContainer();
       
        sectionsClassRec = new sectionsClass();       
        screensClassRec = new screensClass();
        responsesClassRec = new responsesClass();
       
        ResponseContainerRec.sections = new List<sectionsClass>();
        sectionsClassRec.screens = new List<screensClass>();
        screensClassRec.responses = new List<responsesClass>();
       
        responseContainerList = new List<ResponseContainer>();
        walkThrough(root);     
        }
        system.debug('*****999 '+responseContainerList); 
       
        //insertsubmission();
    }
public String walkThrough(DOM.XMLNode node) {
        
        String result = '\n';
       
        if (node.getNodeType() == DOM.XMLNodeType.COMMENT) {
            System.debug('Test comment '+node.getText());
        }
       
        if (node.getNodeType() == DOM.XMLNodeType.TEXT) {
            System.debug('Test element '+node.getText());
        }
       
        if (node.getNodeType() == DOM.XMLNodeType.ELEMENT) {
            if(node.getName() == 'Submission')
            {
                if (node.getAttributeCount() > 0)
                {
                    ResponseContainerRec.submissionId = node.getAttributeValue(node.getAttributeKeyAt(0), node.getAttributeKeyNsAt(0));
                }
               
            }
            if(node.getName() == 'Form')
            {
                if (node.getAttributeCount() > 0)
                {
                    ResponseContainerRec.formId = node.getAttributeValue(node.getAttributeKeyAt(0), node.getAttributeKeyNsAt(0));
                }
            } 
            if(node.getName() == 'Name')
            {
               
                if(node.getText()!=''&&node.getText()!=null)
                {                   
                     if(node.getParent().getName() == 'Form')
                     {  
                        ResponseContainerRec.formName = node.getText();
                     }
                     if(node.getParent().getName() == 'Section')
                     {  
                        sectionsClassRec.sectionName = node.getText();
                     }
                     if(node.getParent().getName() == 'Screen')
                     {  
                        screensClassRec.screenName = node.getText();
                     }
                }   
            }
            if(node.getName() == 'Status')
            {
                if(node.getText()!=''&&node.getText()!=null)
                {                   
                    ResponseContainerRec.formStatus = node.getText();
                }   
            }
            if(node.getName() == 'Version')
            {
                if(node.getText()!=''&&node.getText()!=null)
                {                   
                    ResponseContainerRec.formVersion = node.getText();
                }   
            }
            if(node.getName() == 'Date')
            {
                if(node.getText()!=''&&node.getText()!=null)
                {                   
                    ResponseContainerRec.dateValue= node.getText();
                }   
            }  
            if(node.getName() == 'DeviceDate')
            {
                if(node.getText()!=''&&node.getText()!=null)
                {                   
                    ResponseContainerRec.deviceDate = node.getText();
                }   
            }  
            if(node.getName() == 'UserName')
            {
                if(node.getText()!=''&&node.getText()!=null)
                {                   
                    ResponseContainerRec.UserName = node.getText();
                }   
            }
            if(node.getName() == 'FirstName')
            {
                System.debug('check node '+node.getParent().getName());
               
                if(node.getText()!=''&&node.getText()!=null)
                {                   
                    ResponseContainerRec.FirstName = node.getText();
                }   
            }
            if(node.getName() == 'LastName')
            {
                if(node.getText()!=''&&node.getText()!=null)
                {                   
                    ResponseContainerRec.LastName = node.getText();
                }   
            }
           
           
            if(node.getName() == 'ResponseID')
            {
                if(node.getText()!=''&&node.getText()!=null)
                {                   
                    ResponseContainerRec.ResponseID = node.getText();
                }   
            }
           
            if(node.getName() == 'Label')
            {
                if(node.getText()!=''&&node.getText()!=null)
                {                   
                    responsesClassRec.responseLabel = node.getText();
                }   
            }
            if(node.getName() == 'Value')
            {
                if(node.getText()!=''&&node.getText()!=null)
                {                   
                    responsesClassRec.responseValue = node.getText();
                }   
            }
            if(node.getName() == 'Type')
            {
                if(node.getText()!=''&&node.getText()!=null)
                {                   
                    responsesClassRec.responseType = node.getText();
                }   
            }
       
         result += 'Element: ' + node.getName();
            if (node.getText().trim() != '') {
                result += ', text=' + node.getText().trim();
            }
            if (node.getAttributeCount() > 0) {
              for (Integer i = 0; i< node.getAttributeCount(); i++ ) {
                result += ', attribute #' + i + ':' + node.getAttributeKeyAt(i) + '=' + node.getAttributeValue(node.getAttributeKeyAt(i), node.getAttributeKeyNsAt(i));
              } 
            }
        for (Dom.XMLNode child: node.getChildElements())
        {
            result += walkThrough(child);
        }
       
       
        if(responsesClassRec.responseLabel != null && responsesClassRec.responseValue != null && responsesClassRec.responseType != null)
        {
            System.debug(' Test '+responsesClassRec);
            screensClassRec.responses.add(responsesClassRec);           
            responsesClassRec = new responsesClass();           
        }
       
        if(screensClassRec.screenName != null && screensClassRec.responses.size() > 1)
        {
            System.debug(' Test1 '+screensClassRec);
            sectionsClassRec.screens.add(screensClassRec);
            screensClassRec = new screensClass();
            screensClassRec.responses = new List<responsesClass>();           
        }
       
        if(sectionsClassRec.sectionName!= null && sectionsClassRec.screens.size() > 0)
        {
            System.debug(' Test2 '+sectionsClassRec);
            ResponseContainerRec.sections.add(sectionsClassRec);
            sectionsClassRec = new sectionsClass();           
            sectionsClassRec.screens = new List<screensClass>();
        }
       
        if(ResponseContainerRec.submissionId != null && ResponseContainerRec.formId != null && ResponseContainerRec.formName != null && ResponseContainerRec.formStatus  != null &&
           ResponseContainerRec.formVersion != null && ResponseContainerRec.dateValue != null && ResponseContainerRec.deviceDate != null && ResponseContainerRec.UserName != null &&
           ResponseContainerRec.FirstName != null && ResponseContainerRec.LastName != null && ResponseContainerRec.ResponseID != null && ResponseContainerRec.sections.size() > 0)
        {           
            System.debug('ResponseContainerRec '+ResponseContainerRec);
       
            responseContainerList.add(ResponseContainerRec);
            ResponseContainerRec = new ResponseContainer();
            ResponseContainerRec.sections = new List<sectionsClass>();        
        }
       
            return result;
        }
       
        return '';
     }
Best Answer chosen by externalsystem1.3882268433698855E12
Vinita_SFDCVinita_SFDC
Hello,

You will have to test it using mock callouts. Refer following links for sample code and further details:

http://blogs.developerforce.com/developer-relations/2012/10/testing-http-callouts-with-static-data-in-winter-13.html#comment-688228232

http://salesforce.stackexchange.com/questions/3486/testing-httpcallout-with-httpcalloutmock-and-unittest-created-data

http://salesforce.stackexchange.com/questions/2038/testing-webservicecallout-with-httpcalloutmock

All Answers

Vinita_SFDCVinita_SFDC
Hello,

You will have to test it using mock callouts. Refer following links for sample code and further details:

http://blogs.developerforce.com/developer-relations/2012/10/testing-http-callouts-with-static-data-in-winter-13.html#comment-688228232

http://salesforce.stackexchange.com/questions/3486/testing-httpcallout-with-httpcalloutmock-and-unittest-created-data

http://salesforce.stackexchange.com/questions/2038/testing-webservicecallout-with-httpcalloutmock
This was selected as the best answer
externalsystem1.3882268433698855E12externalsystem1.3882268433698855E12
Hi Vinitha,

Thanks for your reply.

I need to write test class for post method and the references that you provided are for get methods. Couls you please help me on this part
Mohinish NandwanaMohinish Nandwana

Hi 

Any update on this test class?

how do you write test class for this kind of code. i have a similar pattern of code but not getting the segment covered.

Thanks