Updated article code (#12493)

This commit is contained in:
Avin Buricha 2022-07-15 00:08:20 +05:30 committed by GitHub
parent 3db189f71a
commit c252786fa9
3 changed files with 18 additions and 15 deletions

View File

@ -25,7 +25,7 @@ public class FindWithObjectId {
mongoClient = new MongoClient("localhost", 27017);
databaseName = "baeldung";
collectionName = "employee";
collectionName = "vehicle";
database = mongoClient.getDatabase(databaseName);
collection = database.getCollection(collectionName);

View File

@ -26,7 +26,7 @@ public class FindWithObjectIdLiveTest {
private static MongoClient mongoClient;
private static MongoDatabase database;
private static MongoCollection<Document> collection;
private static final String DATASET_JSON = "/employee.json";
private static final String DATASET_JSON = "/vehicle.json";
@BeforeClass
public static void setUp() throws IOException {
@ -34,7 +34,7 @@ public class FindWithObjectIdLiveTest {
mongoClient = new MongoClient("localhost", 27017);
database = mongoClient.getDatabase("baeldung");
collection = database.getCollection("employee");
collection = database.getCollection("vehicle");
collection.drop();
@ -47,10 +47,10 @@ public class FindWithObjectIdLiveTest {
}
@Test
public void givenEmployeeCollection_whenFetchingDocumentsUsingObjectId_thenCheckingForDocuments() {
Document employee = collection.find()
public void givenVehicleCollection_whenFetchingDocumentsUsingObjectId_thenCheckingForDocuments() {
Document vehicle = collection.find()
.first();
ObjectId objectId = (ObjectId) employee.get(OBJECT_ID_FIELD);
ObjectId objectId = (ObjectId) vehicle.get(OBJECT_ID_FIELD);
FindIterable<Document> documents = collection.find(eq(OBJECT_ID_FIELD, objectId));
MongoCursor<Document> cursor = documents.iterator();
@ -60,24 +60,24 @@ public class FindWithObjectIdLiveTest {
}
@Test
public void givenEmployeeCollection_whenFetchingFirstDocumentUsingObjectId_thenCheckingForDocument() {
Document employee = collection.find()
public void givenVehicleCollection_whenFetchingFirstDocumentUsingObjectId_thenCheckingForDocument() {
Document vehicle = collection.find()
.first();
ObjectId objectId = (ObjectId) employee.get(OBJECT_ID_FIELD);
ObjectId objectId = (ObjectId) vehicle.get(OBJECT_ID_FIELD);
Document queriedEmployee = collection.find(eq(OBJECT_ID_FIELD, objectId))
Document queriedVehicle = collection.find(eq(OBJECT_ID_FIELD, objectId))
.first();
assertNotNull(queriedEmployee);
assertEquals(employee.get(OBJECT_ID_FIELD), queriedEmployee.get(OBJECT_ID_FIELD));
assertNotNull(queriedVehicle);
assertEquals(vehicle.get(OBJECT_ID_FIELD), queriedVehicle.get(OBJECT_ID_FIELD));
}
@Test
public void givenEmployeeCollection_whenFetchingUsingRandomObjectId_thenCheckingForDocument() {
Document employee = collection.find(eq(OBJECT_ID_FIELD, new ObjectId()))
public void givenVehicleCollection_whenFetchingUsingRandomObjectId_thenCheckingForDocument() {
Document vehicle = collection.find(eq(OBJECT_ID_FIELD, new ObjectId()))
.first();
assertNull(employee);
assertNull(vehicle);
}
@AfterClass

View File

@ -0,0 +1,3 @@
{"companyName":"Skoda","modelName":"Octavia","launchYear":2016,"type":"Sports","registeredNo":"SKO 1134"}
{"companyName":"BMW","modelName":"X5","launchYear":2020,"type":"SUV","registeredNo":"BMW 3325"}
{"companyName":"Mercedes","modelName":"Maybach","launchYear":2021,"type":"Luxury","registeredNo":"MER 9754"}