0
votes

I have passed chgId as parameter in the get HTTP request. https://*****?chgId=405

My api response is coming as -

{
  "response": {
    "data": [
      {
        "tid": 3697,
        "chgId": 405,
        "amount": 8.5,
        "Currency": "USD",
       },
      {
        "tid": 3698,
        "chgId": 405,
        "amount": 3.33,
        "Currency": "USD",
       
      }
    ]
  }
}

Now from the response I want to validate in JSR223 assertion that the response is correct based on the chgId field. That means in both 'data' array "chgId": 405 text should come.

Can anyone suggest?

1

1 Answers

0
votes

You could do something like:

def params = org.apache.http.client.utils.URLEncodedUtils.parse(prev.getURL().toURI(), 'UTF-8')

def expected = params.find { 'chgId' }.value as int

def actual1 = new groovy.json.JsonSlurper().parse(prev.getResponseData()).response.data[0].chgId
def actual2 = new groovy.json.JsonSlurper().parse(prev.getResponseData()).response.data[1].chgId

def success = (expected == actual1 && expected == actual2)

if (!success) {
    AssertionResult.setFailure(true)
    AssertionResult.setFailureMessage('chgId mismatch')
}

where prev stands for previous SampleResult

More information: