cancel
Showing results for 
Search instead for 
Did you mean: 

Help with JSON Negative Assertion

SOLVED
Highlighted
Contributor

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: Help with JSON Negative Assertion

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.



Regards,
Rao.

View solution in original post

3 REPLIES 3
Highlighted
Community Hero

Re: Help with JSON Negative Assertion

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.



Regards,
Rao.

View solution in original post

Highlighted
Contributor

Re: Help with JSON Negative Assertion

I can't thank you enough, Rao!

Highlighted
Community Hero

Re: Help with JSON Negative Assertion

Glad to know the script is helpful.


Regards,
Rao.
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors