Conversion from scala DSL to java DSL: different behaviour with findAll

Hi gatling experts,
I’ve been using gatling for a while and now with 3.7 version I’d like to give a try to the new java DSL since I’ve always been struggling a bit with scala syntax.
So I’ve taken a project I did with gatling and I’ve tried to convert to use java DSL. I’m still in the process and testing if everything works as expected. Up to now I found a major difference in the checks using findAll function. Here’s an example of such a check in Scala:

def selectProductCode(): HttpCheck = {
css(“input[name=‘productCodePost’]”, “value”).findAll.transform(random).optional.saveAs(“productCode”)
}

and the custom random function was this one:

val random = (list: Seq[String]) => {
list(Random.nextInt(list.size))

}

and here how I translated them in java DSL

public static final CheckBuilder.Final selectProductCode() {
return css(“input[name=‘productCodePost’]”, “value”).findAll().transform(random).optional().saveAs(“productCode”);
}

and the random function became:

private static Function<List, String> random = (final List list) → list.get(ThreadLocalRandom.current().nextInt(list.size()));

Now, I’ve noticed that when the css selector find some occurrences in the html the translated java version works fine as the scala one, but when the css selector find no occurrences of the search then on Scala everything is fine and the only effect is the “productCode” attribute not being stored in the session (as expected), while on java the check breaks with a NPE:

19:44:00.243 [DEBUG] i.g.h.e.r.DefaultStatsProcessor - Request ‘product detail page - pdpTypeAJAX’ failed for user 1: css((input[name=‘productCodePost’],Some(value))).findAll.transform.transformOption.noop extraction crashed: j.l.NullPointerException

I’m not sure if the NPE is happenng in my custom “random” function because I have no check on the list of elements or if the function should not be called at all because there’s no list of elements and in fact the optional call after the transform had the purpose to optionally save the attribute if some value was retrieved.

Has anyone faced something similar with new java DSL?

Thanks
Michele

Please provide a reproducer we can run on our side so we can investigate.

Here’s what I’ve done to try to reproduce your issue, to no avail:

public class Foo extends Simulation {

  private static Function<List<String>, String> random = list -> list.get(ThreadLocalRandom.current().nextInt(list.size()));

  ScenarioBuilder scn = scenario("scn")
    .exec(http("Search")
      .get("[https://computer-database.gatling.io/computers?f=MacBook](https://computer-database.gatling.io/computers?f=MacBook)")
      .check(css("a", "href").findAll().transform(random).optional().saveAs("url"))
    ).exec(session -> {
      System.out.println(session.getString("url"));
      return session;
    });

  {
    setUp(scn.injectOpen(atOnceUsers(1)));
  }
}

Hi Stephane,
today preparing the SSCCE i realized that the problem happens also with the Scala version ( I wrongly assumed it was introduced with java DSL)…in fact the problem I think is just that my random function assume that the list that is passed is always populated but when the findAll of the css selector doesn’t match anything the function is called passing a null list and then the NPE happens when trying to call list.get. Solution was to check if list was not null, so the check didn’t brake.
I guess you can reproduce it in your example with css(“a”,“notexistingAttribute”) and you should see the NPE.

Thanks

i realized that the problem happens also with the Scala version

I don’t think that’s true (I’ve checked).

  • the Scala DSL has transform and transformOption. The former is only invoked when the check could capture something. The latter is always invoked.
  • the Java DSL only has transform that is always invoked, but the function can be invoked with a null parameter if the check could not capture anything.

I guess you’re again right…I’ve messed up something with the execution before…I’ve attached the 2 reproducers (Scala and Java) and the Scala one is working fine even without the null check on random function. Instead the java one is failing with NPE and to fix it I had to perform the null check on random function.

SSCCE.zip (24.5 KB)