BAEL-5363 Mongodb crud by date (#12293)

Created new mongo module, model, mongo client and unit test

Co-authored-by: Christian Jaimes <christian.jaimes@oracle>
Co-authored-by: bipster <openbip@gmail.com>
This commit is contained in:
chrisjaimes 2022-07-06 23:57:53 -04:00 committed by GitHub
parent b14f9aabac
commit b094ba8098
6 changed files with 336 additions and 1 deletions

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>java-mongodb-queries</artifactId>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,183 @@
package com.baeldung.mongo.crud;
import com.baeldung.mongo.crud.model.Event;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.MongoException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.InsertOneResult;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.codecs.configuration.CodecProvider;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;
import org.bson.conversions.Bson;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry;
import static com.mongodb.client.model.Filters.gte;
import static com.mongodb.client.model.Filters.lt;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
public class CrudClient {
private static String uri = "mongodb://localhost:27017";
private static MongoClient mongoClient = MongoClients.create(uri);
private static MongoDatabase db;
private static MongoCollection<Event> collection;
public static OffsetDateTime offsetDateTime = OffsetDateTime.of(LocalDateTime.of(2022, 6, 4, 11, 0, 0),
ZoneOffset.ofHours(2));
public static Event pianoLessons = new Event(
"Piano lessons",
"Foo Bvld",
LocalDateTime.of(2022, 6, 4, 11, 0, 0));
public static Event soccerGame = new Event(
"Soccer game",
"Bar Avenue",
LocalDateTime.of(2022, 6, 10, 17, 0, 0));
public static Event pianoLessonsTZ = new Event(
"Piano lessons",
"Baz Bvld",
LocalDateTime.of(2022, 12, 31, 12, 0, 0),
ZoneOffset.ofHours(2).toString());
public static LocalDateTime dateQuery = LocalDateTime.of(2022, 6, 10, 17, 0, 0);
public static LocalDateTime dateQueryEventWithTZ = LocalDateTime.of(2022, 12, 31, 12, 0, 0);
public static LocalDateTime from = LocalDateTime.of(2022, 06, 04, 12, 0, 0);
public static LocalDateTime to = LocalDateTime.of(2022, 06, 10, 17, 0, 0);
public static LocalDate updateManyFrom = LocalDate.of(2022, 1, 1);
public static LocalDate updateManyTo = LocalDate.of(2023, 1, 1);
public static LocalDate deleteFrom = LocalDate.of(2022, 1, 1);
public static LocalDate deleteTo = LocalDate.of(2023, 01, 01);
public static void setup() {
CodecProvider codecProvider = PojoCodecProvider.builder().automatic(true).build();
CodecRegistry codecRegistry = fromRegistries(getDefaultCodecRegistry(), fromProviders(codecProvider));
db = mongoClient.getDatabase("calendar").withCodecRegistry(codecRegistry);
collection = db.getCollection("my_events", Event.class);
}
public static void close() {
mongoClient.close();
}
public static BsonValue insertEventsWithDate(Event e) {
try {
InsertOneResult insertResult = collection.insertOne(e);
return insertResult.getInsertedId();
} catch (MongoException me) {
System.err.println("Failed to insert with error: " + me);
throw me;
}
}
public static Event readEventsByDate(LocalDateTime localDateTime) {
try {
Event event = collection
.find(eq("dateTime", localDateTime))
.first();
return event;
} catch (MongoException me) {
System.err.println("Failed to read with error: " + me);
throw me;
}
}
public static List<Event> readEventsByDateRange(LocalDateTime from, LocalDateTime to) {
BasicDBObject object = new BasicDBObject();
object.put("dateTime", BasicDBObjectBuilder
.start("$gte", from)
.add("$lte", to)
.get());
try {
List<Event> list = new ArrayList<Event>(collection.find(object).into(new ArrayList<Event>()));
return list;
} catch (MongoException me) {
System.err.println("Failed to read with error: " + me);
throw me;
}
}
public static LocalDateTime readEventsByDateWithTZ(LocalDateTime localDateTime) {
try {
Event event = collection
.find(eq("dateTime", localDateTime))
.first();
OffsetDateTime offsetDateTime = OffsetDateTime.of(event.dateTime, ZoneOffset.of(pianoLessonsTZ.timeZoneOffset));
ZonedDateTime zoned = offsetDateTime.atZoneSameInstant(ZoneId.of("America/Toronto"));
return zoned.toLocalDateTime();
} catch (MongoException me) {
System.err.println("Failed to read with error: " + me);
throw me;
}
}
public static long updateDateField() {
Document document = new Document().append("title", "Piano lessons");
Bson update = Updates.currentDate("updatedAt");
UpdateOptions options = new UpdateOptions().upsert(false);
try {
UpdateResult result = collection.updateOne(document, update, options);
return result.getModifiedCount();
} catch (MongoException me) {
System.err.println("Failed to update with error: " + me);
throw me;
}
}
public static long updateManyEventsWithDateCriteria(LocalDate updateManyFrom, LocalDate updateManyTo) {
Bson query = and(gte("dateTime", updateManyFrom), lt("dateTime", updateManyTo));
Bson updates = Updates.currentDate("dateTime");
try {
UpdateResult result = collection.updateMany(query, updates);
return result.getModifiedCount();
} catch(MongoException me) {
System.err.println("Failed to replace/update with error: " + me);
throw me;
}
}
public static long deleteEventsByDate(LocalDate from, LocalDate to) {
Bson query = and(gte("dateTime", from), lt("dateTime", to));
try {
DeleteResult result = collection.deleteMany(query);
return result.getDeletedCount();
} catch (MongoException me) {
System.err.println("Failed to delete with error: " + me);
throw me;
}
}
public static void dropDb() {
db.drop();
}
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.mongo.crud.model;
import java.time.LocalDateTime;
public class Event {
public String title;
public String location;
public LocalDateTime dateTime;
public String timeZoneOffset;
public Event() {}
public Event(String title, String location, LocalDateTime dateTime) {
this.title = title;
this.location = location;
this.dateTime = dateTime;
}
public Event(String title, String location, LocalDateTime dateTime, String timeZoneOffset) {
this.title = title;
this.location = location;
this.dateTime = dateTime;
this.timeZoneOffset = timeZoneOffset;
}
@Override
public String toString() { return "\nEvent: " + title + "\nWhere: " + location + "\nWhen: " + dateTime; }
}

View File

@ -0,0 +1,87 @@
package com.baeldung.mongo.crud;
import com.baeldung.mongo.crud.model.Event;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.io.IOException;
import java.util.List;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class CrudClientLiveTest {
@BeforeAll
public static void setup() {
CrudClient.setup();
}
@Test
@Order(1)
public void whenInsertingEventsWithDate_thenCheckForDocument() {
Assertions.assertNotNull(CrudClient.insertEventsWithDate(CrudClient.pianoLessons));
Assertions.assertNotNull(CrudClient.insertEventsWithDate(CrudClient.soccerGame));
}
@Test
@Order(2)
public void whenReadingEventsByDate_thenCheckForReturnedDocument() {
Assertions.assertNotNull(CrudClient.readEventsByDate(CrudClient.dateQuery));
}
@Test
@Order(3)
public void whenReadingEventsByDateRange_thenCheckForReturnedDocument() {
List<Event> events = CrudClient.readEventsByDateRange(CrudClient.from, CrudClient.to);
Assertions.assertNotNull(events);
Assertions.assertEquals(1, events.size());
}
@Test
@Order(5)
public void whenUpdatingEventsDateField_thenCheckUpdatedCount() {
Assertions.assertEquals(1, CrudClient.updateDateField());
}
@Test
@Order(6)
public void whenUpdatingManyEvents_thenCheckUpdatedCount() {
long updates = CrudClient.updateManyEventsWithDateCriteria(CrudClient.updateManyFrom, CrudClient.updateManyTo);
Assertions.assertTrue(1 < updates);
}
@Test
@Order(7)
public void whenDeletingEventsWithDate_thenCheckDeletedCount() {
Assertions.assertEquals(2, CrudClient.deleteEventsByDate(CrudClient.deleteFrom, CrudClient.deleteTo));
}
@Test
@Order(8)
public void whenInsertingEventWithDateAndTimeZone_thenCheckForDocument() {
Assertions.assertNotNull(CrudClient.insertEventsWithDate(CrudClient.pianoLessonsTZ));
}
@Test
@Order(9)
public void whenReadingEventsWithDateAndTimeZone_thenCheckInsertedCount() {
Assertions.assertNotEquals(CrudClient.pianoLessonsTZ.dateTime, CrudClient.readEventsByDateWithTZ(CrudClient.dateQueryEventWithTZ));
}
@Test
@Order(10)
public void whenDeletingEventsWithDateAndTimeZone_thenCheckDeletedCount() {
Assertions.assertEquals(1, CrudClient.deleteEventsByDate(CrudClient.deleteFrom, CrudClient.deleteTo));
}
@AfterAll
public static void close() throws IOException {
CrudClient.dropDb();
CrudClient.close();
}
}

View File

@ -7,7 +7,10 @@
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>parent-modules</name>
<modules>
<module>persistence-modules/java-mongodb-queries</module>
</modules>
<name>parent-modules</name>
<packaging>pom</packaging>
<dependencies>