-1

I am trying to declare a map in a Gatling Java simulation class and fill the map with values. I seem to be able to declare the map itself without an issue, however when I try adding lines such as map.put("authToken","token") Then I get errors such as Identifier expected and illegal start to type.

Could somebody please explain how I could declare a map at the top of the file (eg before 'teacherFeeder') and then add values to it lower down the file?

import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Stream;

import java.util.function.Supplier;
import java.net.*;

//import static com.hmhco.assignments.performance.simulations.config.Constants.*;
import static com.hmhco.assignments.performance.config.Constants.*;
import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;

import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;

@Slf4j
public class BaseSimulation extends Simulation {

    HttpProtocolBuilder httpProtocol = http
        .baseUrl(BASE_URL) // Here is the root for all relative URLs
        .shareConnections();

    public String teachers = "data/teachers.csv";

    FeederBuilder<String> teacherFeeder = csv(teachers).random();

    HashMap<String, String> map = new HashMap<>();
    map.put("authToken", "token");

    ScenarioBuilder scn1 = scenario("auth-flow").repeat(AUTH_FLOW_REPEAT).on(
            feed(teacherFeeder)
                    .exec(http("Get Token from IDS")
                            .post(IDS_LOGIN_URL)
                            .header("Content-Type", "application/json")
                            .body(StringBody(
                                    """
                                            {
                                            "username":"#{username}",
                                            "password":"#{password}",
                                            "tenantPid":"#{connection}"
                                            }
                                            """
                            ))
                            .check(status().is(200))
                            .check(jsonPath("$.sifToken").saveAs("token"))
                    )

    SetUp testSetup = setUp(
            scn1.injectOpen(rampUsers(NUMBER_OF_USERS).during(RAMP_UP))
    ).maxDuration(MAX_DURATION);

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Alexb1999
  • 79
  • 2
  • 10

1 Answers1

0

It's possible to do what you're asking with a class-level code block:

public class BaseSimulation extends Simulation {
    HashMap<String, String> map = new HashMap<>();
    
    {
        map.put("authToken", "token");
    }
}

In contrast to functions, this code executes alongside field initialisers. While it is possible to initialise your map in this way, it is not recommended. Generally, you should aim to initialise non-static class fields in your constructor instead:

public class BaseSimulation extends Simulation {
    Map<String, String> map;
    
    public BaseSimulation() {
        map = new HashMap<>();
        map.put("authToken", "token");
    }
}

Or, alternatively, use a field initializer (be aware this creates an immutable Map):

public class BaseSimulation extends Simulation {
    Map<String, String> map = Map.of("authToken", "token");
}
Paul Benn
  • 1,911
  • 11
  • 26
  • Could ScenarioBuilder be called from inside this constructor? I would like to put the scenario and setup functionality so that I can pass the map if that makes sense. I want to map the SIF token after the 200 status. (eg. map.put("SIF", "token")) – Alexb1999 Jul 25 '22 at 13:03