1+ import com.mongodb.MongoNamespace
2+ import com.mongodb.client.model.Filters
3+ import com.mongodb.client.model.bulk.ClientBulkWriteOptions
4+ import com.mongodb.client.model.bulk.ClientBulkWriteResult
5+ import com.mongodb.client.model.bulk.ClientNamespacedWriteModel
6+ import com.mongodb.kotlin.client.coroutine.MongoClient
7+ import config.getConfig
8+ import kotlinx.coroutines.runBlocking
9+ import org.bson.codecs.pojo.annotations.BsonId
10+ import org.junit.jupiter.api.AfterAll
11+ import org.junit.jupiter.api.Assertions.assertEquals
12+ import org.junit.jupiter.api.BeforeAll
13+ import org.junit.jupiter.api.TestInstance
14+ import kotlin.test.Ignore
15+
16+ @TestInstance(TestInstance .Lifecycle .PER_CLASS )
17+ internal class ClientBulkTest {
18+
19+ // :snippet-start: data-classes
20+ data class Person (
21+ @BsonId val id : Int ,
22+ val name : String ,
23+ )
24+
25+ data class Object (
26+ @BsonId val id : Int ,
27+ val type : String ,
28+ )
29+ // :snippet-end:
30+
31+
32+ companion object {
33+ val config = getConfig()
34+ val client = MongoClient .create(config.connectionUri)
35+ val database = client.getDatabase(" sample_db" )
36+ val personCollection = database.getCollection<Person >(" people" )
37+ val objectCollection = database.getCollection<Object >(" objects" )
38+
39+ @BeforeAll
40+ @JvmStatic
41+ fun beforeAll () {
42+ runBlocking {
43+ personCollection.insertOne(Person (1 , " Sandy King" ))
44+ objectCollection.insertOne(Object (1 , " artist easel" ))
45+ }
46+ }
47+
48+ @AfterAll
49+ @JvmStatic
50+ fun afterAll () {
51+ runBlocking {
52+ personCollection.drop()
53+ objectCollection.drop()
54+ client.close()
55+ }
56+ }
57+ }
58+
59+ // Ignoring tests because successful completion of
60+ // writes is blocked on https://jira.mongodb.org/browse/CLOUDP-288992
61+ @Ignore
62+ fun insertOperationTest () = runBlocking {
63+ // :snippet-start: insert-models
64+ val docsToInsert = mutableListOf<ClientNamespacedWriteModel >()
65+
66+ docsToInsert.add(ClientNamespacedWriteModel
67+ .insertOne(
68+ MongoNamespace (" sample_db" , " people" ),
69+ Person (2 , " Julia Smith" )
70+ )
71+ )
72+
73+ docsToInsert.add(ClientNamespacedWriteModel
74+ .insertOne(
75+ MongoNamespace (" sample_db" , " objects" ),
76+ Object (2 , " washing machine" )
77+ )
78+ )
79+
80+ val clientBulkResult = client
81+ .bulkWrite(docsToInsert)
82+ // :snippet-end:
83+
84+ // Junit test for the above code
85+ assertEquals(2 , objectCollection.countDocuments())
86+ assertEquals(2 , personCollection.countDocuments())
87+ }
88+
89+ @Ignore
90+ fun replaceOperationTest () = runBlocking {
91+ // :snippet-start: replace-models
92+ val docsReplacements = mutableListOf<ClientNamespacedWriteModel >()
93+
94+ docsReplacements.add(ClientNamespacedWriteModel
95+ .replaceOne(
96+ MongoNamespace (" sample_restaurants" , " restaurants" ),
97+ Filters .eq(Person ::id.name, 1 ),
98+ Person (1 , " Frederic Hilbert" )
99+ )
100+ )
101+
102+ docsReplacements.add(ClientNamespacedWriteModel
103+ .replaceOne(
104+ MongoNamespace (" sample_mflix" , " movies" ),
105+ Filters .eq(Object ::id.name, 1 ),
106+ Object (1 , " ironing board" )
107+ )
108+ )
109+
110+ val clientBulkResult = client
111+ .bulkWrite(docsReplacements)
112+ // :snippet-end:
113+
114+ // Junit test for the above code
115+ assertEquals(1 , objectCollection.countDocuments())
116+ }
117+
118+ @Ignore
119+ fun orderOfOperationsTest () = runBlocking {
120+ // :snippet-start: options
121+ val namespace = MongoNamespace (" sample_db" , " people" )
122+
123+ val options = ClientBulkWriteOptions
124+ .clientBulkWriteOptions()
125+ .ordered(false )
126+
127+ val bulkOperations = listOf (
128+ ClientNamespacedWriteModel .insertOne(
129+ namespace,
130+ Person (2 , " Rudra Suraj" )
131+ ),
132+ // Causes duplicate key error
133+ ClientNamespacedWriteModel .insertOne(
134+ namespace,
135+ Person (2 , " Wendy Zhang" )
136+ ),
137+ ClientNamespacedWriteModel .insertOne(
138+ namespace,
139+ Person (4 , " Mario Bianchi" )
140+ )
141+ )
142+
143+ val result: ClientBulkWriteResult = client.bulkWrite(bulkOperations, options)
144+ // :snippet-end:
145+
146+ // Junit test for the above code
147+ assertEquals(3 , personCollection.countDocuments())
148+ }
149+ }
0 commit comments