cancel
Showing results for 
Search instead for 
Did you mean: 

Assert every instance of a json attribute displays a specific pattern

SOLVED
Community Hero

Assert every instance of a json attribute displays a specific pattern

Hey,

 

@nmrao gave me some code to assert that a repeating attribute in my json response had a null as follows::

assert context.response, 'Request parameter is correct'
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.data.DataSourceName.every{null == it}

Here you can see that I was asserting that every single instance of the DataSourceName attribute was always blank in my response

 

I now have to assert that another attribute has a specific pattern - e.g. 3 character alpha code (e.g. GDS) or a 3 digit value starting with two zeros (e.g. 004)

 

I'm guessing I need to use regex for this - but I don't know how to add in the regex for this

 

e.g. if I need to assert that the value for DataSourceName can be any 3 alpha char (e.g. GDS) - I'd use [A-Z]{3} as the regex - but how do I insert it into my script above?  Just replacing the word 'null' with [A-Z]{3} like below doesn't work

 

assert context.response, 'Request parameter is correct'
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.data.DataSourceId_Name.every{[A-Z]{3} == it}

Can anyone advise how I represent a regex value in the script above?

 

Many 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.
1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: Assert every instance of a json attribute displays a specific pattern

@richie 

 

You already have the list and it can be asserted as below.

 

Note that used fixed list to demonstrate

 

 

def list = ['ABC', 'PQR', 'GDS']
assert list.every{it ==~ /[A-Z]{3}/}

So, instead of list varible in the above, use your derivated list i.e., json.data.DataSourceName

 



Regards,
Rao.
2 REPLIES 2
Community Hero

Re: Assert every instance of a json attribute displays a specific pattern

@richie 

 

You already have the list and it can be asserted as below.

 

Note that used fixed list to demonstrate

 

 

def list = ['ABC', 'PQR', 'GDS']
assert list.every{it ==~ /[A-Z]{3}/}

So, instead of list varible in the above, use your derivated list i.e., json.data.DataSourceName

 



Regards,
Rao.
Community Hero

Re: Assert every instance of a json attribute displays a specific pattern

@rao saves my day yet again!

 

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.
New Here?
Join us and watch the welcome video:
Watch the New Interview
7Mistakes
Join the exciting event
SeptemberHubBub
Top Kudoed Authors
Join the September Hub-bub to show off, learn and win