1
votes

I have set-up a mock SOAPUI project that returns mock responses when I call the endpoint. What I'm trying to do is return a different mock result depending on the requested content is. At the moment when I hit the endpoint URL, it is returning the same response all of the time.

Is it possible with a groovy script to look at the content of the request and send a mock response based on its content? The request is SOAP so i'd be inspecting XML

Here I have two mock requests. What I'm looking at is the Request details node and what type it is i.e. TelephoneNumber or PostCode and then send the response based on that. Is it possible to check for the node type i.e TelephoneNumber, PostCode and send a response based on that?

Post Code Request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lin="http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS">
   <soapenv:Header/>
   <soapenv:Body>
      <lin:GetLineCharacteristics>
         <lin:request>
            <!--Optional:-->
            <lin:UserCredentials>
               <!--Optional:-->
               <lin:Username>Test</lin:Username>
               <!--Optional:-->
               <lin:Password>Test</lin:Password>
               <lin:AgentID>321</lin:AgentID>
            </lin:UserCredentials>
            <lin:RequestDetails xsi:type="lin:PostcodeRequest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <lin:Postcode>L00 0DD</lin:Postcode>
            </lin:RequestDetails>
            <lin:UserConsent>Yes</lin:UserConsent>
            <lin:ServiceType>SMPF</lin:ServiceType>
         </lin:request>
      </lin:GetLineCharacteristics>
   </soapenv:Body>
</soapenv:Envelope>

Post Code Response

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetLineCharacteristicsResponse xmlns="http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS">
         <GetLineCharacteristicsResult>
            <Status>
               <HasErrors>false</HasErrors>
               <Errors/>
            </Status>
            <ResponseDetails>
               <CharacteristicsBase xsi:type="LineCharacteristicsForTelephoneNumber">
                  <ExchangeDetails>
                     <CSSExchangeDistrictCode>LV</CSSExchangeDistrictCode>
                     <ExchangeCode>LVSIM</ExchangeCode>
                     <ExchangeName>Simonswood</ExchangeName>
                     <ExchangeState>Live</ExchangeState>
                     <ExchangeCapacity>G</ExchangeCapacity>
                     <ForecastDate>0001-01-01T00:00:00</ForecastDate>
                     <CircuitLineLength>
                        <MeasureType>CircuitEstimated</MeasureType>
                        <Length>3549</Length>
                     </CircuitLineLength>
                  </ExchangeDetails>
                  <TechnologyMessages>
                     <TechnologyMessage>
                        <Code>LLUSMPF</Code>
                        <Message>LLU Installation</Message>
                     </TechnologyMessage>
                  </TechnologyMessages>
                  <EstimatedSpeedInformation>
                     <EstimatedSpeedInformation>
                        <EstimatedSpeedInformationType>Standard</EstimatedSpeedInformationType>
                        <EstimatedAnnexADownstreamSpeed>3647</EstimatedAnnexADownstreamSpeed>
                        <EstimatedAnnexAMaxRange>5519</EstimatedAnnexAMaxRange>
                        <EstimatedAnnexAMinRange>2287</EstimatedAnnexAMinRange>
                        <MinThresholdAnnexA>1657</MinThresholdAnnexA>
                        <EstimatedAnnexMDownstreamSpeed>2647</EstimatedAnnexMDownstreamSpeed>
                        <EstimatedAnnexMUpstreamSpeed>1000</EstimatedAnnexMUpstreamSpeed>
                     </EstimatedSpeedInformation>
                  </EstimatedSpeedInformation>
                  <TelephoneNumber>01515481460</TelephoneNumber>
                  <AccessLineID>LVAAACJ8C2</AccessLineID>
                  <AccessLineStatus>BTLive</AccessLineStatus>
                  <NumberRangeHolder>BT</NumberRangeHolder>
                  <NumberPortingInformation>NotSet</NumberPortingInformation>
                  <WorkingLineDetails>
                     <ProductType>PSTN Single Line</ProductType>
                     <PendingCeaseOrderIndicator>N</PendingCeaseOrderIndicator>
                  </WorkingLineDetails>
               </CharacteristicsBase>
            </ResponseDetails>
         </GetLineCharacteristicsResult>
      </GetLineCharacteristicsResponse>
   </soap:Body>
</soap:Envelope>

Telephone Request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lin="http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS">
   <soapenv:Header/>
   <soapenv:Body>
      <lin:GetLineCharacteristics>
         <lin:request>
            <!--Optional:-->
            <lin:UserCredentials>
               <!--Optional:-->
               <lin:Username>"Test</lin:Username>
               <!--Optional:-->
               <lin:Password>Test</lin:Password>
               <lin:AgentID>111</lin:AgentID>
            </lin:UserCredentials>
            <lin:RequestDetails xsi:type="lin:TelephoneNumberRequest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <lin:TelephoneNumber>11111111111</lin:TelephoneNumber>
            </lin:RequestDetails>
            <lin:UserConsent>Yes</lin:UserConsent>
            <lin:ServiceType>SMPF</lin:ServiceType>
         </lin:request>
      </lin:GetLineCharacteristics>
   </soapenv:Body>
</soapenv:Envelope>

Telephone Response

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetLineCharacteristicsResponse xmlns="http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS">
         <GetLineCharacteristicsResult>
            <Status>
               <HasErrors>false</HasErrors>
               <Errors/>
            </Status>
            <ResponseDetails>
               <CharacteristicsBase xsi:type="LineCharacteristicsForTelephoneNumber">
                  <ExchangeDetails>
                     <CSSExchangeDistrictCode>LV</CSSExchangeDistrictCode>
                     <ExchangeCode>LVSIM</ExchangeCode>
                     <ExchangeName>Simonswood</ExchangeName>
                     <ExchangeState>Live</ExchangeState>
                     <ExchangeCapacity>G</ExchangeCapacity>
                     <ForecastDate>0001-01-01T00:00:00</ForecastDate>
                     <CircuitLineLength>
                        <MeasureType>CircuitEstimated</MeasureType>
                        <Length>3549</Length>
                     </CircuitLineLength>
                  </ExchangeDetails>
                  <TechnologyMessages>
                     <TechnologyMessage>
                        <Code>LLUSMPF</Code>
                        <Message>LLU Installation</Message>
                     </TechnologyMessage>
                  </TechnologyMessages>
                  <EstimatedSpeedInformation>
                     <EstimatedSpeedInformation>
                        <EstimatedSpeedInformationType>Standard</EstimatedSpeedInformationType>
                        <EstimatedAnnexADownstreamSpeed>3647</EstimatedAnnexADownstreamSpeed>
                        <EstimatedAnnexAMaxRange>5519</EstimatedAnnexAMaxRange>
                        <EstimatedAnnexAMinRange>2287</EstimatedAnnexAMinRange>
                        <MinThresholdAnnexA>1657</MinThresholdAnnexA>
                        <EstimatedAnnexMDownstreamSpeed>2647</EstimatedAnnexMDownstreamSpeed>
                        <EstimatedAnnexMUpstreamSpeed>1000</EstimatedAnnexMUpstreamSpeed>
                     </EstimatedSpeedInformation>
                  </EstimatedSpeedInformation>
                  <TelephoneNumber>11111111111</TelephoneNumber>
                  <AccessLineID>LVAAACJ8C2</AccessLineID>
                  <AccessLineStatus>BTLive</AccessLineStatus>
                  <NumberRangeHolder>BT</NumberRangeHolder>
                  <NumberPortingInformation>NotSet</NumberPortingInformation>
                  <WorkingLineDetails>
                     <ProductType>PSTN Single Line</ProductType>
                     <PendingCeaseOrderIndicator>N</PendingCeaseOrderIndicator>
                  </WorkingLineDetails>
               </CharacteristicsBase>
            </ResponseDetails>
         </GetLineCharacteristicsResult>
      </GetLineCharacteristicsResponse>
   </soap:Body>
</soap:Envelope>

I can't do this using the headers due to the Webservice that I'm trying to mock.

this is a version of the Groovy script I'm writing

import com.eviware.soapui.support.GroovyUtils
import groovy.xml.XmlUtil

def groovyUtils = new GroovyUtils(context)
def xmlParser = new XmlParser()
def responseContent

def requestXmlHolder = groovyUtils.getXmlHolder(mockRequest.getRequestContent())
requestXmlHolder.declareNamespace("lin", "http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS")

log.info(requestXmlHolder)

def postCode = requestXmlHolder.getNodeValue("//PostCode")

log.info(postCode)

thanks

Chris

1
Here is an example of rest mock and it should be similar even if you are looking for soap request. - Rao
thank you. I have managed to do this with Rest. I'm wondering how you would go about it using soap? - chrisblue13
Sorry, did not get your question. - Rao
the example that you suggested is using Rest. I'm looking at soap xml request to check which response to return - chrisblue13
Ok, do you want to provide the same request and the output expected? Edit the question and add details. - Rao

1 Answers

2
votes

Yes it's possible to use different responses based on the request contents in SOAPUI mock services.

First of all in your mockService generate the possible responses. Right click on your MockOperation and select New MockResponse, make the action for your both responses (in your case Post code, and Telephone responses).

enter image description here

Then in the MockOperation editor, select Dispatch SCRIPT.

enter image description here

And use a Groovy script to evaluate which response the service will send based on the request content, to do so you can use the follow Groovy script:

// parse the content of the SOAP request
def slurper = new XmlSlurper().parseText(mockRequest.getRequestContent())
// find for example if <TelephoneNumber> node exists
def phone = slurper.'**'.find { it.name() == 'TelephoneNumber' }
if(phone?.size() > 0){
    // if there is at least one <TelephoneNumber> return telephoneResponse
    return 'TelephoneResponse'
}

// if no <TelephoneNumber> is found, return postCode
return 'PostCodeResponse'

Note that mockRequest object is available at script context on mock operation. Note also that the return in script on mock operation must return an string which match the name of the configured response.

Hope this helps,