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