Class PactImporter

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

public class PactImporter extends Object
Imports a Pact v3 consumer contract (JSON) and generates one MockServer Expectation per interaction, so a contract published by a consumer (or exported via PactExporter) can be replayed by MockServer as a stub provider.

This is the inverse of PactVerifier: rather than checking that existing expectations satisfy a contract, it creates expectations directly from the contract's interactions. For each interaction it builds:

  • an HttpRequest matcher from request (method, path, query, headers, body), and
  • an HttpResponse from response (status, headers, body).

matchingRules mapping

Pact v3 carries match instructions in an interaction-level matchingRules.request object keyed by category (path, query, header, body). MockServer's RegexStringMatcher already treats a matcher value as a regular expression (falling back to exact-string equality), so the importer maps Pact rules onto plain matcher values:
Pact matchingRule to MockServer matcher
Pact ruleMockServer matcher value
regexthe supplied regex pattern (matched as a regex)
include.*<value>.* substring regex
type / number / integer / decimal / boolean relaxed to a value-presence regex .+ (any non-empty value of that field)
anything else / no rulethe concrete example value (exact match)
Unmapped rule types fall back to exact matching and are logged at DEBUG.

Redaction (see ImportRedaction) is applied to the generated expectations exactly as for HAR/Postman imports, so credentials carried in a contract never land verbatim in the expectation store. Redaction is on by default; pass ImportRedaction.Options.disabled() to keep values verbatim.

  • Constructor Details

    • PactImporter

      public PactImporter()
    • PactImporter

      public PactImporter(MockServerLogger mockServerLogger)
  • Method Details

    • importExpectations

      public List<Expectation> importExpectations(String pactJson)
      Parses a Pact v3 contract and returns one redacted expectation per interaction.
      Parameters:
      pactJson - the Pact v3 contract as a JSON string
      Returns:
      the generated (redacted) expectations
      Throws:
      IllegalArgumentException - if the JSON is null, blank, malformed, or has no interactions
    • importExpectations

      public List<Expectation> importExpectations(String pactJson, ImportRedaction.Options redactionOptions)
      Parses a Pact v3 contract and returns one expectation per interaction, applying the supplied redaction options before the expectations are returned.
      Parameters:
      pactJson - the Pact v3 contract as a JSON string
      redactionOptions - controls whether/how sensitive data is masked; pass ImportRedaction.Options.disabled() to keep values verbatim
      Returns:
      the generated expectations
      Throws:
      IllegalArgumentException - if the JSON is null, blank, malformed, or has no interactions