Class PactProviderStates

java.lang.Object
org.mockserver.mock.pact.PactProviderStates

public final class PactProviderStates extends Object
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 (the name is used; params are preserved for callbacks but do not affect matching)
An interaction may legitimately declare several provider states. Because a single MockServer scenario holds exactly one active state at a time, only the first non-blank state name gates 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

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The single well-known scenario name under which all Pact provider states are tracked.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    activate(ScenarioManager scenarioManager, String providerState)
    Activates a provider state: the provider-state callback.
    static void
    clear(ScenarioManager scenarioManager)
    Clears any active Pact provider state, returning the well-known scenario to its implicit ScenarioManager.STARTED state.
    static String
    gatingStateOf(com.fasterxml.jackson.databind.JsonNode interaction)
    Returns the gating provider-state name for an interaction — the first declared state — or null if the interaction declares no provider state.
    static List<String>
    namesOf(com.fasterxml.jackson.databind.JsonNode interaction)
    Extracts the ordered list of provider-state names declared on a Pact interaction node, tolerating both the v2 providerState string and the v3 providerStates array.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SCENARIO_NAME

      public static final String 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

      public static List<String> namesOf(com.fasterxml.jackson.databind.JsonNode interaction)
      Extracts the ordered list of provider-state names declared on a Pact interaction node, tolerating both the v2 providerState string and the v3 providerStates array. 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

      public static String gatingStateOf(com.fasterxml.jackson.databind.JsonNode interaction)
      Returns the gating provider-state name for an interaction — the first declared state — or null if the interaction declares no provider state. This is the value stored as an expectation's scenarioState.
      Parameters:
      interaction - the Pact interaction JSON node
      Returns:
      the gating provider-state name, or null when stateless
    • activate

      public static void activate(ScenarioManager scenarioManager, String providerState)
      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 (from RequestMatchers)
      providerState - the provider-state name to activate; blank/null is a no-op
    • clear

      public static void clear(ScenarioManager scenarioManager)
      Clears any active Pact provider state, returning the well-known scenario to its implicit ScenarioManager.STARTED state. Useful between verification runs or when tearing down.
      Parameters:
      scenarioManager - the scenario manager backing matching