Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit e48ad3b

Browse files
committed
Align Spring Session MongoDB with WebSession API.
Only perform a save after verifying the MongoSession already exists in MongoDB. Otherwise, throw an IllegalStateException. Resolved #47.
1 parent b04a030 commit e48ad3b

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

src/main/java/org/springframework/session/data/mongo/ReactiveMongoOperationsSessionRepository.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ public Mono<MongoSession> createSession() {
107107
@Override
108108
public Mono<Void> save(MongoSession session) {
109109

110-
return this.mongoOperations
111-
.save(convertToDBObject(this.mongoSessionConverter, session), this.collectionName)
110+
return findSession(session.getId())
111+
.map(document -> this.mongoOperations.save(convertToDBObject(this.mongoSessionConverter, session), this.collectionName))
112+
.switchIfEmpty(Mono.error(new IllegalStateException("Session was invalidated")))
112113
.then();
113114
}
114115

src/test/java/org/springframework/session/data/mongo/ReactiveMongoOperationsSessionRepositoryTest.java

+35-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.mockito.BDDMockito.mock;
2424
import static org.mockito.BDDMockito.times;
2525
import static org.mockito.Mockito.verify;
26+
import static org.springframework.session.data.mongo.ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME;
2627

2728
import java.util.UUID;
2829

@@ -114,8 +115,12 @@ public void shouldSaveSession() {
114115

115116
// given
116117
MongoSession session = new MongoSession();
118+
Document sessionDocument = new Document();
117119
BasicDBObject dbSession = new BasicDBObject();
118120

121+
given(this.mongoOperations.findById(session.getId(), Document.class,
122+
DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
123+
119124
given(this.converter.convert(session,
120125
TypeDescriptor.valueOf(MongoSession.class),
121126
TypeDescriptor.valueOf(DBObject.class))).willReturn(dbSession);
@@ -127,7 +132,27 @@ public void shouldSaveSession() {
127132
.as(StepVerifier::create)
128133
.verifyComplete();
129134

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 shouldCreateAnErrorWhenSavingSessionNotInMongo() {
142+
143+
// given
144+
MongoSession session = new MongoSession();
145+
146+
given(this.mongoOperations.findById(session.getId(), Document.class,
147+
DEFAULT_COLLECTION_NAME)).willReturn(Mono.empty());
148+
149+
// when
150+
this.repository.save(session)
151+
.as(StepVerifier::create)
152+
.verifyErrorMessage("Session was invalidated");
153+
154+
verify(this.mongoOperations).findById(session.getId(), Document.class, DEFAULT_COLLECTION_NAME);
155+
verifyNoMoreInteractions(this.mongoOperations);
131156
}
132157

133158
@Test
@@ -138,7 +163,7 @@ public void shouldGetSession() {
138163
Document sessionDocument = new Document();
139164

140165
given(this.mongoOperations.findById(sessionId, Document.class,
141-
ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
166+
DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
142167

143168
MongoSession session = new MongoSession();
144169

@@ -160,9 +185,9 @@ public void shouldHandleExpiredSession() {
160185
Document sessionDocument = new Document();
161186

162187
given(this.mongoOperations.findById(sessionId, Document.class,
163-
ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
188+
DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
164189

165-
given(this.mongoOperations.remove(sessionDocument, ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME))
190+
given(this.mongoOperations.remove(sessionDocument, DEFAULT_COLLECTION_NAME))
166191
.willReturn(Mono.just(DeleteResult.acknowledged(1)));
167192

168193
MongoSession session = mock(MongoSession.class);
@@ -177,8 +202,7 @@ public void shouldHandleExpiredSession() {
177202
.verifyComplete();
178203

179204
// then
180-
verify(this.mongoOperations).remove(any(Document.class),
181-
eq(ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME));
205+
verify(this.mongoOperations).remove(any(Document.class), eq(DEFAULT_COLLECTION_NAME));
182206
}
183207

184208
@Test
@@ -189,7 +213,7 @@ public void shouldDeleteSession() {
189213
Document sessionDocument = new Document();
190214

191215
given(this.mongoOperations.findById(sessionId, Document.class,
192-
ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
216+
DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
193217

194218
given(this.mongoOperations.remove(sessionDocument, "sessions"))
195219
.willReturn(Mono.just(DeleteResult.acknowledged(1)));
@@ -204,9 +228,7 @@ public void shouldDeleteSession() {
204228
.as(StepVerifier::create)
205229
.verifyComplete();
206230

207-
verify(this.mongoOperations).remove(any(Document.class),
208-
eq(ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME));
209-
231+
verify(this.mongoOperations).remove(any(Document.class), eq(DEFAULT_COLLECTION_NAME));
210232
verify(this.eventPublisher).publishEvent(any(SessionDeletedEvent.class));
211233
}
212234

@@ -225,5 +247,8 @@ public void shouldInvokeMethodToCreateIndexesImperatively() {
225247
// then
226248
verify(this.blockingMongoOperations, times(1)).indexOps((String) any());
227249
verify(this.converter, times(1)).ensureIndexes(indexOperations);
250+
251+
verifyNoMoreInteractions(this.blockingMongoOperations);
252+
verifyNoMoreInteractions(this.converter);
228253
}
229254
}

0 commit comments

Comments
 (0)