Class AbstractTestContainersProvider<T extends org.testcontainers.containers.GenericContainer<? extends T>>
java.lang.Object
io.micronaut.testresources.testcontainers.AbstractTestContainersProvider<T>
- Type Parameters:
T- the container type
- All Implemented Interfaces:
io.micronaut.core.order.Ordered, TestResourcesResolver, ToggableTestResourcesResolver
- Direct Known Subclasses:
AbstractHibernateReactiveTestResourceProvider, AbstractJdbcTestResourceProvider, AbstractR2DBCTestResourceProvider, AzuriteTestResourceProvider, ConsulTestResourceProvider, CouchbaseTestResourceProvider, HazelcastTestResourceProvider, HiveMQTestResourceProvider, InfinispanTestResourceProvider, KafkaTestResourceProvider, KeycloakTestResourceProvider, LocalStackTestResourceProvider, MinioTestResourceProvider, MongoDBTestResourceProvider, Neo4jTestResourceProvider, OpenSearchTestResourceProvider, PulsarTestResourceProvider, RabbitMQTestResourceProvider, RedisClusterTestResourceProvider, RedisTestResourceProvider, SeaweedFsTestResourceProvider, SolrTestResourceProvider, VaultTestResourceProvider, WireMockTestResourceProvider
public abstract class AbstractTestContainersProvider<T extends org.testcontainers.containers.GenericContainer<? extends T>>
extends Object
implements ToggableTestResourcesResolver
The base class for test resources providers which spawn test containers.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interface -
Field Summary
Fields inherited from interface io.micronaut.core.order.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCEFields inherited from interface TestResourcesResolver
TEST_RESOURCES_PROPERTY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidconfigureContainer(T container, Map<String, Object> properties, Map<String, Object> testResourcesConfig) protected abstract TcreateContainer(org.testcontainers.utility.DockerImageName imageName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfig) Creates the test container.protected final voidexecuteInContainer(String failureMessage, AbstractTestContainersProvider.ContainerCommand command) Executes a command inside a running container and converts failures into a consistentIllegalStateException.protected StringgetContainerOwnerKey(String propertyName, Map<String, Object> properties, Map<String, Object> testResourcesConfig) Returns the owner key used to scope cached containers for this resolver.getContainerQuery(String propertyName, Map<String, Object> properties, Map<String, Object> testResourcesConfig) Returns the query object used to look up or create a cached container.protected abstract StringReturns the default image name.Returns the default startup timeout for this provider before user metadata is applied.getName()intgetOrder()protected abstract StringReturns the name of the resource resolver, for example "kafka" or "mysql".protected voidprepareContainer(String propertyName, T container, Map<String, Object> properties, Map<String, Object> testResourcesConfig) Resolves the given property.resolveProperty(String propertyName, T container) resolveProperty(String propertyName, T container, Map<String, Object> properties, Map<String, Object> testResourcesConfig) Resolves the requested property from the started container with access to the full requested-property map and test-resources configuration.resolveWithoutContainer(String propertyName, Map<String, Object> properties, Map<String, Object> testResourcesConfig) Lets a resolver provide a value for the requested property without triggering the creation of a test container.protected booleanshouldAnswer(String propertyName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfig) Determines if this resolver can resolve the requested property.protected final StringstringOrNull(Object value) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface TestResourcesResolver
getId, getRequiredProperties, getRequiredPropertyEntries, getResolvablePropertiesMethods inherited from interface ToggableTestResourcesResolver
getDisplayName, isEnabled
-
Constructor Details
-
AbstractTestContainersProvider
public AbstractTestContainersProvider()
-
-
Method Details
-
getName
- Specified by:
getNamein interfaceToggableTestResourcesResolver
-
getOrder
public int getOrder()- Specified by:
getOrderin interfaceio.micronaut.core.order.Ordered
-
getSimpleName
Returns the name of the resource resolver, for example "kafka" or "mysql".- Returns:
- the name of the resolver
-
getDefaultImageName
Returns the default image name.- Returns:
- the default image name.
-
createContainer
protected abstract T createContainer(org.testcontainers.utility.DockerImageName imageName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfig) Creates the test container.- Parameters:
imageName- the docker image namerequestedProperties- the resolved propertiestestResourcesConfig- the test resources configuration- Returns:
- a container
-
shouldAnswer
protected boolean shouldAnswer(String propertyName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfig) Determines if this resolver can resolve the requested property. It is used in order to make sure that a "Postgres" resolver wouldn't provide a value if the requested container type is for MySQL, for example.- Parameters:
propertyName- the property to resolverequestedProperties- the resolved propertiestestResourcesConfig- the test resources configuration- Returns:
- if this resolver should answer
-
resolveWithoutContainer
protected Optional<String> resolveWithoutContainer(String propertyName, Map<String, Object> properties, Map<String, Object> testResourcesConfig) Lets a resolver provide a value for the requested property without triggering the creation of a test container. This can be used in case a resolver wants to check existing containers first.- Parameters:
propertyName- the name of the property to resolveproperties- the properties used to resolvetestResourcesConfig- the test resources configuration- Returns:
- a resolved property
-
resolve
public final Optional<String> resolve(String propertyName, Map<String, Object> properties, Map<String, Object> testResourcesConfig) Description copied from interface:TestResourcesResolverResolves the given property.- Specified by:
resolvein interfaceTestResourcesResolver- Parameters:
propertyName- the property to resolveproperties- the resolved required propertiestestResourcesConfig- the test resources configuration- Returns:
- the resolved property or empty if not found
-
getContainerOwnerKey
protected String getContainerOwnerKey(String propertyName, Map<String, Object> properties, Map<String, Object> testResourcesConfig) Returns the owner key used to scope cached containers for this resolver. Subclasses may override to share a physical container across multiple logical consumers, but should keep the returned key stable for equivalent requests.- Parameters:
propertyName- the property being resolvedproperties- the resolved properties for the requesttestResourcesConfig- the test resources configuration- Returns:
- the owner key used for container reuse
-
getContainerQuery
protected Map<String,Object> getContainerQuery(String propertyName, Map<String, Object> properties, Map<String, Object> testResourcesConfig) Returns the query object used to look up or create a cached container. Subclasses may override to normalize request-specific properties into a stable physical-resource identity while preserving any keys needed for safe reuse.- Parameters:
propertyName- the property being resolvedproperties- the resolved properties for the requesttestResourcesConfig- the test resources configuration- Returns:
- the container query used for cache lookup
-
configureContainer
-
getDefaultStartupTimeout
protected Optional<Duration> getDefaultStartupTimeout(Map<String, Object> properties, Map<String, Object> testResourcesConfig) Returns the default startup timeout for this provider before user metadata is applied.- Parameters:
properties- the resolved properties for the requesttestResourcesConfig- the test resources configuration- Returns:
- the provider default startup timeout, if any
-
prepareContainer
-
resolveProperty
protected Optional<String> resolveProperty(String propertyName, T container, Map<String, Object> properties, Map<String, Object> testResourcesConfig) Resolves the requested property from the started container with access to the full requested-property map and test-resources configuration. Subclasses may override when the resolved value depends on request metadata in addition to the container itself.- Parameters:
propertyName- the property being resolvedcontainer- the started containerproperties- the resolved properties for the requesttestResourcesConfig- the test resources configuration- Returns:
- the resolved value, if any
-
resolveProperty
-
executeInContainer
protected final void executeInContainer(String failureMessage, AbstractTestContainersProvider.ContainerCommand command) Executes a command inside a running container and converts failures into a consistentIllegalStateException. Interrupted executions preserve the current thread interrupt status before the exception is raised.- Parameters:
failureMessage- the message to use when the command failscommand- the command to execute
-
stringOrNull
-