1

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.

5
  • Json maps don't have order Commented Oct 9, 2017 at 19:03
  • @ oh ok then, may I ask then how come it sometimes passes and fails the assertion? is there something I have done incorrect in the code? Commented Oct 9, 2017 at 19:30
  • Because sometimes it comes out in the right order, and others not. You can't rely on the order Commented Oct 9, 2017 at 19:58
  • However the order shouldn't matter when comparing maps in groovy. I assume these are not standard maps... What does log.warn json.policies*.getClass() and log.warn policies*.getClass() show you they are? My guess is the json one is a LazyMap, and this is why they are not the same... They are different classes (containing the same data) Commented Oct 9, 2017 at 20:46
  • @BigHendo, please see the solution to see if that helps. Commented Oct 10, 2017 at 0:02

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.