We're trying to migrate our Gatling tests from Scala to Java. We have a function that chains some execs and has a doIfElse between then. Attempting to add the same on Java affects the return type for the method and being not very familiar with Scala we're not sure how to migrate our whole login function to the Java class.
Scala version
def loginFlow() = group("Login Flow") {
{
login
}
}
def login(): ChainBuilder = exec { session =>
// Need to construct dynamic urls here and store in session as they won't be evaluated while the scenario is running
session.set("authorize_url", s"${getBaseUrl()}/authorize")
.set("redirect_uri", s"${getRedirectURI(Config.TARGET_ENV)}")
.set("client_id", Config.getProperty("client_id", "152ced50-1369-4b19-8b26-8f3d5d9bfd6a.hmhco.com"))
}.doIfOrElse(session => session.contains("token")) {
// If a token is provided for the user, don't login
exec { session =>
logger.debug("Using a provided token")
session
}
} {
exec { session =>
val username = session("username").as[String]
val password = session("password").as[String]
logger.debug(s"Logging In with user: ${username}/${password}")
session.set("nonce", "99999")
}
}
Java
public ChainBuilder loginFlow() {
return group("Login Flow").on(exec(login()));
}
public ChainBuilder login() {
return exec(session -> {
Session newSession = session.set("authorize_url", getBaseUrl());
session.set("redirect_uri", getRedirectURI());
session.set("client_id", Config.getProperty("client_id", "152ced50-1369-4b19-8b26-8f3d5d9bfd6a.hmhco.com")) // Missing semicolon. When it's added the return type for the method changes.
return newSession;
})
.doIfOrElse(session -> session.contains("token")).then(
exec(session -> session)
);
}
Thank you.
CodePudding user response:
It turns out that the else block is missing in the java code. Since Java does not support curried functions, gatling provides some methods in order to chain the actions together. so based on Gatling docs, this will do the trick:
.doIfOrElse(session -> session.contains("token")).then(
exec(session -> session)
).orElse(session -> {...});