Class ScenarioManager
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a ScenarioManager with the default in-memory KV store.ScenarioManager(KeyValueStore<String> scenarioStates) Creates a ScenarioManager backed by the givenKeyValueStore. -
Method Summary
Modifier and TypeMethodDescriptionvoidCancels all pending timed transitions and clears generation counters.voidcancelPendingTransition(String scenarioName) Cancels any pending timed transition for the given scenario name.voidClears ALL isolation variants of the given scenario name.Returns all states as a flat map of display key to state.Returns all states as a map ofScenarioManager.ScenarioKeyto state string, suitable for programmatic access without relying on the display-oriented string format produced bygetAllStates().booleanmatchesAndTransition(String scenarioName, String requiredState, String newState) booleanmatchesAndTransition(String scenarioName, String isolation, String requiredState, String newState) Atomically checks whether the scenario is inrequiredStateand, if so, transitions it tonewState.booleanmatchesState(String scenarioName, String requiredState) booleanmatchesState(String scenarioName, String isolation, String requiredState) voidreset()voidscheduleTransition(TimedScenarioTransition transition, Scheduler scheduler) Schedules a timed transition: aftertransition.getTransitionAfterMs()ms, if the scenario is still intransition.getCurrentState(), it will be advanced totransition.getNextState().voidsetScenarioStates(KeyValueStore<String> scenarioStates) Replaces the scenario states store.voidvoidvoidtransitionState(String scenarioName, String newState) voidtransitionState(String scenarioName, String isolation, String newState)
-
Field Details
-
STARTED
- See Also:
-
-
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
Creates a ScenarioManager backed by the givenKeyValueStore. For single-node deployments, pass anInMemoryKeyValueStore. For clustered deployments, pass the replicatedStateBackend.scenarioStates()store.- Parameters:
scenarioStates- the KV store for scenario state strings
-
-
Method Details
-
setScenarioStates
Replaces the scenario states store. Called byRequestMatchers.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
-
setState
-
matchesState
-
matchesAndTransition
-
transitionState
-
getState
-
setState
-
matchesState
-
matchesAndTransition
public boolean matchesAndTransition(String scenarioName, String isolation, String requiredState, String newState) Atomically checks whether the scenario is inrequiredStateand, if so, transitions it tonewState. UsesKeyValueStore.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. IfrequiredStateequals "Started", the key is created with aput()(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 transitionnewState- the target state (null = no-op transition)- Returns:
- true if the current state matched
requiredState(and the transition was applied ifnewState != null)
-
transitionState
-
clear
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
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, usegetAllStatesStructured(). -
getAllStatesStructured
Returns all states as a map ofScenarioManager.ScenarioKeyto state string, suitable for programmatic access without relying on the display-oriented string format produced bygetAllStates(). -
scheduleTransition
Schedules a timed transition: aftertransition.getTransitionAfterMs()ms, if the scenario is still intransition.getCurrentState(), it will be advanced totransition.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
Cancels any pending timed transition for the given scenario name. -
cancelAllPendingTransitions
public void cancelAllPendingTransitions()Cancels all pending timed transitions and clears generation counters.
-