Forum Discussion

chathurad's avatar
chathurad
Contributor
7 years ago

How to assert or validate the sorting order of a response json data on SoapUI

For a get request im getting a json response and it contain some school data.The schools should be appear by descending order for the school rating number.

Somthing looks like this.

 

"result":[
{ "location": "Sydney", "minStudentAge": 13, "schoolType": "Business", "rating": 5, }, { "location": "Sydney", "minStudentAge": 16, "schoolType": "Business", "rating": 3, }, { "location": "Sydney", "minStudentAge": 18, "schoolType": "Business", "rating": 1, }, { "location": "Sydney", "minStudentAge": 16, "schoolType": "Business", "rating": 2, } ]

So every node is having a value named "Rating" and i need to validate whether the return json contains data on correct order. Rating top (5) should be at the top of the list and rating low (1) should be at the bottom of the list.

 

I did somthing like this, 

 

import com.eviware.soapui.support.XmlHolder
import net.sf.*;
import net.sf.json.*;
import net.sf.json.groovy.*;

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)

//Getting response from methods
def response = context.expand( '${Schools#Response}' ).toString()

 

 

But after this what should i do?

  • JHunt's avatar
    JHunt
    Community Hero

    You could use this as a Script Assertion:

     

    String unsorted = new groovy.json.JsonSlurper().parseText(messageExchange.responseContent)
    ."result" .with { groovy.json.JsonOutput.toJson( it ) } String sorted = new groovy.json.JsonSlurper().parseText(messageExchange.responseContent)
    ."result"
    .sort { -it."rating" } .with { groovy.json.JsonOutput.toJson( it ) } assert sorted == unsorted

     

     

      • JHunt's avatar
        JHunt
        Community Hero
        Hi, did you get a notification for my edit? Should be messageExchange.responseContent instead of messageExchange.response.