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

Commit 51ca7f1

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 51ca7f1

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-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

+43-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,35 @@ 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 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);
131164
}
132165

133166
@Test
@@ -138,7 +171,7 @@ public void shouldGetSession() {
138171
Document sessionDocument = new Document();
139172

140173
given(this.mongoOperations.findById(sessionId, Document.class,
141-
ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
174+
DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
142175

143176
MongoSession session = new MongoSession();
144177

@@ -160,9 +193,9 @@ public void shouldHandleExpiredSession() {
160193
Document sessionDocument = new Document();
161194

162195
given(this.mongoOperations.findById(sessionId, Document.class,
163-
ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
196+
DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
164197

165-
given(this.mongoOperations.remove(sessionDocument, ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME))
198+
given(this.mongoOperations.remove(sessionDocument, DEFAULT_COLLECTION_NAME))
166199
.willReturn(Mono.just(DeleteResult.acknowledged(1)));
167200

168201
MongoSession session = mock(MongoSession.class);
@@ -177,8 +210,7 @@ public void shouldHandleExpiredSession() {
177210
.verifyComplete();
178211

179212
// 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));
182214
}
183215

184216
@Test
@@ -189,7 +221,7 @@ public void shouldDeleteSession() {
189221
Document sessionDocument = new Document();
190222

191223
given(this.mongoOperations.findById(sessionId, Document.class,
192-
ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
224+
DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
193225

194226
given(this.mongoOperations.remove(sessionDocument, "sessions"))
195227
.willReturn(Mono.just(DeleteResult.acknowledged(1)));
@@ -204,9 +236,7 @@ public void shouldDeleteSession() {
204236
.as(StepVerifier::create)
205237
.verifyComplete();
206238

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));
210240
verify(this.eventPublisher).publishEvent(any(SessionDeletedEvent.class));
211241
}
212242

@@ -225,5 +255,8 @@ public void shouldInvokeMethodToCreateIndexesImperatively() {
225255
// then
226256
verify(this.blockingMongoOperations, times(1)).indexOps((String) any());
227257
verify(this.converter, times(1)).ensureIndexes(indexOperations);
258+
259+
verifyNoMoreInteractions(this.blockingMongoOperations);
260+
verifyNoMoreInteractions(this.converter);
228261
}
229262
}

0 commit comments

Comments
 (0)