1+ import com.mongodb.ConnectionString
2+ import com.mongodb.MongoClientSettings
3+ import com.mongodb.MongoException
4+ import com.mongodb.connection.ClusterConnectionMode
5+ import com.mongodb.kotlin.client.coroutine.MongoClient
6+ import config.getConfig
7+ import kotlinx.coroutines.runBlocking
8+ import org.bson.BsonInt64
9+ import org.bson.Document
10+ import org.junit.jupiter.api.AfterAll
11+ import org.junit.jupiter.api.Assertions
12+ import org.junit.jupiter.api.Test
13+ import java.util.concurrent.TimeUnit
14+ import kotlin.test.Ignore
15+
16+
17+ // :replace-start: {
18+ // "terms": {
19+ // "uri": "\"<your connection string>\"",
20+ // "uriString": "\"mongodb+srv:/<username>:<password>@<hostname>:<port>?connectTimeoutMS(2000)\"",
21+ // "uriAcmeString": "\"mongodb+srv://host1.acme.com\""
22+ // }
23+ // }
24+
25+ /* NOTE: These tests are not run by default because they require a MongoDB deployment
26+ with a username and password. To run these tests locally, you need to set up your .env
27+ and replace the @Ignore annotation with @Test on the tests you want to run.
28+ */
29+ class MongoClientSettingsTest {
30+
31+ companion object {
32+ private val config = getConfig()
33+ val CONNECTION_URI_PLACEHOLDER = config.connectionUri
34+ var higherScopedClient: MongoClient ? = null
35+
36+ @AfterAll
37+ @JvmStatic
38+ fun afterAll () {
39+ runBlocking {
40+ higherScopedClient?.close()
41+ }
42+ }
43+ }
44+
45+ @Ignore
46+ fun exampleConnectionString () = runBlocking {
47+ val uri = CONNECTION_URI_PLACEHOLDER
48+ // :snippet-start: example-connection-string
49+ val mongoClient = MongoClient .create(
50+ MongoClientSettings .builder()
51+ .applyConnectionString(ConnectionString (uri))
52+ .build()
53+ )
54+ // :snippet-end:
55+ lateinit var higherScopedCommandResult: Document
56+ val database = mongoClient.getDatabase(" admin" )
57+ try {
58+ // Send a ping to confirm a successful connection
59+ val command = Document (" ping" , BsonInt64 (1 ))
60+ val commandResult = database.runCommand(command)
61+ println (" Pinged your deployment. You successfully connected to MongoDB!" )
62+ higherScopedCommandResult = commandResult
63+ } catch (me: MongoException ) {
64+ System .err.println (me)
65+ }
66+ higherScopedClient = mongoClient
67+ Assertions .assertEquals(1.0 , higherScopedCommandResult[" ok" ].toString().toDouble())
68+ }
69+
70+ @Ignore
71+ fun chainOrderConnectionString () = runBlocking {
72+ val uriString = CONNECTION_URI_PLACEHOLDER
73+ // :snippet-start: chain-order-connection-string
74+ val mongoClient = MongoClient .create(
75+ MongoClientSettings .builder()
76+ .applyConnectionString(ConnectionString (uriString))
77+ .applyToSocketSettings{ builder ->
78+ builder.connectTimeout(5 , TimeUnit .SECONDS )
79+ }
80+ .build()
81+ )
82+ // :snippet-end:
83+ lateinit var higherScopedCommandResult: Document
84+ val database = mongoClient.getDatabase(" admin" )
85+ try {
86+ // Send a ping to confirm a successful connection
87+ val command = Document (" ping" , BsonInt64 (1 ))
88+ val commandResult = database.runCommand(command)
89+ println (" Pinged your deployment. You successfully connected to MongoDB!" )
90+ higherScopedCommandResult = commandResult
91+ } catch (me: MongoException ) {
92+ System .err.println (me)
93+ }
94+ higherScopedClient = mongoClient
95+ Assertions .assertEquals(1.0 , higherScopedCommandResult[" ok" ].toString().toDouble())
96+ }
97+
98+ @Ignore
99+ fun srvHostString () = runBlocking {
100+ val uriAcmeString = CONNECTION_URI_PLACEHOLDER
101+ // :snippet-start: srv-host-connection-string
102+ val mongoClient = MongoClient .create(
103+ MongoClientSettings .builder()
104+ .applyConnectionString(ConnectionString (uriAcmeString))
105+ .build()
106+ )
107+ // :snippet-end:
108+ lateinit var higherScopedCommandResult: Document
109+ val database = mongoClient.getDatabase(" admin" )
110+ try {
111+ // Send a ping to confirm a successful connection
112+ val command = Document (" ping" , BsonInt64 (1 ))
113+ val commandResult = database.runCommand(command)
114+ println (" Pinged your deployment. You successfully connected to MongoDB!" )
115+ higherScopedCommandResult = commandResult
116+ } catch (me: MongoException ) {
117+ System .err.println (me)
118+ }
119+ higherScopedClient = mongoClient
120+ Assertions .assertEquals(1.0 , higherScopedCommandResult[" ok" ].toString().toDouble())
121+ }
122+
123+ @Ignore
124+ fun exampleClusterSettingsString () = runBlocking {
125+ // :snippet-start: cluster-settings-connection-string
126+ val mongoClient = MongoClient .create(
127+ MongoClientSettings .builder()
128+ .applyToClusterSettings{ builder ->
129+ builder.mode(ClusterConnectionMode .SINGLE )
130+ }
131+ .build()
132+ )
133+ // :snippet-end:
134+ lateinit var higherScopedCommandResult: Document
135+ val database = mongoClient.getDatabase(" admin" )
136+ try {
137+ // Send a ping to confirm a successful connection
138+ val command = Document (" ping" , BsonInt64 (1 ))
139+ val commandResult = database.runCommand(command)
140+ println (" Pinged your deployment. You successfully connected to MongoDB!" )
141+ higherScopedCommandResult = commandResult
142+ } catch (me: MongoException ) {
143+ System .err.println (me)
144+ }
145+ higherScopedClient = mongoClient
146+ Assertions .assertEquals(1.0 , higherScopedCommandResult[" ok" ].toString().toDouble())
147+ }
148+
149+ @Ignore
150+ fun exampleSocketSettingsString () = runBlocking {
151+ val uri = CONNECTION_URI_PLACEHOLDER
152+ // :snippet-start: socket-settings-connection-string
153+ val mongoClient = MongoClient .create(
154+ MongoClientSettings .builder()
155+ .applyConnectionString(ConnectionString (uri))
156+ .applyToSocketSettings{ builder ->
157+ builder
158+ .connectTimeout(10 , TimeUnit .SECONDS )
159+ .readTimeout(15 , TimeUnit .SECONDS )
160+ }
161+ .build()
162+ )
163+ // :snippet-end:
164+ lateinit var higherScopedCommandResult: Document
165+ val database = mongoClient.getDatabase(" admin" )
166+ try {
167+ // Send a ping to confirm a successful connection
168+ val command = Document (" ping" , BsonInt64 (1 ))
169+ val commandResult = database.runCommand(command)
170+ println (" Pinged your deployment. You successfully connected to MongoDB!" )
171+ higherScopedCommandResult = commandResult
172+ } catch (me: MongoException ) {
173+ System .err.println (me)
174+ }
175+ higherScopedClient = mongoClient
176+ Assertions .assertEquals(1.0 , higherScopedCommandResult[" ok" ].toString().toDouble())
177+ }
178+
179+ @Ignore
180+ fun connectionPoolSettingsString () = runBlocking {
181+ val uri = CONNECTION_URI_PLACEHOLDER
182+ // :snippet-start: connection-pool-connection-string
183+ val mongoClient = MongoClient .create(
184+ MongoClientSettings .builder()
185+ .applyConnectionString(ConnectionString (uri))
186+ .applyToConnectionPoolSettings{ builder ->
187+ builder
188+ .maxWaitTime(10 , TimeUnit .SECONDS )
189+ .maxSize(200 )
190+ }
191+ .build()
192+ )
193+ // :snippet-end:
194+ lateinit var higherScopedCommandResult: Document
195+ val database = mongoClient.getDatabase(" admin" )
196+ try {
197+ // Send a ping to confirm a successful connection
198+ val command = Document (" ping" , BsonInt64 (1 ))
199+ val commandResult = database.runCommand(command)
200+ println (" Pinged your deployment. You successfully connected to MongoDB!" )
201+ higherScopedCommandResult = commandResult
202+ } catch (me: MongoException ) {
203+ System .err.println (me)
204+ }
205+ higherScopedClient = mongoClient
206+ Assertions .assertEquals(1.0 , higherScopedCommandResult[" ok" ].toString().toDouble())
207+ }
208+
209+ @Ignore
210+ fun serverSettingsString () = runBlocking {
211+ val uri = CONNECTION_URI_PLACEHOLDER
212+ // :snippet-start: server-settings-connection-string
213+ val mongoClient = MongoClient .create(
214+ MongoClientSettings .builder()
215+ .applyConnectionString(ConnectionString (uri))
216+ .applyToServerSettings{ builder ->
217+ builder
218+ .minHeartbeatFrequency(700 , TimeUnit .MILLISECONDS )
219+ .heartbeatFrequency(15 , TimeUnit .SECONDS )
220+ }
221+ .build()
222+ )
223+ // :snippet-end:
224+ lateinit var higherScopedCommandResult: Document
225+ val database = mongoClient.getDatabase(" admin" )
226+ try {
227+ // Send a ping to confirm a successful connection
228+ val command = Document (" ping" , BsonInt64 (1 ))
229+ val commandResult = database.runCommand(command)
230+ println (" Pinged your deployment. You successfully connected to MongoDB!" )
231+ higherScopedCommandResult = commandResult
232+ } catch (me: MongoException ) {
233+ System .err.println (me)
234+ }
235+ higherScopedClient = mongoClient
236+ Assertions .assertEquals(1.0 , higherScopedCommandResult[" ok" ].toString().toDouble())
237+ }
238+
239+ @Ignore
240+ fun tlsSslString () = runBlocking {
241+ val uri = CONNECTION_URI_PLACEHOLDER
242+ // :snippet-start: tls-ssl-connection-string
243+ val mongoClient = MongoClient .create(
244+ MongoClientSettings .builder()
245+ .applyConnectionString(ConnectionString (uri))
246+ .applyToSslSettings{ builder ->
247+ builder.enabled(true )
248+ }
249+ .build()
250+ )
251+ // :snippet-end:
252+ lateinit var higherScopedCommandResult: Document
253+ val database = mongoClient.getDatabase(" admin" )
254+ try {
255+ // Send a ping to confirm a successful connection
256+ val command = Document (" ping" , BsonInt64 (1 ))
257+ val commandResult = database.runCommand(command)
258+ println (" Pinged your deployment. You successfully connected to MongoDB!" )
259+ higherScopedCommandResult = commandResult
260+ } catch (me: MongoException ) {
261+ System .err.println (me)
262+ }
263+ higherScopedClient = mongoClient
264+ Assertions .assertEquals(1.0 , higherScopedCommandResult[" ok" ].toString().toDouble())
265+ }
266+
267+ }
268+
269+ // :replace-end:
0 commit comments