Package org.mockserver.mock.pact
Class PactImporter
java.lang.Object
org.mockserver.mock.pact.PactImporter
Imports a Pact v3 consumer contract (JSON) and generates one
MockServer
Unmapped rule types fall back to exact matching and are logged at DEBUG.
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
HttpRequestmatcher fromrequest(method,path,query,headers,body), and - an
HttpResponsefromresponse(status,headers,body).
matchingRules mapping
Pact v3 carries match instructions in an interaction-levelmatchingRules.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 rule | MockServer matcher value |
|---|---|
regex | the 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 rule | the concrete example value (exact match) |
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionimportExpectations(String pactJson) Parses a Pact v3 contract and returns one redacted expectation per interaction.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.
-
Constructor Details
-
PactImporter
public PactImporter() -
PactImporter
-
-
Method Details
-
importExpectations
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 stringredactionOptions- controls whether/how sensitive data is masked; passImportRedaction.Options.disabled()to keep values verbatim- Returns:
- the generated expectations
- Throws:
IllegalArgumentException- if the JSON is null, blank, malformed, or has no interactions
-