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