Compare two REST response and ignore some fields
- 5 years ago
Thanks Msiadak and HimanshuTayal, I was able to solve it at last below is the generic function that I used and solved most of my needs
private void compare(def expected, def actual, def ignore)
{
def expectedParse = new JsonSlurper().parseText(expected)
def actualParse = new JsonSlurper().parseText(actual)
def parentnode
def dynmaicparent
def ignorefields = ignore.split(',')
for(int i =0; i <ignorefields.size(); i++){
values=ignorefields[i].trim()
//log.info( i + "id to remove " + values)
if(values.contains('.')){
parentnode = values.tokenize(".")[0]
childnode = values.substring(parentnode.length()+1)
log.info( expectedParse.get(parentnode).size())
for(int j =0; j <expectedParse.get(parentnode).size(); j++){
expectedParse.get(parentnode).get(j).remove(childnode)
actualParse.get(parentnode).get(j).remove(childnode)
}
}
else
{
expectedParse.remove(values)
actualParse.remove(values)
}
}
log.info("after removing ignore field $expectedParse")
log.info("updated actualParse is $actualParse")
assert expectedParse == actualParse
}