Forum Discussion

bmcim123's avatar
bmcim123
Occasional Contributor
7 years ago
Solved

How to create a Groovy Script in order to perform assertions for sorting (ascending/descending)

My API response returns customer file details and can sort a chosen field in ascending or descending order.  

 

Example,  the JSON REST RESPONSE as detailed further below returns the 'UploadedBy' field as expected in ascending order.  

 

I now need to verify this.  Can someone help me with a groovy script for this and PLEASE INCLUDE COMMENTS so that I can completely understand the code, for my own learning purposes.

 

Thank-you, much appreciated.

 

 

JSON RESPONSE
"results": [
{
"trackingEntryId": 24,
"documentId": 24,
"fileId": 24,
"fileName": "Sample.xlsx",
"status": "Success",
"uploadedBy": "Gibson, Mike",
"uploadedDate": "2017-06-21T08:31:23.2021627",
"transactions": 1
},
{
"trackingEntryId": 25,
"documentId": 25,
"fileId": 25,
"fileName": Sample1.xlsx",
"status": "Success",
"uploadedBy": "Hill, Lucy",
"uploadedDate": "2017-06-21T08:32:05.4842927",
"transactions": 1
},
{
"trackingEntryId": 26,
"documentId": 26,
"fileId": 26,
"fileName": "TrackIT_Test4.xlsx",
"status": "Success",
"uploadedBy": "Sinatra, Frank",
"uploadedDate": "2017-06-21T08:33:24.0482512",
"transactions": 0
}

  • Not a problem, always happy to help. Let me know if you run into issues. 

3 Replies

  • groovyguy's avatar
    groovyguy
    Champion Level 1

     Here's what I have. It'll need some customizing based on where your json response is.

     

    package json
    import com.eviware.soapui.support.XmlHolder
    import groovy.json.*
    
    // get the response of the JSON test step / request. 
    // def response = context.expand( '${TestStepName s#Response}' ).toString()
    // below is a string representing the value provided in the forum post. The above line can replace the below, with appropriate name of JSON response location
    def response = "[{\n\"trackingEntryId\": 24,\n\"documentId\": 24,\n\"fileId\": 24,\n\"fileName\": \"Sample.xlsx\",\n\"status\": \"Success\",\n\"uploadedBy\": \"Gibson, Mike\",\n\"uploadedDate\": \"2017-06-21T08:31:23.2021627\",\n\"transactions\": 1\n},\n{\n\"trackingEntryId\": 25,\n\"documentId\": 25,\n\"fileId\": 25,\n\"fileName\": \"Sample1.xlsx\",\n\"status\": \"Success\",\n\"uploadedBy\": \"Hill, Lucy\",\n\"uploadedDate\": \"2017-06-21T08:32:05.4842927\",\n\"transactions\": 1\n},\n{\n\"trackingEntryId\": 26,\n\"documentId\": 26,\n\"fileId\": 26,\n\"fileName\": \"TrackIT_Test4.xlsx\",\n\"status\": \"Success\",\n\"uploadedBy\": \"Sinatra, Frank\",\n\"uploadedDate\": \"2017-06-21T08:33:24.0482512\",\n\"transactions\": 0\n}]";
    
    // Set up a JsonSlurper and parse the response
    def json = new JsonSlurper().parseText(response);
    
    // This tests that the collection of "uploadedBy", when sorted, is equal to the original list.
    // Setting sort to false returns a copy of the list without sorting the original. 
    log.info((json.uploadedBy.sort(false) == json.uploadedBy));
    assert (json.uploadedBy.sort(false) == json.uploadedBy);
    
    // To assert that the list is in descending order, you can use the same logic and the reverse of the list.
    // assert (json.uploadedBy.sort(false) == json.uploadedBy.reverse());
    • bmcim123's avatar
      bmcim123
      Occasional Contributor

      Hi. Thanks. I will try this and let you know if I can get it to work.

       

      Thanks so much for your time :-) :-)

      • groovyguy's avatar
        groovyguy
        Champion Level 1

        Not a problem, always happy to help. Let me know if you run into issues.