Package org.mockserver.mock.pact
Class PactProviderStates
java.lang.Object
org.mockserver.mock.pact.PactProviderStates
Maps a Pact interaction's provider states (the
"given ..." preconditions) onto MockServer's existing scenario-state mechanism, so that
an imported interaction can be gated on its provider state being activated before the
interaction is served or verified.
Why scenario state
A Pact provider state is a named precondition the provider must establish before an interaction is exercised (e.g."given a user with id 1 exists"). MockServer already
models named, transition-able preconditions via ScenarioManager (a single scenario
holding one active state at a time). Rather than inventing a new engine, every provider state
is mapped onto the single, well-known scenario SCENARIO_NAME with the provider-state
name as the required scenario state. An interaction with provider state "user exists"
yields an expectation with scenarioName = "pact-provider-state" and
scenarioState = "user exists"; that expectation only matches once the state is
activated via activate(ScenarioManager, String).
Parsing
Both Pact wire forms are recognised:- v2:
"providerState": "a user exists"— a single string - v3:
"providerStates": [{"name": "a user exists", "params": {...}}]— an array of objects (thenameis used;paramsare preserved for callbacks but do not affect matching)
gatingStateOf(com.fasterxml.jackson.databind.JsonNode)); any additional states on the same interaction are not
currently honoured. namesOf(com.fasterxml.jackson.databind.JsonNode) still returns the full ordered list for callers that want to
inspect or display every declared state.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe single well-known scenario name under which all Pact provider states are tracked. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidactivate(ScenarioManager scenarioManager, String providerState) Activates a provider state: the provider-state callback.static voidclear(ScenarioManager scenarioManager) Clears any active Pact provider state, returning the well-known scenario to its implicitScenarioManager.STARTEDstate.static StringgatingStateOf(com.fasterxml.jackson.databind.JsonNode interaction) Returns the gating provider-state name for an interaction — the first declared state — ornullif the interaction declares no provider state.namesOf(com.fasterxml.jackson.databind.JsonNode interaction) Extracts the ordered list of provider-state names declared on a Pact interaction node, tolerating both the v2providerStatestring and the v3providerStatesarray.
-
Field Details
-
SCENARIO_NAME
The single well-known scenario name under which all Pact provider states are tracked. Stateless interactions never touch this scenario, so they are unaffected.- See Also:
-
-
Method Details
-
namesOf
Extracts the ordered list of provider-state names declared on a Pact interaction node, tolerating both the v2providerStatestring and the v3providerStatesarray. Blank names are skipped. Returns an empty list when the interaction declares no state.- Parameters:
interaction- the Pact interaction JSON node (may be a missing node)- Returns:
- the provider-state names in declared order (never null)
-
gatingStateOf
Returns the gating provider-state name for an interaction — the first declared state — ornullif the interaction declares no provider state. This is the value stored as an expectation'sscenarioState.- Parameters:
interaction- the Pact interaction JSON node- Returns:
- the gating provider-state name, or null when stateless
-
activate
Activates a provider state: the provider-state callback. Sets the well-known scenario into the given state so that any imported expectation gated on that provider state will match. A blank state name is a no-op, so callers can activate unconditionally.- Parameters:
scenarioManager- the scenario manager backing matching (fromRequestMatchers)providerState- the provider-state name to activate; blank/null is a no-op
-
clear
Clears any active Pact provider state, returning the well-known scenario to its implicitScenarioManager.STARTEDstate. Useful between verification runs or when tearing down.- Parameters:
scenarioManager- the scenario manager backing matching
-