Class ScenarioManager

java.lang.Object
org.mockserver.mock.ScenarioManager

public class ScenarioManager extends Object
  • Field Details

  • Constructor Details

    • ScenarioManager

      public ScenarioManager()
      Creates a ScenarioManager with the default in-memory KV store. This preserves backward compatibility: ScenarioManager constructed without arguments behaves identically to the pre-clustering version (node-local ConcurrentHashMap-backed store, zero network I/O).
    • ScenarioManager

      public ScenarioManager(KeyValueStore<String> scenarioStates)
      Creates a ScenarioManager backed by the given KeyValueStore. For single-node deployments, pass an InMemoryKeyValueStore. For clustered deployments, pass the replicated StateBackend.scenarioStates() store.
      Parameters:
      scenarioStates - the KV store for scenario state strings
  • Method Details

    • setScenarioStates

      public void setScenarioStates(KeyValueStore<String> scenarioStates)
      Replaces the scenario states store. Called by RequestMatchers.setStateBackend(org.mockserver.state.StateBackend) when a backend is wired (or removed). Existing state in the old store is NOT migrated — the caller is responsible for resetting if needed.
      Parameters:
      scenarioStates - the new KV store (must not be null)
    • getState

      public String getState(String scenarioName)
    • setState

      public void setState(String scenarioName, String state)
    • matchesState

      public boolean matchesState(String scenarioName, String requiredState)
    • matchesAndTransition

      public boolean matchesAndTransition(String scenarioName, String requiredState, String newState)
    • transitionState

      public void transitionState(String scenarioName, String newState)
    • getState

      public String getState(String scenarioName, String isolation)
    • setState

      public void setState(String scenarioName, String isolation, String state)
    • matchesState

      public boolean matchesState(String scenarioName, String isolation, String requiredState)
    • matchesAndTransition

      public boolean matchesAndTransition(String scenarioName, String isolation, String requiredState, String newState)
      Atomically checks whether the scenario is in requiredState and, if so, transitions it to newState. Uses KeyValueStore.compareAndSet(java.lang.String, long, V) for cross-node atomicity when backed by a replicated store.

      For the in-memory default backend, this is equivalent to a ConcurrentHashMap.compute() — identical single-node behaviour with no performance overhead.

      When the scenario key does not yet exist in the store (first access), the implicit state is STARTED. If requiredState equals "Started", the key is created with a put() (since there is no prior version to CAS against), then immediately CAS'd to the new state to guard against concurrent first-access races.

      Parameters:
      scenarioName - the scenario name (null = always matches)
      isolation - the isolation key (null = legacy single-key)
      requiredState - the state that must be current for the transition
      newState - the target state (null = no-op transition)
      Returns:
      true if the current state matched requiredState (and the transition was applied if newState != null)
    • transitionState

      public void transitionState(String scenarioName, String isolation, String newState)
    • clear

      public void clear(String scenarioName)
      Clears ALL isolation variants of the given scenario name. Both the legacy null-isolation key and any composite keys with the same scenario name are removed.
    • reset

      public void reset()
    • getAllStates

      public Map<String,String> getAllStates()
      Returns all states as a flat map of display key to state. For entries with null isolation, the display key is just the scenario name. For entries with non-null isolation, the display key is "name[isolation]".

      Warning: The display string format ("name[isolation]") is NOT a stable API — it is intended for display and logging only. For programmatic access, use getAllStatesStructured().

    • getAllStatesStructured

      public Map<org.mockserver.mock.ScenarioManager.ScenarioKey,String> getAllStatesStructured()
      Returns all states as a map of ScenarioManager.ScenarioKey to state string, suitable for programmatic access without relying on the display-oriented string format produced by getAllStates().
    • scheduleTransition

      public void scheduleTransition(TimedScenarioTransition transition, Scheduler scheduler)
      Schedules a timed transition: after transition.getTransitionAfterMs() ms, if the scenario is still in transition.getCurrentState(), it will be advanced to transition.getNextState().

      Only one pending transition per scenario is logically active; scheduling a new transition for the same scenario logically cancels any pending one (the stale runnable becomes a no-op via generation counters).

      Parameters:
      transition - the transition descriptor (must have scenarioName, currentState, nextState, transitionAfterMs)
      scheduler - the MockServer scheduler to use for delayed execution
    • cancelPendingTransition

      public void cancelPendingTransition(String scenarioName)
      Cancels any pending timed transition for the given scenario name.
    • cancelAllPendingTransitions

      public void cancelAllPendingTransitions()
      Cancels all pending timed transitions and clears generation counters.