From c1751dc317e3a363611c5a9306a7521cc9b1ad2a Mon Sep 17 00:00:00 2001 From: Kapil Khandelwal Date: Wed, 9 Mar 2022 02:19:19 +0530 Subject: [PATCH] BAEL-5360: Check Collection Existence in MongoDB (#11905) * BAEL-5360: Check Collection Existence in MongoDB * BAEL-5360: update test names using the bdd scheme --- .../baeldung/mongo/CollectionExistence.java | 100 ++++++++++++++++++ .../mongo/CollectionExistenceLiveTest.java | 98 +++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/CollectionExistence.java create mode 100644 persistence-modules/java-mongodb/src/test/java/com/baeldung/mongo/CollectionExistenceLiveTest.java diff --git a/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/CollectionExistence.java b/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/CollectionExistence.java new file mode 100644 index 0000000000..074913af4e --- /dev/null +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/CollectionExistence.java @@ -0,0 +1,100 @@ +package com.baeldung.mongo; + +import java.util.ArrayList; + +import org.bson.Document; + +import com.mongodb.DB; +import com.mongodb.MongoClient; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; + +public class CollectionExistence { + + private static MongoClient mongoClient; + + private static String testCollectionName; + private static String databaseName; + + public static void setUp() { + if (mongoClient == null) { + mongoClient = new MongoClient("localhost", 27017); + } + databaseName = "baeldung"; + testCollectionName = "student"; + } + + public static void collectionExistsSolution() { + + DB db = mongoClient.getDB(databaseName); + + System.out.println("collectionName " + testCollectionName + db.collectionExists(testCollectionName)); + + } + + public static void createCollectionSolution() { + + MongoDatabase database = mongoClient.getDatabase(databaseName); + + try { + database.createCollection(testCollectionName); + + } catch (Exception exception) { + System.err.println("Collection already Exists"); + } + + } + + public static void listCollectionNamesSolution() { + + MongoDatabase database = mongoClient.getDatabase(databaseName); + boolean collectionExists = database.listCollectionNames() + .into(new ArrayList()) + .contains(testCollectionName); + + System.out.println("collectionExists:- " + collectionExists); + + } + + public static void countSolution() { + + MongoDatabase database = mongoClient.getDatabase(databaseName); + + MongoCollection collection = database.getCollection(testCollectionName); + + System.out.println(collection.count()); + + } + + public static void main(String args[]) { + + // + // Connect to cluster (default is localhost:27017) + // + setUp(); + + // + // Check the db existence using DB class's method + // + collectionExistsSolution(); + + // + // Check the db existence using the createCollection method of MongoDatabase class + // + createCollectionSolution(); + + // + // Check the db existence using the listCollectionNames method of MongoDatabase class + // + listCollectionNamesSolution(); + + // + // Check the db existence using the count method of MongoDatabase class + // + countSolution(); + + } + +} + + diff --git a/persistence-modules/java-mongodb/src/test/java/com/baeldung/mongo/CollectionExistenceLiveTest.java b/persistence-modules/java-mongodb/src/test/java/com/baeldung/mongo/CollectionExistenceLiveTest.java new file mode 100644 index 0000000000..ad839d1219 --- /dev/null +++ b/persistence-modules/java-mongodb/src/test/java/com/baeldung/mongo/CollectionExistenceLiveTest.java @@ -0,0 +1,98 @@ +package com.baeldung.mongo; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; + +import org.bson.Document; +import org.junit.Before; +import org.junit.Test; + +import com.mongodb.DB; +import com.mongodb.MongoClient; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; + +public class CollectionExistenceLiveTest { + + private MongoClient mongoClient; + private String testCollectionName; + private String databaseName; + + @Before + public void setup() { + if (mongoClient == null) { + mongoClient = new MongoClient("localhost", 27017); + databaseName = "baeldung"; + testCollectionName = "student"; + + // Create a new collection if it doesn't exists. + try { + MongoDatabase database = mongoClient.getDatabase(databaseName); + database.createCollection(testCollectionName); + + } catch (Exception exception) { + + System.out.println("Collection already Exists"); + } + + } + } + + @Test + public void givenCreateCollection_whenCollectionAlreadyExists_thenCheckingForCollectionExistence() { + + MongoDatabase database = mongoClient.getDatabase(databaseName); + Boolean collectionStatus = false; + Boolean expectedStatus = true; + + try { + database.createCollection(testCollectionName); + + } catch (Exception exception) { + collectionStatus = true; + System.err.println("Collection already Exists"); + } + + assertEquals(expectedStatus, collectionStatus); + + } + + @Test + public void givenCollectionExists_whenCollectionAlreadyExists_thenCheckingForCollectionExistence() { + + DB db = mongoClient.getDB(databaseName); + Boolean collectionStatus = db.collectionExists(testCollectionName); + + Boolean expectedStatus = true; + assertEquals(expectedStatus, collectionStatus); + + } + + @Test + public void givenListCollectionNames_whenCollectionAlreadyExists_thenCheckingForCollectionExistence() { + + MongoDatabase database = mongoClient.getDatabase(databaseName); + boolean collectionExists = database.listCollectionNames() + .into(new ArrayList()) + .contains(testCollectionName); + + Boolean expectedStatus = true; + assertEquals(expectedStatus, collectionExists); + + } + + @Test + public void givenCount_whenCollectionAlreadyExists_thenCheckingForCollectionExistence() { + + MongoDatabase database = mongoClient.getDatabase(databaseName); + + MongoCollection collection = database.getCollection(testCollectionName); + Boolean collectionExists = collection.count() > 0 ? true : false; + + Boolean expectedStatus = false; + assertEquals(expectedStatus, collectionExists); + + } +} +