Forum Discussion

richie's avatar
richie
Community Hero
6 years ago
Solved

Not Contains Assertion (JSON attribute value) - not XML

Hey,

 

I've been investigating the Not Contains assertion to verify that a json attribute does NOT contain a specific value.

 

HOWEVER - all the posts I've seen on this on this forum/stackoverflow (stackoverflow post)etc. and soapuiforum either provide a groovyscript option to handle the not contains or uses XML as the example for the not contains.

 

HOWEVER - my response is in .json and my groovyscript is rubbish so I'd like to find the OTB solution.

 

I want to assert that a specific name/value pair attribute in my json response does NOT contain a certain value.

 

As stated above - I've seen the following Not Contains assertion value repeated in lots of places to assert that a tag doesnt contain the value zero, but this is XML, NOT json.

(?s).*<recordCount>[^0]</recordCount>(?s).*

Does anyone know the syntax to do this in jsonpath? 

 

I miss XPATH & XML....

 

thanks to all! :)

 

richie 

 

 

  • richie ,

     

    Here is the script assertion for the same.

     

    If I understand right, the value of "entityLogicalName" should not be "appointment". And below script checks that.

     

    /**
    * Below is the script assertion for the REST Request Test step
    **/
    def json = new groovy.json.JsonSlurper().parseText(context.response)
    def logicalNames = json.sessiondata.'appointment.nhs_slottype'.entityLogicalName
    def checkFor = 'appointment'
    //Negative check - value should not have appointment "!=", 
    //Positive check -  use "==" to match value with appointment
    assert logicalNames.every {it != checkFor}, "Not expecting value ${checkFor} for entityLogicalName, but found"

14 Replies

  • Lucian's avatar
    Lucian
    Community Hero

    Hey richie,

     

    The Not Contains assertion is not concerned with json nor xml. With this type of assertion you can search for a string and you can use regex in the process. Thus this should work the same on json as on xml.

    • richie's avatar
      richie
      Community Hero

      Hey nmrao 

       

      nah - this is a fresh new project (I'm juggling 3 different ones at the moment!)

       

      p.s. - I haven't responded to that other post yet (the one you link to) cos my project moved the endpoints into some secure Azure resource and I cant hit the endpoint anymore.  Trying to get it sorted today - as soon as I do - I can generate a response and comment on the help so far!

       

      Cheers to All!

       

      richie 

      • nmrao's avatar
        nmrao
        Champion Level 3
        Use case please?
        Contains what? Substring ? A value from the list?