Forum Discussion

h_ixon's avatar
h_ixon
Occasional Contributor
16 years ago

HOWTO - Check 2 fields in the request to choose mockResponse

Hello,

I have a mockService (with many mockOperation) in SoapUI wich work without problem. My request is sent and a response is received.

But now i want to choose my response on a mockOperation depending on two parameters in my request.

For example my request is (its a quick example)

<example>
<name>Tom</name>
<language>EN</language>
</example>


I have 3 possible response in my mockOperation

The default response is "DefaultResponse"
If name is empty my response is "EmptyResponse"
If name is Billy and language is ES, my response will be "OtherResponse"
If name is Joe and language is EN, my response is "OtherResponseInEN"

I think check my parameters with Query_match but i don't know if query_match can do this (check many parameters).

So how to choose the response in a mockOperation by checking differents parameters in the request ? (the simple way)

3 Replies

  • From some basic sample project I found a while back:

    There are probably tons of other ways to do this, but I've found scripting to be the most powerful/straightforward thing in soapUI.

    In the mock service, under the operation that you're preforming, change dispatch to script and add in these lines:


    //String representation of the request message
    def soapMSG = mockRequest.getRequestContent()

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    //Deserializes the string into a tree structure for XPath functionality
    def holder = groovyUtils.getXmlHolder( soapMSG )

    //XPath query to get the node you care about (see below)
    String myXPath = "//name"

    //DOM Node that can contain attributes, a value, child nodes, etc. (see below)
    def node = holder.getDomNode( myXPath )
    String nodeValue = node.getNodeValue()

    if(nodeValue == "Billy"){
    return "OtherResponse"
    }


    This code will check the value of the first "name" element in your request and if it's "Billy" then it will respond with "OtherResponse"

    I hope this is enough to get you started with the rest of it.

    Learning about XPath: http://www.w3schools.com/xpath/default.asp
    Documentation on DOM nodes: http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/Node.html
  • h_ixon's avatar
    h_ixon
    Occasional Contributor
    Thank's a lot that it's exactly what i needed.

    Thank's for all comment's in the code, it's very usefull to understand how to change the script for differents cases.


    This topic is resolved.
  • thanks for the head start (am also a newbie, having just installed it today)

    thot i share the code i did, so that someone may find it useful


    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)

    //get the request header
    def ncdRequestType = String.valueOf(holder.getNodeValue("//etiq:soaHeader/ncdRequestType"))
    def RqUID = String.valueOf(holder.getNodeValue("//etiq:soaHeader/RqUID"))

    //get the request parameters
    def curVehNo = String.valueOf(holder.getNodeValue("//etiq:NCDRequest/ncdReqSOABO/curVehNo"))
    def idNo1 = String.valueOf(holder.getNodeValue("//etiq:NCDRequest/ncdReqSOABO/idNo1"))

    //set the response header
    //in the response, change the parameter from ? to $(txtResponse)
    context.setProperty("txtRqUID", RqUID)
    context.setProperty("txtNcdRequestType", ncdRequestType)

    //set the default response values
    //in the response, change the parameter from ? to $(txtResponse)
    context.setProperty("txtResult",0)
    context.setProperty("txterrorCode",0)
    context.setProperty("txterrorMessage",'Record not found')

    //list the text cases
    //in the response, change the parameter from ? to $(txtResponse)
    if(ncdRequestType == "FetchNCD"){
    if (curVehNo == "WPM4688" && idNo1 == "661013055464"){
    context.setProperty("txterrorCode",1)
    context.setProperty("txterrorMessage",'Success')
    context.setProperty("txtResult",55)
    }
    if (curVehNo == "NAA59" && idNo1 == "661013055464"){
    context.setProperty("txterrorCode",1)
    context.setProperty("txterrorMessage",'Success')
    context.setProperty("txtResult",40)
    }
    }