Home > database >  Gatling check every value in array
Gatling check every value in array

Time:03-17

I am writing gatling tests and using JSONPath $..parentId.
I am getting the response:

[
  "102044",
  "102044",
  "102044"
] 

I want to check that every value is equal to 102044. The amount of values can change.
By now I'm checking only random one:

@Then("then is successful")
override def thenStep(): Seq[HttpCheck] = {
    var checks = List[HttpCheck]()
    checks ::= status.is(session => 200)

    checks ::= jsonPath("$..totalElements").ofType[Int].gt(8)
    checks ::= jsonPath("$..totalElements").ofType[Int].lt(15)
    checks ::= jsonPath("$.data[*].parentId").findRandom.is("102044")
    
    checks
}

I tried to save the amount of values and the do something with it, like this:

checks ::= jsonPath("$..totalElements").ofType[Int].saveAs("all")
checks ::= jsonPath("$.data[*].parentId").findAll.is(Seq.fill(s"$all")("102044"))

But this is not working.

Is there a simple solution?
This question is similar to: In Gatling, how to iterate json array in .check to validate all values

CodePudding user response:

What you should be able to do, using JMESPath is:

totalElements

To find the totalElements property of your JSON.

And

length(data[?parentId != '102044'])

To find if there are elements where the parentId is not 102044.

So, your assertions end up being be:

checks ::= jmesPath("totalElements").ofInt().gt(8)
checks ::= jmesPath("totalElements").ofInt().lt(15)
checks ::= jmesPath("length(data[?parentId != '102044'])").ofInt().is(0)

All this is based on the assumption that your input looks something like what you provided in your previous question:

{
  "pageable": {
    "currentPage": 1,
    "totalPages": 1,
    "pageSize": 20,
    "last": true,
    "first": true
  },
  "sort": {
    "orders": [],
    "sorted": false,
    "unsorted": true,
    "empty": true
  },
  "totalElements": 6,
  "data": [
    {
      "id": 1,
      "roleName": "test1",
      "userCount": 5,
      "parentId": "102044"
    },
    {
      "id": 2,
      "roleName": "test2",
      "userCount": 5,
      "parentId": "102045"
    }
  ]
}
  • Related