SoapUI Mock. How to select response relying on tag value in request

624 Views Asked by At

I have SOAP request looks like this:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gd="http://test.com/gds-mvmnt">
    <env:Header/>
    <env:Body>
        <ns1:getContainer env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
            <code xsi:type="xsd:string">PCK26397841</code>
            <messageId xsi:type="xsd:string"/>
        </ns1:getContainer>
    </env:Body>
    </env:Envelope>

I need to create mock which select response based on code tag value. No need to modify responses. Responses added to mock as is. Just need logic like this: if code A then Response 1, if code B then Response 2 and so on. Please help me with basic groovy script.

1

There are 1 best solutions below

0
On

That is possible by writing simple Script Dispatch.

Here is the approach:

  • Check if the request is not empty
  • Define a map which consists of Code and its respective Response name
  • Extract the code from the mock request and send respective response based on the above map
  • Hope you know to define multiple responses and script dispatch method

Here is the Script:

//Define desired code and response name
def responseMap = [A: 'Response1', B: 'Response2']

//Check if the request is not empty
assert mockRequest.requestContent, 'Request is empty'

//Extract the code
def code = new XmlSlurper().parseText(mockRequest.requestContent).'**'.find{it.name() == 'code'}?.text()

//Return the respective response
return responseMap[code]