Forum Discussion

M0nkeyG's avatar
M0nkeyG
New Contributor
6 years ago

SOAP UI assert on JSON Response check not 0

Hi. Cannot find an aswer in other posts, hoping this isn't too niche!

Basically I have an API getting a response on data size (along with a few other APIs calling on other info)

I have automated test scripts in SOAP UI calling on each API to check the repsonses at various stages of the process.

I have an assert on one of the calls that checks the response is 0 bytes in size then the following test scripts creates data and checks other APIs then it deletes the data and checks again.

I want to add an assert on an API call JSON response that checks the data size is not 0 (i.e. there is now data where there was none before).

I can assert the JSON to check size is 0 at the start and at the end of the process, that was easy, but because the data size in other tests is completely random I cannot assert a specific data size so I just want it to check that it is not 0.

Any ideas?

FYI - I cannot provide any more clues than this, what the API is calling on does not matter and what the JSON looks like does not matter, I just need a way to assert that JSON response is not 0. Extra bonus point if I can get another that then says data size is less than previous.

Step 1 - assert JSON response data size is 0 (done)

Step 2 - assert JSON response data size is not 0

Step 3 (bonus point) - assert JSON response data size is less than size in step 2

Step 4 - assert JSON response data size is 0 (done)

  • Thank you to: https://thetestsuite.wordpress.com/2013/09/30/soapui-groovy-slurping-json-in-script-assertions/

     

    Script assert....

     

    //imports
    import groovy.json.JsonSlurper

    //grab the response
    def ResponseMessage = messageExchange.response.responseContent
    //define a JsonSlurper
    def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)

    //verify the slurper isn't empty
    assert !(jsonSlurper.isEmpty())

    //verify the queue size is not 0
    assert jsonSlurper.lastRecordedQueueSize != 0

    //verify the queue size is not null
    assert jsonSlurper.lastRecordedQueueSize != null

     

    Can't figure out the bonus point question though (yet) - can't see how to verify 1 assert from one response against the value of another assert from another response - playing with Groovy script but anyone got any tips on this would be amazeballs!

  • //imports
    import groovy.json.JsonSlurper

    //grab the response
    def ResponseMessage = testRunner.testCase.testSteps["Local Cluster During Flush"].testRequest.response.responseContent
    def oldResponseMessage = testRunner.testCase.testSteps["Local Cluster With Queued Data"].testRequest.response.responseContent
    //define a JsonSlurper
    def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
    def oldJsonSlurper = new JsonSlurper().parseText(oldResponseMessage)

    //verify the slurper isn't empty
    assert !(jsonSlurper.isEmpty())

    //verify the queue size is not 0
    assert jsonSlurper.lastRecordedQueueSize != 0

    //verify the queue size is not null
    assert jsonSlurper.lastRecordedQueueSize != null

    // Convert old size String to an Integer
    def oldSizeStr = oldJsonSlurper.lastRecordedQueueSize
    oldSizeStr = oldSizeStr.substring(0, oldSizeStr.indexOf("(")).trim()
    oldSizeStr = oldSizeStr.replaceAll(",", "")
    int oldSize = oldSizeStr.toInteger()

    // Convert new size String to an Integer
    def newSizeStr = jsonSlurper.lastRecordedQueueSize
    newSizeStr = newSizeStr.substring(0, newSizeStr.indexOf("(")).trim()
    newSizeStr = newSizeStr.replaceAll(",", "")
    int newSize = newSizeStr.toInteger()

    // Compare old and new sizes
    assert newSize < oldSize

  • M0nkeyG's avatar
    M0nkeyG
    New Contributor

    Thank you to: https://thetestsuite.wordpress.com/2013/09/30/soapui-groovy-slurping-json-in-script-assertions/

     

    Script assert....

     

    //imports
    import groovy.json.JsonSlurper

    //grab the response
    def ResponseMessage = messageExchange.response.responseContent
    //define a JsonSlurper
    def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)

    //verify the slurper isn't empty
    assert !(jsonSlurper.isEmpty())

    //verify the queue size is not 0
    assert jsonSlurper.lastRecordedQueueSize != 0

    //verify the queue size is not null
    assert jsonSlurper.lastRecordedQueueSize != null

     

    Can't figure out the bonus point question though (yet) - can't see how to verify 1 assert from one response against the value of another assert from another response - playing with Groovy script but anyone got any tips on this would be amazeballs!

    • M0nkeyG's avatar
      M0nkeyG
      New Contributor

      //imports
      import groovy.json.JsonSlurper

      //grab the response
      def ResponseMessage = testRunner.testCase.testSteps["Local Cluster During Flush"].testRequest.response.responseContent
      def oldResponseMessage = testRunner.testCase.testSteps["Local Cluster With Queued Data"].testRequest.response.responseContent
      //define a JsonSlurper
      def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
      def oldJsonSlurper = new JsonSlurper().parseText(oldResponseMessage)

      //verify the slurper isn't empty
      assert !(jsonSlurper.isEmpty())

      //verify the queue size is not 0
      assert jsonSlurper.lastRecordedQueueSize != 0

      //verify the queue size is not null
      assert jsonSlurper.lastRecordedQueueSize != null

      // Convert old size String to an Integer
      def oldSizeStr = oldJsonSlurper.lastRecordedQueueSize
      oldSizeStr = oldSizeStr.substring(0, oldSizeStr.indexOf("(")).trim()
      oldSizeStr = oldSizeStr.replaceAll(",", "")
      int oldSize = oldSizeStr.toInteger()

      // Convert new size String to an Integer
      def newSizeStr = jsonSlurper.lastRecordedQueueSize
      newSizeStr = newSizeStr.substring(0, newSizeStr.indexOf("(")).trim()
      newSizeStr = newSizeStr.replaceAll(",", "")
      int newSize = newSizeStr.toInteger()

      // Compare old and new sizes
      assert newSize < oldSize