Class CloudStub
- All Implemented Interfaces:
AutoCloseable
Boots an embedded HTTP server on a random available port, redirects all AWS SDK v2 traffic to
that port via the aws.endpoint-url system property, and invokes every CloudStubService discovered on the classpath to register their stubs.
This class coordinates lifecycle and exposes the public API; the work of each concern is
delegated to focused collaborators in io.cloudstub.core.internal: building the server
(WireMockServerFactory), choosing the state store (StateStoreFactory),
discovering and registering modules (ModuleInitializer), translating the request journal
(RequestHistory), and the SDK endpoint override (AwsEndpointOverride).
Typical usage in a JUnit 6 test:
@BeforeAll
static void start() { cloudMock = new CloudStub(); cloudMock.start(); }
@AfterAll
static void stop() { cloudMock.stop(); }
Or with try-with-resources:
try (CloudStub cm = new CloudStub()) {
cm.start();
// run tests
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault cap on retained request-history entries; bounds memory in long-lived processes. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidRemoves all active fault stubs for every service.voidclearFaults(String serviceId) Removes all active fault stubs forserviceId, restoring normal stub behaviour.voidClears the captured request history, leaving registered stubs and stored state intact.voidclose()modules()Returns a live snapshot of all loaded modules and their registered stubs.intport()Returns the port the server is listening on.Returns all requests served since startup, newest first.requestHistory(String serviceId) Returns all requests served to the given service since startup, newest first.voidsimulateNetworkBrownout(String serviceId, double rate) Causes approximatelyratefraction of requests toserviceIdto fail with a connection reset; the remainder are served normally.voidsimulateThrottle(String serviceId) Causes all stubs forserviceIdto return an AWS-style throttling error (HTTP 400,ThrottlingException) for the duration of the current test.voidsimulateTimeout(String serviceId) Causes all stubs forserviceIdto respond after a long fixed delay, triggering the AWS SDK's call timeout exception.voidstart()Starts the embedded server, registers all discovered service stubs, and injectsaws.endpoint-urlso the AWS SDK v2 routes traffic locally.Returns the instant the server was started.Returns the shared state store.voidstop()Stops the embedded server and removes theaws.endpoint-urlsystem property.withEnabledServices(Collection<String> serviceIds) RestrictsServiceLoaderdiscovery to the given service IDs.withMaxRequestHistory(int maxEntries) Caps the number of request-history entries retained in memory.withPersistenceBackend(StatePersistence backend) Selects the persistent state backend used when a store directory is configured viawithStoreDirectory(java.nio.file.Path).withPort(int port) Binds the server to a specific port instead of a random available one.withService(CloudStubService service) Registers a service module explicitly, in addition to any modules discovered viaServiceLoader.withStoreDirectory(Path directory) Configures a directory for persistent state storage.
-
Field Details
-
DEFAULT_MAX_REQUEST_HISTORY
public static final int DEFAULT_MAX_REQUEST_HISTORYDefault cap on retained request-history entries; bounds memory in long-lived processes.- See Also:
-
-
Constructor Details
-
CloudStub
public CloudStub()
-
-
Method Details
-
withStoreDirectory
Configures a directory for persistent state storage. State written to the store will survive a CloudStub restart. When not set, an in-memory store is used (state lost on stop).Must be called before
start().- Parameters:
directory- directory where the state file is written (acloudstub-state.logappend-only log by default; seewithPersistenceBackend(io.cloudstub.core.StatePersistence))- Throws:
CloudStubAlreadyStartedException- if already started
-
withPersistenceBackend
Selects the persistent state backend used when a store directory is configured viawithStoreDirectory(java.nio.file.Path). Defaults toStatePersistence.APPEND_LOG, whose write cost scales with the change rather than the whole store. Has no effect on an in-memory store (no directory set). Must be called beforestart().- Throws:
CloudStubAlreadyStartedException- if already started
-
withMaxRequestHistory
Caps the number of request-history entries retained in memory. Older entries are discarded once the limit is reached, bounding memory use in a long-lived standalone process. A value of0or less retains an unbounded history. Defaults toDEFAULT_MAX_REQUEST_HISTORY. Must be called beforestart().- Throws:
CloudStubAlreadyStartedException- if already started
-
withPort
Binds the server to a specific port instead of a random available one. Must be called beforestart().- Throws:
CloudStubAlreadyStartedException- if already started
-
withEnabledServices
RestrictsServiceLoaderdiscovery to the given service IDs. Only discovered modules whoseCloudStubService.serviceId()is inserviceIdsare registered; all others are ignored. Modules added viawithService(io.cloudstub.core.spi.CloudStubService)are always registered and are not affected by this filter.Passing
null(the default) registers every discovered module. Must be called beforestart().- Throws:
CloudStubAlreadyStartedException- if the instance is already started
-
withService
Registers a service module explicitly, in addition to any modules discovered viaServiceLoader. Must be called beforestart().Useful in module-level tests where the test classpath structure may prevent ServiceLoader from discovering the module under test automatically.
- Throws:
CloudStubAlreadyStartedException- if already started
-
start
public void start()Starts the embedded server, registers all discovered service stubs, and injectsaws.endpoint-urlso the AWS SDK v2 routes traffic locally.- Throws:
CloudStubAlreadyStartedException- if this instance is already started
-
stop
public void stop()Stops the embedded server and removes theaws.endpoint-urlsystem property. Safe to call on an instance that was never started. -
stateStore
Returns the shared state store. Only valid afterstart(). Exposed for direct state inspection in tests and for the REST API.- Throws:
CloudStubNotStartedException- if not yet started
-
port
public int port()Returns the port the server is listening on. Only valid afterstart(). -
startedAt
Returns the instant the server was started. Only valid afterstart(). -
modules
Returns a live snapshot of all loaded modules and their registered stubs. Only valid afterstart(). -
requestHistory
Returns all requests served since startup, newest first. Only valid afterstart(). -
requestHistory
Returns all requests served to the given service since startup, newest first. Unmatched requests (null serviceId) are excluded. Only valid afterstart(). -
clearHistory
public void clearHistory()Clears the captured request history, leaving registered stubs and stored state intact. Only valid afterstart(). -
simulateThrottle
Causes all stubs forserviceIdto return an AWS-style throttling error (HTTP 400,ThrottlingException) for the duration of the current test.Call
clearFaults(String)orclearAllFaults()to restore normal behaviour.- Throws:
CloudStubNotStartedException- if not yet started
-
simulateTimeout
Causes all stubs forserviceIdto respond after a long fixed delay, triggering the AWS SDK's call timeout exception.- Throws:
CloudStubNotStartedException- if not yet started
-
simulateNetworkBrownout
Causes approximatelyratefraction of requests toserviceIdto fail with a connection reset; the remainder are served normally.Use
rate = 0.0orrate = 1.0for deterministic test assertions. Fractional rates are statistical and unsuitable for exact-count assertions.- Parameters:
rate- fraction of requests to fault, in [0.0, 1.0]- Throws:
CloudStubNotStartedException- if not yet started
-
clearFaults
Removes all active fault stubs forserviceId, restoring normal stub behaviour.- Throws:
CloudStubNotStartedException- if not yet started
-
clearAllFaults
public void clearAllFaults()Removes all active fault stubs for every service. Safe to call even when no faults are active.Called automatically by
CloudStubExtensionafter each test method. -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-