11package org .testcontainers .containers ;
22
3- import com .mongodb .ReadConcern ;
4- import com .mongodb .ReadPreference ;
5- import com .mongodb .TransactionOptions ;
6- import com .mongodb .WriteConcern ;
7- import com .mongodb .client .ClientSession ;
8- import com .mongodb .client .MongoClient ;
9- import com .mongodb .client .MongoClients ;
10- import com .mongodb .client .MongoCollection ;
11- import com .mongodb .client .TransactionBody ;
12- import org .bson .Document ;
133import org .junit .Test ;
14- import org .testcontainers .utility .DockerImageName ;
154
165import static org .assertj .core .api .Assertions .assertThat ;
176
18- public class MongoDBContainerTest {
7+ public class MongoDBContainerTest extends AbstractMongo {
198
209 /**
2110 * Taken from <a href="https://docs.mongodb.com/manual/core/transactions/">https://docs.mongodb.com</a>
@@ -24,7 +13,7 @@ public class MongoDBContainerTest {
2413 public void shouldExecuteTransactions () {
2514 try (
2615 // creatingMongoDBContainer {
27- final MongoDBContainer mongoDBContainer = new MongoDBContainer (DockerImageName . parse ( "mongo:4.0.10" ) )
16+ final MongoDBContainer mongoDBContainer = new MongoDBContainer ("mongo:4.0.10" )
2817 // }
2918 ) {
3019 // startingMongoDBContainer {
@@ -34,91 +23,19 @@ public void shouldExecuteTransactions() {
3423 }
3524 }
3625
37- private void executeTx (MongoDBContainer mongoDBContainer ) {
38- final MongoClient mongoSyncClientBase = MongoClients .create (mongoDBContainer .getConnectionString ());
39- final MongoClient mongoSyncClient = MongoClients .create (mongoDBContainer .getReplicaSetUrl ());
40- mongoSyncClient
41- .getDatabase ("mydb1" )
42- .getCollection ("foo" )
43- .withWriteConcern (WriteConcern .MAJORITY )
44- .insertOne (new Document ("abc" , 0 ));
45- mongoSyncClient
46- .getDatabase ("mydb2" )
47- .getCollection ("bar" )
48- .withWriteConcern (WriteConcern .MAJORITY )
49- .insertOne (new Document ("xyz" , 0 ));
50- mongoSyncClientBase
51- .getDatabase ("mydb3" )
52- .getCollection ("baz" )
53- .withWriteConcern (WriteConcern .MAJORITY )
54- .insertOne (new Document ("def" , 0 ));
55-
56- final ClientSession clientSession = mongoSyncClient .startSession ();
57- final TransactionOptions txnOptions = TransactionOptions
58- .builder ()
59- .readPreference (ReadPreference .primary ())
60- .readConcern (ReadConcern .LOCAL )
61- .writeConcern (WriteConcern .MAJORITY )
62- .build ();
63-
64- final String trxResult = "Inserted into collections in different databases" ;
65-
66- TransactionBody <String > txnBody = () -> {
67- final MongoCollection <Document > coll1 = mongoSyncClient .getDatabase ("mydb1" ).getCollection ("foo" );
68- final MongoCollection <Document > coll2 = mongoSyncClient .getDatabase ("mydb2" ).getCollection ("bar" );
69-
70- coll1 .insertOne (clientSession , new Document ("abc" , 1 ));
71- coll2 .insertOne (clientSession , new Document ("xyz" , 999 ));
72- return trxResult ;
73- };
74-
75- try {
76- final String trxResultActual = clientSession .withTransaction (txnBody , txnOptions );
77- assertThat (trxResultActual ).isEqualTo (trxResult );
78- } catch (RuntimeException re ) {
79- throw new IllegalStateException (re .getMessage (), re );
80- } finally {
81- clientSession .close ();
82- mongoSyncClient .close ();
83- }
84- }
85-
8626 @ Test
8727 public void supportsMongoDB_4_4 () {
88- try (final MongoDBContainer mongoDBContainer = new MongoDBContainer (DockerImageName . parse ( "mongo:4.4" ) )) {
28+ try (final MongoDBContainer mongoDBContainer = new MongoDBContainer ("mongo:4.4" )) {
8929 mongoDBContainer .start ();
9030 }
9131 }
9232
9333 @ Test
9434 public void shouldTestDatabaseName () {
95- try (final MongoDBContainer mongoDBContainer = new MongoDBContainer (DockerImageName . parse ( "mongo:4.0.10" ) )) {
35+ try (final MongoDBContainer mongoDBContainer = new MongoDBContainer ("mongo:4.0.10" )) {
9636 mongoDBContainer .start ();
9737 final String databaseName = "my-db" ;
9838 assertThat (mongoDBContainer .getReplicaSetUrl (databaseName )).endsWith (databaseName );
9939 }
10040 }
101-
102- @ Test
103- public void shouldSupportSharding () {
104- try (final MongoDBContainer mongoDBContainer = new MongoDBContainer ("mongo:6" ).withSharding ()) {
105- mongoDBContainer .start ();
106- final MongoClient mongoClient = MongoClients .create (mongoDBContainer .getReplicaSetUrl ());
107-
108- mongoClient .getDatabase ("mydb1" ).getCollection ("foo" ).insertOne (new Document ("abc" , 0 ));
109-
110- Document shards = mongoClient .getDatabase ("config" ).getCollection ("shards" ).find ().first ();
111- assertThat (shards ).isNotNull ();
112- assertThat (shards ).isNotEmpty ();
113- assertThat (isReplicaSet (mongoClient )).isFalse ();
114- }
115- }
116-
117- private boolean isReplicaSet (MongoClient mongoClient ) {
118- return runIsMaster (mongoClient ).get ("setName" ) != null ;
119- }
120-
121- private Document runIsMaster (MongoClient mongoClient ) {
122- return mongoClient .getDatabase ("admin" ).runCommand (new Document ("ismaster" , 1 ));
123- }
12441}
0 commit comments