Forum Discussion

uma6kasturi's avatar
uma6kasturi
Occasional Contributor
8 years ago
Solved

replace response with parameter from request

Hi All,

 

In the below case i need a script for both rest and soap services 

 

SOAP services

1.Request consists of a value UPRN which is the key driver 

2.I have external file where my UPRN file specific responses are placed , if specific file is found response should be specific to file --- I have implemented this already

3.if in the folder of responses the UPRN file is not found then I have a default xml where in , the UPRN field of the default xml should be changed to the one supplied in my request and I should get an amended response back with the UPRN field only changed to the value that came from request , rest all content be same as its from default file .

 

Is this possible ??

 

attaching request and response 

 

I would need thsi for both soap and rest services , in rest I have a parameter which needs to be replaced 

 

 

 

Adding files for my Rest request and response too  and also the code I implemented till now .

 

I would need code to be modified such that in case if post file doesn't exist the post code in the response should be changed to the post code from request and the response to be served back , only the post code to be changed rest all to be retained as is 

 

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
String URL = mockRequest.getRequest().getQueryString()
log.info "QueryString: " + URL

def postcode = URL.substring(URL.lastIndexOf("postcode")+9, URL.lastIndexOf("postcode")+15);
log.info "Postcode: " + postcode
/*def soNumber = holder.getNodeValue("//*:BillingOrderLoadRequest/*:salesOrderNumber")*/

def responseFileContent = new StringBuilder()
File testDataFile = new File(groovyUtils.projectPath+"/mockResponsesjson/${postcode}.json") //Load file containing test data response
if (testDataFile.exists()){
testDataFile.eachLine {content, lineNumber ->
responseFileContent.append(content) //Add each line of test data file to a String
}
}else{
responseFileContent="File not found!" //Or perhaps return a SOAP fault?
}
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"]=["application/json"]
mockResponse.responseHeaders=headers //Headers can also be set in the UI

  • Hi,

     

    I think if you create a new mock response called Default Response and paste the default.xml XML into it (with the property expansion mentioned before) then the following code change should put the customerid into the property uprn and despatch the Default Response containing the customerid:

     

     

    else{
    /*
    def tempContent = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils.projectPath+"/MOCK/default.xml"))
    /**replacing the place holders in the response **/
    tempContent = tempContent.replace("\$uprnumber",customerId)
    
    //finall setting the response content
    context.content = tempContent
    */
    
    requestContext["uprn"]=customerId
    return "Default Response"
    }

    Make sense?

    Regards,

    Rupert

     

6 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    I believe that, you talking about mocking.

    Would you want to show the script you implemented already? And what is the exact issue that you have now with the implemented script?
  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    Yes, from your description I would say that that is completely possible - you're talking about creating REST and SOAP mocks to do this right?

     

    If so, to get the default response to contain the UPRN you could set a property in the requestContext e.g. 

    requestContext["uprn"]=.. value taken from the request...

    Then, if no file exists, you can despatch the default response and insert property into the default response using a property expansion e.g.

    <SiteClassification>Detached</SiteClassification>
    <SiteClassificationCode>RD02</SiteClassificationCode>
    <UPRN>${uprn}</UPRN>
    </Site>

    Is this the sort of thing you meant?

    Regards,

    Rupert

     

     

    • uma6kasturi's avatar
      uma6kasturi
      Occasional Contributor

      Hi rupert_anderson

      nmrao

      I am posting my script I am having problem with the else part where I want Rupert above solution to be inserted and the new xml to be served back 

       


      def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
      def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
      def soNumber = holder.getNodeValue("//UPRN")
      def customerId = mockRequest.requestHeaders['UPRND'][0]

      def file = new File (groovyUtils.projectPath+"/mockResponses/${soNumber}.xml")
      log.info soNumber
      def fileToLoad = 'SoapFault'
      if (file.exists()) {
      fileToLoad = soNumber
      context.content = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils.projectPath+"/mockResponses/${fileToLoad}.xml"))
      }
      else{

      def tempContent = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils.projectPath+"/MOCK/default.xml"))
      /**replacing the place holders in the response **/
      tempContent = tempContent.replace("\$uprnumber",customerId)

      //finall setting the response content
      context.content = tempContent
      }

    • uma6kasturi's avatar
      uma6kasturi
      Occasional Contributor

      Hi Rupert ,

       

      Yes this is the sort of stuff I am looking at , I have pasted my code which I wrote already in the post 

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Hi,

         

        I think if you create a new mock response called Default Response and paste the default.xml XML into it (with the property expansion mentioned before) then the following code change should put the customerid into the property uprn and despatch the Default Response containing the customerid:

         

         

        else{
        /*
        def tempContent = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils.projectPath+"/MOCK/default.xml"))
        /**replacing the place holders in the response **/
        tempContent = tempContent.replace("\$uprnumber",customerId)
        
        //finall setting the response content
        context.content = tempContent
        */
        
        requestContext["uprn"]=customerId
        return "Default Response"
        }

        Make sense?

        Regards,

        Rupert