16
16
package org .springframework .session .data .mongo .config .annotation .web .reactive ;
17
17
18
18
import static org .assertj .core .api .Assertions .*;
19
- import static org .mockito .Mockito .*;
19
+ import static org .mockito .BDDMockito .*;
20
+ import static org .mockito .Mockito .any ;
21
+ import static org .mockito .Mockito .mock ;
20
22
21
23
import java .lang .reflect .Field ;
24
+ import java .util .Collections ;
22
25
26
+ import org .junit .After ;
23
27
import org .junit .Test ;
24
28
import org .springframework .beans .factory .UnsatisfiedDependencyException ;
25
29
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
26
30
import org .springframework .context .annotation .Bean ;
31
+ import org .springframework .data .mongodb .core .MongoOperations ;
27
32
import org .springframework .data .mongodb .core .ReactiveMongoOperations ;
33
+ import org .springframework .data .mongodb .core .index .IndexOperations ;
28
34
import org .springframework .session .EnableSpringWebSession ;
29
35
import org .springframework .session .ReactorSessionRepository ;
30
36
import org .springframework .session .data .mongo .AbstractMongoSessionConverter ;
42
48
*/
43
49
public class ReactiveMongoWebSessionConfigurationTest {
44
50
51
+ private AnnotationConfigApplicationContext context ;
52
+
53
+ @ After
54
+ public void tearDown () {
55
+
56
+ if (this .context != null ) {
57
+ this .context .close ();
58
+ }
59
+ }
60
+
45
61
@ Test
46
62
public void enableSpringWebSessionConfiguresThings () {
47
63
48
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
49
- ctx .register (GoodConfig .class );
50
- ctx .refresh ();
64
+ this . context = new AnnotationConfigApplicationContext ();
65
+ this . context .register (GoodConfig .class );
66
+ this . context .refresh ();
51
67
52
- WebSessionManager webSessionManagerFoundByType = ctx .getBean (WebSessionManager .class );
53
- Object webSessionManagerFoundByName = ctx .getBean (WebHttpHandlerBuilder .WEB_SESSION_MANAGER_BEAN_NAME );
68
+ WebSessionManager webSessionManagerFoundByType = this . context .getBean (WebSessionManager .class );
69
+ Object webSessionManagerFoundByName = this . context .getBean (WebHttpHandlerBuilder .WEB_SESSION_MANAGER_BEAN_NAME );
54
70
55
71
assertThat (webSessionManagerFoundByType ).isNotNull ();
56
72
assertThat (webSessionManagerFoundByName ).isNotNull ();
57
73
assertThat (webSessionManagerFoundByType ).isEqualTo (webSessionManagerFoundByName );
58
74
59
- assertThat (ctx .getBean (ReactorSessionRepository .class )).isNotNull ();
75
+ assertThat (this . context .getBean (ReactorSessionRepository .class )).isNotNull ();
60
76
}
61
77
62
78
@ Test
63
79
public void missingReactorSessionRepositoryBreaksAppContext () {
64
80
65
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
66
- ctx .register (BadConfig .class );
81
+ this . context = new AnnotationConfigApplicationContext ();
82
+ this . context .register (BadConfig .class );
67
83
68
84
assertThatExceptionOfType (UnsatisfiedDependencyException .class )
69
- .isThrownBy (ctx ::refresh )
85
+ .isThrownBy (this . context ::refresh )
70
86
.withMessageContaining ("Error creating bean with name 'webSessionManager'" )
71
87
.withMessageContaining ("No qualifying bean of type '" + ReactiveMongoOperations .class .getCanonicalName ());
72
88
}
73
89
74
90
@ Test
75
91
public void defaultSessionConverterShouldBeJdkWhenOnClasspath () throws IllegalAccessException {
76
92
77
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
78
- ctx .register (GoodConfig .class );
79
- ctx .refresh ();
93
+ this . context = new AnnotationConfigApplicationContext ();
94
+ this . context .register (GoodConfig .class );
95
+ this . context .refresh ();
80
96
81
- ReactiveMongoOperationsSessionRepository repository = ctx .getBean (ReactiveMongoOperationsSessionRepository .class );
97
+ ReactiveMongoOperationsSessionRepository repository = this . context .getBean (ReactiveMongoOperationsSessionRepository .class );
82
98
83
99
AbstractMongoSessionConverter converter = findMongoSessionConverter (repository );
84
100
@@ -90,11 +106,11 @@ public void defaultSessionConverterShouldBeJdkWhenOnClasspath() throws IllegalAc
90
106
@ Test
91
107
public void overridingMongoSessionConverterWithBeanShouldWork () throws IllegalAccessException {
92
108
93
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
94
- ctx .register (OverrideSessionConverterConfig .class );
95
- ctx .refresh ();
109
+ this . context = new AnnotationConfigApplicationContext ();
110
+ this . context .register (OverrideSessionConverterConfig .class );
111
+ this . context .refresh ();
96
112
97
- ReactiveMongoOperationsSessionRepository repository = ctx .getBean (ReactiveMongoOperationsSessionRepository .class );
113
+ ReactiveMongoOperationsSessionRepository repository = this . context .getBean (ReactiveMongoOperationsSessionRepository .class );
98
114
99
115
AbstractMongoSessionConverter converter = findMongoSessionConverter (repository );
100
116
@@ -106,11 +122,11 @@ public void overridingMongoSessionConverterWithBeanShouldWork() throws IllegalAc
106
122
@ Test
107
123
public void overridingIntervalAndCollectionNameThroughAnnotationShouldWork () throws IllegalAccessException {
108
124
109
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
110
- ctx .register (OverrideMongoParametersConfig .class );
111
- ctx .refresh ();
125
+ this . context = new AnnotationConfigApplicationContext ();
126
+ this . context .register (OverrideMongoParametersConfig .class );
127
+ this . context .refresh ();
112
128
113
- ReactiveMongoOperationsSessionRepository repository = ctx .getBean (ReactiveMongoOperationsSessionRepository .class );
129
+ ReactiveMongoOperationsSessionRepository repository = this . context .getBean (ReactiveMongoOperationsSessionRepository .class );
114
130
115
131
Field inactiveField = ReflectionUtils .findField (ReactiveMongoOperationsSessionRepository .class , "maxInactiveIntervalInSeconds" );
116
132
ReflectionUtils .makeAccessible (inactiveField );
@@ -124,6 +140,21 @@ public void overridingIntervalAndCollectionNameThroughAnnotationShouldWork() thr
124
140
assertThat (collectionName ).isEqualTo ("test-case" );
125
141
}
126
142
143
+ @ Test
144
+ public void reactiveAndBlockingMongoOperationsShouldEnsureIndexing () {
145
+
146
+ this .context = new AnnotationConfigApplicationContext ();
147
+ this .context .register (ConfigWithReactiveAndImperativeMongoOperations .class );
148
+ this .context .refresh ();
149
+
150
+ MongoOperations operations = this .context .getBean (MongoOperations .class );
151
+ IndexOperations indexOperations = this .context .getBean (IndexOperations .class );
152
+
153
+ verify (operations , times (1 )).indexOps ((String ) any ());
154
+ verify (indexOperations , times (1 )).getIndexInfo ();
155
+ verify (indexOperations , times (1 )).ensureIndex (any ());
156
+ }
157
+
127
158
/**
128
159
* Reflectively extract the {@link AbstractMongoSessionConverter} from the {@link ReactiveMongoOperationsSessionRepository}.
129
160
* This is to avoid expanding the surface area of the API.
@@ -184,4 +215,29 @@ ReactiveMongoOperations operations() {
184
215
return mock (ReactiveMongoOperations .class );
185
216
}
186
217
}
218
+
219
+ @ EnableMongoWebSession
220
+ static class ConfigWithReactiveAndImperativeMongoOperations {
221
+
222
+ @ Bean
223
+ ReactiveMongoOperations reactiveMongoOperations () {
224
+ return mock (ReactiveMongoOperations .class );
225
+ }
226
+
227
+ @ Bean
228
+ IndexOperations indexOperations () {
229
+
230
+ IndexOperations indexOperations = mock (IndexOperations .class );
231
+ given (indexOperations .getIndexInfo ()).willReturn (Collections .emptyList ());
232
+ return indexOperations ;
233
+ }
234
+
235
+ @ Bean
236
+ MongoOperations mongoOperations (IndexOperations indexOperations ) {
237
+
238
+ MongoOperations mongoOperations = mock (MongoOperations .class );
239
+ given (mongoOperations .indexOps ((String ) any ())).willReturn (indexOperations );
240
+ return mongoOperations ;
241
+ }
242
+ }
187
243
}
0 commit comments