From 31a903b5522175518788c56c7952a8ac3f37fba5 Mon Sep 17 00:00:00 2001 From: lucaCambi77 Date: Fri, 29 Apr 2022 01:20:55 +0200 Subject: [PATCH] fix: move to spring-data-mongodb (#12089) --- .../spring-data-mongodb/pom.xml | 7 +++ .../baeldung/projection/model/InStock.java | 0 .../baeldung/projection/model/Inventory.java | 3 +- .../com/baeldung/projection/model/Size.java | 0 .../repository/InventoryRepository.java | 0 .../projection/AbstractTestProjection.java | 39 ----------------- .../MongoTemplateProjectionUnitTest.java | 17 +++++--- .../RepositoryProjectionUnitTest.java | 12 +++--- .../projection/config/ProjectionConfig.java | 43 +++++++++++++++++++ 9 files changed, 71 insertions(+), 50 deletions(-) rename persistence-modules/{spring-boot-persistence-mongodb => spring-data-mongodb}/src/main/java/com/baeldung/projection/model/InStock.java (100%) rename persistence-modules/{spring-boot-persistence-mongodb => spring-data-mongodb}/src/main/java/com/baeldung/projection/model/Inventory.java (96%) rename persistence-modules/{spring-boot-persistence-mongodb => spring-data-mongodb}/src/main/java/com/baeldung/projection/model/Size.java (100%) rename persistence-modules/{spring-boot-persistence-mongodb => spring-data-mongodb}/src/main/java/com/baeldung/projection/repository/InventoryRepository.java (100%) rename persistence-modules/{spring-boot-persistence-mongodb => spring-data-mongodb}/src/test/java/com/baeldung/projection/AbstractTestProjection.java (54%) rename persistence-modules/{spring-boot-persistence-mongodb => spring-data-mongodb}/src/test/java/com/baeldung/projection/MongoTemplateProjectionUnitTest.java (92%) rename persistence-modules/{spring-boot-persistence-mongodb => spring-data-mongodb}/src/test/java/com/baeldung/projection/RepositoryProjectionUnitTest.java (94%) create mode 100644 persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/config/ProjectionConfig.java diff --git a/persistence-modules/spring-data-mongodb/pom.xml b/persistence-modules/spring-data-mongodb/pom.xml index ef5a0f0550..afb5e8b9c1 100644 --- a/persistence-modules/spring-data-mongodb/pom.xml +++ b/persistence-modules/spring-data-mongodb/pom.xml @@ -73,6 +73,12 @@ querydsl-apt ${querydsl.version} + + de.flapdoodle.embed + de.flapdoodle.embed.mongo + ${embed.mongo.version} + test + @@ -103,6 +109,7 @@ 4.1.0 3.2.0.RELEASE 4.0.5 + 3.2.6 \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/projection/model/InStock.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/projection/model/InStock.java similarity index 100% rename from persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/projection/model/InStock.java rename to persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/projection/model/InStock.java diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/projection/model/Inventory.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/projection/model/Inventory.java similarity index 96% rename from persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/projection/model/Inventory.java rename to persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/projection/model/Inventory.java index 28e6607dc9..0f4cf6b814 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/projection/model/Inventory.java +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/projection/model/Inventory.java @@ -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; diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/projection/model/Size.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/projection/model/Size.java similarity index 100% rename from persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/projection/model/Size.java rename to persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/projection/model/Size.java diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/projection/repository/InventoryRepository.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/projection/repository/InventoryRepository.java similarity index 100% rename from persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/projection/repository/InventoryRepository.java rename to persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/projection/repository/InventoryRepository.java diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/projection/AbstractTestProjection.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/AbstractTestProjection.java similarity index 54% rename from persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/projection/AbstractTestProjection.java rename to persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/AbstractTestProjection.java index c86d8c170e..b197ada7aa 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/projection/AbstractTestProjection.java +++ b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/AbstractTestProjection.java @@ -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 getInventories() { Inventory journal = new Inventory(); journal.setItem("journal"); diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/projection/MongoTemplateProjectionUnitTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/MongoTemplateProjectionUnitTest.java similarity index 92% rename from persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/projection/MongoTemplateProjectionUnitTest.java rename to persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/MongoTemplateProjectionUnitTest.java index a174b73f2c..1cd6415a1b 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/projection/MongoTemplateProjectionUnitTest.java +++ b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/MongoTemplateProjectionUnitTest.java @@ -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 inventoryList = getInventories(); mongoTemplate.insert(inventoryList, Inventory.class); diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/projection/RepositoryProjectionUnitTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/RepositoryProjectionUnitTest.java similarity index 94% rename from persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/projection/RepositoryProjectionUnitTest.java rename to persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/RepositoryProjectionUnitTest.java index 6b0fa7f7fd..e8f2d4bf7d 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/projection/RepositoryProjectionUnitTest.java +++ b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/RepositoryProjectionUnitTest.java @@ -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 inventoryList = getInventories(); inventoryRepository.saveAll(inventoryList); diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/config/ProjectionConfig.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/config/ProjectionConfig.java new file mode 100644 index 0000000000..06260c0295 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/projection/config/ProjectionConfig.java @@ -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"); + } +}