Skip to content

Commit

Permalink
Let the user choose a custom db name (#83)
Browse files Browse the repository at this point in the history
* Let the user choose a custom db name

Fixes #74

* Hardcode MongoDB image version

This works around a breaking change in the latest image which isn't
compatible with Testcontainers.

See testcontainers/testcontainers-java#5774
  • Loading branch information
melix committed Aug 30, 2022
1 parent 30b062d commit e62ca56
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/docs/guide/modules-mongodb.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
MongoDB support will automatically start a https://www.mongodb.com[MongoDB container] and provide the value of the `mongodb.uri` property.

The default image can be overwritten by setting the `test-resources.containers.mongodb.image-name` property.

The default database name can be overwritten by setting the `test-resources.containers.mongodb.db-name` property.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
public class MongoDBTestResourceProvider extends AbstractTestContainersProvider<MongoDBContainer> {

public static final String MONGODB_SERVER_URI = "mongodb.uri";
public static final String DEFAULT_IMAGE = "mongo";
public static final String DEFAULT_IMAGE = "mongo:5";
public static final String SIMPLE_NAME = "mongodb";
public static final String DB_NAME = "containers." + SIMPLE_NAME + ".db-name";

private String dbName;

@Override
public List<String> getResolvableProperties(Map<String, Collection<String>> propertyEntries, Map<String, Object> testResourcesConfig) {
Expand All @@ -40,7 +44,7 @@ public List<String> getResolvableProperties(Map<String, Collection<String>> prop

@Override
protected String getSimpleName() {
return "mongodb";
return SIMPLE_NAME;
}

@Override
Expand All @@ -50,12 +54,17 @@ protected String getDefaultImageName() {

@Override
protected MongoDBContainer createContainer(DockerImageName imageName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfiguration) {
Object configuredDbName = testResourcesConfiguration.get(DB_NAME);
if (configuredDbName != null) {
this.dbName = configuredDbName.toString();
}
return new MongoDBContainer(imageName);
}

@Override
protected Optional<String> resolveProperty(String propertyName, MongoDBContainer container) {
return Optional.of(container.getReplicaSetUrl());
String url = dbName == null ? container.getReplicaSetUrl() : container.getReplicaSetUrl(dbName);
return Optional.of(url);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.micronaut.testresources.mongodb

import io.micronaut.context.annotation.Value
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject

@MicronautTest(environments = ["test", "custom-db-name"])
class CustomDbNameMongoDBTest extends AbstractMongoDBSpec {

@Inject
BookRepository bookRepository

@Value("\${mongodb.uri}")
String mongodbUri

def "automatically starts a MongoDB container"() {
given:
def book = new Book(title: "Micronaut for Spring developers")
bookRepository.save(book)

when:
def books = bookRepository.findAll()

then:
books.size() == 1

and:
mongodbUri.endsWith "/custom-db-name"
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test-resources:
containers:
mongodb:
db-name: custom-db-name

0 comments on commit e62ca56

Please sign in to comment.