fix: move to spring-data-mongodb (#12089)

This commit is contained in:
lucaCambi77 2022-04-29 01:20:55 +02:00 committed by GitHub
parent c3e0c5b1fb
commit 31a903b552
9 changed files with 71 additions and 50 deletions

View File

@ -73,6 +73,12 @@
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>${embed.mongo.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -103,6 +109,7 @@
<mongodb-reactivestreams.version>4.1.0</mongodb-reactivestreams.version>
<projectreactor.version>3.2.0.RELEASE</projectreactor.version>
<mongodb-driver.version>4.0.5</mongodb-driver.version>
<embed.mongo.version>3.2.6</embed.mongo.version>
</properties>
</project>

View File

@ -3,13 +3,14 @@ package com.baeldung.projection.model;
import java.util.List;
import java.util.Objects;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoId;
@Document(collection = "inventory")
public class Inventory {
@MongoId
@Id
private String id;
private String item;
private String status;

View File

@ -1,54 +1,15 @@
package com.baeldung.projection;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.util.SocketUtils;
import com.baeldung.projection.model.InStock;
import com.baeldung.projection.model.Inventory;
import com.baeldung.projection.model.Size;
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;
abstract class AbstractTestProjection {
private static final String CONNECTION_STRING = "mongodb://%s:%d";
protected MongodExecutable mongodExecutable;
protected MongoTemplate mongoTemplate;
@AfterEach
void clean() {
mongodExecutable.stop();
}
void setUp() throws IOException {
String ip = "localhost";
int port = SocketUtils.findAvailableTcpPort();
ImmutableMongodConfig mongodbConfig = MongodConfig.builder()
.version(Version.Main.PRODUCTION)
.net(new Net(ip, port, Network.localhostIsIPv6()))
.build();
MongodStarter starter = MongodStarter.getDefaultInstance();
mongodExecutable = starter.prepare(mongodbConfig);
mongodExecutable.start();
mongoTemplate = new MongoTemplate(MongoClients.create(String.format(CONNECTION_STRING, ip, port)), "test");
}
public List<Inventory> getInventories() {
Inventory journal = new Inventory();
journal.setItem("journal");

View File

@ -10,20 +10,27 @@ import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.baeldung.projection.config.ProjectionConfig;
import com.baeldung.projection.model.InStock;
import com.baeldung.projection.model.Inventory;
import com.baeldung.projection.model.Size;
@SpringBootTest
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = ProjectionConfig.class)
public class MongoTemplateProjectionUnitTest extends AbstractTestProjection {
@BeforeEach
void setup() throws Exception {
super.setUp();
@Autowired
private MongoTemplate mongoTemplate;
@BeforeEach
void setup() {
List<Inventory> inventoryList = getInventories();
mongoTemplate.insert(inventoryList, Inventory.class);

View File

@ -10,24 +10,26 @@ import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.baeldung.projection.config.ProjectionConfig;
import com.baeldung.projection.model.InStock;
import com.baeldung.projection.model.Inventory;
import com.baeldung.projection.model.Size;
import com.baeldung.projection.repository.InventoryRepository;
@SpringBootTest
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = ProjectionConfig.class)
public class RepositoryProjectionUnitTest extends AbstractTestProjection {
@Autowired
private InventoryRepository inventoryRepository;
@BeforeEach
void setup() throws Exception {
super.setUp();
void setup() {
List<Inventory> inventoryList = getInventories();
inventoryRepository.saveAll(inventoryList);

View File

@ -0,0 +1,43 @@
package com.baeldung.projection.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.util.SocketUtils;
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;
@Configuration
@ComponentScan(basePackages = "com.baeldung.projection")
@EnableMongoRepositories(basePackages = "com.baeldung.projection.repository")
public class ProjectionConfig {
private static final String CONNECTION_STRING = "mongodb://%s:%d";
private static final String HOST = "localhost";
@Bean
public MongoTemplate mongoTemplate() throws Exception {
int randomPort = SocketUtils.findAvailableTcpPort();
ImmutableMongodConfig mongoDbConfig = MongodConfig.builder()
.version(Version.Main.PRODUCTION)
.net(new Net(HOST, randomPort, Network.localhostIsIPv6()))
.build();
MongodStarter starter = MongodStarter.getDefaultInstance();
MongodExecutable mongodExecutable = starter.prepare(mongoDbConfig);
mongodExecutable.start();
return new MongoTemplate(MongoClients.create(String.format(CONNECTION_STRING, HOST, randomPort)), "mongo_auth");
}
}