Forum Discussion

richie's avatar
richie
Community Hero
6 years ago

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 ...
  • richie's avatar
    richie
    6 years ago

    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

  • New2API's avatar
    New2API
    6 years ago

    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   
  • nmrao's avatar
    6 years ago

    richie 

    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 }