BAEL-5360: Check Collection Existence in MongoDB (#11905)

* BAEL-5360: Check Collection Existence in MongoDB

* BAEL-5360: update test names using the bdd scheme
This commit is contained in:
Kapil Khandelwal 2022-03-09 02:19:19 +05:30 committed by GitHub
parent b086c384d4
commit c1751dc317
2 changed files with 198 additions and 0 deletions

View File

@ -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<String>())
.contains(testCollectionName);
System.out.println("collectionExists:- " + collectionExists);
}
public static void countSolution() {
MongoDatabase database = mongoClient.getDatabase(databaseName);
MongoCollection<Document> 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();
}
}

View File

@ -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<String>())
.contains(testCollectionName);
Boolean expectedStatus = true;
assertEquals(expectedStatus, collectionExists);
}
@Test
public void givenCount_whenCollectionAlreadyExists_thenCheckingForCollectionExistence() {
MongoDatabase database = mongoClient.getDatabase(databaseName);
MongoCollection<Document> collection = database.getCollection(testCollectionName);
Boolean collectionExists = collection.count() > 0 ? true : false;
Boolean expectedStatus = false;
assertEquals(expectedStatus, collectionExists);
}
}