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
+ GenericContainer <?> MONGO_CONTAINER = new GenericContainer <>("mongo:5.0.9" )
69
+ .withExposedPorts (27017 );
70
+
71
+ @ BeforeAll
72
+ static void startContainer () {
73
+ MONGO_CONTAINER .start ();
74
+ }
75
+
76
+ static MongoDatabaseFactory createMongoDbFactory () {
77
+ return new SimpleMongoClientDatabaseFactory (
78
+ MongoClients .create (
79
+ MongoClientSettings .builder ()
80
+ .applyConnectionString (new ConnectionString (
81
+ "mongodb://localhost:" + MONGO_CONTAINER .getFirstMappedPort ()))
82
+ .uuidRepresentation (UuidRepresentation .STANDARD ).build ()),
83
+ "test" );
84
+ }
85
+
86
+ static ReactiveMongoDatabaseFactory createReactiveMongoDbFactory () {
87
+ return new SimpleReactiveMongoDatabaseFactory (
88
+ com .mongodb .reactivestreams .client .MongoClients .create (
89
+ MongoClientSettings .builder ().applyConnectionString (new ConnectionString (
90
+ "mongodb://localhost:" + MONGO_CONTAINER .getFirstMappedPort ()))
91
+ .uuidRepresentation (UuidRepresentation .STANDARD ).build ()),
92
+ "test" );
93
+ }
94
+
95
+ static void prepareMongoData (MongoDatabaseFactory mongoDatabaseFactory , String ... additionalCollectionsToDrop ) {
96
+ cleanupCollections (mongoDatabaseFactory , additionalCollectionsToDrop );
79
97
}
80
98
81
- protected ReactiveMongoDatabaseFactory prepareReactiveMongoFactory (String ... additionalCollectionsToDrop ) {
82
- cleanupCollections (REACTIVE_MONGO_DATABASE_FACTORY , additionalCollectionsToDrop );
83
- return REACTIVE_MONGO_DATABASE_FACTORY ;
99
+ static void prepareReactiveMongoData (ReactiveMongoDatabaseFactory mongoDatabaseFactory , String ... additionalCollectionsToDrop ) {
100
+ cleanupCollections (mongoDatabaseFactory , additionalCollectionsToDrop );
84
101
}
85
102
86
- protected void cleanupCollections (ReactiveMongoDatabaseFactory mongoDbFactory ,
103
+ static void cleanupCollections (ReactiveMongoDatabaseFactory MONGO_DATABASE_FACTORY ,
87
104
String ... additionalCollectionsToDrop ) {
88
105
89
- ReactiveMongoTemplate template = new ReactiveMongoTemplate (mongoDbFactory );
106
+ ReactiveMongoTemplate template = new ReactiveMongoTemplate (MONGO_DATABASE_FACTORY );
90
107
template .dropCollection ("messages" ).block (Duration .ofSeconds (3 ));
91
108
template .dropCollection ("configurableStoreMessages" ).block (Duration .ofSeconds (3 ));
92
109
template .dropCollection ("data" ).block (Duration .ofSeconds (3 ));
@@ -95,8 +112,8 @@ protected void cleanupCollections(ReactiveMongoDatabaseFactory mongoDbFactory,
95
112
}
96
113
}
97
114
98
- protected void cleanupCollections (MongoDatabaseFactory mongoDbFactory , String ... additionalCollectionsToDrop ) {
99
- MongoTemplate template = new MongoTemplate (mongoDbFactory );
115
+ static void cleanupCollections (MongoDatabaseFactory MONGO_DATABASE_FACTORY , String ... additionalCollectionsToDrop ) {
116
+ MongoTemplate template = new MongoTemplate (MONGO_DATABASE_FACTORY );
100
117
template .dropCollection ("messages" );
101
118
template .dropCollection ("configurableStoreMessages" );
102
119
template .dropCollection ("data" );
@@ -105,7 +122,7 @@ protected void cleanupCollections(MongoDatabaseFactory mongoDbFactory, String...
105
122
}
106
123
}
107
124
108
- protected Person createPerson () {
125
+ static Person createPerson () {
109
126
Address address = new Address ();
110
127
address .setCity ("Philadelphia" );
111
128
address .setStreet ("2121 Rawn street" );
@@ -117,7 +134,7 @@ protected Person createPerson() {
117
134
return person ;
118
135
}
119
136
120
- protected Person createPerson (String name ) {
137
+ static Person createPerson (String name ) {
121
138
Address address = new Address ();
122
139
address .setCity ("Philadelphia" );
123
140
address .setStreet ("2121 Rawn street" );
@@ -129,7 +146,7 @@ protected Person createPerson(String name) {
129
146
return person ;
130
147
}
131
148
132
- public static class Person {
149
+ class Person {
133
150
134
151
@ Id
135
152
private String id ;
@@ -156,7 +173,7 @@ public void setName(String name) {
156
173
157
174
}
158
175
159
- public static class Address {
176
+ class Address {
160
177
161
178
private String street ;
162
179
@@ -190,13 +207,13 @@ public void setState(String state) {
190
207
191
208
}
192
209
193
- public static class TestMongoConverter extends MappingMongoConverter {
210
+ class TestMongoConverter extends MappingMongoConverter {
194
211
195
212
public TestMongoConverter (
196
- MongoDatabaseFactory mongoDbFactory ,
213
+ MongoDatabaseFactory MONGO_DATABASE_FACTORY ,
197
214
MappingContext <? extends MongoPersistentEntity <?>, MongoPersistentProperty > mappingContext ) {
198
215
199
- super (new DefaultDbRefResolver (mongoDbFactory ), mappingContext );
216
+ super (new DefaultDbRefResolver (MONGO_DATABASE_FACTORY ), mappingContext );
200
217
}
201
218
202
219
@ Override
@@ -211,10 +228,10 @@ public <S> S read(Class<S> clazz, Bson source) {
211
228
212
229
}
213
230
214
- public static class ReactiveTestMongoConverter extends MappingMongoConverter {
231
+ class ReactiveTestMongoConverter extends MappingMongoConverter {
215
232
216
233
public ReactiveTestMongoConverter (
217
- ReactiveMongoDatabaseFactory mongoDbFactory ,
234
+ ReactiveMongoDatabaseFactory MONGO_DATABASE_FACTORY ,
218
235
MappingContext <? extends MongoPersistentEntity <?>, MongoPersistentProperty > mappingContext ) {
219
236
220
237
super (NoOpDbRefResolver .INSTANCE , mappingContext );
@@ -232,7 +249,7 @@ public <S> S read(Class<S> clazz, Bson source) {
232
249
233
250
}
234
251
235
- public static class TestCollectionCallback implements MessageCollectionCallback <Long > {
252
+ class TestCollectionCallback implements MessageCollectionCallback <Long > {
236
253
237
254
@ Override
238
255
public Long doInCollection (MongoCollection <Document > collection , Message <?> message )
@@ -242,5 +259,4 @@ public Long doInCollection(MongoCollection<Document> collection, Message<?> mess
242
259
}
243
260
244
261
}
245
-
246
262
}
0 commit comments