|
| 1 | +package com.baeldung.mongo.find; |
| 2 | + |
| 3 | +import com.mongodb.MongoClient; |
| 4 | +import com.mongodb.client.FindIterable; |
| 5 | +import com.mongodb.client.MongoCollection; |
| 6 | +import com.mongodb.client.MongoCursor; |
| 7 | +import com.mongodb.client.MongoDatabase; |
| 8 | +import org.bson.Document; |
| 9 | +import org.bson.types.ObjectId; |
| 10 | +import org.junit.AfterClass; |
| 11 | +import org.junit.BeforeClass; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import java.io.BufferedReader; |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStream; |
| 17 | +import java.io.InputStreamReader; |
| 18 | + |
| 19 | +import static com.mongodb.client.model.Filters.eq; |
| 20 | +import static org.junit.Assert.*; |
| 21 | + |
| 22 | +public class FindWithObjectIdLiveTest { |
| 23 | + |
| 24 | + private static final String OBJECT_ID_FIELD = "_id"; |
| 25 | + |
| 26 | + private static MongoClient mongoClient; |
| 27 | + private static MongoDatabase database; |
| 28 | + private static MongoCollection<Document> collection; |
| 29 | + private static final String DATASET_JSON = "/employee.json"; |
| 30 | + |
| 31 | + @BeforeClass |
| 32 | + public static void setUp() throws IOException { |
| 33 | + if (mongoClient == null) { |
| 34 | + mongoClient = new MongoClient("localhost", 27017); |
| 35 | + |
| 36 | + database = mongoClient.getDatabase("baeldung"); |
| 37 | + collection = database.getCollection("employee"); |
| 38 | + |
| 39 | + collection.drop(); |
| 40 | + |
| 41 | + InputStream is = FindOperationLiveTest.class.getResourceAsStream(DATASET_JSON); |
| 42 | + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
| 43 | + reader.lines() |
| 44 | + .forEach(line -> collection.insertOne(Document.parse(line))); |
| 45 | + reader.close(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void givenEmployeeCollection_whenFetchingDocumentsUsingObjectId_thenCheckingForDocuments() { |
| 51 | + Document employee = collection.find() |
| 52 | + .first(); |
| 53 | + ObjectId objectId = (ObjectId) employee.get(OBJECT_ID_FIELD); |
| 54 | + |
| 55 | + FindIterable<Document> documents = collection.find(eq(OBJECT_ID_FIELD, objectId)); |
| 56 | + MongoCursor<Document> cursor = documents.iterator(); |
| 57 | + |
| 58 | + assertNotNull(cursor); |
| 59 | + assertTrue(cursor.hasNext()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void givenEmployeeCollection_whenFetchingFirstDocumentUsingObjectId_thenCheckingForDocument() { |
| 64 | + Document employee = collection.find() |
| 65 | + .first(); |
| 66 | + ObjectId objectId = (ObjectId) employee.get(OBJECT_ID_FIELD); |
| 67 | + |
| 68 | + Document queriedEmployee = collection.find(eq(OBJECT_ID_FIELD, objectId)) |
| 69 | + .first(); |
| 70 | + |
| 71 | + assertNotNull(queriedEmployee); |
| 72 | + assertEquals(employee.get(OBJECT_ID_FIELD), queriedEmployee.get(OBJECT_ID_FIELD)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void givenEmployeeCollection_whenFetchingUsingRandomObjectId_thenCheckingForDocument() { |
| 77 | + Document employee = collection.find(eq(OBJECT_ID_FIELD, new ObjectId())) |
| 78 | + .first(); |
| 79 | + |
| 80 | + assertNull(employee); |
| 81 | + } |
| 82 | + |
| 83 | + @AfterClass |
| 84 | + public static void cleanUp() { |
| 85 | + mongoClient.close(); |
| 86 | + } |
| 87 | +} |
0 commit comments