Merge branch '3.1.x'
Closes gh-2530
This commit is contained in:
@@ -48,7 +48,7 @@ public abstract class AbstractClassLoaderTest<T> extends AbstractITest {
|
||||
"mongoSessionConverter");
|
||||
ReflectionUtils.makeAccessible(mongoSessionConverterField);
|
||||
AbstractMongoSessionConverter sessionConverter = (AbstractMongoSessionConverter) ReflectionUtils
|
||||
.getField(mongoSessionConverterField, this.sessionRepository);
|
||||
.getField(mongoSessionConverterField, this.sessionRepository);
|
||||
|
||||
AssertionsForClassTypes.assertThat(sessionConverter).isInstanceOf(JdkMongoSessionConverter.class);
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ public abstract class AbstractMongoRepositoryITest extends AbstractITest {
|
||||
assertThat(session.getId()).isEqualTo(toSave.getId());
|
||||
assertThat(session.getAttributeNames()).isEqualTo(toSave.getAttributeNames());
|
||||
assertThat(session.<String>getAttribute(expectedAttributeName))
|
||||
.isEqualTo(toSave.getAttribute(expectedAttributeName));
|
||||
.isEqualTo(toSave.getAttribute(expectedAttributeName));
|
||||
|
||||
this.repository.deleteById(toSave.getId());
|
||||
|
||||
@@ -400,7 +400,7 @@ public abstract class AbstractMongoRepositoryITest extends AbstractITest {
|
||||
public MongoOperations mongoOperations(MongoDBContainer mongoContainer) {
|
||||
|
||||
MongoClient mongo = MongoClients
|
||||
.create("mongodb://" + mongoContainer.getHost() + ":" + mongoContainer.getFirstMappedPort());
|
||||
.create("mongodb://" + mongoContainer.getHost() + ":" + mongoContainer.getFirstMappedPort());
|
||||
return new MongoTemplate(mongo, "test");
|
||||
}
|
||||
|
||||
|
||||
@@ -192,7 +192,7 @@ public class MongoDbDeleteJacksonSessionVerificationTest {
|
||||
ReactiveMongoOperations mongoOperations(MongoDBContainer mongoContainer) {
|
||||
|
||||
MongoClient mongo = MongoClients
|
||||
.create("mongodb://" + mongoContainer.getHost() + ":" + mongoContainer.getFirstMappedPort());
|
||||
.create("mongodb://" + mongoContainer.getHost() + ":" + mongoContainer.getFirstMappedPort());
|
||||
return new ReactiveMongoTemplate(mongo, "DB_Name_DeleteJacksonSessionVerificationTest");
|
||||
}
|
||||
|
||||
|
||||
@@ -74,13 +74,14 @@ public class MongoDbLogoutVerificationTest {
|
||||
|
||||
// 1. Login and capture the SESSION cookie value.
|
||||
|
||||
FluxExchangeResult<String> loginResult = this.client.post().uri("/login")
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED) //
|
||||
.body(BodyInserters //
|
||||
.fromFormData("username", "admin") //
|
||||
.with("password", "password")) //
|
||||
.exchange() //
|
||||
.returnResult(String.class);
|
||||
FluxExchangeResult<String> loginResult = this.client.post()
|
||||
.uri("/login")
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED) //
|
||||
.body(BodyInserters //
|
||||
.fromFormData("username", "admin") //
|
||||
.with("password", "password")) //
|
||||
.exchange() //
|
||||
.returnResult(String.class);
|
||||
|
||||
AssertionsForClassTypes.assertThat(loginResult.getResponseHeaders().getLocation()).isEqualTo(URI.create("/"));
|
||||
|
||||
@@ -88,42 +89,54 @@ public class MongoDbLogoutVerificationTest {
|
||||
|
||||
// 2. Fetch a protected resource using the SESSION cookie.
|
||||
|
||||
this.client.get().uri("/hello") //
|
||||
.cookie("SESSION", originalSessionId) //
|
||||
.exchange() //
|
||||
.expectStatus().isOk() //
|
||||
.returnResult(String.class).getResponseBody() //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNext("HelloWorld") //
|
||||
.verifyComplete();
|
||||
this.client.get()
|
||||
.uri("/hello") //
|
||||
.cookie("SESSION", originalSessionId) //
|
||||
.exchange() //
|
||||
.expectStatus()
|
||||
.isOk() //
|
||||
.returnResult(String.class)
|
||||
.getResponseBody() //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNext("HelloWorld") //
|
||||
.verifyComplete();
|
||||
|
||||
// 3. Logout using the SESSION cookie, and capture the new SESSION cookie.
|
||||
|
||||
String newSessionId = this.client.post().uri("/logout") //
|
||||
.cookie("SESSION", originalSessionId) //
|
||||
.exchange() //
|
||||
.expectStatus().isFound() //
|
||||
.returnResult(String.class).getResponseCookies().getFirst("SESSION").getValue();
|
||||
String newSessionId = this.client.post()
|
||||
.uri("/logout") //
|
||||
.cookie("SESSION", originalSessionId) //
|
||||
.exchange() //
|
||||
.expectStatus()
|
||||
.isFound() //
|
||||
.returnResult(String.class)
|
||||
.getResponseCookies()
|
||||
.getFirst("SESSION")
|
||||
.getValue();
|
||||
|
||||
AssertionsForClassTypes.assertThat(newSessionId).isNotEqualTo(originalSessionId);
|
||||
|
||||
// 4. Verify the new SESSION cookie is not yet authorized.
|
||||
|
||||
this.client.get().uri("/hello") //
|
||||
.cookie("SESSION", newSessionId) //
|
||||
.exchange() //
|
||||
.expectStatus().isFound() //
|
||||
.expectHeader()
|
||||
.value(HttpHeaders.LOCATION, (value) -> AssertionsForClassTypes.assertThat(value).isEqualTo("/login"));
|
||||
this.client.get()
|
||||
.uri("/hello") //
|
||||
.cookie("SESSION", newSessionId) //
|
||||
.exchange() //
|
||||
.expectStatus()
|
||||
.isFound() //
|
||||
.expectHeader()
|
||||
.value(HttpHeaders.LOCATION, (value) -> AssertionsForClassTypes.assertThat(value).isEqualTo("/login"));
|
||||
|
||||
// 5. Verify the original SESSION cookie no longer works.
|
||||
|
||||
this.client.get().uri("/hello") //
|
||||
.cookie("SESSION", originalSessionId) //
|
||||
.exchange() //
|
||||
.expectStatus().isFound() //
|
||||
.expectHeader()
|
||||
.value(HttpHeaders.LOCATION, (value) -> AssertionsForClassTypes.assertThat(value).isEqualTo("/login"));
|
||||
this.client.get()
|
||||
.uri("/hello") //
|
||||
.cookie("SESSION", originalSessionId) //
|
||||
.exchange() //
|
||||
.expectStatus()
|
||||
.isFound() //
|
||||
.expectHeader()
|
||||
.value(HttpHeaders.LOCATION, (value) -> AssertionsForClassTypes.assertThat(value).isEqualTo("/login"));
|
||||
}
|
||||
|
||||
@RestController
|
||||
@@ -182,7 +195,7 @@ public class MongoDbLogoutVerificationTest {
|
||||
ReactiveMongoOperations mongoOperations(MongoDBContainer mongoContainer) {
|
||||
|
||||
MongoClient mongo = MongoClients
|
||||
.create("mongodb://" + mongoContainer.getHost() + ":" + mongoContainer.getFirstMappedPort());
|
||||
.create("mongodb://" + mongoContainer.getHost() + ":" + mongoContainer.getFirstMappedPort());
|
||||
return new ReactiveMongoTemplate(mongo, "test");
|
||||
}
|
||||
|
||||
|
||||
@@ -86,13 +86,13 @@ public abstract class AbstractMongoSessionConverter implements GenericConverter
|
||||
LOG.info("Creating TTL index on field " + EXPIRE_AT_FIELD_NAME);
|
||||
|
||||
sessionCollectionIndexes
|
||||
.ensureIndex(new Index(EXPIRE_AT_FIELD_NAME, Sort.Direction.ASC).named(EXPIRE_AT_FIELD_NAME).expire(0));
|
||||
.ensureIndex(new Index(EXPIRE_AT_FIELD_NAME, Sort.Direction.ASC).named(EXPIRE_AT_FIELD_NAME).expire(0));
|
||||
}
|
||||
|
||||
protected String extractPrincipal(MongoSession expiringSession) {
|
||||
|
||||
return this.indexResolver.resolveIndexesFor(expiringSession)
|
||||
.get(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);
|
||||
.get(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);
|
||||
}
|
||||
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
|
||||
@@ -144,11 +144,12 @@ public class MongoIndexedSessionRepository
|
||||
public Map<String, MongoSession> findByIndexNameAndIndexValue(String indexName, String indexValue) {
|
||||
|
||||
return Optional.ofNullable(this.mongoSessionConverter.getQueryForIndex(indexName, indexValue))
|
||||
.map((query) -> this.mongoOperations.find(query, Document.class, this.collectionName))
|
||||
.orElse(Collections.emptyList()).stream()
|
||||
.map((dbSession) -> MongoSessionUtils.convertToSession(this.mongoSessionConverter, dbSession))
|
||||
.peek((session) -> session.setSessionIdGenerator(this.sessionIdGenerator))
|
||||
.collect(Collectors.toMap(MongoSession::getId, (mapSession) -> mapSession));
|
||||
.map((query) -> this.mongoOperations.find(query, Document.class, this.collectionName))
|
||||
.orElse(Collections.emptyList())
|
||||
.stream()
|
||||
.map((dbSession) -> MongoSessionUtils.convertToSession(this.mongoSessionConverter, dbSession))
|
||||
.peek((session) -> session.setSessionIdGenerator(this.sessionIdGenerator))
|
||||
.collect(Collectors.toMap(MongoSession::getId, (mapSession) -> mapSession));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -115,42 +115,42 @@ public class ReactiveMongoSessionRepository
|
||||
public Mono<Void> save(MongoSession session) {
|
||||
|
||||
return Mono //
|
||||
.justOrEmpty(MongoSessionUtils.convertToDBObject(this.mongoSessionConverter, session)) //
|
||||
.flatMap((dbObject) -> {
|
||||
if (session.hasChangedSessionId()) {
|
||||
.justOrEmpty(MongoSessionUtils.convertToDBObject(this.mongoSessionConverter, session)) //
|
||||
.flatMap((dbObject) -> {
|
||||
if (session.hasChangedSessionId()) {
|
||||
|
||||
return this.mongoOperations
|
||||
.remove(Query.query(Criteria.where("_id").is(session.getOriginalSessionId())),
|
||||
this.collectionName) //
|
||||
.then(this.mongoOperations.save(dbObject, this.collectionName));
|
||||
}
|
||||
else {
|
||||
return this.mongoOperations
|
||||
.remove(Query.query(Criteria.where("_id").is(session.getOriginalSessionId())),
|
||||
this.collectionName) //
|
||||
.then(this.mongoOperations.save(dbObject, this.collectionName));
|
||||
}
|
||||
else {
|
||||
|
||||
return this.mongoOperations.save(dbObject, this.collectionName);
|
||||
}
|
||||
}) //
|
||||
.then();
|
||||
return this.mongoOperations.save(dbObject, this.collectionName);
|
||||
}
|
||||
}) //
|
||||
.then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<MongoSession> findById(String id) {
|
||||
|
||||
return findSession(id) //
|
||||
.map((document) -> MongoSessionUtils.convertToSession(this.mongoSessionConverter, document)) //
|
||||
.filter((mongoSession) -> !mongoSession.isExpired()) //
|
||||
.doOnNext((mongoSession) -> mongoSession.setSessionIdGenerator(this.sessionIdGenerator))
|
||||
.switchIfEmpty(Mono.defer(() -> this.deleteById(id).then(Mono.empty())));
|
||||
.map((document) -> MongoSessionUtils.convertToSession(this.mongoSessionConverter, document)) //
|
||||
.filter((mongoSession) -> !mongoSession.isExpired()) //
|
||||
.doOnNext((mongoSession) -> mongoSession.setSessionIdGenerator(this.sessionIdGenerator))
|
||||
.switchIfEmpty(Mono.defer(() -> this.deleteById(id).then(Mono.empty())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> deleteById(String id) {
|
||||
|
||||
return findSession(id) //
|
||||
.flatMap((document) -> this.mongoOperations.remove(document, this.collectionName) //
|
||||
.then(Mono.just(document))) //
|
||||
.map((document) -> MongoSessionUtils.convertToSession(this.mongoSessionConverter, document)) //
|
||||
.doOnNext((mongoSession) -> publishEvent(new SessionDeletedEvent(this, mongoSession))) //
|
||||
.then();
|
||||
.flatMap((document) -> this.mongoOperations.remove(document, this.collectionName) //
|
||||
.then(Mono.just(document))) //
|
||||
.map((document) -> MongoSessionUtils.convertToSession(this.mongoSessionConverter, document)) //
|
||||
.doOnNext((mongoSession) -> publishEvent(new SessionDeletedEvent(this, mongoSession))) //
|
||||
.then();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -105,7 +105,7 @@ public class MongoHttpSessionConfiguration implements BeanClassLoaderAware, Embe
|
||||
repository.setSessionIdGenerator(this.sessionIdGenerator);
|
||||
|
||||
this.sessionRepositoryCustomizers
|
||||
.forEach((sessionRepositoryCustomizer) -> sessionRepositoryCustomizer.customize(repository));
|
||||
.forEach((sessionRepositoryCustomizer) -> sessionRepositoryCustomizer.customize(repository));
|
||||
|
||||
return repository;
|
||||
}
|
||||
@@ -126,11 +126,11 @@ public class MongoHttpSessionConfiguration implements BeanClassLoaderAware, Embe
|
||||
public void setImportMetadata(AnnotationMetadata importMetadata) {
|
||||
|
||||
AnnotationAttributes attributes = AnnotationAttributes
|
||||
.fromMap(importMetadata.getAnnotationAttributes(EnableMongoHttpSession.class.getName()));
|
||||
.fromMap(importMetadata.getAnnotationAttributes(EnableMongoHttpSession.class.getName()));
|
||||
|
||||
if (attributes != null) {
|
||||
this.maxInactiveInterval = Duration
|
||||
.ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds"));
|
||||
.ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds"));
|
||||
}
|
||||
|
||||
String collectionNameValue = (attributes != null) ? attributes.getString("collectionName") : "";
|
||||
|
||||
@@ -114,7 +114,7 @@ public class ReactiveMongoWebSessionConfiguration
|
||||
}
|
||||
|
||||
this.sessionRepositoryCustomizers
|
||||
.forEach((sessionRepositoryCustomizer) -> sessionRepositoryCustomizer.customize(repository));
|
||||
.forEach((sessionRepositoryCustomizer) -> sessionRepositoryCustomizer.customize(repository));
|
||||
|
||||
repository.setSessionIdGenerator(this.sessionIdGenerator);
|
||||
|
||||
@@ -130,11 +130,11 @@ public class ReactiveMongoWebSessionConfiguration
|
||||
public void setImportMetadata(AnnotationMetadata importMetadata) {
|
||||
|
||||
AnnotationAttributes attributes = AnnotationAttributes
|
||||
.fromMap(importMetadata.getAnnotationAttributes(EnableMongoWebSession.class.getName()));
|
||||
.fromMap(importMetadata.getAnnotationAttributes(EnableMongoWebSession.class.getName()));
|
||||
|
||||
if (attributes != null) {
|
||||
this.maxInactiveInterval = Duration
|
||||
.ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds"));
|
||||
.ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds"));
|
||||
}
|
||||
|
||||
String collectionNameValue = (attributes != null) ? attributes.getString("collectionName") : "";
|
||||
|
||||
@@ -89,7 +89,7 @@ public class JacksonMongoSessionConverterTest extends AbstractMongoSessionConver
|
||||
void shouldNotAllowNullObjectMapperToBeInjected() {
|
||||
|
||||
Assertions.assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new JacksonMongoSessionConverter((ObjectMapper) null));
|
||||
.isThrownBy(() -> new JacksonMongoSessionConverter((ObjectMapper) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -43,14 +43,14 @@ public class JdkMongoSessionConverterTest extends AbstractMongoSessionConverterT
|
||||
|
||||
@Test
|
||||
void constructorNullSerializer() {
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
() -> new JdkMongoSessionConverter(null, new DeserializingConverter(), this.inactiveInterval));
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new JdkMongoSessionConverter(null, new DeserializingConverter(), this.inactiveInterval));
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructorNullDeserializer() {
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
() -> new JdkMongoSessionConverter(new SerializingConverter(), null, this.inactiveInterval));
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new JdkMongoSessionConverter(new SerializingConverter(), null, this.inactiveInterval));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ public class MongoIndexedSessionRepositoryTest {
|
||||
// then
|
||||
assertThat(session.getId()).isNotEmpty();
|
||||
assertThat(session.getMaxInactiveInterval().getSeconds())
|
||||
.isEqualTo(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
|
||||
.isEqualTo(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -90,7 +90,8 @@ public class MongoIndexedSessionRepositoryTest {
|
||||
BasicDBObject dbSession = new BasicDBObject();
|
||||
|
||||
given(this.converter.convert(session, TypeDescriptor.valueOf(MongoSession.class),
|
||||
TypeDescriptor.valueOf(DBObject.class))).willReturn(dbSession);
|
||||
TypeDescriptor.valueOf(DBObject.class)))
|
||||
.willReturn(dbSession);
|
||||
// when
|
||||
this.repository.save(session);
|
||||
|
||||
@@ -106,12 +107,14 @@ public class MongoIndexedSessionRepositoryTest {
|
||||
Document sessionDocument = new Document();
|
||||
|
||||
given(this.mongoOperations.findById(sessionId, Document.class,
|
||||
MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(sessionDocument);
|
||||
MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME))
|
||||
.willReturn(sessionDocument);
|
||||
|
||||
MongoSession session = new MongoSession();
|
||||
|
||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
|
||||
TypeDescriptor.valueOf(MongoSession.class)))
|
||||
.willReturn(session);
|
||||
|
||||
// when
|
||||
MongoSession retrievedSession = this.repository.findById(sessionId);
|
||||
@@ -128,13 +131,15 @@ public class MongoIndexedSessionRepositoryTest {
|
||||
Document sessionDocument = new Document();
|
||||
|
||||
given(this.mongoOperations.findById(sessionId, Document.class,
|
||||
MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(sessionDocument);
|
||||
MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME))
|
||||
.willReturn(sessionDocument);
|
||||
|
||||
MongoSession session = mock(MongoSession.class);
|
||||
|
||||
given(session.isExpired()).willReturn(true);
|
||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
|
||||
TypeDescriptor.valueOf(MongoSession.class)))
|
||||
.willReturn(session);
|
||||
given(session.getId()).willReturn("sessionId");
|
||||
|
||||
// when
|
||||
@@ -157,9 +162,11 @@ public class MongoIndexedSessionRepositoryTest {
|
||||
MongoSession mongoSession = new MongoSession(sessionId, MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
|
||||
|
||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(mongoSession);
|
||||
TypeDescriptor.valueOf(MongoSession.class)))
|
||||
.willReturn(mongoSession);
|
||||
given(this.mongoOperations.findById(eq(sessionId), eq(Document.class),
|
||||
eq(MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME))).willReturn(sessionDocument);
|
||||
eq(MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME)))
|
||||
.willReturn(sessionDocument);
|
||||
|
||||
// when
|
||||
this.repository.deleteById(sessionId);
|
||||
@@ -180,14 +187,15 @@ public class MongoIndexedSessionRepositoryTest {
|
||||
given(this.converter.getQueryForIndex(anyString(), any(Object.class))).willReturn(mock(Query.class));
|
||||
given(this.mongoOperations.find(any(Query.class), eq(Document.class),
|
||||
eq(MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME)))
|
||||
.willReturn(Collections.singletonList(document));
|
||||
.willReturn(Collections.singletonList(document));
|
||||
|
||||
String sessionId = UUID.randomUUID().toString();
|
||||
|
||||
MongoSession session = new MongoSession(sessionId, 1800);
|
||||
|
||||
given(this.converter.convert(document, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
|
||||
TypeDescriptor.valueOf(MongoSession.class)))
|
||||
.willReturn(session);
|
||||
|
||||
// when
|
||||
Map<String, MongoSession> sessionsMap = this.repository.findByIndexNameAndIndexValue(principalNameIndexName,
|
||||
@@ -231,12 +239,14 @@ public class MongoIndexedSessionRepositoryTest {
|
||||
Document sessionDocument = new Document();
|
||||
|
||||
given(this.mongoOperations.findById("123", Document.class,
|
||||
MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(sessionDocument);
|
||||
MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME))
|
||||
.willReturn(sessionDocument);
|
||||
|
||||
MongoSession session = new MongoSession("123");
|
||||
|
||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
|
||||
TypeDescriptor.valueOf(MongoSession.class)))
|
||||
.willReturn(session);
|
||||
|
||||
MongoSession retrievedSession = this.repository.findById("123");
|
||||
assertThat(retrievedSession.getId()).isEqualTo("123");
|
||||
|
||||
@@ -84,14 +84,14 @@ public class ReactiveMongoSessionRepositoryTest {
|
||||
void shouldCreateSession() {
|
||||
|
||||
this.repository.createSession() //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNextMatches((mongoSession) -> {
|
||||
assertThat(mongoSession.getId()).isNotEmpty();
|
||||
assertThat(mongoSession.getMaxInactiveInterval())
|
||||
.isEqualTo(Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS));
|
||||
return true;
|
||||
}) //
|
||||
.verifyComplete();
|
||||
.as(StepVerifier::create) //
|
||||
.expectNextMatches((mongoSession) -> {
|
||||
assertThat(mongoSession.getId()).isNotEmpty();
|
||||
assertThat(mongoSession.getMaxInactiveInterval())
|
||||
.isEqualTo(Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS));
|
||||
return true;
|
||||
}) //
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -102,14 +102,15 @@ public class ReactiveMongoSessionRepositoryTest {
|
||||
BasicDBObject dbSession = new BasicDBObject();
|
||||
|
||||
given(this.converter.convert(session, TypeDescriptor.valueOf(MongoSession.class),
|
||||
TypeDescriptor.valueOf(DBObject.class))).willReturn(dbSession);
|
||||
TypeDescriptor.valueOf(DBObject.class)))
|
||||
.willReturn(dbSession);
|
||||
|
||||
given(this.mongoOperations.save(dbSession, "sessions")).willReturn(Mono.just(dbSession));
|
||||
|
||||
// when
|
||||
this.repository.save(session) //
|
||||
.as(StepVerifier::create) //
|
||||
.verifyComplete();
|
||||
.as(StepVerifier::create) //
|
||||
.verifyComplete();
|
||||
|
||||
verify(this.mongoOperations).save(dbSession, ReactiveMongoSessionRepository.DEFAULT_COLLECTION_NAME);
|
||||
}
|
||||
@@ -122,18 +123,20 @@ public class ReactiveMongoSessionRepositoryTest {
|
||||
Document sessionDocument = new Document();
|
||||
|
||||
given(this.mongoOperations.findById(sessionId, Document.class,
|
||||
ReactiveMongoSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
|
||||
ReactiveMongoSessionRepository.DEFAULT_COLLECTION_NAME))
|
||||
.willReturn(Mono.just(sessionDocument));
|
||||
|
||||
MongoSession session = new MongoSession();
|
||||
|
||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
|
||||
TypeDescriptor.valueOf(MongoSession.class)))
|
||||
.willReturn(session);
|
||||
|
||||
// when
|
||||
this.repository.findById(sessionId) //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNext(session) //
|
||||
.verifyComplete();
|
||||
.as(StepVerifier::create) //
|
||||
.expectNext(session) //
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -144,21 +147,23 @@ public class ReactiveMongoSessionRepositoryTest {
|
||||
Document sessionDocument = new Document();
|
||||
|
||||
given(this.mongoOperations.findById(sessionId, Document.class,
|
||||
ReactiveMongoSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
|
||||
ReactiveMongoSessionRepository.DEFAULT_COLLECTION_NAME))
|
||||
.willReturn(Mono.just(sessionDocument));
|
||||
|
||||
given(this.mongoOperations.remove(sessionDocument, ReactiveMongoSessionRepository.DEFAULT_COLLECTION_NAME))
|
||||
.willReturn(Mono.just(DeleteResult.acknowledged(1)));
|
||||
.willReturn(Mono.just(DeleteResult.acknowledged(1)));
|
||||
|
||||
MongoSession session = mock(MongoSession.class);
|
||||
|
||||
given(session.isExpired()).willReturn(true);
|
||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
|
||||
TypeDescriptor.valueOf(MongoSession.class)))
|
||||
.willReturn(session);
|
||||
|
||||
// when
|
||||
this.repository.findById(sessionId) //
|
||||
.as(StepVerifier::create) //
|
||||
.verifyComplete();
|
||||
.as(StepVerifier::create) //
|
||||
.verifyComplete();
|
||||
|
||||
// then
|
||||
verify(this.mongoOperations).remove(any(Document.class),
|
||||
@@ -173,20 +178,22 @@ public class ReactiveMongoSessionRepositoryTest {
|
||||
Document sessionDocument = new Document();
|
||||
|
||||
given(this.mongoOperations.findById(sessionId, Document.class,
|
||||
ReactiveMongoSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
|
||||
ReactiveMongoSessionRepository.DEFAULT_COLLECTION_NAME))
|
||||
.willReturn(Mono.just(sessionDocument));
|
||||
|
||||
given(this.mongoOperations.remove(sessionDocument, "sessions"))
|
||||
.willReturn(Mono.just(DeleteResult.acknowledged(1)));
|
||||
.willReturn(Mono.just(DeleteResult.acknowledged(1)));
|
||||
|
||||
MongoSession session = mock(MongoSession.class);
|
||||
|
||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
|
||||
TypeDescriptor.valueOf(MongoSession.class)))
|
||||
.willReturn(session);
|
||||
|
||||
// when
|
||||
this.repository.deleteById(sessionId) //
|
||||
.as(StepVerifier::create) //
|
||||
.verifyComplete();
|
||||
.as(StepVerifier::create) //
|
||||
.verifyComplete();
|
||||
|
||||
verify(this.mongoOperations).remove(any(Document.class),
|
||||
eq(ReactiveMongoSessionRepository.DEFAULT_COLLECTION_NAME));
|
||||
@@ -224,7 +231,7 @@ public class ReactiveMongoSessionRepositoryTest {
|
||||
@Test
|
||||
void setSessionIdGeneratorWhenNullThenThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.repository.setSessionIdGenerator(null))
|
||||
.withMessage("sessionIdGenerator cannot be null");
|
||||
.withMessage("sessionIdGenerator cannot be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -235,12 +242,14 @@ public class ReactiveMongoSessionRepositoryTest {
|
||||
Document sessionDocument = new Document();
|
||||
|
||||
given(this.mongoOperations.findById(sessionId, Document.class,
|
||||
ReactiveMongoSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
|
||||
ReactiveMongoSessionRepository.DEFAULT_COLLECTION_NAME))
|
||||
.willReturn(Mono.just(sessionDocument));
|
||||
|
||||
MongoSession session = new MongoSession(sessionId);
|
||||
|
||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
|
||||
TypeDescriptor.valueOf(MongoSession.class)))
|
||||
.willReturn(session);
|
||||
|
||||
this.repository.findById(sessionId).as(StepVerifier::create).assertNext((mongoSession) -> {
|
||||
String oldId = mongoSession.getId();
|
||||
|
||||
@@ -74,8 +74,8 @@ public class MongoHttpSessionConfigurationTest {
|
||||
void noMongoOperationsConfiguration() {
|
||||
|
||||
assertThatExceptionOfType(UnsatisfiedDependencyException.class)
|
||||
.isThrownBy(() -> registerAndRefresh(EmptyConfiguration.class))
|
||||
.withMessageContaining("mongoSessionRepository");
|
||||
.isThrownBy(() -> registerAndRefresh(EmptyConfiguration.class))
|
||||
.withMessageContaining("mongoSessionRepository");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -116,7 +116,7 @@ public class MongoHttpSessionConfigurationTest {
|
||||
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||
|
||||
assertThat(repository).extracting("defaultMaxInactiveInterval")
|
||||
.isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
|
||||
.isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -127,7 +127,7 @@ public class MongoHttpSessionConfigurationTest {
|
||||
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||
|
||||
assertThat(repository).extracting("defaultMaxInactiveInterval")
|
||||
.isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
|
||||
.isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -147,7 +147,7 @@ public class MongoHttpSessionConfigurationTest {
|
||||
void resolveCollectionNameByPropertyPlaceholder() {
|
||||
|
||||
this.context
|
||||
.setEnvironment(new MockEnvironment().withProperty("session.mongo.collectionName", COLLECTION_NAME));
|
||||
.setEnvironment(new MockEnvironment().withProperty("session.mongo.collectionName", COLLECTION_NAME));
|
||||
registerAndRefresh(CustomMongoJdbcSessionConfiguration.class);
|
||||
|
||||
MongoHttpSessionConfiguration configuration = this.context.getBean(MongoHttpSessionConfiguration.class);
|
||||
@@ -176,8 +176,8 @@ public class MongoHttpSessionConfigurationTest {
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(indexResolver).isNotNull();
|
||||
assertThat(repository).extracting("mongoSessionConverter").hasFieldOrPropertyWithValue("indexResolver",
|
||||
indexResolver);
|
||||
assertThat(repository).extracting("mongoSessionConverter")
|
||||
.hasFieldOrPropertyWithValue("indexResolver", indexResolver);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -191,8 +191,8 @@ public class MongoHttpSessionConfigurationTest {
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(indexResolver).isNotNull();
|
||||
assertThat(repository).extracting("mongoSessionConverter").hasFieldOrPropertyWithValue("indexResolver",
|
||||
indexResolver);
|
||||
assertThat(repository).extracting("mongoSessionConverter")
|
||||
.hasFieldOrPropertyWithValue("indexResolver", indexResolver);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -97,9 +97,8 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
this.context.register(BadConfig.class);
|
||||
|
||||
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(this.context::refresh)
|
||||
.withMessageContaining("Error creating bean with name 'reactiveMongoSessionRepository'")
|
||||
.withMessageContaining(
|
||||
"No qualifying bean of type '" + ReactiveMongoOperations.class.getCanonicalName());
|
||||
.withMessageContaining("Error creating bean with name 'reactiveMongoSessionRepository'")
|
||||
.withMessageContaining("No qualifying bean of type '" + ReactiveMongoOperations.class.getCanonicalName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -195,8 +194,8 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(indexResolver).isNotNull();
|
||||
assertThat(repository).extracting("mongoSessionConverter").hasFieldOrPropertyWithValue("indexResolver",
|
||||
indexResolver);
|
||||
assertThat(repository).extracting("mongoSessionConverter")
|
||||
.hasFieldOrPropertyWithValue("indexResolver", indexResolver);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -211,8 +210,8 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(indexResolver).isNotNull();
|
||||
assertThat(repository).extracting("mongoSessionConverter").hasFieldOrPropertyWithValue("indexResolver",
|
||||
indexResolver);
|
||||
assertThat(repository).extracting("mongoSessionConverter")
|
||||
.hasFieldOrPropertyWithValue("indexResolver", indexResolver);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user