cancel
Showing results for 
Search instead for 
Did you mean: 

How to get soap mock response from external file!

SOLVED
Highlighted
Frequent Contributor

How to get soap mock response from external file!

I have soap request with ordernumber tag. I will be saving mock response with file name same as ordernumber value in request. How to get mock response of specific ordernumber request? There will be multiple users placing response file in same path with name of ordernumber value from request. They need to get their corresponding response only after hiting same mock URL. I am using free version of soapui .Thanks in advance.
1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: How to get soap mock response from external file!

Sorry to interrupt Rup.

The details helped Apoorva.

 

And this is my first try on mocking, followed the video from the same link that I provided earlier.

 

Here are the steps:

 

1. Generate a SOAP Mock Service by right click on to the service that you intended ( I believe, you know it already, added this for those who not aware, and this is first time for me too)

 

2. Selete the intended mock operation.

  • Choose SCRIPT for Dispatch, Response 1 for Default Response
  • Open Response 1 and remove the existing reponse and just have ${content} as value for response
  • In the script editor have the following script:

 

/*This script reads the salesOrderNumber from request using xpath
* and checks corresponding response xml file from
* mockResponses directory of your soapui project location.
* Also expects a soapFault.xml file in the same location
* in order to send soap fault if no sales order number maches the
* existing files.
* For example, soapui project located under C:\soapuiProjects
* create a subdirectory called mockResponses under above directory
* Make sure all your mock response files for sales orders under
* C:\soapuiProjects\mockResponses directory including soapFault.xml
* Assuming that the soap response file extension is xml, not txt
*/
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) def holder = groovyUtils.getXmlHolder(mockRequest.requestContent) def soNumber = holder.getNodeValue("//*:BillingOrderLoadRequest/*:salesOrderNumber") def file = new File (groovyUtils.projectPath+"/mockResponses/${soNumber}.xml") def fileToLoad = 'soapFault' if (file.exists()) { fileToLoad = soNumber } context.content = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils.projectPath+"/mockResponses/${fileToLoad}.xml"))

 

Sample soapFault.xml

 

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">   
   <Body>   
      <Fault>
         <faultcode xsi:type="xsd:string">Client</faultcode>		 
         <faultstring xsi:type="xsd:string">Could not find the mock response file for the given sales order number</faultstring>		 
      </Fault>
   </Body>   
</Envelope>

 

 

 



Regards,
Rao.

View solution in original post

18 REPLIES 18
Frequent Contributor

How to get soap mock response from external file!

I have soap request with ordernumber tag. I will be saving mock response with file name same as ordernumber value in request. How to get mock response of specific ordernumber request? There will be multiple users placing response file in same path with name of ordernumber value from request. They need to get their corresponding response only after hiting same mock URL. I have soapui free version. Thanks in advance.
Community Hero

Re: How to get soap mock response from external file!

You may use groovy script to send the mock response. So, you can read the file there in the script and send that as response.
Following should help you to give an idea to the above:
http://www.soapui.org/soap-mocking/creating-dynamic-mockservices.html


Regards,
Rao.
Frequent Contributor

Re: How to get soap mock response from external file!

Rao, Thank you. However I am poor in groovy. Could you please help me with the script? If we get ordernumber as 1234 then we save the mock response in C:\users\order folder as 1234.txt
Occasional Contributor

Re: How to get soap mock response from external file!

Your mock-response should be a groovy script. There you can read in your file (filename based on the submitted ordernumber) and then you have to set an internal variable like this:

 

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
def holder = groovyUtils.getXmlHolder(mockRequest.requestContent);
String sInputOrderNumber = holder.getNodeValue("//<... here you need the tag from incomming request>");
lUtil.log(LogUtil.LogLevel.INFO, " Incomming ordernumber: " + sInputOrderNumber);

...

// read correct file based on ordernumber

String sResult = ... getOrderNumberResult( sInputOrderNumber );
// set context variable
context.setProperty("result", sResult);

 

Then, in your mock-response, you only put something like this:

${result}

 

That means, whatever you read in from your file will be returned to the caller. That also means tha you should have your complete returnmessage in your mock-file.

 

( P.S.: Of course, you can have your retunr message in the response and only put the return variable there it belonged in the message.)

Valued Contributor

Re: How to get soap mock response from external file!

Hi,

 

Sorry to hop in, but assuming you're happy to extract the file name from the mockRequest (using Groovy), then does the following give you what you need:

 

1. In your mock action, create a new Mock MockResponse with dispatch type 'SEQUENCE' e.g. called 'default'

 

2. In the MockResponse (default) add a script e.g.

 

def fileName = "1234.txt" //Extract this value from the request (ask if you need help)

def responseFileContent = new StringBuilder()
File testDataFile = new File("/Users/test/$fileName") //Load file containing test data response

testDataFile.eachLine {content, lineNumber ->
          responseFileContent.append(content) //Add each line of test data file to a String
}

mockResponse.responseContent=responseFileContent //Populate response body
mockResponse.responseHttpStatus=200 //set status code (can also be done in UI)

def headers = mockResponse.responseHeaders
headers["Content-Type"]=["text/xml"]
mockResponse.responseHeaders=headers //Headers can also be set in the UI

Here is a screen shot:

 

Screen Shot 2015-12-18 at 17.07.25.png

 

This should load and despatch the contents of the file /Users/test/1234.txt as the body of the mock response. All that should then be needed is for you to add some script before my script to set fileName to the value you want to extract from the mockRequest object (let me know if you need any help with this). Setting the responseHttpStatus and responseHeaders is optional, as it can be set in the Mock Response UI - I added it as it can be useful in the case where you want to conditionally simulate error / 404 / 500 statuses or respond with different content types!  

 

Hope this helps,

Rupert

Author of SoapUI Cookbook
Frequent Contributor

Re: How to get soap mock response from external file!

Hi Rupert, thnx for reply. I already have script which can load from file. I need the script which can compare the ordernumber value, compare with file names and load the one which has ordernumber.txt file. Thnx in advance
Valued Contributor

Re: How to get soap mock response from external file!

Hi,

 

Ok, sorry, in that case I'm not totally sure what you need..

 

Where you say compare order number, compare file names, load the matching file - I take that to mean something like:

 

1. Request is made to your mock

2. You extract the ordernumber from the request

3. Build a file path to load the ordernumber.txt file (shown in the sample I provided)

 

Is this what you mean? Then what do you want to do? Since the title of the post says 'get soap mock response from external file' - I was thinking you would then want to despatch the loaded file contents as the mock's response?

Author of SoapUI Cookbook
Frequent Contributor

Re: How to get soap mock response from external file!

Hi, let me put this way. There can be unlimited files in the path containing mock response. If the order number is xyz then we need to load from xyz.txt else we error. So we need to compare ordernumber value with value.txt in path and if not found, we error
Frequent Contributor

Re: How to get soap mock response from external file!

Just to clear myself again.

 

I have ordernumber tag in request . it can have multiple input values (dynamic) say $ordnb. All the time I need to check if ($ordnb).txt file is present in the path C:Users/test path  or not. If present I need to load mock response from ($ordnb).txt file. Else throw an error as Response not found in response.