Forum Discussion

ArturoMartinez's avatar
ArturoMartinez
Occasional Contributor
13 years ago

Stuck with response and file comparer

Hi there!

I've been looking through the forums, Stackoverflow and many blogs and webs.
Now I've reached a point I've get stuck with my code.

I'm trying to compare a XML file with the "original" response from a test, with the response obtained from the same request.
I must said I'm using free SoapUI, so everything "must be coded".

Here are the codes I'm using:

First Script (setup)

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

context.aOriginVals = new ArrayList();
context.aResponseVals = new ArrayList();

//clean logs
com.eviware.soapui.SoapUI.logMonitor.getLogArea("script log").clear();
com.eviware.soapui.SoapUI.logMonitor.getLogArea("error log").clear();
com.eviware.soapui.SoapUI.logMonitor.getLogArea("soapUI log").clear();

public int parseNode(parentNode, arrayList){

if (parentNode.hasChildNodes())
{
for (int i = 0; i < parentNode.getChildNodes().getLength(); i++)
{
Node childNode = parentNode.getChildNodes().item(i);
if (childNode.hasChildNodes())
parseNode(childNode, arrayList);
else
{
def auxObj = new ArrayList();
auxObj[0] = childNode.getNodeName();
auxObj[1] = childNode.getNodeValue();
arrayList.push(auxObj);
auxObj = null;
}
}//for
}
else
{
def auxObj = new ArrayList();
auxObj[0] = parentNode.getNodeName();
auxObj[1] = parentNode.getNodeValue();
arrayList.push(auxObj);
auxObj = null;
}

return 0;
}

def originFile = new File("C:/Users/Arturo/Desktop/XMLComparer/origin/response_BO.xml");
if (!originFile.exists())
{
log.error("'C:/Users/Arturo/Desktop/XMLComparer/origin/response_BO.xml' is no a valid path or file doesn't exists");
testRunner.gotoStepByName('ENDSCRIPT');
}
else
{
def dbFactory = DocumentBuilderFactory.newInstance();
def dBuilder = dbFactory.newDocumentBuilder();
def doc = dBuilder.parse(originFile);

doc.getDocumentElement().normalize();
def nodeList = doc.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++)
parseNode(nodeList.item(i), context.aOriginVals);
testRunner.gotoStepByName('REQUESTSCRIPT');
}


Request Script

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.*;

public int parseNode(parentNode, arrayList){

if (parentNode.hasChildNodes())
{
for (int i = 0; i < parentNode.getChildNodes().getLength(); i++)
{
Node childNode = parentNode.getChildNodes().item(i);
if (childNode.hasChildNodes())
parseNode(childNode, arrayList);
else
{
def auxObj = new ArrayList();
auxObj[0] = childNode.getNodeName();
auxObj[1] = childNode.getNodeValue();
arrayList.push(auxObj);
auxObj = null;
}
}//for
}
else
{
def auxObj = new ArrayList();
auxObj[0] = parentNode.getNodeName();
auxObj[1] = parentNode.getNodeValue();
arrayList.push(auxObj);
auxObj = null;
}

return 0;
}

def utils = new com.eviware.soapui.support.GroovyUtils(context);
def testResponse = testRunner.runTestStepByName("Request").getResponseContentAsXml();
def holder = utils.getXmlHolder(testResponse);
def data = holder.getDomNode("//ProcessApplicationResult/text()").getData().toString();
data.replace('<![CDATA[', '');
data.replace(']]>', '');

def nOS = new FileOutputStream ('C:/temp/temp_responseBO_request.xml', false);
if (nOS != null)
{
def out = new PrintStream(nOS);
if (out != null)
{
out.print(data);
out.close();

def hFile = new File('C:/temp/temp_responseBO_request.xml');
if (hFile.exists() && hFile.canRead())
{

def dbFactory = DocumentBuilderFactory.newInstance();
def dBuilder = dbFactory.newDocumentBuilder();
def doc = dBuilder.parse(hFile);

doc.getDocumentElement().normalize();
def nodeList = doc.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++)
parseNode(nodeList.item(i), context.aResponseVals);

//hFile.delete();
testRunner.gotoStepByName('COMPARER');
}//fi:hFile
else
{
log.error('El fichero "C:/temp/temp_responseBO_request.xml" no existe o no puede ser leido');
testRunner.gotoStepByName('ENDSCRIPT');
}
}//fi:out
else
{
log.error('No se ha podido crear el buffer de impresiĆ³n para la respuesta XML');
testRunner.gotoStepByName('ENDSCRIPT');
}

nOS.close();
}//fi:nFile
else
{
log.error('No se ha podido crear el buffer de salida para "C:/temp/temp_responseBO_request.xml"');
testRunner.gotoStepByName('ENDSCRIPT');
}


Comparer Script

boolean bEndI = false;
boolean bEndJ = false;


for (int i = 0; ((i < context.aResponseVals.size())&&(!bEndI)); i++)
{
def objI = context.aResponseVals[i];

for (int j = 0; ((j < context.aOriginVals.size())&&(!bEndJ)); j++)
{
def objJ = context.aOriginVals[j];

if ((j == (context.aOriginVals.size()-1)&&(!bEndJ))
bEndI = true;

if (objI[0].equals(objJ[0])
{
bEndJ = true;
if (!objI[1].equals(objJ[1])
{
log.error('El campo "'+objI[0]+'" tiene diferente valor!');
log.error('Original: '+objJ[1]+' // Respuesta: '+objI[1]);
}

}
}//for:j

if (bEndI && !bEndJ)
log.error('El campo "'+objI[0]+'" no existe en la respuesta original!');

}//for:i


End Script
log.info('End');


Thanks for all your time and support, in advance!
  • ArturoMartinez's avatar
    ArturoMartinez
    Occasional Contributor
    Well, working all the weekend I have found some mistakes, now that I solved out them I find the following issue.

    My "recursive" function to parse all the XML DOM tree doesn't works fine.
    Here is the updated function:

    public void parseNode(Node node, ArrayList list){
    if (node.hasChildNodes())
    {
    def childrens = node.getChildNodes();
    for (int i = 0; i < childrens.getLength(); i++)
    parseNode(childrens.item(i), list);
    }
    else
    addNewElement(node.getNodeName(), node.getNodeValue(), list);
    }


    The problem is that 'getNodeName()' always returns "#text", which obviously means that I'm moving wrong through the tree.
    What should I change?