DTM
DTM

Reputation: 1

Is there a way to have a gatling check pass if values do not differ by more than 1.00?

I need to modify the code below so the check() will pass, even if values are not exact as long as abs(val1 -val2) < 1.00

.check(
  jsonPath("$.data.attributes.contentValue")
    .is(session -> session.getString("contentValue"))
)

I've tried using the session variable in multiple ways but it seems unavailable inside the validate() method. Also even the replaceAll() method is unavailable there. The code below fails to compile with Cannot resolve method 'replaceAll(String, String)' and Cannot resolve method 'get(String)'

.check(
  jsonPath("$.data.attributes.contentValue")
    .transform(String::valueOf)
    .validate((actualValue, session) -> {
      double actualDouble = Double.parseDouble(actualValue.replaceAll("$", "").replace(",", ""));
      double expectedDouble = (double) session.get("expectedDoubleValue");
      return Math.abs(expectedDouble - actualDouble) <= 1.00;
    }
  )
)

Upvotes: -2

Views: 25

Answers (1)

St&#233;phane LANDELLE
St&#233;phane LANDELLE

Reputation: 6623

This is really a straightforward application of the documentation: https://docs.gatling.io/reference/script/core/checks/#validate

jsonPath("$.data.attributes.contentValue")
  .ofDouble()
  .validate(
    "is +/- 1.0",
    (contentValue, session) -> {
      double expectedDoubleValue = session.getDouble("expectedDoubleValue");
      if (Math.abs(contentValue - expectedDoubleValue) > 0.1) {
        throw new RuntimeException("Wrong value");
      }
      return contentValue;
    }
  )

Upvotes: 0

Related Questions