Forum Discussion

richie's avatar
richie
Community Hero
6 years ago

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

  • CharlesHarold's avatar
    CharlesHarold
    Occasional Contributor

    Try: 

    assert json.data.Name.every{ ['Food', 'Wines', 'Aromatised Wines', 'Spirits'].contains( it ) }