Forum Discussion

abc123's avatar
abc123
New Contributor
9 years ago
Solved

use get as a template for post in RESTful server

Hello all,

I'm new to SOUPUI, and I'm using the free version of SOUPUI. I’m trying to write some test cases.

I spent all morning looking for this and I can't believe this has never been asked.  I'm sure I'm using the wrong nomenclature to ask this and so my search is failing.

 

What I want to do is:

  • make a GET request

[{

   "id": "123",

   "name": “abc”,

   "encrypted": false,

   “happy”: true

}]

 

  • Modify one or two items in the JSON response

[{

   "id": "123",

   "name": “ghi”,

   "encrypted": true,

   “happy”: true

}]

 

 

  • Send the JSON response to my server as a POST – thus modifying the record (123) on the server

If someone could point me to a simple example that would be great.

I see lots of examples on how to do a property transfer and such but nothing on reusing a response.

I might have to use Groovy to do this but it seems like SOAPUI can do it already.

 

Thanks in advance

 

 

 

  • Hi,

     

    As simple as you expected? I thought you were surprised that it wasn't simpler to do in your first post! I am happy you are happy anyway! :-)

     

    I notice you used JSONPath rather than JSON Slurper, which is fair enough, but I would say that JSON Slurper is a little neater e.g. no need to import and a little nicer syntax - would make the solution something like:

     

    import groovy.json.JsonSlurper

     

    def response = context.expand('${GetRecord#Response}')

    slurper = new JsonSlurper().parseText(response)

    slurper.name = 'abc123'

    testRunner.testCase.testSteps["UpdateRecord"].setPropertyValue("Request", slurper)

     

    (I have tested this now, whereas before I was guessing :-) )

     

    Also instead of setting a context property, you have now decided to update the TestStep request property directly:

    testRunner.testCase.testSteps["UpdateRecord"].setPropertyValue("Request", jsonString)

    quite nice though!

     

    If you're happy to please can you mark the topic as solved so that others might benefit? 

     

    Thanks for sharing your solution back,

     

    Cheers,

    Rup

7 Replies

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    I would personally use a Groovy TestStep for this. A Property Transfer TestStep looks like an option, as it provides a higher level means to get the reponse and then use it as the request for a second request, but I can't see a way that it could change any values whilst doing this.

     

    In terms of a Groovy TestStep approach, I would try something like:

    TestCase:

    1) Request 1

    2) Groovy TestStep

    3) Request 2 (POST) using changed response from 1

     

    In the Groovy TestStep, I would:

     

    //Get the response JSON

    def responseJson = context.expand('${Request 1#Response}')

     

    //Use JSON Sluper (part of standard Groovy) to parse and change whatever properties.

    def slurper = new JsonSlurper().parseText(responseJson)
    slurper.name=='ghi'

     

    (see my article for a full example https://www.packtpub.com/books/content/restful-web-service-mocking-and-testing-soapui-raml-and-json-slurper-script-assertion)

    (see also http://www.groovy-lang.org/json.html)

     

    //Add the resulting JSON as a property on the context varaible (part of the Groovy TestStep, which is passed and visible to subsequent TestSteps in the TestCase, like the Request 2 above)

    context["requestJson"]=slurper (think the slurper object should return the JSON string back, but haven't tested this syntax, appologies if you need toString() or similar other method)

     

    //Use the context variable as the body of the POST of request 2

    Use ${requestJson} to reference the altered request JSON in the POST body of the Request TestStep

     

    Hope this helps,


    Cheers,

    Rupert

    • abc123's avatar
      abc123
      New Contributor

      Rupert,

      Thanks for your reply, it got me a little farther.

       

      I'm still baffled why this idea of GET record, Modify record, PUT record is not more common; this makes me think I'm doing something wrong.

       

      I took a look at your book reference and that helped me understand the slurper a little better. I’m ready to purchase a book on SOAPUI if I believe it has enough information on the Free version. I see many references to the NG/ ReadyAPI version which makes me shy away some books.

       

      This is what I have so far:

      def responseJson = context.expand('${Response 1#Response}')
      def slurper = new JsonSlurper().parseText(responseJson)
      slurper.name == 'BBE123'
      def requestJson = context.blah('$Response 2#Request}')
      requestJson.blat = slurper

      I can’t answer your toString() comment until I figure out the ‘blah’ and 'blat' methods above.

       

      As you can see I'm new to objects and what goes where. 

       

      Thanks again for your help.

       

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Hi,

         

        No problem, happy if I can help.

         

        I don't think doing a GET, modify response, followed by a POST is especially rare - but possibly there have been relatively few reported issues or maybe people put it under different titles? It is certainly not as common in my experience as questions relating to data-driven testing e.g loading data, using it to request and looping until finished.

         

        In terms of my article, I thought it might help as an illustration of JSON Slurper. In terms of my book, there are only 5 pro recipes in 75, so there is a strong bais towards the open-source version of SoapUI and ways to fill some common gaps. SoapUI is a highly extensible tool, but you have to work at it sometimes, this is mainly what the book tries to nurture. The pro version sometimes tries to make some of the common issues more convenient by supplying nice tools e.g. point and click XPath expresion builder and ability to create property expansion expressions. But I try not to talk about the book too much in this community, as it isn't free and looks like I'm trying to self publicize! :-)

         

        You're JSON Sluper script is looking good as far as: 

        def responseJson = context.expand('${Response 1#Response}')
        def slurper = new JsonSlurper().parseText(responseJson)
        slurper.name == 'BBE123'

        This might get the response from the first call, parse it and modify the name.

         

        Then you could do:

        context["requestBodyJSON"]=slurper (assuming thats the syntax for now, think it is)

         

        Then in the POST body of the Test Request, use ${requestBodyJSON} to insert the modified content when the TestCase is run.

         

        Cheers,

        Rup