简体   繁体   中英

SoapUI Script Assertion to validate the keys are present without validating the values within the keys

I have a REST request that will return a json response with a set of nine keys and there values. No the input values for the request are randomized and therefore will I will get different values every time it is run.

Is is possible to create a script assertion that will just validated whether the json structure is correct.

Json Response:

{
   "sid": 636811,
   "poss": 122,
   "mis": -150,
   "pres": 253,
   "aea": 0,
   "aa": 12,
   "ua": 7,
   "lar": null,
   "lbr": 1
}

Script Assertion:

def expectedMap = [sid:'', poss:'', mis:'', pres:'', aea:'', aa:'', ua:'', lar:'', lbr:'']
def json = new groovy.json.JsonSlurper().parseText(context.response))

assert json.keySet().sort() == expectedMap.keySet().sort()

I believe the following script assertion I have is failing because is it asserting the key values as well.

log.info expectedMap.keySet().sort()
log.info json.keySet().sort()

Tue Jun 26 14:27:52 BST 2018:INFO:[aa, aea, lar, lbr, mis, poss, pres, sid, ua]
Tue Jun 26 14:27:52 BST 2018:INFO:[aa, aea, lar, lbr, mis, poss, pres, sid, ua]

log.info expectedMap.keySet().sort().getClass()
log.info json.keySet().sort().getClass()

Tue Jun 26 14:17:12 BST 2018:INFO:class java.util.ArrayList
Tue Jun 26 14:17:12 BST 2018:INFO:class java.util.TreeMap$KeySet

You are almost there. Just need to get the keys, sort them and compare.

Change from:

assert expectedMap == json, 'Actual response is not matching with expected data'

To:

assert expectedMap.keySet().sort() == json.keySet().sort() as List, 'Actual response is not matching with expected data'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM