cancel
Showing results for 
Search instead for 
Did you mean: 

Not Contains Assertion (JSON attribute value) - not XML

SOLVED
Community Hero

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! Smiley Happy

 

richie 

 

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

@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"


Regards,
Rao.
14 REPLIES 14
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

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.



If my answer helped please click on the 'Accept as Solution' button.
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

Sample data please?


Regards,
Rao.
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

Are you referring to the json attached in the below question?
https://community.smartbear.com/t5/SoapUI-Pro/Asserting-on-contents-of-a-json-response-in-output-log...


Regards,
Rao.
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

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 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

Use case please?
Contains what? Substring ? A value from the list?


Regards,
Rao.
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

Hi,

 

Sorry I was being flippant. Smiley Wink

 

Ok - I make a RESTful POST request and I get a .json response that lists most of the attributes from the newly created record.

 

The requirement is that when the response comes back for certain situations the attribute 'entityLogicalName' attribute value is NOT 'appointment'.

 

JSONPATH  to the relevant attribute is 'x.sessiondata[0]["appointment.nhs_slottype"].entityLogicalName'

 

I've attached an example of the response.

 

This is an example of one of the Not Contains scenarios I need to add to my tests, but I have others too.

 

@Lucian indicates the Not Contains assertion isn't specific to XML nor JSON - it just parses the content for a string as a whole or some regex - so it looks like I was using the assertion incorrectly!

 

The trouble is - the string 'appointment' appears elsewhere in the response  - so I can't just use input 'appointment' in the Not Contains assertion editable field - I need to somehow supply the path to the specific attribute I need to focus on.

 

Unless there's another nifty way of asserting a .json attribute does NOT contain a certain value?

 

Thanks guys, I appreciate your efforts on my behalf!

 

richie

 

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

@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"


Regards,
Rao.
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

Got a chance to try?


Regards,
Rao.
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

Hey @nmrao 

 

 

trying this afternoon...yesterday was all about documentation! Smiley Sad

 

nice one!

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Ask a SmartBear
Top Kudoed Authors