Forum Discussion

richie's avatar
richie
Community Hero
8 months ago

GroovyScript Help - Repeating JSON Attributes Have Specific Value.....some of the time?

Hey!

Ok - so I've got GET request with multiple different query parms -one of these query parms is 'handling_codes'.

The GET returns a JSON response payload that contains multiple 'CONSIGNMENT' records.

Each of these CONSIGNMENT records has a handling_code array. (JSONPATH = $.data[0].handling_codes ).

Each CONSIGNMENT record in the response payload can have ZERO or multiple handling_code values, and if they exist, are specified by the 'code' name/value pair attribute (JSONPATH = $.data[1].handling_codes[0].code)

if I specify the handling_code = 'SPX' as my query parm on the GET request, then the records in the payload response will include CONSIGNMENT records that have at least 1 $.data[1].handling_codes[0].code = 'SPX', but could include handling_code.code values that are <>'SPX'

The records may have other handling_code.code values <> 'SPX', but the rules are as long as at least 1 handling code for the parent CONSIGNMENT record includes 'SPX', then the record is returned in the response.

Ages back nmrao gave me a groovy snippet to iterate through a payload and asserting that a specific JSON attribute had a specific value - i.e. value of '12345' - such as below

def json = new groovy.json.JsonSlurper().parseText(context.response)
assert JSONPATH_to_relevant_attribute.every{it == 12345}

HOWEVER - I can't use this because some of the handling_code.code values will NOT equate to 'SPX' -

I need to create an assertion that verifies that if the GET request's handling_code query parm = 'SPX', the generated list of consignment records includes at least one handling_code of 'SPX', despite how many other handling_codes it may have.

I've added an example payload below (with a massive amount stripped out) so you can see the general structure and JSONPATH of the payload.

There are 3 consignment records in the example.  1 consignment has 5 handling code values, 1 consignment has zero, and 1 consignment has 1 handling code specified.

I'll repeat - if the handling_code query parm is set to 'SPX' - then a consignment will be retrieved if it possesses a handling_code.code which is equal to 'SPX', but it will also retrieve consignments that have handling_code.code values <> 'SPX' as long as one of them is equal to 'SPX'.

{
   "meta" : {
      "total" : 3
   },
   "data" : [
      {
         "$uri" : "/v2/consignments/546be556-eb68-48f9-9f82-ca0e9b9d4e6f",
         "exceptions_count" : 3,
         "handling_codes" : [
            {
               "code" : "BUC",
               "description" : null
            },
            {
               "code" : "SPX",
               "description" : "COOL GOODS"
            },
            {
               "code" : "HEA",
               "description" : "HEAVY CARGO  150 KG. AND OVER PER PIECE"
            },
            {
               "code" : "PER",
               "description" : "PERISHABLE CARGO"
            },
            {
               "code" : "MGO",
               "description" : null
            }
         ],
         "has_exceptions" : true,
         "updated_by" : "a74dfe57-37ef-4b1b-9801-167bfda17d59"
      },
      {
         "$uri" : "/v2/consignments/3bacb93c-24b3-4989-8048-fe4791706593",
         "exceptions_count" : 0,
         "handling_codes" : [ ],
         "has_exceptions" : false,
         "updated_by" : "a74dfe57-37ef-4b1b-9801-167bfda17d59"
      },
            {
         "$uri" : "/v2/consignments/7555fbb3-7ca3-4166-b243-cf8813d376ef",
         "exceptions_count" : 0,
         "handling_codes" : [
            {
               "code" : "SPX",
               "description" : "ON PAX A/C ONLY"
            }
         ],
         "has_exceptions" : false,
         "updated_by" : "a74dfe57-37ef-4b1b-9801-167bfda17d59"
      }
   ]
}

To repeat --> I need to create an assertion that verifies that if the GET request's handling_code query parm = 'SPX', the generated list of consignment records includes at least one handling_code of 'SPX', despite how many other handling_codes it may have - hence that if a consignment record does NOT include a handling_code of 'SPX' then it isn't retrieved in the response.

Can anyone help?  I just don't know where to start with this - I can write the pseudo code - but that's about it and I don't even know if I'm on the right track with that

I've attached a genuine response file (generated using the GET with handling_code = SPX query parm set) for a more accurate example

 

Cheers - as always I appreciate all/any help anyone can give me!

 

Rich

  • Here is the complete script which uses the attached file and asserts the codes 

    Please follow the inline comments:

     

    //Script Assertion
    def json = new groovy.json.JsonSlurper().parseText(context.response)
    def expectedCode = 'SPX'
    //find all non empty handling_codes
    def hc_list = json.data.'handling_codes'.findAll() 
    //List the non empty handling_codes item count
    log.info hc_list.size
    //Iterate thru each handling_codes, then check the code list contains at least expectedCode
    hc_list.each { hc ->
    	log.info hc.code
    	assert hc.code.any {it == expectedCode} , "$hc does not contain $expectedCode"
    }
    

    EDIT:

    Use the above script as Script Assertion where you get the response (not separate groovy script test step)

    There are some empty handling_codes are there in the data and assuming that you want to ignore them, hence .findAll() used as shown below (line #5 of the script)

    def hc_list = json.data.'handling_codes'.findAll()

    if you also need to consider them, then change it to

    def hc_list = json.data.'handling_codes'

     

  • nmrao's avatar
    nmrao
    Champion Level 3

    Here is the complete script which uses the attached file and asserts the codes 

    Please follow the inline comments:

     

    //Script Assertion
    def json = new groovy.json.JsonSlurper().parseText(context.response)
    def expectedCode = 'SPX'
    //find all non empty handling_codes
    def hc_list = json.data.'handling_codes'.findAll() 
    //List the non empty handling_codes item count
    log.info hc_list.size
    //Iterate thru each handling_codes, then check the code list contains at least expectedCode
    hc_list.each { hc ->
    	log.info hc.code
    	assert hc.code.any {it == expectedCode} , "$hc does not contain $expectedCode"
    }
    

    EDIT:

    Use the above script as Script Assertion where you get the response (not separate groovy script test step)

    There are some empty handling_codes are there in the data and assuming that you want to ignore them, hence .findAll() used as shown below (line #5 of the script)

    def hc_list = json.data.'handling_codes'.findAll()

    if you also need to consider them, then change it to

    def hc_list = json.data.'handling_codes'

     

    • richie's avatar
      richie
      Community Hero

      nmrao 

      straight up - can't thankyou enough - you always help me out when I need.

      really appreciate that you explained it all as well - means I can re-use in other situations.

      you are a life saver! ;)

      Cheers!

      rich

  • nmrao's avatar
    nmrao
    Champion Level 3

    every - method checks all the items meet the given condtion.

    Now, you need any method which checks any one item in the list meet the given condition.

    To illustrate a simple example, the items should at least have a juice

    list = ['fruit', 'milk', 'juice']
    assert list.any { it == 'juice' }, 'list doe not contain juice'

    Of course, there is another way in this particular case to check if the list contains item.

    assert list.contains('milk'), 'list does not contain milk'

    See the beauty of the groovy language, as if we are talking in English, same verbs.