23
23
import static org .mockito .BDDMockito .mock ;
24
24
import static org .mockito .BDDMockito .times ;
25
25
import static org .mockito .Mockito .verify ;
26
+ import static org .springframework .session .data .mongo .ReactiveMongoOperationsSessionRepository .DEFAULT_COLLECTION_NAME ;
26
27
27
28
import java .util .UUID ;
28
29
@@ -84,6 +85,7 @@ public void shouldCreateSession() {
84
85
.as (StepVerifier ::create )
85
86
.expectNextMatches (mongoSession -> {
86
87
assertThat (mongoSession .getId ()).isNotEmpty ();
88
+ assertThat (mongoSession .isNew ()).isTrue ();
87
89
assertThat (mongoSession .getMaxInactiveInterval ().getSeconds ())
88
90
.isEqualTo (ReactiveMongoOperationsSessionRepository .DEFAULT_INACTIVE_INTERVAL );
89
91
return true ;
@@ -102,6 +104,7 @@ public void shouldCreateSessionWhenMaxInactiveIntervalNotDefined() {
102
104
.as (StepVerifier ::create )
103
105
.expectNextMatches (mongoSession -> {
104
106
assertThat (mongoSession .getId ()).isNotEmpty ();
107
+ assertThat (mongoSession .isNew ()).isTrue ();
105
108
assertThat (mongoSession .getMaxInactiveInterval ().getSeconds ())
106
109
.isEqualTo (ReactiveMongoOperationsSessionRepository .DEFAULT_INACTIVE_INTERVAL );
107
110
return true ;
@@ -114,8 +117,12 @@ public void shouldSaveSession() {
114
117
115
118
// given
116
119
MongoSession session = new MongoSession ();
120
+ Document sessionDocument = new Document ();
117
121
BasicDBObject dbSession = new BasicDBObject ();
118
122
123
+ given (this .mongoOperations .findById (session .getId (), Document .class ,
124
+ DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
125
+
119
126
given (this .converter .convert (session ,
120
127
TypeDescriptor .valueOf (MongoSession .class ),
121
128
TypeDescriptor .valueOf (DBObject .class ))).willReturn (dbSession );
@@ -127,7 +134,30 @@ public void shouldSaveSession() {
127
134
.as (StepVerifier ::create )
128
135
.verifyComplete ();
129
136
130
- verify (this .mongoOperations ).save (dbSession , ReactiveMongoOperationsSessionRepository .DEFAULT_COLLECTION_NAME );
137
+ assertThat (session .isNew ()).isFalse ();
138
+ verify (this .mongoOperations ).save (dbSession , DEFAULT_COLLECTION_NAME );
139
+ verifyNoMoreInteractions (this .mongoOperations );
140
+ }
141
+
142
+ @ Test
143
+ public void shouldCreateAnErrorWhenSavingSessionNotInMongo () {
144
+
145
+ // given
146
+ MongoSession session = new MongoSession ();
147
+ session .setNew (false );
148
+
149
+ given (this .mongoOperations .findById (session .getId (), Document .class ,
150
+ DEFAULT_COLLECTION_NAME )).willReturn (Mono .empty ());
151
+
152
+ // when
153
+ this .repository .save (session )
154
+ .as (StepVerifier ::create )
155
+ .verifyErrorMessage ("Session was invalidated" );
156
+
157
+ assertThat (session .isNew ()).isFalse ();
158
+
159
+ verify (this .mongoOperations ).findById (session .getId (), Document .class , DEFAULT_COLLECTION_NAME );
160
+ verifyNoMoreInteractions (this .mongoOperations );
131
161
}
132
162
133
163
@ Test
@@ -138,7 +168,7 @@ public void shouldGetSession() {
138
168
Document sessionDocument = new Document ();
139
169
140
170
given (this .mongoOperations .findById (sessionId , Document .class ,
141
- ReactiveMongoOperationsSessionRepository . DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
171
+ DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
142
172
143
173
MongoSession session = new MongoSession ();
144
174
@@ -160,9 +190,9 @@ public void shouldHandleExpiredSession() {
160
190
Document sessionDocument = new Document ();
161
191
162
192
given (this .mongoOperations .findById (sessionId , Document .class ,
163
- ReactiveMongoOperationsSessionRepository . DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
193
+ DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
164
194
165
- given (this .mongoOperations .remove (sessionDocument , ReactiveMongoOperationsSessionRepository . DEFAULT_COLLECTION_NAME ))
195
+ given (this .mongoOperations .remove (sessionDocument , DEFAULT_COLLECTION_NAME ))
166
196
.willReturn (Mono .just (DeleteResult .acknowledged (1 )));
167
197
168
198
MongoSession session = mock (MongoSession .class );
@@ -177,8 +207,7 @@ public void shouldHandleExpiredSession() {
177
207
.verifyComplete ();
178
208
179
209
// then
180
- verify (this .mongoOperations ).remove (any (Document .class ),
181
- eq (ReactiveMongoOperationsSessionRepository .DEFAULT_COLLECTION_NAME ));
210
+ verify (this .mongoOperations ).remove (any (Document .class ), eq (DEFAULT_COLLECTION_NAME ));
182
211
}
183
212
184
213
@ Test
@@ -189,7 +218,7 @@ public void shouldDeleteSession() {
189
218
Document sessionDocument = new Document ();
190
219
191
220
given (this .mongoOperations .findById (sessionId , Document .class ,
192
- ReactiveMongoOperationsSessionRepository . DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
221
+ DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
193
222
194
223
given (this .mongoOperations .remove (sessionDocument , "sessions" ))
195
224
.willReturn (Mono .just (DeleteResult .acknowledged (1 )));
@@ -204,9 +233,7 @@ public void shouldDeleteSession() {
204
233
.as (StepVerifier ::create )
205
234
.verifyComplete ();
206
235
207
- verify (this .mongoOperations ).remove (any (Document .class ),
208
- eq (ReactiveMongoOperationsSessionRepository .DEFAULT_COLLECTION_NAME ));
209
-
236
+ verify (this .mongoOperations ).remove (any (Document .class ), eq (DEFAULT_COLLECTION_NAME ));
210
237
verify (this .eventPublisher ).publishEvent (any (SessionDeletedEvent .class ));
211
238
}
212
239
@@ -225,5 +252,8 @@ public void shouldInvokeMethodToCreateIndexesImperatively() {
225
252
// then
226
253
verify (this .blockingMongoOperations , times (1 )).indexOps ((String ) any ());
227
254
verify (this .converter , times (1 )).ensureIndexes (indexOperations );
255
+
256
+ verifyNoMoreInteractions (this .blockingMongoOperations );
257
+ verifyNoMoreInteractions (this .converter );
228
258
}
229
259
}
0 commit comments