• externalsystem1.3882268433698855E12
  • NEWBIE
  • 10 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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 '';
     }
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 '';
     }