Forum Discussion

CByler's avatar
CByler
Contributor
8 years ago

Help with JSON Negative Assertion

This goes with a prior thread that it seems I can no longer update.... https://community.smartbear.com/t5/SoapUI-NG/Help-with-JSON-Negative-Assertion/m-p/126958#M29266

 

I have messed with this for a while and cannot find what I'm doing wrong. I modified the example you provided above and attempted to use it for another scenario, but I am struggling. For this test case I want to make sure none of my responses contain submitCount": "1" or submitCount": "4" Acceptable values are 2 or 3

{"getResubmittedReturnsResult": {"getResubmittedReturnsIdentificationOut": [
{
"meid": 671,
"transID": 92556,
"status": "Resubmitted",
"submitCount": "2",
"addedTime": "09/23/2016 14:28:49"
},
{
"meid": 671,
"transID": 92558,
"status": "Resubmitted",
"submitCount": "3",
"addedTime": "09/23/2016 14:28:52"
}
]}}

This is what I modified it to...

import groovy.json.JsonSlurper
def str = '''{
"getResubmittedReturnsResult":{
"getResubmittedReturnsIdentificationOut":[
{
"submitCount": "2"
}
]
}
}'''
def json = new JsonSlurper().parseText(str)
def existingsubmitCount = json.getResubmittedReturnsResult.getResubmittedReturnsIdentificationOut.submitCount
def shouldNotBePresent = [1,4]
assert [] == existingsubmitCount.intersect(shouldNotBePresent), "There are submitCounts of 1 present which is incorrect"

I should mention, the data set that comes back will contain hundreds of transactions so it needs to loop through all of them. They key is to make sure that none of them contain a anything other than a 2 or 3. Can you please help me?

Thanks!

18 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    //Assign your json to below variable and uncomment
    //def jsonString = ''' your string goes here '''
    def
    parsedJson = new groovy.json.JsonSlurper().parseText(jsonString)
    def items = parsedJson.getResubmittedReturnsResult.getResubmittedReturnsIdentificationOut

    def meids = items.meid
    def submitCounts = items.submitCount
    log.info meids
    log.info submitCounts

     Can you please check the above?

    • CByler's avatar
      CByler
      Contributor

      I'm sorry Rao,

       

      I don't quite understand what you mean by 

      Assign your json to below variable

      What string do I place there?   And how to I dictate that I only get 2's and 3's? I'm not very literate in groovy scripting.  I see that you have two comments regarding meids:

       

      def meids = items.meid
      def submitCounts = items.submitCount
      log.info meids

       Just trying to understand why the meids would be relevant for what I'm trying to accomplish.  Can the sample I attached, where I modified your prior script, not be fixed to work in this case?  I wanted to get comfortable with modifying that if possible so I can reuse it for different response parameters in other web service calls :)

      • nmrao's avatar
        nmrao
        Champion Level 3
        def jsonString = '''{
        "getResubmittedReturnsResult":{
        "getResubmittedReturnsIdentificationOut":[
        {
        "meid":671,
        "transID":92556,
        "status":"Resubmitted",
        "submitCount":"2",
        "addedTime":"09/23/2016 14:28:49"
        },
        {
        "meid":671,
        "transID":92558,
        "status":"Resubmitted",
        "submitCount":"3",
        "addedTime":"09/23/2016 14:28:52"
        }
        ]
        }
        }'''

        def parsedJson = new groovy.json.JsonSlurper().parseText(jsonString)
        def items = parsedJson.getResubmittedReturnsResult.getResubmittedReturnsIdentificationOut

        def shouldNotBePresent = [1, 4]
        //Currently the value received in submitCount is String type, so coverting to Integer
        def submitCounts = items.submitCount.collect { it as Integer}
        log.info submitCounts
        assert [] == shouldNotBePresent.intersect(submitCounts), "There are unwanted submitCount value present in the response"

         

         

        CByler,

        sorry for multiple cycles. Hope this helps.