@@ -35,14 +35,16 @@ public class MongoDB_conn_test {
3535
3636 @ Test
3737 public void connectivityTest () {
38-
38+
3939 //-------------------------------------
4040 // Connectivity Setup
4141 //-------------------------------------
42-
42+
4343 // Get the full_url
44- String full_url = "mongodb://" +DStackTestConfig .MONGODB_HOST ()+":" +DStackTestConfig .MONGODB_PORT ()+"/testdb" +"?r=majority&w=majority&retryWrites=true&maxPoolSize=50" ;
45-
44+ String full_url = "mongodb://" + DStackTestConfig .MONGODB_HOST () + ":"
45+ + DStackTestConfig .MONGODB_PORT () + "/testdb"
46+ + "?r=majority&w=majority&retryWrites=true&maxPoolSize=50" ;
47+
4648 // Lets build using the stable API settings
4749 ServerApi serverApi = ServerApi .builder ().version (ServerApiVersion .V1 ).build ();
4850 MongoClientSettings settings = MongoClientSettings .builder ()
@@ -51,15 +53,15 @@ public void connectivityTest() {
5153 // The mongodb client
5254 MongoClient client = MongoClients .create (settings );
5355 assertNotNull (client );
54-
56+
5557 // The mongodb database
5658 MongoDatabase database = client .getDatabase ("testdb" );
5759 assertNotNull (database );
58-
60+
5961 // The mongodb collection
6062 MongoCollection <Document > collection = database .getCollection ("testcollection" );
6163 assertNotNull (collection );
62-
64+
6365 // Unique _oid index setup
6466 IndexOptions opt = new IndexOptions ();
6567 opt = opt .unique (true );
@@ -69,7 +71,7 @@ public void connectivityTest() {
6971 //-------------------------------------
7072 // Data cleanup
7173 //-------------------------------------
72-
74+
7375 // Remove any stale data from previous test
7476
7577 // Delete all items
@@ -83,92 +85,92 @@ public void connectivityTest() {
8385 Filters .exists ("_oid" , false ) //
8486 ) //
8587 ); //
86-
88+
8789 //-------------------------------------
8890 // C : Create the first document
8991 //-------------------------------------
90-
92+
9193 // Generate the document to first "insert"
9294 Document doc = new Document ();
9395 doc .put ("hello" , "world" );
9496 doc .put ("_oid" , "001" );
95-
97+
9698 // Lets insert the first document
9799 collection .insertOne (doc );
98-
100+
99101 //-------------------------------------
100102 // R : Read the first document
101103 //-------------------------------------
102-
104+
103105 // Get the find result
104106 FindIterable <Document > findRes = collection .find (Filters .eq ("_oid" , "001" ));
105- assertNotNull ( findRes );
107+ assertNotNull (findRes );
106108
107109 // Export the data without native mongodb '_id'
108110 findRes = findRes .projection (Projections .excludeId ());
109111
110112 // Get the document (as a map???)
111113 // in theory its possible, because a Document, is a Map
112- Map <String ,Object > resMap = findRes .first ();
113- assertNotNull ( resMap );
114-
114+ Map <String , Object > resMap = findRes .first ();
115+ assertNotNull (resMap );
116+
115117 // Lets validate the values
116118 assertEquals ("001" , resMap .get ("_oid" ));
117119 assertEquals ("world" , resMap .get ("hello" ));
118120 // Ensure the native mongodb '_id' is scrubbed out
119121 assertEquals (null , resMap .get ("_id" ));
120-
122+
121123 //-------------------------------------
122124 // U : Update the first document
123125 //-------------------------------------
124-
126+
125127 // Generate the details, that needs updating
126128 //
127129 // See: https://www.mongodb.com/docs/manual/reference/operator/update/set/
128130 // for how to use the $set operator
129131 doc = new Document ();
130132 doc .append ("messsage" , "the world is both big and small" );
131133 // doc.append("_oid", "001");
132-
134+
133135 Document updateDoc = new Document ();
134136 updateDoc .put ("$set" , doc );
135137
136138 // Lets do a find and update
137- collection .findOneAndUpdate (Filters .eq ("_oid" ,"001" ), updateDoc );
138-
139+ collection .findOneAndUpdate (Filters .eq ("_oid" , "001" ), updateDoc );
140+
139141 // Lets validate that the changes were made
140142 //----------------------------------------------
141-
143+
142144 // Get the find result
143145 findRes = collection .find (Filters .eq ("_oid" , "001" ));
144- assertNotNull ( findRes );
146+ assertNotNull (findRes );
145147
146148 // Export the data without native mongodb '_id'
147149 findRes = findRes .projection (Projections .excludeId ());
148150
149151 // Get the document (as a map?)
150152 resMap = findRes .first ();
151- assertNotNull ( resMap );
152-
153+ assertNotNull (resMap );
154+
153155 // Lets validate the values
154156 assertEquals ("001" , resMap .get ("_oid" ));
155157 assertEquals ("world" , resMap .get ("hello" ));
156158 assertEquals (null , resMap .get ("_id" ));
157159 assertEquals ("the world is both big and small" , resMap .get ("messsage" ));
158-
160+
159161 //-------------------------------------
160162 // D : Delete the document !
161163 //-------------------------------------
162-
164+
163165 // Delete
164166 DeleteResult delRes = collection .deleteOne (Filters .eq ("_oid" , "001" ));
165167 assertEquals (1 , delRes .getDeletedCount ());
166-
168+
167169 // And validate if it exists
168170 findRes = collection .find (Filters .eq ("_oid" , "001" ));
169171 findRes = findRes .projection (Projections .excludeId ());
170- assertNull ( findRes .first () );
171-
172+ assertNull (findRes .first ());
173+
172174 }
173-
175+
174176}
0 commit comments