Search code examples
jsonpathgatlingscala-gatling

How to validate array lenght with jsonpath in Gatling?


Given I have the following simulation test:

package click.joaopedroschmitt.simulation

import io.gatling.core.Predef._
import io.gatling.http.Predef._

import click.joaopedroschmitt.config.Configuration

class AddressServiceSimulation extends Simulation {

  val search =
    exec(
      http("Search Address")
        .get("/addresses")
        .queryParam("q", "New york")
        .check(
          status.is(200),
          jsonPath("$").count.gt(2)
        )
    )

  val httpProtocol =
    http.baseUrl(Configuration.apiURL)
      .authorizationHeader(Configuration.accessToken)

  var users =
    scenario("Users").exec(search)

  setUp(
    users.inject(atOnceUsers(1))
  ).protocols(httpProtocol)

}

I have enabled request response logging in Gatling to show the JSON body, and I can see the following content being logged:

HTTP response:
status:
    200 OK
headers:
    Content-Type: application/json
    Connection: keep-alive
    ...

body:
[
{"id":344900560,"display_name":"City of New York, New York, United States","lat":"40.7127281","lon":"-74.0060152"},
{"id":297246698,"display_name":"New York, United States","lat":"43.1561681","lon":"-75.8449946"},
{"id":45124892,"display_name":"92nd Street Y, New York, 1395, Lexington Avenue, Manhattan Community Board 8, Manhattan, New York County, City of New York, New York, 10128, United States","lat":"40.7830439","lon":"-73.9527658"},
{"id":19764374,"display_name":"New York, North Tyneside, North of Tyne, England, NE29 8EP, United Kingdom","lat":"55.0252998","lon":"-1.4869496"},
{"id":192741,"display_name":"New York, Caldwell County, Missouri, United States","lat":"39.6852874","lon":"-93.9268836"},
{"id":28172459,"display_name":"New York, Bo, Bo District, Southern Province, Sierra Leone","lat":"7.9631123","lon":"-11.7636869"},
{"id":297403526,"display_name":"Lake Oswego, Clackamas County, Oregon, United States","lat":"45.4206749","lon":"-122.670649"},
{"id":299407622,"display_name":"Niagara Falls, New York, 825, Bath Avenue, City of Niagara Falls, Town of Niagara, Niagara County, New York, 14305, United States","lat":"43.10987915","lon":"-79.05500714247852"},
{"id":189410,"display_name":"New York, Henderson County, Texas, 75770, United States","lat":"32.1679321","lon":"-95.6691277"},
{"id":208470,"display_name":"New York, Santa Rosa County, Florida, United States","lat":"30.8385202","lon":"-87.2008048"}
]

My assertion defined by jsonPath("$").count.gt(2) keeps failing:

================================================================================
---- Global Information --------------------------------------------------------
> request count                                          1 (OK=0      KO=1     )
> min response time                                    443 (OK=-      KO=443   )
> max response time                                    443 (OK=-      KO=443   )
> mean response time                                   443 (OK=-      KO=443   )
> std deviation                                          0 (OK=-      KO=0     )
> response time 50th percentile                        443 (OK=-      KO=443   )
> response time 75th percentile                        443 (OK=-      KO=443   )
> response time 95th percentile                        443 (OK=-      KO=443   )
> response time 99th percentile                        443 (OK=-      KO=443   )
> mean requests/sec                                      1 (OK=-      KO=1     )
---- Response Time Distribution ------------------------------------------------
> t < 800 ms                                             0 (  0%)
> 800 ms <= t < 1200 ms                                  0 (  0%)
> t ≥ 1200 ms                                            0 (  0%)
> failed                                                 1 (100%)
---- Errors --------------------------------------------------------------------
> jsonPath($).count.greaterThan(2), but actually 1 is not greate      1 (100.0%)
r than 2
================================================================================

Reports generated in 0s.
Please open the following file: file:///home/schmitt/projects/aws-load-tests/target/gatling/addressservicesimulation-20221022141836830/index.html

As you can see in the message jsonPath($).count.greaterThan(2), but actually 1 is not greater than 2 the JSON path is not properly counting the number of elements in the JSON response (that cleary contains more than two objects).

Am I doing something wrong?


Solution

  • I found the solution to my problem by doing the following:

    jsonPath("$[*].id").count.gt(1)
    

    I had to get the ids to get the right counting