Samarth
Samarth

Reputation: 308

XPATH Dispatch in SoapUI Mock service/ Mock operation

I am new in SoapUI, and was trying to understand the use of XPATH dispatch for a mock operation in a mock service.

Here is what I have done so far

  1. Created a mock service for a calculator
  2. Added mock operation substract

Following is a sample request for the operation

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="http://www.parasoft.com/wsdl/calculator/">
   <soapenv:Header/>
   <soapenv:Body>
      <cal:subtract>
         <cal:x>1</cal:x>
         <cal:y>1</cal:y>
      </cal:subtract>
   </soapenv:Body>
</soapenv:Envelope>

Following is a sample response for the same

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="http://www.parasoft.com/wsdl/calculator/">
   <soapenv:Header/>
   <soapenv:Body>
      <cal:subtractResponse>
         <cal:Result>?</cal:Result>
      </cal:subtractResponse>
   </soapenv:Body>
</soapenv:Envelope>

I was able to understand about other dispatch but not about XPATH as following is what I have entered in the XPATH dispatch

declare namespace cal='http://www.parasoft.com/wsdl/calculator/';
declare namespace soapenv='http://schemas.xmlsoap.org/soap/envelope/';
//cal:subtract/cal:x

It was also observed that if I have already used SCRIPT dispatch and switch to XPATH from the dropdown, the script is visible in the declaration/scripting box/area

Following are the questions:

  1. Is XPATH and SCRIPT dispatch same
  2. If not, how does the XPATH dispatch actually work to identify what response to dispatch out of all form MockResponses list

Kindly help me with this.

PS: I have already gone through http://www.soapui.org/soap-mocking/reference/mockoperation.html http://www.soapui.org/soap-mocking/mockoperations-and-responses.html

Upvotes: 2

Views: 4061

Answers (1)

Rao
Rao

Reputation: 21379

The soapUI documentation that you mentioned in your question is the right location to get the information. However, the information available is not complete.

After searching for sometime, found the details.

Initially, got confused between Xpath and Script Dispatch methods.

Here is the additional information than what you are looking for:

Is XPATH and SCRIPT dispatch same

Answer is NO. Both are different

If not, how does the XPATH dispatch actually work to identify what response to dispatch out of all form MockResponses list

The following information found in the documentation:

XQUERY - This is similar to the QUERY_MATCH but not quite as powerful; an XPath expression is applied to the incoming request and the resulting value is used for selecting which MockResponse to be returned. In our previous example of search results, we could set the XPath expression to select a search term and then create MockResponses named after each expected value. The advantage being that we don’t need to add new XPath statements for new search criteria, just another MockResponse.

Assume that you created multiple responses say PositiveResponse, NegativeResponse, ZeroResponse for subtract operation of Mock Service.

Here are the sample conditions that you may want to apply on the request and dispatch appropriate response. Of course, you may have as many as you require.

  1. PositiveResponse - if x, y values are 10, 5 respectively.
  2. NegativeResponse - if x, y values are 5, 10 respectively.
  3. ZeroResponse - otherwise (this is mandatory if none of the above satisfy).

Here is how you need to write in the editor given for XPATH Dispatch mode

declare namespace cal='http://www.parasoft.com/wsdl/calculator/';
if (//cal:subtract/cal:x[. = '10'] and //cal:subtract/cal:y[. = '5']) then
    'PositiveResponse'
    else 
    if (//cal:subtract/cal:x[. = '5'] and //cal:subtract/cal:y[. = '10']) then
    'NegativeResponse'
    else
    'ZeroResponse'

Hope you are now aware and differentiate the SCRIPT Dispatch mode.

I think the confusion created because both SCRIPT and XPATH shows an editor of same type. But the content inside of it will be entirely different. Also you can easily see a message on top of the editor that log, context, mockRequest availability of variables if you select SCRIPT Dispatch mode and it vanishes when XPATH is choosen.

Just giving SCIRPT example in case if you are interested in it:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
def x = holder.getNodeValue("//*:x") as int
def y = holder.getNodeValue("//*:y") as int
context.result = x - y

A simple test can be (to differentiate between the two), copy the above script for xpath and try testing and soap fault is received saying does not know groovyUtils. This test will confirm that xpath and script are different.

Here you may not needed to create multiple responses as the above code can handle dynamic input values and set the result in the response. MockReponse for subtract operation may look like below with place holder as ${result}.

MOCKRESPONSE for SCRIPT:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="http://www.parasoft.com/wsdl/calculator/">
   <soapenv:Header/>
   <soapenv:Body>
      <cal:subtractResponse>
         <cal:Result>${result}</cal:Result>
      </cal:subtractResponse>
   </soapenv:Body>
</soapenv:Envelope>

Hope this clarifies.

Upvotes: 7

Related Questions