cancel
Showing results for 
Search instead for 
Did you mean: 

Assertion to Verify Repeating Attribute has Specific Value Domain?

Community Hero

Assertion to Verify Repeating Attribute has Specific Value Domain?

Hey!

 

I was wondering if there's a way of asserting that a repeating attribute in a json response has a specific value domain - say there are the possible values of "food", "wines", "spirits", "aromatized wines"

 

Rao provided the following whizzy script assertion that asserts that a repeating attribute has a specific value

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

I've been trying to play with this - but I don't know enough about the iterate and every methods

obviously I tried 

assert context.response, 'Request parameter is correct' 
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.data.Name.every{['Food', 'Wines', 'Aromatised Wines', 'Spirits'] == it}

but I knew this wasn't going to work even before I tried.  Done a bit of searching google/the forum - and I'm pretty sure I can't use the it method in this way.

 

This time, I remembered to attach the .json response for my request.  As you can see - $[data][Name] is repeated 4 times and holds the values of either 'Food', 'Wines', 'Aromatised Wines', 'Spirits'.

 

Can anyone advise?

 

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 REPLY 1
Occasional Contributor

Re: Assertion to Verify Repeating Attribute has Specific Value Domain?

Try: 

assert json.data.Name.every{ ['Food', 'Wines', 'Aromatised Wines', 'Spirits'].contains( it ) }
New Here?
Join us and watch the welcome video:
Watch the new interview
Top Kudoed Authors