Big Hendo
Big Hendo

Reputation: 185

How to order a json output via groovy?

I have seen something unusual with my SOAP UI script. I just want to perform an assertion that the data with is correct so I have written this code beLow:

import com.eviware.soapui.support.GroovyUtils
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def response = messageExchange.response.responseContent
def json =  new JsonSlurper().parseText(response)
def jsonFormat = (response).toString()

def policies = [
    [x: 28, xxx: 41, xxxxx: 1, name: 'Individual 18-50', aaa: true], 
    [x: 31, xxx: 41, xxxxx: 1, name: 'Individual 51-60', aaa: true], 
    [x: 34, xxx: 41, xxxxx: 1, name: 'Individual 61-75', aaa: true], 
    [x: 37, xxx: 41, xxxxx: 1, name: 'Individual 76-85', aaa: false]
]

log.warn json.policies
log.error policies
assert json.policies == policies

When I look at the log.warn and log.error info, it displays the json response in the incorrect order as it display 'isActive' field first.

log.warn json.policies displays this:

[{aaa=true, xx=28, xxxxx=1, name=Individual 18-50, xxxx=41}, {aaa=true, x=31, xxxxx=1, name=Individual 51-60, xxx=41}, {aaa=true, x=34, xxxxx=1, name=Individual 61-75, xxx=41}, {aaa=true, x=37, xxxxx=1, name=Individual 76-85, xxx=41}]

log.error policies displays this:

[{x=28, xxx=41, xxxxx=1, name=Individual 18-50, aaa=true}, {x=31, xxx=41, xxxxx=1, name=Individual 51-60, aaa=true}, {x=34, xxxx=41, xxxxxx=1, name=Individual 61-75, aaa=true}, {x=37, xxx=41, xxxxx=1, name=Individual 76-85, aaa=false}]

How can I have the DTOs to be displayed within the correct order with json.policies so that it is displayed in the correct order as policies?

One more unusual thing, I ran the test case 10 times and the test step which this assertion checks has passed 3 out of 10 times. It should never have passed as if you compare the last DTO as the end for policies, it displays isActive as false which the last isActive in json.policies is true.

Upvotes: 1

Views: 1289

Answers (1)

Rao
Rao

Reputation: 21389

You are almost close. But, couple of things to correct.

  • you do not have to convert json toString.
  • list should be sorted before comparison.
  • and it does not matter in which order each map entries are shown in the log, each map can be compared.

Here is the Script Assertion

//Check if the response is empty or not
assert context.response, 'Response is empty'

def json = new groovy.json.JsonSlurper().parseText(context.response)

//Assign the policies from current response; assuming above json contains that; change below statement otherwise.
def actualPolicies = json.policies
//Expected Polities
def expectedPolicies = [
    [id: 28, providerId: 41, coverTypeId: 1, name: 'Individual 18-50', isActive: true], 
    [id: 31, providerId: 41, coverTypeId: 1, name: 'Individual 51-60', isActive: true], 
    [id: 34, providerId: 41, coverTypeId: 1, name: 'Individual 61-75', isActive: true], 
    [id: 37, providerId: 41, coverTypeId: 1, name: 'Individual 76-85', isActive: false]
]

log.info "List from response $actualPolicies"
log.info "List from policies $expectedPolicies"

//Sort the list and compare; each item is a map and using id to compare both 
assert expectedPolicies.sort{it.id} == actualPolicies.sort{it.id}, 'Both policies are not matching'

You can quickly the online demo with fixed data based on your description to see it working the comparison.

Upvotes: 1

Related Questions