1
1
/*
2
- * Copyright 2012-2020 the original author or authors.
2
+ * Copyright 2012-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.
16
16
17
17
package org .springframework .boot .autoconfigure .session ;
18
18
19
+ import java .time .Duration ;
20
+
19
21
import org .junit .jupiter .api .Test ;
22
+ import org .testcontainers .containers .MongoDBContainer ;
23
+ import org .testcontainers .junit .jupiter .Container ;
24
+ import org .testcontainers .junit .jupiter .Testcontainers ;
20
25
21
26
import org .springframework .boot .autoconfigure .AutoConfigurations ;
22
27
import org .springframework .boot .autoconfigure .data .mongo .MongoDataAutoConfiguration ;
23
28
import org .springframework .boot .autoconfigure .data .mongo .MongoReactiveDataAutoConfiguration ;
24
29
import org .springframework .boot .autoconfigure .mongo .MongoAutoConfiguration ;
25
30
import org .springframework .boot .autoconfigure .mongo .MongoReactiveAutoConfiguration ;
26
- import org .springframework .boot .autoconfigure .mongo .embedded .EmbeddedMongoAutoConfiguration ;
27
31
import org .springframework .boot .test .context .FilteredClassLoader ;
28
32
import org .springframework .boot .test .context .assertj .AssertableReactiveWebApplicationContext ;
29
33
import org .springframework .boot .test .context .runner .ContextConsumer ;
30
34
import org .springframework .boot .test .context .runner .ReactiveWebApplicationContextRunner ;
35
+ import org .springframework .boot .testsupport .testcontainers .DockerImageNames ;
31
36
import org .springframework .session .data .mongo .ReactiveMongoSessionRepository ;
32
37
import org .springframework .session .data .redis .ReactiveRedisSessionRepository ;
33
38
38
43
*
39
44
* @author Andy Wilkinson
40
45
*/
46
+ @ Testcontainers
41
47
class ReactiveSessionAutoConfigurationMongoTests extends AbstractSessionAutoConfigurationTests {
42
48
49
+ @ Container
50
+ static final MongoDBContainer mongoDb = new MongoDBContainer (DockerImageNames .mongo ()).withStartupAttempts (5 )
51
+ .withStartupTimeout (Duration .ofMinutes (5 ));
52
+
43
53
private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner ()
44
54
.withConfiguration (AutoConfigurations .of (SessionAutoConfiguration .class ));
45
55
46
56
@ Test
47
57
void defaultConfig () {
48
- this .contextRunner .withPropertyValues ("spring.session.store-type=mongodb" )
49
- .withConfiguration (AutoConfigurations .of (EmbeddedMongoAutoConfiguration .class ,
50
- MongoAutoConfiguration .class , MongoDataAutoConfiguration .class ,
58
+ this .contextRunner
59
+ .withPropertyValues ("spring.session.store-type=mongodb" ,
60
+ "spring.data.mongodb.uri=" + mongoDb .getReplicaSetUrl ())
61
+ .withConfiguration (AutoConfigurations .of (MongoAutoConfiguration .class , MongoDataAutoConfiguration .class ,
51
62
MongoReactiveAutoConfiguration .class , MongoReactiveDataAutoConfiguration .class ))
52
63
.run (validateSpringSessionUsesMongo ("sessions" ));
53
64
}
54
65
55
66
@ Test
56
67
void defaultConfigWithUniqueStoreImplementation () {
57
68
this .contextRunner .withClassLoader (new FilteredClassLoader (ReactiveRedisSessionRepository .class ))
58
- .withConfiguration ( AutoConfigurations . of ( EmbeddedMongoAutoConfiguration . class ,
59
- MongoAutoConfiguration .class , MongoDataAutoConfiguration .class ,
69
+ .withPropertyValues ( "spring.data.mongodb.uri=" + mongoDb . getReplicaSetUrl ())
70
+ . withConfiguration ( AutoConfigurations . of ( MongoAutoConfiguration .class , MongoDataAutoConfiguration .class ,
60
71
MongoReactiveAutoConfiguration .class , MongoReactiveDataAutoConfiguration .class ))
61
72
.run (validateSpringSessionUsesMongo ("sessions" ));
62
73
}
63
74
64
75
@ Test
65
76
void defaultConfigWithCustomTimeout () {
66
- this .contextRunner .withPropertyValues ("spring.session.store-type=mongodb" , "spring.session.timeout=1m" )
67
- .withConfiguration (AutoConfigurations .of (EmbeddedMongoAutoConfiguration .class ,
68
- MongoAutoConfiguration .class , MongoDataAutoConfiguration .class ,
77
+ this .contextRunner
78
+ .withPropertyValues ("spring.session.store-type=mongodb" , "spring.session.timeout=1m" ,
79
+ "spring.data.mongodb.uri=" + mongoDb .getReplicaSetUrl ())
80
+ .withConfiguration (AutoConfigurations .of (MongoAutoConfiguration .class , MongoDataAutoConfiguration .class ,
69
81
MongoReactiveAutoConfiguration .class , MongoReactiveDataAutoConfiguration .class ))
70
82
.run ((context ) -> {
71
83
ReactiveMongoSessionRepository repository = validateSessionRepository (context ,
@@ -77,10 +89,10 @@ void defaultConfigWithCustomTimeout() {
77
89
@ Test
78
90
void mongoSessionStoreWithCustomizations () {
79
91
this .contextRunner
80
- .withConfiguration (AutoConfigurations .of (EmbeddedMongoAutoConfiguration .class ,
81
- MongoAutoConfiguration .class , MongoDataAutoConfiguration .class ,
92
+ .withConfiguration (AutoConfigurations .of (MongoAutoConfiguration .class , MongoDataAutoConfiguration .class ,
82
93
MongoReactiveAutoConfiguration .class , MongoReactiveDataAutoConfiguration .class ))
83
- .withPropertyValues ("spring.session.store-type=mongodb" , "spring.session.mongodb.collection-name=foo" )
94
+ .withPropertyValues ("spring.session.store-type=mongodb" , "spring.session.mongodb.collection-name=foo" ,
95
+ "spring.data.mongodb.uri=" + mongoDb .getReplicaSetUrl ())
84
96
.run (validateSpringSessionUsesMongo ("foo" ));
85
97
}
86
98
0 commit comments