public class MockServerClient extends Object implements Stoppable
Modifier and Type | Field and Description |
---|---|
protected CompletableFuture<Integer> |
portFuture |
Constructor and Description |
---|
MockServerClient(ClientConfiguration configuration,
CompletableFuture<Integer> portFuture)
Start the client communicating to a MockServer on localhost at the port
specified with the Future
|
MockServerClient(ClientConfiguration configuration,
String host,
int port)
Start the client communicating to a MockServer at the specified host and port
for example:
|
MockServerClient(ClientConfiguration configuration,
String host,
int port,
String contextPath)
Start the client communicating to a MockServer at the specified host and port
and contextPath for example:
|
MockServerClient(Configuration configuration,
CompletableFuture<Integer> portFuture)
Start the client communicating to a MockServer on localhost at the port
specified with the Future
|
MockServerClient(Configuration configuration,
String host,
int port)
Start the client communicating to a MockServer at the specified host and port
for example:
|
MockServerClient(Configuration configuration,
String host,
int port,
String contextPath)
Start the client communicating to a MockServer at the specified host and port
and contextPath for example:
|
MockServerClient(String host,
int port)
Start the client communicating to a MockServer at the specified host and port
for example:
|
MockServerClient(String host,
int port,
String contextPath)
Start the client communicating to a MockServer at the specified host and port
and contextPath for example:
|
Modifier and Type | Method and Description |
---|---|
List<Integer> |
bind(Integer... ports)
Bind new ports to listen on
|
MockServerClient |
clear(ExpectationId expectationId)
Clear all expectations and logs that match the expectation id
|
MockServerClient |
clear(ExpectationId expectationId,
ClearType type)
Clear expectations, logs or both that match the expectation id
|
MockServerClient |
clear(RequestDefinition requestDefinition)
Clear all expectations and logs that match the request matcher
|
MockServerClient |
clear(RequestDefinition requestDefinition,
ClearType type)
Clear expectations, logs or both that match the request matcher
|
MockServerClient |
clear(String expectationId)
Clear all expectations and logs that match the expectation id
|
MockServerClient |
clear(String expectationId,
ClearType type)
Clear expectations, logs or both that match the expectation id
|
void |
close() |
String |
contextPath() |
Integer |
getPort() |
boolean |
hasStarted()
Returns whether MockServer has started, if called after MockServer has been stopped
this method will block for 5 seconds while confirming MockServer is not starting
|
boolean |
hasStarted(int attempts,
long timeout,
TimeUnit timeUnit)
Returns whether server MockServer has started, by polling the MockServer a configurable amount of times
|
boolean |
hasStopped()
Returns whether MockServer has stopped, if called too quickly after starting MockServer
this may return false because MockServer has not yet started, to ensure MockServer has
started use hasStarted()
|
boolean |
hasStopped(int attempts,
long timeout,
TimeUnit timeUnit)
Returns whether server MockServer has stopped, by polling the MockServer a configurable
amount of times.
|
boolean |
isRunning()
Deprecated.
use hasStopped() or hasStarted() instead
|
boolean |
isRunning(int attempts,
long timeout,
TimeUnit timeUnit)
Deprecated.
use hasStopped() or hasStarted() instead
|
boolean |
isSecure() |
MockServerClient |
openUI()
Launch UI and wait the default period to allow the UI to launch and start collecting logs,
this ensures that the log are visible in the UI even if MockServer is shutdown by a test
shutdown function, such as After, AfterClass, AfterAll, etc
|
MockServerClient |
openUI(TimeUnit timeUnit,
long pause)
Launch UI and wait a specified period to allow the UI to launch and start collecting logs,
this ensures that the log are visible in the UI even if MockServer is shutdown by a test
shutdown function, such as After, AfterClass, AfterAll, etc
|
InetSocketAddress |
remoteAddress() |
MockServerClient |
reset()
Reset MockServer by clearing all expectations
|
Expectation[] |
retrieveActiveExpectations(RequestDefinition requestDefinition)
Retrieve the active expectations match the httpRequest parameter, use null for the parameter to retrieve all expectations
|
String |
retrieveActiveExpectations(RequestDefinition requestDefinition,
Format format)
Retrieve the active expectations match the httpRequest parameter, use null for the parameter to retrieve all expectations
|
String |
retrieveLogMessages(RequestDefinition requestDefinition)
Retrieve the logs associated to a specific requests, this shows all logs for expectation matching, verification, clearing, etc
|
String[] |
retrieveLogMessagesArray(RequestDefinition requestDefinition)
Retrieve the logs associated to a specific requests, this shows all logs for expectation matching, verification, clearing, etc
|
Expectation[] |
retrieveRecordedExpectations(RequestDefinition requestDefinition)
Retrieve the request-response combinations that have been recorded as a list of expectations, only those that match the httpRequest parameter are returned, use null to retrieve all requests
|
String |
retrieveRecordedExpectations(RequestDefinition requestDefinition,
Format format)
Retrieve the request-response combinations that have been recorded as a list of expectations, only those that match the httpRequest parameter are returned, use null to retrieve all requests
|
HttpRequest[] |
retrieveRecordedRequests(RequestDefinition requestDefinition)
Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
|
String |
retrieveRecordedRequests(RequestDefinition requestDefinition,
Format format)
Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
|
LogEventRequestAndResponse[] |
retrieveRecordedRequestsAndResponses(RequestDefinition requestDefinition)
Retrieve the recorded requests and responses that match the httpRequest parameter, use null for the parameter to retrieve all requests and responses
|
String |
retrieveRecordedRequestsAndResponses(RequestDefinition requestDefinition,
Format format)
Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
|
Expectation[] |
sendExpectation(Expectation... expectations)
Deprecated.
this is deprecated due to unclear naming, use method upsert(Expectation... expectations) instead
|
MockServerClient |
setProxyConfiguration(ProxyConfiguration proxyConfiguration)
Deprecated.
use withProxyConfiguration which is more consistent with MockServer API style
|
MockServerClient |
setRequestOverride(HttpRequest requestOverride)
Deprecated.
use withRequestOverride which is more consistent with MockServer API style
|
void |
stop()
Stop MockServer gracefully (only support for Netty version, not supported for WAR version)
|
CompletableFuture<MockServerClient> |
stop(boolean ignoreFailure)
Stop MockServer gracefully (only support for Netty version, not supported for WAR version)
|
Future<MockServerClient> |
stopAsync()
Stop MockServer gracefully (only support for Netty version, not supported for WAR version)
|
Expectation[] |
upsert(Expectation... expectations)
Specify one or more expectations to be create, or updated (if the id matches).
|
Expectation[] |
upsert(OpenAPIExpectation... openAPIExpectations)
Specify OpenAPI and operations and responses to create matchers and example responses
|
MockServerClient |
verify(ExpectationId... expectationIds)
Verify a list of requests have been sent in the order specified for example:
|
MockServerClient |
verify(ExpectationId expectationId,
VerificationTimes times)
Verify a request has been sent for example:
|
MockServerClient |
verify(ExpectationId expectationId,
VerificationTimes times,
Integer maximumNumberOfRequestToReturnInVerificationFailure)
Verify a request has been sent for example:
|
MockServerClient |
verify(Integer maximumNumberOfRequestToReturnInVerificationFailure,
ExpectationId... expectationIds)
Verify a list of requests have been sent in the order specified for example:
|
MockServerClient |
verify(Integer maximumNumberOfRequestToReturnInVerificationFailure,
RequestDefinition... requestDefinitions)
Verify a list of requests have been sent in the order specified for example:
|
MockServerClient |
verify(RequestDefinition... requestDefinitions)
Verify a list of requests have been sent in the order specified for example:
|
MockServerClient |
verify(RequestDefinition requestDefinition,
VerificationTimes times)
Verify a request has been sent for example:
|
MockServerClient |
verify(RequestDefinition requestDefinition,
VerificationTimes times,
Integer maximumNumberOfRequestToReturnInVerificationFailure)
Verify a request has been sent for example:
|
MockServerClient |
verify(String... expectationIds)
Verify a list of requests have been sent in the order specified for example:
|
MockServerClient |
verify(String expectationId,
VerificationTimes times)
Verify a request has been sent for example:
|
MockServerClient |
verifyZeroInteractions()
Verify no requests have been sent.
|
ForwardChainExpectation |
when(RequestDefinition requestDefinition)
Specify an unlimited expectation that will respond regardless of the number of matching http
for example:
|
ForwardChainExpectation |
when(RequestDefinition requestDefinition,
Times times)
Specify a limited expectation that will respond a specified number of times when the http is matched
|
ForwardChainExpectation |
when(RequestDefinition requestDefinition,
Times times,
TimeToLive timeToLive)
Specify a limited expectation that will respond a specified number of times when the http is matched
|
ForwardChainExpectation |
when(RequestDefinition requestDefinition,
Times times,
TimeToLive timeToLive,
Integer priority)
Specify a limited expectation that will respond a specified number of times when the http is matched and will be matched according to priority as follows:
|
MockServerClient |
withControlPlaneJWT(String controlPlaneJWT)
Specify JWT to use for control plane authorisation
|
MockServerClient |
withControlPlaneJWT(Supplier<String> controlPlaneJWTSupplier)
Specify JWT supplier to use for control plane authorisation
|
MockServerClient |
withProxyConfiguration(ProxyConfiguration proxyConfiguration)
Configure communication to MockServer to go via a proxy
|
MockServerClient |
withRequestOverride(HttpRequest requestOverride) |
MockServerClient |
withSecure(boolean secure) |
protected CompletableFuture<Integer> portFuture
public MockServerClient(Configuration configuration, CompletableFuture<Integer> portFuture)
portFuture
- the port for the MockServer to communicate withpublic MockServerClient(ClientConfiguration configuration, CompletableFuture<Integer> portFuture)
portFuture
- the port for the MockServer to communicate withpublic MockServerClient(String host, int port)
MockServerClient mockServerClient = new MockServerClient("localhost", 1080);
host
- the host for the MockServer to communicate withport
- the port for the MockServer to communicate withpublic MockServerClient(Configuration configuration, String host, int port)
MockServerClient mockServerClient = new MockServerClient("localhost", 1080);
host
- the host for the MockServer to communicate withport
- the port for the MockServer to communicate withpublic MockServerClient(ClientConfiguration configuration, String host, int port)
MockServerClient mockServerClient = new MockServerClient("localhost", 1080);
host
- the host for the MockServer to communicate withport
- the port for the MockServer to communicate withpublic MockServerClient(String host, int port, String contextPath)
MockServerClient mockServerClient = new MockServerClient("localhost", 1080, "/mockserver");
host
- the host for the MockServer to communicate withport
- the port for the MockServer to communicate withcontextPath
- the context path that the MockServer war is deployed topublic MockServerClient(Configuration configuration, String host, int port, String contextPath)
MockServerClient mockServerClient = new MockServerClient("localhost", 1080, "/mockserver");
host
- the host for the MockServer to communicate withport
- the port for the MockServer to communicate withcontextPath
- the context path that the MockServer war is deployed topublic MockServerClient(ClientConfiguration configuration, String host, int port, String contextPath)
MockServerClient mockServerClient = new MockServerClient("localhost", 1080, "/mockserver");
host
- the host for the MockServer to communicate withport
- the port for the MockServer to communicate withcontextPath
- the context path that the MockServer war is deployed to@Deprecated public MockServerClient setProxyConfiguration(ProxyConfiguration proxyConfiguration)
public MockServerClient withProxyConfiguration(ProxyConfiguration proxyConfiguration)
public MockServerClient withControlPlaneJWT(String controlPlaneJWT)
public MockServerClient withControlPlaneJWT(Supplier<String> controlPlaneJWTSupplier)
@Deprecated public MockServerClient setRequestOverride(HttpRequest requestOverride)
public MockServerClient withRequestOverride(HttpRequest requestOverride)
public boolean isSecure()
public MockServerClient withSecure(boolean secure)
public InetSocketAddress remoteAddress()
public String contextPath()
public Integer getPort()
public MockServerClient openUI()
public MockServerClient openUI(TimeUnit timeUnit, long pause)
timeUnit
- TimeUnit the time unit, for example TimeUnit.SECONDSpause
- the number of time units to delay before the function returns to ensure the UI is receiving logs@Deprecated public boolean isRunning()
@Deprecated public boolean isRunning(int attempts, long timeout, TimeUnit timeUnit)
public boolean hasStopped()
public boolean hasStopped(int attempts, long timeout, TimeUnit timeUnit)
public boolean hasStarted()
public boolean hasStarted(int attempts, long timeout, TimeUnit timeUnit)
public Future<MockServerClient> stopAsync()
public void stop()
public CompletableFuture<MockServerClient> stop(boolean ignoreFailure)
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public MockServerClient reset()
public MockServerClient clear(RequestDefinition requestDefinition)
requestDefinition
- the http request that is matched against when deciding whether to clear each expectation if null all expectations are clearedpublic MockServerClient clear(String expectationId)
expectationId
- the expectation id that is used to clear expectations and logspublic MockServerClient clear(ExpectationId expectationId)
expectationId
- the expectation id that is used to clear expectations and logspublic MockServerClient clear(RequestDefinition requestDefinition, ClearType type)
requestDefinition
- the http request that is matched against when deciding whether to clear each expectation if null all expectations are clearedtype
- the type to clear, EXPECTATION, LOG or BOTHpublic MockServerClient clear(String expectationId, ClearType type)
expectationId
- the expectation id that is used to clear expectations and logstype
- the type to clear, EXPECTATION, LOG or BOTHpublic MockServerClient clear(ExpectationId expectationId, ClearType type)
expectationId
- the expectation id that is used to clear expectations and logstype
- the type to clear, EXPECTATION, LOG or BOTHpublic MockServerClient verify(RequestDefinition... requestDefinitions) throws AssertionError
mockServerClient .verify( request() .withPath("/first_request") .withBody("some_request_body"), request() .withPath("/second_request") .withBody("some_request_body") );
requestDefinitions
- the http requests that must be matched for this verification to passAssertionError
- if the request has not been foundpublic MockServerClient verify(Integer maximumNumberOfRequestToReturnInVerificationFailure, RequestDefinition... requestDefinitions) throws AssertionError
mockServerClient .verify( request() .withPath("/first_request") .withBody("some_request_body"), request() .withPath("/second_request") .withBody("some_request_body") );
maximumNumberOfRequestToReturnInVerificationFailure
- the maximum number requests return in the error response when the verification failsrequestDefinitions
- the http requests that must be matched for this verification to passAssertionError
- if the request has not been foundpublic MockServerClient verify(String... expectationIds) throws AssertionError
mockServerClient .verify( request() .withPath("/first_request") .withBody("some_request_body"), request() .withPath("/second_request") .withBody("some_request_body") );
expectationIds
- the http requests that must be matched for this verification to passAssertionError
- if the request has not been foundpublic MockServerClient verify(ExpectationId... expectationIds) throws AssertionError
mockServerClient .verify( request() .withPath("/first_request") .withBody("some_request_body"), request() .withPath("/second_request") .withBody("some_request_body") );
expectationIds
- the http requests that must be matched for this verification to passAssertionError
- if the request has not been foundpublic MockServerClient verify(Integer maximumNumberOfRequestToReturnInVerificationFailure, ExpectationId... expectationIds) throws AssertionError
mockServerClient .verify( request() .withPath("/first_request") .withBody("some_request_body"), request() .withPath("/second_request") .withBody("some_request_body") );
maximumNumberOfRequestToReturnInVerificationFailure
- the maximum number requests return in the error response when the verification failsexpectationIds
- the http requests that must be matched for this verification to passAssertionError
- if the request has not been foundpublic MockServerClient verify(RequestDefinition requestDefinition, VerificationTimes times) throws AssertionError
mockServerClient .verify( request() .withPath("/some_path") .withBody("some_request_body"), VerificationTimes.exactly(3) );VerificationTimes supports multiple static factory methods:
once() - verify the request was only received once exactly(n) - verify the request was only received exactly n times atLeast(n) - verify the request was only received at least n times
requestDefinition
- the http request that must be matched for this verification to passtimes
- the number of times this request must be matchedAssertionError
- if the request has not been foundpublic MockServerClient verify(RequestDefinition requestDefinition, VerificationTimes times, Integer maximumNumberOfRequestToReturnInVerificationFailure) throws AssertionError
mockServerClient .verify( request() .withPath("/some_path") .withBody("some_request_body"), VerificationTimes.exactly(3) );VerificationTimes supports multiple static factory methods:
once() - verify the request was only received once exactly(n) - verify the request was only received exactly n times atLeast(n) - verify the request was only received at least n times
requestDefinition
- the http request that must be matched for this verification to passtimes
- the number of times this request must be matchedmaximumNumberOfRequestToReturnInVerificationFailure
- the maximum number requests return in the error response when the verification failsAssertionError
- if the request has not been foundpublic MockServerClient verify(String expectationId, VerificationTimes times) throws AssertionError
mockServerClient .verify( request() .withPath("/some_path") .withBody("some_request_body"), VerificationTimes.exactly(3) );VerificationTimes supports multiple static factory methods:
once() - verify the request was only received once exactly(n) - verify the request was only received exactly n times atLeast(n) - verify the request was only received at least n times
expectationId
- the http request that must be matched for this verification to passtimes
- the number of times this request must be matchedAssertionError
- if the request has not been foundpublic MockServerClient verify(ExpectationId expectationId, VerificationTimes times) throws AssertionError
mockServerClient .verify( request() .withPath("/some_path") .withBody("some_request_body"), VerificationTimes.exactly(3) );VerificationTimes supports multiple static factory methods:
once() - verify the request was only received once exactly(n) - verify the request was only received exactly n times atLeast(n) - verify the request was only received at least n times
expectationId
- the http request that must be matched for this verification to passtimes
- the number of times this request must be matchedAssertionError
- if the request has not been foundpublic MockServerClient verify(ExpectationId expectationId, VerificationTimes times, Integer maximumNumberOfRequestToReturnInVerificationFailure) throws AssertionError
mockServerClient .verify( request() .withPath("/some_path") .withBody("some_request_body"), VerificationTimes.exactly(3) );VerificationTimes supports multiple static factory methods:
once() - verify the request was only received once exactly(n) - verify the request was only received exactly n times atLeast(n) - verify the request was only received at least n times
expectationId
- the http request that must be matched for this verification to passtimes
- the number of times this request must be matchedmaximumNumberOfRequestToReturnInVerificationFailure
- the maximum number requests return in the error response when the verification failsAssertionError
- if the request has not been foundpublic MockServerClient verifyZeroInteractions() throws AssertionError
AssertionError
- if any request has been foundpublic HttpRequest[] retrieveRecordedRequests(RequestDefinition requestDefinition)
requestDefinition
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestspublic String retrieveRecordedRequests(RequestDefinition requestDefinition, Format format)
requestDefinition
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestsformat
- the format to retrieve the expectations, either JAVA or JSONpublic LogEventRequestAndResponse[] retrieveRecordedRequestsAndResponses(RequestDefinition requestDefinition)
requestDefinition
- the http request that is matched against when deciding whether to return each request (and its corresponding response), use null for the parameter to retrieve for all requestspublic String retrieveRecordedRequestsAndResponses(RequestDefinition requestDefinition, Format format)
requestDefinition
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestsformat
- the format to retrieve the expectations, either JAVA or JSONpublic Expectation[] retrieveRecordedExpectations(RequestDefinition requestDefinition)
requestDefinition
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestspublic String retrieveRecordedExpectations(RequestDefinition requestDefinition, Format format)
requestDefinition
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestsformat
- the format to retrieve the expectations, either JAVA or JSONpublic String retrieveLogMessages(RequestDefinition requestDefinition)
requestDefinition
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestspublic String[] retrieveLogMessagesArray(RequestDefinition requestDefinition)
requestDefinition
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestspublic ForwardChainExpectation when(RequestDefinition requestDefinition)
mockServerClient .when( request() .withPath("/some_path") .withBody("some_request_body") ) .respond( response() .withBody("some_response_body") .withHeader("responseName", "responseValue") )
requestDefinition
- the http request that must be matched for this expectation to respondpublic ForwardChainExpectation when(RequestDefinition requestDefinition, Times times)
Example use:
mockServerClient .when( request() .withPath("/some_path") .withBody("some_request_body"), Times.exactly(5) ) .respond( response() .withBody("some_response_body") .withHeader("responseName", "responseValue") )
requestDefinition
- the http request that must be matched for this expectation to respondtimes
- the number of times to respond when this http is matchedpublic ForwardChainExpectation when(RequestDefinition requestDefinition, Times times, TimeToLive timeToLive)
Example use:
mockServerClient .when( request() .withPath("/some_path") .withBody("some_request_body"), Times.exactly(5), TimeToLive.exactly(TimeUnit.SECONDS, 120) ) .respond( response() .withBody("some_response_body") .withHeader("responseName", "responseValue") )
requestDefinition
- the http request that must be matched for this expectation to respondtimes
- the number of times to respond when this http is matchedtimeToLive
- the length of time from when the server receives the expectation that the expectation should be activepublic ForwardChainExpectation when(RequestDefinition requestDefinition, Times times, TimeToLive timeToLive, Integer priority)
- higher priority expectation will be matched first - identical priority expectations will be match in the order they were submitted - default priority is 0
Example use:
mockServerClient .when( request() .withPath("/some_path") .withBody("some_request_body"), Times.exactly(5), TimeToLive.exactly(TimeUnit.SECONDS, 120), 10 ) .respond( response() .withBody("some_response_body") .withHeader("responseName", "responseValue") )
requestDefinition
- the http request that must be matched for this expectation to respondtimes
- the number of times to respond when this http is matchedtimeToLive
- the length of time from when the server receives the expectation that the expectation should be activepriority
- the priority for the expectation when matching, higher priority expectation will be matched first, identical priority expectations will be match in the order they were submittedpublic Expectation[] upsert(OpenAPIExpectation... openAPIExpectations)
openAPIExpectations
- the OpenAPI and operations and responses to create matchers and example responsespublic Expectation[] upsert(Expectation... expectations)
This method should be used to update existing expectation by id. All fields will be updated for expectations with a matching id as the existing expectation is deleted and recreated.
To retrieve the id(s) for existing expectation(s) the retrieveActiveExpectations(HttpRequest httpRequest) method can be used.
Typically, to create expectations this method should not be used directly instead the when(...) and response(...) or forward(...) or error(...) methods should be used for example:
mockServerClient .when( request() .withPath("/some_path") .withBody("some_request_body"), Times.exactly(5), TimeToLive.exactly(TimeUnit.SECONDS, 120) ) .respond( response() .withBody("some_response_body") .withHeader("responseName", "responseValue") )
expectations
- one or more expectations to create or update (if the id field matches)@Deprecated public Expectation[] sendExpectation(Expectation... expectations)
mockServerClient .when( request() .withPath("/some_path") .withBody("some_request_body"), Times.exactly(5), TimeToLive.exactly(TimeUnit.SECONDS, 120) ) .respond( response() .withBody("some_response_body") .withHeader("responseName", "responseValue") )
expectations
- one or more expectationspublic Expectation[] retrieveActiveExpectations(RequestDefinition requestDefinition)
requestDefinition
- the http request that is matched against when deciding whether to return each expectation, use null for the parameter to retrieve for all requestspublic String retrieveActiveExpectations(RequestDefinition requestDefinition, Format format)
requestDefinition
- the http request that is matched against when deciding whether to return each expectation, use null for the parameter to retrieve for all requestsformat
- the format to retrieve the expectations, either JAVA or JSONCopyright © 2022. All rights reserved.