Merge branch 'master' into JAVA-13136
This commit is contained in:
+1
-1
@@ -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);
|
||||
|
||||
+14
-14
@@ -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
|
||||
|
||||
@@ -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"}
|
||||
@@ -0,0 +1,3 @@
|
||||
|
||||
### Relevant Articles:
|
||||
- [Using Dates in CRUD Operations in MongoDB](https://www.baeldung.com/mongodb-java-date-operations)
|
||||
|
||||
+8
@@ -31,6 +31,11 @@ public class CrudClientLiveTest {
|
||||
@Order(2)
|
||||
public void whenReadingEventsByDate_thenCheckForReturnedDocument() {
|
||||
Assertions.assertNotNull(CrudClient.readEventsByDate(CrudClient.dateQuery));
|
||||
Event event = CrudClient.readEventsByDate(CrudClient.dateQuery);
|
||||
Assertions.assertNotNull(event);
|
||||
Assertions.assertEquals("Soccer game", event.title);
|
||||
Assertions.assertEquals("Bar Avenue", event.location);
|
||||
Assertions.assertEquals(CrudClient.soccerGame.dateTime, event.dateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -39,6 +44,9 @@ public class CrudClientLiveTest {
|
||||
List<Event> events = CrudClient.readEventsByDateRange(CrudClient.from, CrudClient.to);
|
||||
Assertions.assertNotNull(events);
|
||||
Assertions.assertEquals(1, events.size());
|
||||
Assertions.assertEquals("Soccer game", events.get(0).title);
|
||||
Assertions.assertEquals("Bar Avenue", events.get(0).location);
|
||||
Assertions.assertEquals(CrudClient.soccerGame.dateTime, events.get(0).dateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -45,6 +45,7 @@
|
||||
<module>java-mongodb</module> <!-- long running -->
|
||||
<module>java-mongodb-2</module> <!-- long running -->
|
||||
<module>java-mongodb-3</module> <!-- long running -->
|
||||
<module>java-mongodb-queries</module> <!-- long running -->
|
||||
<module>jnosql</module> <!-- long running -->
|
||||
<module>jooq</module>
|
||||
<module>jpa-hibernate-cascade-type</module>
|
||||
|
||||
Reference in New Issue
Block a user