22
33// Test system include
44import static org .junit .Assert .*;
5+
6+ import java .util .Map ;
7+
58import org .junit .*;
69
710import picoded .dstack .*;
811
912// MongoDB include
1013import com .mongodb .client .*;
1114import com .mongodb .client .model .*;
15+ import com .mongodb .client .result .*;
1216import com .mongodb .ServerApi ;
1317import com .mongodb .ServerApiVersion ;
1418import com .mongodb .MongoClientSettings ;
1519import com .mongodb .ConnectionString ;
1620import org .bson .Document ;
1721
1822/**
23+ * ## Purpose of test
24+ *
1925 * Minimal mongoDB connectivity test.
20- * Used to test assumptions used to build MongoDBStack implementation
26+ * Used to test assumptions used to build MongoDBStack implementation.
27+ *
28+ * We start with the basic CRUD, then expend accordingly to our use case.
29+ *
30+ * As much as possible we use native mongodb classes, and java base classes,
31+ * to avoid any unintended side effect from our library code. As such there is lots
32+ * of code reptition, to ensure clarity of functionality.
2133 */
2234public class MongoDB_conn_test {
2335
2436 @ Test
2537 public void connectivityTest () {
38+
39+ //-------------------------------------
40+ // Connectivity Setup
41+ //-------------------------------------
42+
2643 // Get the full_url
27- String full_url = "mongodb://" +DStackTestConfig .MONGODB_HOST ()+":" +DStackTestConfig .MONGODB_PORT ()+"/testdb" ;
44+ String full_url = "mongodb://" +DStackTestConfig .MONGODB_HOST ()+":" +DStackTestConfig .MONGODB_PORT ()+"/testdb" + "?r=majority&w=majority&retryWrites=true&maxPoolSize=50" ;
2845
2946 // Lets build using the stable API settings
3047 ServerApi serverApi = ServerApi .builder ().version (ServerApiVersion .V1 ).build ();
@@ -43,18 +60,115 @@ public void connectivityTest() {
4360 MongoCollection <Document > collection = database .getCollection ("testcollection" );
4461 assertNotNull (collection );
4562
46- // Configure the UPSERT option we plan to use
47- FindOneAndUpdateOptions upsertOpt = new FindOneAndUpdateOptions ();
48- upsertOpt .upsert (true );
63+ // Unique _oid index setup
64+ IndexOptions opt = new IndexOptions ();
65+ opt = opt .unique (true );
66+ opt = opt .name ("_oid" );
67+ collection .createIndex (Indexes .ascending ("_oid" ), opt );
68+
69+ //-------------------------------------
70+ // Data cleanup
71+ //-------------------------------------
72+
73+ // Remove any stale data from previous test
74+
75+ // Delete all items
76+ //
77+ // Due to the lack of an all * wildcard
78+ // we are using a exists OR condition, which is true
79+ // for all objects
80+ collection .deleteMany ( //
81+ Filters .or ( //
82+ Filters .exists ("_oid" , true ), //
83+ Filters .exists ("_oid" , false ) //
84+ ) //
85+ ); //
4986
50- // Generate the document to "insert"
87+ //-------------------------------------
88+ // C : Create the first document
89+ //-------------------------------------
90+
91+ // Generate the document to first "insert"
5192 Document doc = new Document ();
52- doc .put ("_oid" , "123" );
5393 doc .put ("hello" , "world" );
94+ doc .put ("_oid" , "001" );
95+
96+ // Lets insert the first document
97+ collection .insertOne (doc );
98+
99+ //-------------------------------------
100+ // R : Read the first document
101+ //-------------------------------------
102+
103+ // Get the find result
104+ FindIterable <Document > findRes = collection .find (Filters .eq ("_oid" , "001" ));
105+ assertNotNull ( findRes );
106+
107+ // Export the data without native mongodb '_id'
108+ findRes = findRes .projection (Projections .excludeId ());
109+
110+ // Get the document (as a map???)
111+ // in theory its possible, because a Document, is a Map
112+ Map <String ,Object > resMap = findRes .first ();
113+ assertNotNull ( resMap );
114+
115+ // Lets validate the values
116+ assertEquals ("001" , resMap .get ("_oid" ));
117+ assertEquals ("world" , resMap .get ("hello" ));
118+ // Ensure the native mongodb '_id' is scrubbed out
119+ assertEquals (null , resMap .get ("_id" ));
120+
121+ //-------------------------------------
122+ // U : Update the first document
123+ //-------------------------------------
124+
125+ // Generate the details, that needs updating
126+ //
127+ // See: https://www.mongodb.com/docs/manual/reference/operator/update/set/
128+ // for how to use the $set operator
129+ doc = new Document ();
130+ doc .append ("messsage" , "the world is both big and small" );
131+ // doc.append("_oid", "001");
132+
133+ Document updateDoc = new Document ();
134+ updateDoc .put ("$set" , doc );
135+
136+ // Lets do a find and update
137+ collection .findOneAndUpdate (Filters .eq ("_oid" ,"001" ), updateDoc );
138+
139+ // Lets validate that the changes were made
140+ //----------------------------------------------
141+
142+ // Get the find result
143+ findRes = collection .find (Filters .eq ("_oid" , "001" ));
144+ assertNotNull ( findRes );
145+
146+ // Export the data without native mongodb '_id'
147+ findRes = findRes .projection (Projections .excludeId ());
148+
149+ // Get the document (as a map?)
150+ resMap = findRes .first ();
151+ assertNotNull ( resMap );
152+
153+ // Lets validate the values
154+ assertEquals ("001" , resMap .get ("_oid" ));
155+ assertEquals ("world" , resMap .get ("hello" ));
156+ assertEquals (null , resMap .get ("_id" ));
157+ assertEquals ("the world is both big and small" , resMap .get ("messsage" ));
158+
159+
160+ //-------------------------------------
161+ // D : Delete the document !
162+ //-------------------------------------
54163
55- // Lets perform the upsert
56- collection .findOneAndUpdate (Filters .eq ("_oid" , "123" ), doc , upsertOpt );
164+ // Delete
165+ DeleteResult delRes = collection .deleteOne (Filters .eq ("_oid" , "001" ));
166+ assertEquals (1 , delRes .getDeletedCount ());
57167
168+ // And validate if it exists
169+ findRes = collection .find (Filters .eq ("_oid" , "001" ));
170+ findRes = findRes .projection (Projections .excludeId ());
171+ assertNull ( findRes .first () );
58172
59173 }
60174
0 commit comments