[JAVA-11876] Split spring-boot-mongodb module

This commit is contained in:
Haroon Khan
2022-05-12 18:59:40 +01:00
parent 6590d5c870
commit dd3457a7e5
12 changed files with 72 additions and 3 deletions

View File

@@ -0,0 +1,13 @@
package com.baeldung;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootPersistenceApplication {
public static void main(String ... args) {
SpringApplication.run(SpringBootPersistenceApplication.class, args);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1 @@
spring.application.name=spring-boot-persistence-mongodb-2

View File

@@ -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;
}
}

View File

@@ -0,0 +1,159 @@
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 org.springframework.util.SocketUtils;
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=INFO" })
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 = Network.freeServerPort(Network.getLocalHost());
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<GroupByAuthor> aggregationResults = mongoTemplate.aggregate(aggregation, "book", GroupByAuthor.class);
List<GroupByAuthor> 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);
}
}

View File

@@ -0,0 +1 @@
spring.mongodb.embedded.version=4.4.9

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="15 seconds" debug="false">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{ISO8601}]-[%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>