Skip to content

Commit

Permalink
Allow users to specify a MongoDB database name (#2980)
Browse files Browse the repository at this point in the history
Co-authored-by: Richard North <rich.north@gmail.com>
  • Loading branch information
silaev and rnorth authored Oct 13, 2020
1 parent b073523 commit 8ab49ee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,30 @@ public MongoDBContainer(final DockerImageName dockerImageName) {
);
}

/**
* Gets a replica set url for the default {@value #MONGODB_DATABASE_NAME_DEFAULT} database.
*
* @return a replica set url.
*/
public String getReplicaSetUrl() {
return getReplicaSetUrl(MONGODB_DATABASE_NAME_DEFAULT);
}

/**
* Gets a replica set url for a provided <code>databaseName</code>.
*
* @param databaseName a database name.
* @return a replica set url.
*/
public String getReplicaSetUrl(final String databaseName) {
if (!isRunning()) {
throw new IllegalStateException("MongoDBContainer should be started first");
}
return String.format(
"mongodb://%s:%d/%s",
getContainerIpAddress(),
getMappedPort(MONGODB_INTERNAL_PORT),
MONGODB_DATABASE_NAME_DEFAULT
databaseName
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import org.junit.Test;
import org.testcontainers.utility.DockerImageName;

import static org.hamcrest.CoreMatchers.endsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;


public class MongoDBContainerTest {
Expand Down Expand Up @@ -82,4 +84,15 @@ public void supportsMongoDB_4_4() {
mongoDBContainer.start();
}
}

@Test
public void shouldTestDatabaseName() {
try (
final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10"))
) {
mongoDBContainer.start();
final String databaseName = "my-db";
assertThat(mongoDBContainer.getReplicaSetUrl(databaseName), endsWith(databaseName));
}
}
}

0 comments on commit 8ab49ee

Please sign in to comment.