13
13
*/
14
14
public class MongoDBAtlasLocalContainer extends GenericContainer <MongoDBAtlasLocalContainer > {
15
15
16
- private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName .parse ("mongodb/mongodb-atlas-local" );
16
+ private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName .parse (
17
+ "mongodb/mongodb-atlas-local" );
17
18
18
19
private static final int MONGODB_INTERNAL_PORT = 27017 ;
20
+ private static final String MONGODB_DATABASE_NAME_DEFAULT = "test" ;
21
+ private static final String DIRECT_CONNECTION = "directConnection=true" ;
19
22
20
23
public MongoDBAtlasLocalContainer (final String dockerImageName ) {
21
24
this (DockerImageName .parse (dockerImageName ));
@@ -33,6 +36,32 @@ public MongoDBAtlasLocalContainer(final DockerImageName dockerImageName) {
33
36
* Get the connection string to MongoDB.
34
37
*/
35
38
public String getConnectionString () {
36
- return String .format ("mongodb://%s:%d/?directConnection=true" , getHost (), getMappedPort (MONGODB_INTERNAL_PORT ));
39
+ return baseConnectionString () + "/?" + DIRECT_CONNECTION ;
40
+ }
41
+
42
+ private String baseConnectionString () {
43
+ return String .format ("mongodb://%s:%d" , getHost (), getMappedPort (MONGODB_INTERNAL_PORT ));
44
+ }
45
+
46
+ /**
47
+ * Gets a database specific connection string for the default {@value #MONGODB_DATABASE_NAME_DEFAULT} database.
48
+ *
49
+ * @return a database specific connection string.
50
+ */
51
+ public String getDatabaseConnectionString () {
52
+ return getDatabaseConnectionString (MONGODB_DATABASE_NAME_DEFAULT );
53
+ }
54
+
55
+ /**
56
+ * Gets a database specific connection string for a provided <code>databaseName</code>.
57
+ *
58
+ * @param databaseName a database name.
59
+ * @return a database specific connection string.
60
+ */
61
+ public String getDatabaseConnectionString (final String databaseName ) {
62
+ if (!isRunning ()) {
63
+ throw new IllegalStateException ("MongoDBContainer should be started first" );
64
+ }
65
+ return baseConnectionString () + "/" + databaseName + "?" + DIRECT_CONNECTION ;
37
66
}
38
67
}
0 commit comments