Showing posts with label soap ui mock service. Show all posts
Showing posts with label soap ui mock service. Show all posts

Wednesday, October 16, 2013

SOAP UI Dynamic mock service using Groovy Script


SOAP UI Dynamic mock service using Groovy Script

Create dynamic mock service using SOAP UI . You will just need the WSDL . You should be familiar using the soap ui and creating the mock service . Following is to only make it dynamic
  1. Create a SoapUI project using the WSDL . Right click and select the mock service .
  2. Select the  WebService Method and Open the Response that has to be customized
  3. Create two files for Success and Error response and place them in folder  {Project-Path}/responses
  4. WebService Response ( Right portion of the Soap UI Window )will be made dynamic based on request.  Delete everything from mock response and  replace with${content} .  This value will be dynamically rendered from  groovy script
  5. Choose Dispatch -> Script , and then choose Dynamic Reponse
  6. Use the groovy script below , to render Success and Error Responses .

It works in the following way ,

  • if quotenumber is 9999  - send Error response
  • anything else - send Success Response


Now you can create more test cases and test them by changing the input in the xml . Providing  the input , output and the script below


Input ( for ref )

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://www.testing.com/WS/quoteWS">

  <soapenv:Header/>

  <soapenv:Body>

     <test:Quote>

        <QuoteRequest>

           <quoteNumber>9999</quoteNumber>

           <quoteTitle>?</quoteTitle>

           <quotePrice>?</quotePrice>

        </QuoteRequest>

     </test:Quote>

  </soapenv:Body>

</soapenv:Envelope>'



Output

Success
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://www.testing.com/WS/quoteWS">

  <soapenv:Header/>

  <soapenv:Body>

     <test:QuoteResponse>

        <QuoteReply>

           <status>Success</status>

           <!--Optional:-->

           <errorCode>0</errorCode>

           <!--Optional:-->

           <errorMessage></errorMessage>

           <!--Optional:-->

           <errorDetail></errorDetail>

        </QuoteReply>

     </test:QuoteResponse>

  </soapenv:Body>

</soapenv:Envelope>



Error

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://www.testing.com/WS/quoteWS">

  <soapenv:Header/>

  <soapenv:Body>

     <test:QuoteResponse>

        <QuoteReply>

           <status>ERROR</status>

           <!--Optional:-->

           <errorCode>11</errorCode>

           <!--Optional:-->

           <errorMessage>Failed</errorMessage>

           <!--Optional:-->

           <errorDetail>Something failed </errorDetail>

        </QuoteReply>

     </test:QuoteResponse>

  </soapenv:Body>

</soapenv:Envelope>


Groovy Script
import com.eviware.soapui.support.GroovyUtils

import groovy.xml.XmlUtil

log.info 'start'

def groovyUtils = new GroovyUtils(context)

def xmlParser = new XmlParser()

def responseContent

def requestXmlHolder = groovyUtils.getXmlHolder(mockRequest.getRequestContent())

requestXmlHolder.declareNamespace("test","http://www.testing.com/WS/quoteWS")

def quoteNumber = requestXmlHolder.getNodeValue("//test:Quote/QuoteRequest[1]/quoteNumber[1]")

log.info  quoteNumber



if(quoteNumber == '9999')

{

responseContent = xmlParser.parse(groovyUtils.projectPath +"/responses/error-response.xml")


}

else

{

responseContent = xmlParser.parse(groovyUtils.projectPath +"/responses/success-response.xml")

}

context.content = XmlUtil.serialize(responseContent)

log.info 'complete'