cancel
Showing results for 
Search instead for 
Did you mean: 

How to Assert Repeating Json Attributes Have The Same Value?

SOLVED
Community Hero

How to Assert Repeating Json Attributes Have The Same Value?

Hey!

 

I am testing a REST service and some of my GET request's Query Parameters actually appear as an attribute in my .json response (obviously) filtering the request.  

 

e.g. if request is GET --> whatever/rich?VersionNumber=1

 

my resultset returns records where VersionNumber=1 and furthermore includes a VersionNumber field in the response.

 

e.g.

 

$['data'][0]['VersionNumber']=1

$['data'][1]['VersionNumber']=1

$['data'][2]['VersionNumber']=1

 

I'm trying to put a nifty assertion together (I don't know if it's possible) so that I can assert that each of the VersionNumber attributes in the .json response is identical to the VersionNumber QueryParameter in my REST request. 

 

If I select the 'Select Content' button, I can navigate through to the properties associated with my testcase and select the VersionNumber property which generates the '${REST Request#VersionNumber}' parm in the expected results.

 

This would be perfect if there was only 1 record returned in the resultset - but there can be ANY number - I need to be able to assert that EVERY 'VersionNumber' attribute in the resultset has the same value as the QueryParameter VersionNumber value (or at the least - all VersionNumber attributes have the SAME value - even if I can't map it directly back to the VersionNumber QueryParameter property value.)

 

Would anyone know how to do this?

 

I found this --> which looks like someone else asked the same question BUT it was for XML - but this works for xml - I don't think it works for jsonSmiley Sad

 

I hope I've been clear!

 

Many thanks!

 

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: How to Assert Repeating Json Attributes Have The Same Value?

@richie ,

 

Thank you.

 

Below is the script assertion which will fetch all the VersionNumber's from data array and check each value is 1

 

assert context.response, 'Response is empty or null'
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.data.VersionNumber.every{1 == it}


Regards,
Rao.
6 REPLIES 6
Community Hero

Re: How to Assert Repeating Json Attributes Have The Same Value?

@richie,

It would always be helpful for quick & better understanding if explanation associated with a sample data (not really be the actual data) resembling the same structure that you need to work with. Because, people do not have to imagine or easy to help by copying your data and apply logic reducing their time.



Regards,
Rao.
Community Hero

Re: How to Assert Repeating Json Attributes Have The Same Value?

you're right - I know better than that  - so apologies!

 

Please find attached an example response - I removed most of the records - but I left 3 in there so you can see the repeating pattern.

 

thanks man,

 

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: How to Assert Repeating Json Attributes Have The Same Value?

@richie ,

 

Thank you.

 

Below is the script assertion which will fetch all the VersionNumber's from data array and check each value is 1

 

assert context.response, 'Response is empty or null'
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.data.VersionNumber.every{1 == it}


Regards,
Rao.
Community Hero

Re: How to Assert Repeating Json Attributes Have The Same Value?

Hey @rao

 

that's perfect fella - exactly what I need! Smiley Happy

 

I was trying to expand the snippet a little - as I have a property created in the request via the VersionNumber QueryParameter that is available via ('${REST Request#VersionNumber}') - I was trying to edit the snippet  - changing the script from 

 

assert context.response, 'Response is empty or null'
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.data.VersionNumber.every{1 == it}

to 

assert context.response, 'Response is empty or null'
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.data.VersionNumber.every{context.expand ('${REST Request#VersionNumber}') == it}

I tried variations of this and I've done a bit of reading about the 'it' and 'every' groovy functions -  but I can't quite tweak the script assertion so it's successful - the assertion always fails

 

I was wondering about the datatype - and I think I've noticed beforehand that the context.expand treats properties as string rather than numerics, so I tried adding the .toInteger() method at the end of the parameterization as follows:

assert context.response, 'Response is empty or null'
def json = new groovy.json.JsonSlurper().parseText(context.response)
//assert json.data.VersionNumber.every{1== it}
assert json.data.VersionNumber.every{context.expand('${REST Request#VersionNumber}').toInteger() == it}

 

and it worked! Smiley Happy

 

Hahahaha! very excited I managed to do this without someone giving me the answer! Smiley HappySmiley HappySmiley HappySmiley HappySmiley HappySmiley Happy

 

Thanks to all, and special thanks to @rao - yet again for saving my life

 

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.
Community Hero

Re: How to Assert Repeating Json Attributes Have The Same Value?

Glad to know that. Goovy is easier. Isn't it?



Regards,
Rao.
Community Hero

Re: How to Assert Repeating Json Attributes Have The Same Value?

@rao

 

hahahaha!  well I don't know about 'easier' - but it certainly allows for tailoring your test cases to do exactly what you need - which the OTB functionality doesn't (as good as the OTB functionality actually is) and it's also a lot more fun when you solve a problem!

 

I have another question - based on your previous script to build the request (using groovy) and adding this script assertion into that script.  I was going to create a new subject post - but honestly? this is so specific - I couldn't think of a decent title - and I'm unsure if anyone else would have this specific issue - so I just continued on this post.  If you feel this needs a separate post, I will do so.

 

So - in summary I wanted to add in the script assertion you've given me, to the groovy script you've already provided to build the GET request.  I've managed to update the script so it works, if I don't parameterize the asserted value in the script assertion - if I do parameterize it, the assertion now fails - see below for the updated script - the bit I'm concerned with is in red text

//script courtesy of Rao of https://community.smartbear.com
//import decs import groovy.json.* import wslite.rest.* def serviceHost = context.expand ('${#Project#TestEndpoint}') //'https://whatevs.azurewebsites.net' //Below to handle template parameter, additional $ required before def getNamespaceDatasetPath = '''/api/1/${namespace}/${dataset}''' //define all template param values as shown below def binding = [namespace : context.expand( '${#TestCase#namespace}') , dataset : context.expand('${#TestCase#dataset}')] //def binding = [namespace : 'certification-nomenclature', dataset : 'certification-nomenclature'] def template = new groovy.text.SimpleTemplateEngine().createTemplate(getNamespaceDatasetPath) def queryParams = [:] //Get the properties of Property Test step context.testCase.testSteps["Properties"].properties.each { queryParams[it.key] = it.value.value } def client = new RESTClient(serviceHost) def response = client.get(path: template.make(binding) as String, accept: ContentType.JSON, query : queryParams ) assert response.statusCode == 200 log.info groovy.json.JsonOutput.prettyPrint(response.text) def object = new JsonSlurper().parseText(response.text) assert object.header.success == true //the next four lines are the original script assertion assert response.text, 'Request parameter is correct' //def json = new groovy.json.JsonSlurper().parseText(response.text) //this line isn't needed as I already pass the response to the object variable above assert object.data.CertificateType_Code.every{'C'== it}
//I want to replace the line above with the next line so I parameterize the value being asserted on //assert json.data.CertificateType_Code.every{context.expand('${REST Request#CertificateType_Code}') == it}
def count = 'count' def step3 = context.testCase.testSteps['Properties2'] //step3.propertyNames?.each { step3.removeProperty(it) } def prop3 = step3.hasProperty(count) ? step3.getProperty(count) : step3.addProperty (count) prop3.value = object.meta.count

I think I understand why the script is failing - the script is NOT recognising the teststep name of 'REST Request' <-- this is the name of the groovy step that contains the above groovy - but I don't know why it's not recognising the test step name.

 

 

The script assertion works if I don't parameterize the assertion value - so I know this is the problem - can anyone advise?

 

I should highlight that I did a quick check to see what ReadyAPI! recognised the test step name as using the 'Get Data' function to point to the groovy step - but it created a line of code that indicated ReadyAPI! sees the step as 'REST Request', so I don't know why this parameterized assertion isnt working.

 

I've attached a screenshot so you can see the test case just to add a little clarity!

 

thanks 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.
New Here?
Join us and watch the welcome video:
Top Kudoed Authors