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": {"correctionsIdentificationOut": [
{
"returnID": 1093,
"meid": 673,
"checkDate": "12/01/2015"
}
,
{
"returnID": 8103,
"meid": 673,
"checkDate": "02/01/2016"
}
]}}

 

What I am looking to accomplish is to make sure that these other values do not appear in the "meid": field

 

671
672
980
994
993
995

 

I was going to manually add a Property Content, using Not Contains, however, I do not know what to put in the 'Content' field.  Is this the best way to accomplish this or is there a better way via Groovy, etc?  I appreciate any help!

  • 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.

3 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    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.