Convert Scala gatling to Java gatling

340 Views Asked by At

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

1

There are 1 best solutions below

2
On

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);