Compare two REST response and ignore some fields
to compare two json response i can use below function but in the current context response return some dynamic fields which will be unique every time, can some one please explain how we can handle this
void compareResponse(def atcualresponse, def expectedREsponse){
def map1 = new JsonSlurper().parseText(atcualresponse)
def map2 = new JsonSlurper().parseText(expectedREsponse)
assert map1 == map2
}
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
}