1

I have initially written the following code using Scala and gatling.Here I am picking the json post body that is rendered using pebble and applying some string manipulations and encryption on that output message(in method Utils.encrypt()) and passing the encrypted message each time to the rest endpoint.

def publishMessage() = {
    repeat(1) {
      exec(http("Requests...")
        .post(url).body(StringBody { session =>{
        val bodyExpr = PebbleFileBody("body.json")
        val bodyStr = bodyExpr(session).toString
        Utils.encrypt(bodyStr) /*this method encrypt the rendered body after some manipulations*/
}}).asJson
        .check(status.is(200)))
    }
  }

I wanted to migrate the same code to Java ,since newer versions of gatling support the same and in my scenario which is easier to maintain .I written the following code

 ChainBuilder create = repeat(1).on(feed(DataFeeder.feed)
            .exec(http("Requests ...")
                    .post(url).body( StringBody(session -> {
                        var body = PebbleFileBody("body.json");
                        var bodyStr = body.toString();
                        return Utils.encrypt(bodyStr));
                    })).asJson().check(status().is(200))));

which is giving the following in bodyStr variable ..

Session(Create API,1,HashMap(6f2f1ce1-961e-45d7-bede-9c30b62691c2 -> 0, gatling.http.cache.dns -> io.gatling.http.resolver.ShufflingNameResolver@1a0e7ec5, firstName -> Marketta, referenceId -> 94964, gatling.http.ssl.sslContexts -> io.gatling.http.util.SslContexts@10154be2, userId -> 53547, name -> Zieme, gatling.http.cache.baseUrl -> http://localhost:8080/api, referenceType -> 7),OK,List(ExitOnCompleteLoopBlock(6f2f1ce1-961e-45d7-bede-9c30b62691c2)),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda$571/0x0000000800639840@353383dd,io.netty.channel.nio.NioEventLoop@41e1455d) bodySession(Create WebUser API,1,HashMap(6f2f1ce1-961e-45d7-bede-9c30b62691c2 -> 0, gatling.http.cache.dns -> io.gatling.http.resolver.ShufflingNameResolver@1a0e7ec5, firstName -> Marketta, referenceId -> 94964, gatling.http.ssl.sslContexts -> io.gatling.http.util.SslContexts@10154be2, userId -> 53547, name -> Zieme, gatling.http.cache.baseUrl -> http://localhost:8080/api, referenceType -> 7),OK,List(ExitOnCompleteLoopBlock(6f2f1ce1-961e-45d7-bede-9c30b62691c2)),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda$571/0x0000000800639840@353383dd,io.netty.channel.nio.NioEventLoop@41e1455d)

instead of pebble rendered message.In Scala I am getting the actual pebble rendered output message as parameter string in method Utils.encrypt(String message).Seems like the way i need to fetch the rendered message from session in java is different.can anyone point out what is the exact way to fetch the rendered message from session using java

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
Akhil
  • 101
  • 1
  • 5
  • What does *"is not working as expected"* mean exactly? – Dmytro Mitin Apr 05 '23 at 04:33
  • 1
    @DmytroMitin .Oops..modified the question with details – Akhil Apr 05 '23 at 04:39
  • 2
    You added the desired behavior (the behavior in Scala). Please write what's wrong with the behavior in Java (*"not working as expected"*). [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Dmytro Mitin Apr 05 '23 at 04:42

1 Answers1

1

After little more research, I solved the issue. There is a method called apply() available in gatling.The following is the code I used.

var body = PebbleFileBody("body.json");
var bodyStr = body.apply(session);
return Utils.encrypt(bodyStr);
Akhil
  • 101
  • 1
  • 5
  • Yeah, `bodyExpr(session)` in Scala is `bodyExpr.apply(session)` in Java – Dmytro Mitin Apr 05 '23 at 06:40
  • 1
    Note: allocating `val bodyExpr = PebbleFileBody("body.json")` inside your function while it's actually a constant is a performance waste. You should allocate it outside of your function. – Stéphane LANDELLE Apr 05 '23 at 07:58