Skip to content

Commit a9e3fe5

Browse files
authored
fix: move to spring-data-mongodb (eugenp#12089)
1 parent 7f53814 commit a9e3fe5

File tree

9 files changed

+71
-50
lines changed

9 files changed

+71
-50
lines changed

persistence-modules/spring-data-mongodb/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
<artifactId>querydsl-apt</artifactId>
7474
<version>${querydsl.version}</version>
7575
</dependency>
76+
<dependency>
77+
<groupId>de.flapdoodle.embed</groupId>
78+
<artifactId>de.flapdoodle.embed.mongo</artifactId>
79+
<version>${embed.mongo.version}</version>
80+
<scope>test</scope>
81+
</dependency>
7682
</dependencies>
7783

7884
<build>
@@ -103,6 +109,7 @@
103109
<mongodb-reactivestreams.version>4.1.0</mongodb-reactivestreams.version>
104110
<projectreactor.version>3.2.0.RELEASE</projectreactor.version>
105111
<mongodb-driver.version>4.0.5</mongodb-driver.version>
112+
<embed.mongo.version>3.2.6</embed.mongo.version>
106113
</properties>
107114

108115
</project>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import java.util.List;
44
import java.util.Objects;
55

6+
import org.springframework.data.annotation.Id;
67
import org.springframework.data.mongodb.core.mapping.Document;
78
import org.springframework.data.mongodb.core.mapping.MongoId;
89

910
@Document(collection = "inventory")
1011
public class Inventory {
1112

12-
@MongoId
13+
@Id
1314
private String id;
1415
private String item;
1516
private String status;
Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,15 @@
11
package com.baeldung.projection;
22

3-
import java.io.IOException;
43
import java.util.Arrays;
54
import java.util.Collections;
65
import java.util.List;
76

8-
import org.junit.jupiter.api.AfterEach;
9-
import org.springframework.data.mongodb.core.MongoTemplate;
10-
import org.springframework.util.SocketUtils;
11-
127
import com.baeldung.projection.model.InStock;
138
import com.baeldung.projection.model.Inventory;
149
import com.baeldung.projection.model.Size;
15-
import com.mongodb.client.MongoClients;
16-
17-
import de.flapdoodle.embed.mongo.MongodExecutable;
18-
import de.flapdoodle.embed.mongo.MongodStarter;
19-
import de.flapdoodle.embed.mongo.config.ImmutableMongodConfig;
20-
import de.flapdoodle.embed.mongo.config.MongodConfig;
21-
import de.flapdoodle.embed.mongo.config.Net;
22-
import de.flapdoodle.embed.mongo.distribution.Version;
23-
import de.flapdoodle.embed.process.runtime.Network;
2410

2511
abstract class AbstractTestProjection {
2612

27-
private static final String CONNECTION_STRING = "mongodb://%s:%d";
28-
29-
protected MongodExecutable mongodExecutable;
30-
protected MongoTemplate mongoTemplate;
31-
32-
@AfterEach
33-
void clean() {
34-
mongodExecutable.stop();
35-
}
36-
37-
void setUp() throws IOException {
38-
String ip = "localhost";
39-
int port = SocketUtils.findAvailableTcpPort();
40-
41-
ImmutableMongodConfig mongodbConfig = MongodConfig.builder()
42-
.version(Version.Main.PRODUCTION)
43-
.net(new Net(ip, port, Network.localhostIsIPv6()))
44-
.build();
45-
46-
MongodStarter starter = MongodStarter.getDefaultInstance();
47-
mongodExecutable = starter.prepare(mongodbConfig);
48-
mongodExecutable.start();
49-
mongoTemplate = new MongoTemplate(MongoClients.create(String.format(CONNECTION_STRING, ip, port)), "test");
50-
}
51-
5213
public List<Inventory> getInventories() {
5314
Inventory journal = new Inventory();
5415
journal.setItem("journal");
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,27 @@
1010

1111
import org.junit.jupiter.api.BeforeEach;
1212
import org.junit.jupiter.api.Test;
13-
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.junit.jupiter.api.extension.ExtendWith;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.data.mongodb.core.MongoTemplate;
1416
import org.springframework.data.mongodb.core.query.Query;
17+
import org.springframework.test.context.ContextConfiguration;
18+
import org.springframework.test.context.junit.jupiter.SpringExtension;
1519

20+
import com.baeldung.projection.config.ProjectionConfig;
1621
import com.baeldung.projection.model.InStock;
1722
import com.baeldung.projection.model.Inventory;
1823
import com.baeldung.projection.model.Size;
1924

20-
@SpringBootTest
25+
@ExtendWith(SpringExtension.class)
26+
@ContextConfiguration(classes = ProjectionConfig.class)
2127
public class MongoTemplateProjectionUnitTest extends AbstractTestProjection {
2228

23-
@BeforeEach
24-
void setup() throws Exception {
25-
super.setUp();
29+
@Autowired
30+
private MongoTemplate mongoTemplate;
2631

32+
@BeforeEach
33+
void setup() {
2734
List<Inventory> inventoryList = getInventories();
2835

2936
mongoTemplate.insert(inventoryList, Inventory.class);
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,26 @@
1010

1111
import org.junit.jupiter.api.BeforeEach;
1212
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.extension.ExtendWith;
1314
import org.springframework.beans.factory.annotation.Autowired;
14-
import org.springframework.boot.test.context.SpringBootTest;
15+
import org.springframework.test.context.ContextConfiguration;
16+
import org.springframework.test.context.junit.jupiter.SpringExtension;
1517

18+
import com.baeldung.projection.config.ProjectionConfig;
1619
import com.baeldung.projection.model.InStock;
1720
import com.baeldung.projection.model.Inventory;
1821
import com.baeldung.projection.model.Size;
1922
import com.baeldung.projection.repository.InventoryRepository;
2023

21-
@SpringBootTest
24+
@ExtendWith(SpringExtension.class)
25+
@ContextConfiguration(classes = ProjectionConfig.class)
2226
public class RepositoryProjectionUnitTest extends AbstractTestProjection {
2327

2428
@Autowired
2529
private InventoryRepository inventoryRepository;
2630

2731
@BeforeEach
28-
void setup() throws Exception {
29-
super.setUp();
30-
32+
void setup() {
3133
List<Inventory> inventoryList = getInventories();
3234

3335
inventoryRepository.saveAll(inventoryList);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.projection.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.data.mongodb.core.MongoTemplate;
7+
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
8+
import org.springframework.util.SocketUtils;
9+
10+
import com.mongodb.client.MongoClients;
11+
12+
import de.flapdoodle.embed.mongo.MongodExecutable;
13+
import de.flapdoodle.embed.mongo.MongodStarter;
14+
import de.flapdoodle.embed.mongo.config.ImmutableMongodConfig;
15+
import de.flapdoodle.embed.mongo.config.MongodConfig;
16+
import de.flapdoodle.embed.mongo.config.Net;
17+
import de.flapdoodle.embed.mongo.distribution.Version;
18+
import de.flapdoodle.embed.process.runtime.Network;
19+
20+
@Configuration
21+
@ComponentScan(basePackages = "com.baeldung.projection")
22+
@EnableMongoRepositories(basePackages = "com.baeldung.projection.repository")
23+
public class ProjectionConfig {
24+
25+
private static final String CONNECTION_STRING = "mongodb://%s:%d";
26+
private static final String HOST = "localhost";
27+
28+
@Bean
29+
public MongoTemplate mongoTemplate() throws Exception {
30+
int randomPort = SocketUtils.findAvailableTcpPort();
31+
32+
ImmutableMongodConfig mongoDbConfig = MongodConfig.builder()
33+
.version(Version.Main.PRODUCTION)
34+
.net(new Net(HOST, randomPort, Network.localhostIsIPv6()))
35+
.build();
36+
37+
MongodStarter starter = MongodStarter.getDefaultInstance();
38+
MongodExecutable mongodExecutable = starter.prepare(mongoDbConfig);
39+
mongodExecutable.start();
40+
41+
return new MongoTemplate(MongoClients.create(String.format(CONNECTION_STRING, HOST, randomPort)), "mongo_auth");
42+
}
43+
}

0 commit comments

Comments
 (0)