1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2020-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
14
14
* limitations under the License.
15
15
*/
16
16
17
- package org .springframework .integration .mongodb . rules ;
17
+ package org .springframework .integration .mongodb ;
18
18
19
19
import java .time .Duration ;
20
20
21
21
import org .bson .Document ;
22
22
import org .bson .UuidRepresentation ;
23
23
import org .bson .conversions .Bson ;
24
- import org .junit .Rule ;
24
+ import org .junit .jupiter .api .BeforeAll ;
25
+ import org .testcontainers .containers .GenericContainer ;
26
+ import org .testcontainers .junit .jupiter .Testcontainers ;
25
27
26
28
import org .springframework .dao .DataAccessException ;
27
29
import org .springframework .data .annotation .Id ;
40
42
import org .springframework .integration .mongodb .outbound .MessageCollectionCallback ;
41
43
import org .springframework .messaging .Message ;
42
44
45
+ import com .mongodb .ConnectionString ;
43
46
import com .mongodb .MongoClientSettings ;
44
47
import com .mongodb .MongoException ;
45
48
import com .mongodb .client .MongoClients ;
46
49
import com .mongodb .client .MongoCollection ;
47
50
48
-
49
51
/**
50
- * Convenience base class that enables unit test methods to rely upon the {@link MongoDbAvailable} annotation.
52
+ * The base contract for all tests requiring a MongoDb connection.
53
+ * The Testcontainers 'reuse' option must be disabled, so, Ryuk container is started
54
+ * and will clean all the containers up from this test suite after JVM exit.
55
+ * Since the Redis container instance is shared via static property, it is going to be
56
+ * started only once per JVM, therefore the target Docker container is reused automatically.
51
57
*
52
58
* @author Oleg Zhurakousky
53
59
* @author Xavier Padro
54
60
* @author Artem Bilan
55
61
* @author David Turanski
62
+ * @author Artem Vozhdayenko
56
63
*
57
- * @since 2.1
64
+ * @since 6.0
58
65
*/
59
- public abstract class MongoDbAvailableTests {
60
-
61
- @ Rule
62
- public MongoDbAvailableRule mongoDbAvailableRule = new MongoDbAvailableRule ();
63
-
64
- public static final MongoDatabaseFactory MONGO_DATABASE_FACTORY =
65
- new SimpleMongoClientDatabaseFactory (
66
- MongoClients .create (
67
- MongoClientSettings .builder ().uuidRepresentation (UuidRepresentation .STANDARD ).build ()),
68
- "test" );
69
-
70
- public static final ReactiveMongoDatabaseFactory REACTIVE_MONGO_DATABASE_FACTORY =
71
- new SimpleReactiveMongoDatabaseFactory (
72
- com .mongodb .reactivestreams .client .MongoClients .create (
73
- MongoClientSettings .builder ().uuidRepresentation (UuidRepresentation .STANDARD ).build ()),
74
- "test" );
75
-
76
- protected MongoDatabaseFactory prepareMongoFactory (String ... additionalCollectionsToDrop ) {
77
- cleanupCollections (MONGO_DATABASE_FACTORY , additionalCollectionsToDrop );
78
- return MONGO_DATABASE_FACTORY ;
66
+ @ Testcontainers (disabledWithoutDocker = true )
67
+ public interface MongoDbContainerTest {
68
+
69
+ GenericContainer <?> MONGO_CONTAINER = new GenericContainer <>("mongo:5.0.9" )
70
+ .withExposedPorts (27017 );
71
+
72
+ @ BeforeAll
73
+ static void startContainer () {
74
+ MONGO_CONTAINER .start ();
75
+ }
76
+
77
+ static MongoDatabaseFactory createMongoDbFactory () {
78
+ return new SimpleMongoClientDatabaseFactory (MongoClients .create (getMongoClientSettings ()), "test" );
79
+ }
80
+
81
+ static ReactiveMongoDatabaseFactory createReactiveMongoDbFactory () {
82
+ return new SimpleReactiveMongoDatabaseFactory (
83
+ com .mongodb .reactivestreams .client .MongoClients .create (getMongoClientSettings ()),
84
+ "test" );
85
+ }
86
+
87
+ private static MongoClientSettings getMongoClientSettings () {
88
+ return MongoClientSettings .builder ()
89
+ .applyConnectionString (new ConnectionString (
90
+ "mongodb://localhost:" + MONGO_CONTAINER .getFirstMappedPort ()))
91
+ .uuidRepresentation (UuidRepresentation .STANDARD ).build ();
92
+ }
93
+
94
+ static void prepareMongoData (MongoDatabaseFactory mongoDatabaseFactory , String ... additionalCollectionsToDrop ) {
95
+ cleanupCollections (mongoDatabaseFactory , additionalCollectionsToDrop );
79
96
}
80
97
81
- protected ReactiveMongoDatabaseFactory prepareReactiveMongoFactory (String ... additionalCollectionsToDrop ) {
82
- cleanupCollections (REACTIVE_MONGO_DATABASE_FACTORY , additionalCollectionsToDrop );
83
- return REACTIVE_MONGO_DATABASE_FACTORY ;
98
+ static void prepareReactiveMongoData (ReactiveMongoDatabaseFactory mongoDatabaseFactory , String ... additionalCollectionsToDrop ) {
99
+ cleanupCollections (mongoDatabaseFactory , additionalCollectionsToDrop );
84
100
}
85
101
86
- protected void cleanupCollections (ReactiveMongoDatabaseFactory mongoDbFactory ,
102
+ static void cleanupCollections (ReactiveMongoDatabaseFactory mongoDbFactory ,
87
103
String ... additionalCollectionsToDrop ) {
88
104
89
105
ReactiveMongoTemplate template = new ReactiveMongoTemplate (mongoDbFactory );
@@ -95,7 +111,7 @@ protected void cleanupCollections(ReactiveMongoDatabaseFactory mongoDbFactory,
95
111
}
96
112
}
97
113
98
- protected void cleanupCollections (MongoDatabaseFactory mongoDbFactory , String ... additionalCollectionsToDrop ) {
114
+ static void cleanupCollections (MongoDatabaseFactory mongoDbFactory , String ... additionalCollectionsToDrop ) {
99
115
MongoTemplate template = new MongoTemplate (mongoDbFactory );
100
116
template .dropCollection ("messages" );
101
117
template .dropCollection ("configurableStoreMessages" );
@@ -105,7 +121,7 @@ protected void cleanupCollections(MongoDatabaseFactory mongoDbFactory, String...
105
121
}
106
122
}
107
123
108
- protected Person createPerson () {
124
+ static Person createPerson () {
109
125
Address address = new Address ();
110
126
address .setCity ("Philadelphia" );
111
127
address .setStreet ("2121 Rawn street" );
@@ -117,7 +133,7 @@ protected Person createPerson() {
117
133
return person ;
118
134
}
119
135
120
- protected Person createPerson (String name ) {
136
+ static Person createPerson (String name ) {
121
137
Address address = new Address ();
122
138
address .setCity ("Philadelphia" );
123
139
address .setStreet ("2121 Rawn street" );
@@ -129,7 +145,7 @@ protected Person createPerson(String name) {
129
145
return person ;
130
146
}
131
147
132
- public static class Person {
148
+ class Person {
133
149
134
150
@ Id
135
151
private String id ;
@@ -156,7 +172,7 @@ public void setName(String name) {
156
172
157
173
}
158
174
159
- public static class Address {
175
+ class Address {
160
176
161
177
private String street ;
162
178
@@ -190,7 +206,7 @@ public void setState(String state) {
190
206
191
207
}
192
208
193
- public static class TestMongoConverter extends MappingMongoConverter {
209
+ class TestMongoConverter extends MappingMongoConverter {
194
210
195
211
public TestMongoConverter (
196
212
MongoDatabaseFactory mongoDbFactory ,
@@ -211,7 +227,7 @@ public <S> S read(Class<S> clazz, Bson source) {
211
227
212
228
}
213
229
214
- public static class ReactiveTestMongoConverter extends MappingMongoConverter {
230
+ class ReactiveTestMongoConverter extends MappingMongoConverter {
215
231
216
232
public ReactiveTestMongoConverter (
217
233
ReactiveMongoDatabaseFactory mongoDbFactory ,
@@ -232,7 +248,7 @@ public <S> S read(Class<S> clazz, Bson source) {
232
248
233
249
}
234
250
235
- public static class TestCollectionCallback implements MessageCollectionCallback <Long > {
251
+ class TestCollectionCallback implements MessageCollectionCallback <Long > {
236
252
237
253
@ Override
238
254
public Long doInCollection (MongoCollection <Document > collection , Message <?> message )
0 commit comments