Script Assertion to validate the keys nested json structure

173 Views Asked by At

I am trying to create a script assertion that validates the keys from a nested JSON structure returned from a rest request.

The issue I am having is that I don't know whether the values in the response with be true or false. Therefore the validation I want to do is just to validate the structure of the response.

Expected Response:

{
   "atd" : {
      "es" : "",
      "eu" : "",
      "ns" : "",
      "nu" : ""
   },
   "bh" : {
      "es" : "",
      "eu" : "",
      "ns" : "",
      "nu" : ""
   },
   "hw" : {
      "es" : "",
      "eu" : "",
      "ns" : "",
      "nu" : ""
   },
   "thr" : {
      "es" : "",
      "eu" : "",
      "ns" : "",
      "nu" : ""
   }
}

Actual Response:

{
   "atd" : {
      "es" : false,
      "eu" : false,
      "ns" : false,
      "nu" : false
   },
   "bh" : {
      "es" : true,
      "eu" : true,
      "ns" : false,
      "nu" : false
   },
   "hw" : {
      "es" : false,
      "eu" : false,
      "ns" : false,
      "nu" : false
   },
   "thr" : {
      "es" : false,
      "eu" : false,
      "ns" : false,
      "nu" : false
   }
}

Current Assertion:

def expectedMap = [atd:[es:"",eu:"",ns:"",nu:""], bh:[es:"",eu:"",ns:"",nu:""],...]
def json = new groovy.json.JsonSlurper().parseText(context.response))
assert expectedMap.keySet().sort() == json.keySet().sort() as List, 'Actual response is not matching with expected data'

The code above works to a certain extent, in which is does not assert the nested keys.

Is there a way I can take json from the response and remove the true & false values and then assert from that?

0

There are 0 best solutions below