Script Assertion Query - Verify a repeating .json attribute has specific value domain?
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Script Assertion Query - Verify a repeating .json attribute has specific value domain?
Hey!
@rao gave me a script assertion that asserts that a repeating .json attribute has a specific value via a GET REST request
assert context.response, 'Request parameter is correct' def json = new groovy.json.JsonSlurper().parseText(context.response) assert json.data.Name.every{'Geographical Indication' == it} //assert json.data.Name.every{context.expand('${REST Request#Name}') == it}
I need to extend this script assertion so that it verifies that this repeating 'Name' attribute has a specific value domain - e.g. Geographical Indication', 'Protected Designation of Origin', 'Protected Geographical Indication', 'Traditional Speciality Guaranteed'
I tried updating the script assertion as follows:
assert context.response, 'Request parameter is correct' def json = new groovy.json.JsonSlurper().parseText(context.response) assert json.data.Name.every{['Geographical Indication', 'Protected Designation of Origin', 'Protected Geographical Indication', 'Traditional Speciality Guaranteed'] == it}
When I execute the script assertion - I get the following failure response:
assert json.data.Name.every{['Geographical Indication', 'Protected Designation of Origin', 'Protected Geographical Indication', 'Traditional Speciality Guaranteed'] == it}
| | | | | | | false | | [Geographical Indication, Protected Designation of Origin, Protected Geographical Indication, Traditional Speciality Guaranteed] | .............
I've adding a .toString() method to the assertion value
assert json.data.Name.every{['Geographical Indication', 'Protected Designation of Origin', 'Protected Geographical Indication', 'Traditional Speciality Guaranteed'].toString() == it}
but that didn't work.
Can anyone advise how to get this working? Am I mis-using the 'every' method here?
This time I remembered! 🙂 I've attached the .json response I'm trying to get the assertion working on! 🙂
Thanks to all!
Cheers,
richie
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I will try to help you. Can you please post a response example so that I can look into it?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hey @Lucian
.json response is as below: - thank you
{ "header" : { "success" : true, "message" : "" }, "meta" : { "count" : 4, "total" : 4, "type" : "award-classification", "pages" : { "next" : null, "previous" : null }, "fields" : [ "ID", "VersionNumber", "Name", "Code", "AwardClassificationDescription", "EffectiveFrom", "EffectiveTo", "State" ], "custodian" : "DDTS", "last_updated" : "2019-02-12T18:00:00" }, "data" : [ { "ID" : "89501561-12cf-4009-8a2b-ee62bc21041a", "Links" : { "href" : "https://ref-tst.azure.defra.cloud/api/1/geographical-indication/award-classification/89501561-12cf-4009-8a2b-ee62bc21041a", "rel" : "self", "method" : "GET" }, "VersionNumber" : 1, "Name" : "Geographical Indication", "Code" : "GI", "AwardClassificationDescription" : "product is produced, processed or prepared in the geographical area you want to associate it with.", "EffectiveFrom" : "2019-02-08T00:00:00.0000000", "EffectiveTo" : null, "State" : "Active" }, { "ID" : "07eeb9ee-3ab4-4dd9-a14e-faa8c9b86508", "Links" : { "href" : "https://ref-tst.azure.defra.cloud/api/1/geographical-indication/award-classification/07eeb9ee-3ab4-4dd9-a14e-faa8c9b86508", "rel" : "self", "method" : "GET" }, "VersionNumber" : 1, "Name" : "Protected Designation of Origin", "Code" : "PDO", "AwardClassificationDescription" : "product must be produced, processed and prepared in one area and have distinct characteristics from this area.\t", "EffectiveFrom" : "2019-02-08T00:00:00.0000000", "EffectiveTo" : null, "State" : "Active" }, { "ID" : "b9d48e13-5d39-4997-ad72-5ddb40785265", "Links" : { "href" : "https://ref-tst.azure.defra.cloud/api/1/geographical-indication/award-classification/b9d48e13-5d39-4997-ad72-5ddb40785265", "rel" : "self", "method" : "GET" }, "VersionNumber" : 1, "Name" : "Protected Geographical Indication", "Code" : "PGI", "AwardClassificationDescription" : "product is produced, processed or prepared in the geographical area you want to associate it with.", "EffectiveFrom" : "2019-02-08T00:00:00.0000000", "EffectiveTo" : null, "State" : "Active" }, { "ID" : "c4b4cb4c-bbe4-4a55-9f63-d414fd71c7e9", "Links" : { "href" : "https://ref-tst.azure.defra.cloud/api/1/geographical-indication/award-classification/c4b4cb4c-bbe4-4a55-9f63-d414fd71c7e9", "rel" : "self", "method" : "GET" }, "VersionNumber" : 1, "Name" : "Traditional Speciality Guaranteed", "Code" : "TSG", "AwardClassificationDescription" : "product must have a traditional name and characteristics that distinguish it from other similar products.", "EffectiveFrom" : "2019-02-08T00:00:00.0000000", "EffectiveTo" : null, "State" : "Active" } ] }
Cheers,
richie
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@richie, following script might work. though I have hardcoded Names in "expected" list.
import net.sf.* import net.sf.json.* import net.sf.json.groovy.* import groovy.json.JsonSlurper def json = new groovy.json.JsonSlurper().parseText(context.response) def expected = ['Geographical Indication', 'Protected Designation of Origin', 'Protected Geographical Indication', 'Traditional Speciality Guaranteed'] def actual = [] json.data.Name.each{ actual = actual+it } assert expected == actual
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How about this?
def domainList = ['Geographical Indication', 'Protected Designation of Origin', 'Protected Geographical Indication', 'Traditional Speciality Guaranteed'] assert json.data.Name.every{ it in domainList }
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
lads - thanks so much - both scripts do exactly what I need!
nice one
richie
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hey guys,
I thought the code snippets you'd provided would satisfy - but I wasn't specific enough in my original post it would seem.
When I originally said that i needed to assert that a specific attribute had a specific value domain for repeating records within a .json response this was fine if there were only 7 records and the following script assertion works great!
def json = new groovy.json.JsonSlurper().parseText(context.response) def domainList = ['Food-PGI', 'Food-PDO', 'Food-TSG', 'Wines-PGI', 'Wines-PDO', 'Aromatised Wines-GI', 'Spirits-GI'] assert json.data.Name.every{ it in domainList }
HOWEVER - if I have 1000s of records the above script assertion won't work - because I need to supply the values in the array in the same order in which they appear in the returned .json response.
when I said a specific value domain I meant like a distinct SQL query - so if 900 records come back and they have the Name attribute is populated with 1 of 7 possible values - this was what I was hoping for.
The problem is that currently - if I submit my GET request on certain datasets - I have thousands of records and I would need to add in the values in the correct sequence.
Is there any way to alter the above so it asserts like a distinct SQL query (just asserts that the Name attribute in the 1000s of records is populated with 1 of 7 possible values)???
Thanks guys - I sorry for having to come back on this, especially after I marked it resolved - I only noticed when I started hitting large datasets that this didnt work.
richie
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@richie, how about using unique()
import net.sf.* import net.sf.json.* import net.sf.json.groovy.* import groovy.json.JsonSlurper def json = new groovy.json.JsonSlurper().parseText(context.response) def expected = ['Geographical Indication', 'Protected Designation of Origin', 'Protected Geographical Indication', 'Traditional Speciality Guaranteed'] def actual = [] json.data.Name.each{ actual = actual+it } def acutalUnique = [] acutalUnique = actual.unique() //creates a new array or list with unique values assert expected == acutalUnique
