From f27e8e8dd9b47578b2f590c7aad54b1c5d54a1be Mon Sep 17 00:00:00 2001 From: lucaCambi77 Date: Mon, 28 Mar 2022 03:06:14 +0200 Subject: [PATCH] [ BAEL-5405 ] Logging MongoDB Queries with Spring Boot (#11952) * feat: spring data mongodb logging * fix: PMD * fix: typo * fix: move to main logging * fix: add manual config --- .../java/com/baeldung/logging/model/Book.java | 53 ++++++ .../com/baeldung/logging/GroupByAuthor.java | 32 ++++ .../com/baeldung/logging/LoggingUnitTest.java | 158 ++++++++++++++++++ 3 files changed, 243 insertions(+) create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/logging/model/Book.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/GroupByAuthor.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/LoggingUnitTest.java diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/logging/model/Book.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/logging/model/Book.java new file mode 100644 index 0000000000..d15c7f47c5 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/logging/model/Book.java @@ -0,0 +1,53 @@ +package com.baeldung.logging.model; + +import java.util.Objects; + +import org.bson.types.ObjectId; +import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.data.mongodb.core.mapping.MongoId; + +@Document(collection = "book") +public class Book { + + @MongoId + private ObjectId id; + + private String bookName; + + private String authorName; + + public ObjectId getId() { + return id; + } + + public String getBookName() { + return bookName; + } + + public void setBookName(String bookName) { + this.bookName = bookName; + } + + public String getAuthorName() { + return authorName; + } + + public void setAuthorName(String authorName) { + this.authorName = authorName; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Book book = (Book) o; + return Objects.equals(id, book.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/GroupByAuthor.java b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/GroupByAuthor.java new file mode 100644 index 0000000000..170e76f23e --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/GroupByAuthor.java @@ -0,0 +1,32 @@ +package com.baeldung.logging; + +import org.springframework.data.annotation.Id; + +public class GroupByAuthor { + + @Id + private String authorName; + private int authCount; + + public GroupByAuthor(String authorName, int authCount) { + this.authorName = authorName; + this.authCount = authCount; + } + + public String getAuthorName() { + return authorName; + } + + public void setAuthorName(String authorName) { + this.authorName = authorName; + } + + public int getAuthCount() { + return authCount; + } + + public void setAuthCount(int authCount) { + this.authCount = authCount; + } + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/LoggingUnitTest.java b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/LoggingUnitTest.java new file mode 100644 index 0000000000..577e039176 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/LoggingUnitTest.java @@ -0,0 +1,158 @@ +package com.baeldung.logging; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.springframework.data.mongodb.core.aggregation.Aggregation.group; +import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation; +import static org.springframework.data.mongodb.core.query.Criteria.where; +import static org.springframework.data.mongodb.core.query.Query.query; +import static org.springframework.data.mongodb.core.query.Update.update; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; +import org.springframework.data.mongodb.core.aggregation.AggregationResults; +import org.springframework.data.mongodb.core.aggregation.GroupOperation; +import org.springframework.test.context.TestPropertySource; + +import com.baeldung.logging.model.Book; +import com.mongodb.client.MongoClients; + +import de.flapdoodle.embed.mongo.MongodExecutable; +import de.flapdoodle.embed.mongo.MongodStarter; +import de.flapdoodle.embed.mongo.config.ImmutableMongodConfig; +import de.flapdoodle.embed.mongo.config.MongodConfig; +import de.flapdoodle.embed.mongo.config.Net; +import de.flapdoodle.embed.mongo.distribution.Version; +import de.flapdoodle.embed.process.runtime.Network; + +@SpringBootTest +@TestPropertySource(properties = { "logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG" }) +public class LoggingUnitTest { + + private static final String CONNECTION_STRING = "mongodb://%s:%d"; + + private MongodExecutable mongodExecutable; + private MongoTemplate mongoTemplate; + + @AfterEach + void clean() { + mongodExecutable.stop(); + } + + @BeforeEach + void setup() throws Exception { + String ip = "localhost"; + int port = 27017; + + ImmutableMongodConfig mongodConfig = MongodConfig.builder() + .version(Version.Main.PRODUCTION) + .net(new Net(ip, port, Network.localhostIsIPv6())) + .build(); + + MongodStarter starter = MongodStarter.getDefaultInstance(); + mongodExecutable = starter.prepare(mongodConfig); + mongodExecutable.start(); + mongoTemplate = new MongoTemplate(MongoClients.create(String.format(CONNECTION_STRING, ip, port)), "test"); + } + + @Test + void whenInsertDocument_thenFindByIdOk() { + Book book = new Book(); + book.setBookName("Book"); + book.setAuthorName("Author"); + + mongoTemplate.insert(book); + + assertThat(mongoTemplate.findById(book.getId(), Book.class)).isEqualTo(book); + } + + @Test + void givenExistingDocument_whenUpdateDocument_thenFieldIsUpdatedOk() { + Book book = new Book(); + book.setBookName("Book"); + book.setAuthorName("Author"); + + mongoTemplate.insert(book); + + String authorNameUpdate = "AuthorNameUpdate"; + + book.setAuthorName(authorNameUpdate); + mongoTemplate.updateFirst(query(where("bookName").is("Book")), update("authorName", authorNameUpdate), Book.class); + + assertThat(mongoTemplate.findById(book.getId(), Book.class)).extracting(Book::getAuthorName) + .isEqualTo(authorNameUpdate); + } + + @Test + void whenInsertMultipleDocuments_thenFindAllOk() { + Book book = new Book(); + book.setBookName("Book"); + book.setAuthorName("Author"); + + Book book1 = new Book(); + book1.setBookName("Book1"); + book1.setAuthorName("Author1"); + + mongoTemplate.insert(Arrays.asList(book, book1), Book.class); + + assertThat(mongoTemplate.findAll(Book.class) + .size()).isEqualTo(2); + } + + @Test + void givenExistingDocument_whenRemoveDocument_thenDocumentIsDeleted() { + Book book = new Book(); + book.setBookName("Book"); + book.setAuthorName("Author"); + + mongoTemplate.insert(book); + + mongoTemplate.remove(book); + + assertThat(mongoTemplate.findAll(Book.class) + .size()).isEqualTo(0); + } + + @Test + void whenAggregateByField_thenGroupByCountIsOk() { + Book book = new Book(); + book.setBookName("Book"); + book.setAuthorName("Author"); + + Book book1 = new Book(); + book1.setBookName("Book1"); + book1.setAuthorName("Author"); + + Book book2 = new Book(); + book2.setBookName("Book2"); + book2.setAuthorName("Author"); + + mongoTemplate.insert(Arrays.asList(book, book1, book2), Book.class); + + GroupOperation groupByAuthor = group("authorName").count() + .as("authCount"); + + Aggregation aggregation = newAggregation(groupByAuthor); + + AggregationResults aggregationResults = mongoTemplate.aggregate(aggregation, "book", GroupByAuthor.class); + + List groupByAuthorList = StreamSupport.stream(aggregationResults.spliterator(), false) + .collect(Collectors.toList()); + + assertThat(groupByAuthorList.stream() + .filter(l -> l.getAuthorName() + .equals("Author")) + .findFirst() + .orElse(null)).extracting(GroupByAuthor::getAuthCount) + .isEqualTo(3); + } + +}