cancel
Showing results for 
Search instead for 
Did you mean: 

How to get soap mock response from external file!

SOLVED
rupert_anderson
Valued Contributor

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

Ok, thanks for the extra info, I think the main problem here is actually agreeing on exactly whats required rather than the script - hopefully we'll get there in the end! 🙂

 

In terms of the order number being dynamic (extracted from the Soap request), I thought that was understood - in the script sample I only hard coded it as an example i.e.

 

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

Of course this is no use in practical terms, as there will obviously be many different 'order number' values extracted from the various requests that are made to the mock. Now, just to check are you happy to extract the order number tag value from the SOAP request using Groovy script? Otherwise I can offers help on this.

 

If you are happy to extract the order number tag, then the script will effectively match the 'order number' tag value against the file names, since the statement: 

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

 

Will not find any files if $fileName doesn't match and you'll get a blank response. So in your case, if your 'order number' tag value is ordnb you'd put something like:

File testDataFile = new File("c:\Users\path\$fileName.txt")

If you'd like to check whether the file exists first (matches), then you could instead do something like:

...
File testDataFile = new File("/Users/test/$fileName") 

if (testDataFile.exists()){
//Matches, read response data from file testDataFile.eachLine {content, lineNumber -> responseFileContent.append(content) } }else{ responseFileContent="File not found!" //Or perhaps return a SOAP fault? } ...

Is any of this getting closer to what you are looking for?

 

Might have helped if I'd provided a SOAP example, rathet than this REST based one - let me know if you need that and perhaps even supply your WSDL so that I can build a Mock matching your exact case.

 

Cheers,

Rup

 

Author of SoapUI Cookbook
Apoorva6
Frequent Contributor

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

Hi Rup,

 

Rest does not work for me. Please provide Soap groovy script. I have attached sample WSDL here. in that salesOrderNumber is the dynamic value which corresponding mock response need to be loaded.  sample request is also attached.

 

 

nmrao
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

nmrao
Community Hero

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

/* If you already have all the directory structure of the project
* defined(in cvs, svn etc) and can't change as mentioned in the above script
* you may use below one which uses the location of respones as project property,
* say define a project custom property MOCK_RESPONSES_LOCATION and set the path
* in unix style ('/' as path separator) even on windows. Of course, you may use at test suite, or 
* testcase level property too if you wish. * soapFault.xml is need here too in case of failure. */ def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) def holder = groovyUtils.getXmlHolder(mockRequest.requestContent) def soNumber = holder.getNodeValue("//*:BillingOrderLoadRequest/*:salesOrderNumber") def project = mockResponse.mockOperation.mockService.project def responseLocation = project.getPropertyValue('MOCK_RESPONSES_LOCATION') def file = new File("${responseLocation}/${soNumber}.xml") def fileToLoad = 'soapFault' if (file.exists()) { fileToLoad = soNumber } context.content = new File("${responseLocation}/${fileToLoad}.xml").text


Regards,
Rao.
Apoorva6
Frequent Contributor

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

Hi Rao,

 

Thanks a lot for script. Can you please modify the script bit to include the external path instead of Soap project path ? Because I need to fetch the mock response files from external path. Also I am not using any external property.xml to include the predifined properties. So the second script you provided, I cannot use. I am creating an external path C:Users\mockResponse path to store mockresponse and Fault.xml

nmrao
Community Hero

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

All you need is to add a property at project level and specify your local path into order to use the second one. Did you try any of the above at least?


Regards,
Rao.
Apoorva6
Frequent Contributor

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

Thank you very much. It worked finally with project path. Thank you Rao and Rup both...
uma6kasturi
Occasional Contributor

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

Hi Rao,

 

can you give similar example for rest mocking(.json) too please , similar to soap  which you had suggested above 

nmrao
Community Hero

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

@uma6kasturi, It is recommended that not to cross post. Please create a new topic with as much details as you can, that will help for every one on the forum.


Regards,
Rao.
New Here?
Join us and watch the welcome video:
Top Kudoed Authors