Forum Discussion

vpnkumar's avatar
vpnkumar
Occasional Contributor
5 years ago

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 thi...
  • vpnkumar's avatar
    vpnkumar
    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
    }