Forum Discussion

CByler's avatar
CByler
Contributor
8 years ago
Solved

Help with JSON Negative Assertion

I've done some searching but haven't found my answer.  I need a little help setting up a Negative Assertion.  I'm calling a JSON Get and below is a sample of my response:   {"getCorrectionsResult":...
  • nmrao's avatar
    8 years ago

    Here is the groovy script which extracts the array of "meid" values. Then you can check against array of values.

     

    import groovy.json.JsonSlurper
    
    def str = '''{
      "getCorrectionsResult":{
        "correctionsIdentificationOut":[
          {
            "returnID":1093,
            "meid":673,
            "checkDate":"12/01/2015"
          },
          {
            "returnID":8103,
            "meid":673,
            "checkDate":"02/01/2016"
          }
        ]
      }
    }'''
    
    def json = new JsonSlurper().parseText(str)
    def existingMeids = json.getCorrectionsResult.correctionsIdentificationOut.meid
    def shouldNotBePresent = [671, 672, 980, 994, 993, 995]
    //there should not be any common elements between existingMeids and shouldNotBePresent, so intersection is empty
    assert [] == existingMeids.intersect(shouldNotBePresent), "There are common meids present which is incorrect"
    
    

     

    In the json data, there is 673 present. 

    So to make another test if the script is working as desired or not, you can just add 673 to "shouldNotBePresent" list, then the test should fail.