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
@@ -114,8 +115,12 @@ public void shouldSaveSession() {
114
115
115
116
// given
116
117
MongoSession session = new MongoSession ();
118
+ Document sessionDocument = new Document ();
117
119
BasicDBObject dbSession = new BasicDBObject ();
118
120
121
+ given (this .mongoOperations .findById (session .getId (), Document .class ,
122
+ DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
123
+
119
124
given (this .converter .convert (session ,
120
125
TypeDescriptor .valueOf (MongoSession .class ),
121
126
TypeDescriptor .valueOf (DBObject .class ))).willReturn (dbSession );
@@ -127,7 +132,35 @@ public void shouldSaveSession() {
127
132
.as (StepVerifier ::create )
128
133
.verifyComplete ();
129
134
130
- verify (this .mongoOperations ).save (dbSession , ReactiveMongoOperationsSessionRepository .DEFAULT_COLLECTION_NAME );
135
+ verify (this .mongoOperations ).findById (session .getId (), Document .class , DEFAULT_COLLECTION_NAME );
136
+ verify (this .mongoOperations ).save (dbSession , DEFAULT_COLLECTION_NAME );
137
+ verifyNoMoreInteractions (this .mongoOperations );
138
+ }
139
+
140
+ @ Test
141
+ public void shouldFailSaveWhenSessionDoesntExist () {
142
+
143
+ // given
144
+ MongoSession session = new MongoSession ();
145
+ Document sessionDocument = new Document ();
146
+ BasicDBObject dbSession = new BasicDBObject ();
147
+
148
+ given (this .mongoOperations .findById (session .getId (), Document .class ,
149
+ DEFAULT_COLLECTION_NAME )).willReturn (Mono .empty ());
150
+
151
+ given (this .converter .convert (session ,
152
+ TypeDescriptor .valueOf (MongoSession .class ),
153
+ TypeDescriptor .valueOf (DBObject .class ))).willReturn (dbSession );
154
+
155
+ given (this .mongoOperations .save (dbSession , "sessions" )).willReturn (Mono .just (dbSession ));
156
+
157
+ // when
158
+ this .repository .save (session )
159
+ .as (StepVerifier ::create )
160
+ .verifyErrorMessage ("Session was invalidated" );
161
+
162
+ verify (this .mongoOperations ).findById (session .getId (), Document .class , DEFAULT_COLLECTION_NAME );
163
+ verifyNoMoreInteractions (this .mongoOperations );
131
164
}
132
165
133
166
@ Test
@@ -138,7 +171,7 @@ public void shouldGetSession() {
138
171
Document sessionDocument = new Document ();
139
172
140
173
given (this .mongoOperations .findById (sessionId , Document .class ,
141
- ReactiveMongoOperationsSessionRepository . DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
174
+ DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
142
175
143
176
MongoSession session = new MongoSession ();
144
177
@@ -160,9 +193,9 @@ public void shouldHandleExpiredSession() {
160
193
Document sessionDocument = new Document ();
161
194
162
195
given (this .mongoOperations .findById (sessionId , Document .class ,
163
- ReactiveMongoOperationsSessionRepository . DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
196
+ DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
164
197
165
- given (this .mongoOperations .remove (sessionDocument , ReactiveMongoOperationsSessionRepository . DEFAULT_COLLECTION_NAME ))
198
+ given (this .mongoOperations .remove (sessionDocument , DEFAULT_COLLECTION_NAME ))
166
199
.willReturn (Mono .just (DeleteResult .acknowledged (1 )));
167
200
168
201
MongoSession session = mock (MongoSession .class );
@@ -177,8 +210,7 @@ public void shouldHandleExpiredSession() {
177
210
.verifyComplete ();
178
211
179
212
// then
180
- verify (this .mongoOperations ).remove (any (Document .class ),
181
- eq (ReactiveMongoOperationsSessionRepository .DEFAULT_COLLECTION_NAME ));
213
+ verify (this .mongoOperations ).remove (any (Document .class ), eq (DEFAULT_COLLECTION_NAME ));
182
214
}
183
215
184
216
@ Test
@@ -189,7 +221,7 @@ public void shouldDeleteSession() {
189
221
Document sessionDocument = new Document ();
190
222
191
223
given (this .mongoOperations .findById (sessionId , Document .class ,
192
- ReactiveMongoOperationsSessionRepository . DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
224
+ DEFAULT_COLLECTION_NAME )).willReturn (Mono .just (sessionDocument ));
193
225
194
226
given (this .mongoOperations .remove (sessionDocument , "sessions" ))
195
227
.willReturn (Mono .just (DeleteResult .acknowledged (1 )));
@@ -204,9 +236,7 @@ public void shouldDeleteSession() {
204
236
.as (StepVerifier ::create )
205
237
.verifyComplete ();
206
238
207
- verify (this .mongoOperations ).remove (any (Document .class ),
208
- eq (ReactiveMongoOperationsSessionRepository .DEFAULT_COLLECTION_NAME ));
209
-
239
+ verify (this .mongoOperations ).remove (any (Document .class ), eq (DEFAULT_COLLECTION_NAME ));
210
240
verify (this .eventPublisher ).publishEvent (any (SessionDeletedEvent .class ));
211
241
}
212
242
@@ -225,5 +255,8 @@ public void shouldInvokeMethodToCreateIndexesImperatively() {
225
255
// then
226
256
verify (this .blockingMongoOperations , times (1 )).indexOps ((String ) any ());
227
257
verify (this .converter , times (1 )).ensureIndexes (indexOperations );
258
+
259
+ verifyNoMoreInteractions (this .blockingMongoOperations );
260
+ verifyNoMoreInteractions (this .converter );
228
261
}
229
262
}
0 commit comments