Introduce MongoIndexedSessionRepository.
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.session.data.mongo;
|
||||
|
||||
import static org.springframework.session.data.mongo.MongoSessionUtils.*;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.index.IndexOperations;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||
import org.springframework.session.events.SessionCreatedEvent;
|
||||
import org.springframework.session.events.SessionDeletedEvent;
|
||||
import org.springframework.session.events.SessionExpiredEvent;
|
||||
|
||||
/**
|
||||
* Session repository implementation which stores sessions in Mongo. Uses {@link AbstractMongoSessionConverter} to
|
||||
* transform session objects from/to native Mongo representation ({@code DBObject}). Repository is also responsible for
|
||||
* removing expired sessions from database. Cleanup is done every minute.
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Greg Turnquist
|
||||
* @since 2.2.0
|
||||
*/
|
||||
public class MongoIndexedSessionRepository
|
||||
implements FindByIndexNameSessionRepository<MongoSession>, ApplicationEventPublisherAware, InitializingBean {
|
||||
|
||||
/**
|
||||
* The default time period in seconds in which a session will expire.
|
||||
*/
|
||||
public static final int DEFAULT_INACTIVE_INTERVAL = 1800;
|
||||
/**
|
||||
* the default collection name for storing session.
|
||||
*/
|
||||
public static final String DEFAULT_COLLECTION_NAME = "sessions";
|
||||
private static final Logger logger = LoggerFactory.getLogger(MongoIndexedSessionRepository.class);
|
||||
private final MongoOperations mongoOperations;
|
||||
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
|
||||
private String collectionName = DEFAULT_COLLECTION_NAME;
|
||||
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
|
||||
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
|
||||
public MongoIndexedSessionRepository(MongoOperations mongoOperations) {
|
||||
this.mongoOperations = mongoOperations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MongoSession createSession() {
|
||||
|
||||
MongoSession session = new MongoSession();
|
||||
|
||||
if (this.maxInactiveIntervalInSeconds != null) {
|
||||
session.setMaxInactiveInterval(Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
||||
}
|
||||
|
||||
publishEvent(new SessionCreatedEvent(this, session));
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(MongoSession session) {
|
||||
this.mongoOperations.save(Assert.requireNonNull(convertToDBObject(this.mongoSessionConverter, session),
|
||||
"convertToDBObject must not null!"), this.collectionName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public MongoSession findById(String id) {
|
||||
|
||||
Document sessionWrapper = findSession(id);
|
||||
|
||||
if (sessionWrapper == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
MongoSession session = convertToSession(this.mongoSessionConverter, sessionWrapper);
|
||||
|
||||
if (session != null && session.isExpired()) {
|
||||
publishEvent(new SessionExpiredEvent(this, session));
|
||||
deleteById(id);
|
||||
return null;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Currently this repository allows only querying against {@code PRINCIPAL_NAME_INDEX_NAME}.
|
||||
*
|
||||
* @param indexName the name if the index (i.e. {@link FindByIndexNameSessionRepository#PRINCIPAL_NAME_INDEX_NAME})
|
||||
* @param indexValue the value of the index to search for.
|
||||
* @return sessions map
|
||||
*/
|
||||
@Override
|
||||
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 -> convertToSession(this.mongoSessionConverter, dbSession))
|
||||
.collect(Collectors.toMap(MongoSession::getId, mapSession -> mapSession));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(String id) {
|
||||
|
||||
Optional.ofNullable(findSession(id)).ifPresent(document -> {
|
||||
MongoSession session = convertToSession(this.mongoSessionConverter, document);
|
||||
if (session != null) {
|
||||
publishEvent(new SessionDeletedEvent(this, session));
|
||||
}
|
||||
this.mongoOperations.remove(document, this.collectionName);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
|
||||
IndexOperations indexOperations = this.mongoOperations.indexOps(this.collectionName);
|
||||
this.mongoSessionConverter.ensureIndexes(indexOperations);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Document findSession(String id) {
|
||||
return this.mongoOperations.findById(id, Document.class, this.collectionName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
private void publishEvent(ApplicationEvent event) {
|
||||
|
||||
try {
|
||||
this.eventPublisher.publishEvent(event);
|
||||
} catch (Throwable ex) {
|
||||
logger.error("Error publishing " + event + ".", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) {
|
||||
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
|
||||
}
|
||||
|
||||
public void setCollectionName(final String collectionName) {
|
||||
this.collectionName = collectionName;
|
||||
}
|
||||
|
||||
public void setMongoSessionConverter(final AbstractMongoSessionConverter mongoSessionConverter) {
|
||||
this.mongoSessionConverter = mongoSessionConverter;
|
||||
}
|
||||
}
|
||||
@@ -15,159 +15,19 @@
|
||||
*/
|
||||
package org.springframework.session.data.mongo;
|
||||
|
||||
import static org.springframework.session.data.mongo.MongoSessionUtils.*;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.index.IndexOperations;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||
import org.springframework.session.events.SessionCreatedEvent;
|
||||
import org.springframework.session.events.SessionDeletedEvent;
|
||||
import org.springframework.session.events.SessionExpiredEvent;
|
||||
|
||||
/**
|
||||
* Session repository implementation which stores sessions in Mongo. Uses {@link AbstractMongoSessionConverter} to
|
||||
* transform session objects from/to native Mongo representation ({@code DBObject}). Repository is also responsible for
|
||||
* removing expired sessions from database. Cleanup is done every minute.
|
||||
* This {@link org.springframework.session.FindByIndexNameSessionRepository} implementation is kept to support backwards
|
||||
* compatibility.
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Greg Turnquist
|
||||
* @since 1.2
|
||||
* @deprecated since 2.2.0 in favor of {@link MongoIndexedSessionRepository}.
|
||||
*/
|
||||
public class MongoOperationsSessionRepository
|
||||
implements FindByIndexNameSessionRepository<MongoSession>, ApplicationEventPublisherAware, InitializingBean {
|
||||
/**
|
||||
* The default time period in seconds in which a session will expire.
|
||||
*/
|
||||
public static final int DEFAULT_INACTIVE_INTERVAL = 1800;
|
||||
/**
|
||||
* the default collection name for storing session.
|
||||
*/
|
||||
public static final String DEFAULT_COLLECTION_NAME = "sessions";
|
||||
private static final Logger logger = LoggerFactory.getLogger(MongoOperationsSessionRepository.class);
|
||||
private final MongoOperations mongoOperations;
|
||||
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
|
||||
private String collectionName = DEFAULT_COLLECTION_NAME;
|
||||
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
|
||||
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
@Deprecated
|
||||
public class MongoOperationsSessionRepository extends MongoIndexedSessionRepository {
|
||||
|
||||
public MongoOperationsSessionRepository(MongoOperations mongoOperations) {
|
||||
this.mongoOperations = mongoOperations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MongoSession createSession() {
|
||||
MongoSession session = new MongoSession();
|
||||
if (this.maxInactiveIntervalInSeconds != null) {
|
||||
session.setMaxInactiveInterval(Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
||||
}
|
||||
publishEvent(new SessionCreatedEvent(this, session));
|
||||
return session;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(MongoSession session) {
|
||||
this.mongoOperations.save(Assert.requireNonNull(convertToDBObject(this.mongoSessionConverter, session),
|
||||
"convertToDBObject must not null!"), this.collectionName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public MongoSession findById(String id) {
|
||||
|
||||
Document sessionWrapper = findSession(id);
|
||||
|
||||
if (sessionWrapper == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
MongoSession session = convertToSession(this.mongoSessionConverter, sessionWrapper);
|
||||
|
||||
if (session != null && session.isExpired()) {
|
||||
publishEvent(new SessionExpiredEvent(this, session));
|
||||
deleteById(id);
|
||||
return null;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Currently this repository allows only querying against {@code PRINCIPAL_NAME_INDEX_NAME}.
|
||||
*
|
||||
* @param indexName the name if the index (i.e. {@link FindByIndexNameSessionRepository#PRINCIPAL_NAME_INDEX_NAME})
|
||||
* @param indexValue the value of the index to search for.
|
||||
* @return sessions map
|
||||
*/
|
||||
@Override
|
||||
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 -> convertToSession(this.mongoSessionConverter, dbSession))
|
||||
.collect(Collectors.toMap(MongoSession::getId, mapSession -> mapSession));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(String id) {
|
||||
|
||||
Optional.ofNullable(findSession(id)).ifPresent(document -> {
|
||||
MongoSession session = convertToSession(this.mongoSessionConverter, document);
|
||||
if (session != null) {
|
||||
publishEvent(new SessionDeletedEvent(this, session));
|
||||
}
|
||||
this.mongoOperations.remove(document, this.collectionName);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
IndexOperations indexOperations = this.mongoOperations.indexOps(this.collectionName);
|
||||
this.mongoSessionConverter.ensureIndexes(indexOperations);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Document findSession(String id) {
|
||||
return this.mongoOperations.findById(id, Document.class, this.collectionName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
private void publishEvent(ApplicationEvent event) {
|
||||
try {
|
||||
this.eventPublisher.publishEvent(event);
|
||||
} catch (Throwable ex) {
|
||||
logger.error("Error publishing " + event + ".", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) {
|
||||
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
|
||||
}
|
||||
|
||||
public void setCollectionName(final String collectionName) {
|
||||
this.collectionName = collectionName;
|
||||
}
|
||||
|
||||
public void setMongoSessionConverter(final AbstractMongoSessionConverter mongoSessionConverter) {
|
||||
this.mongoSessionConverter = mongoSessionConverter;
|
||||
super(mongoOperations);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ public class MongoSession implements Session {
|
||||
private Map<String, Object> attrs = new HashMap<>();
|
||||
|
||||
public MongoSession() {
|
||||
this(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
this(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
}
|
||||
|
||||
public MongoSession(long maxInactiveIntervalInSeconds) {
|
||||
@@ -147,7 +147,7 @@ public class MongoSession implements Session {
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
||||
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||
|
||||
/**
|
||||
* Add this annotation to a {@code @Configuration} class to expose the SessionRepositoryFilter as a bean named
|
||||
@@ -59,12 +59,12 @@ public @interface EnableMongoHttpSession {
|
||||
*
|
||||
* @return default max inactive interval in seconds
|
||||
*/
|
||||
int maxInactiveIntervalInSeconds() default MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL;
|
||||
int maxInactiveIntervalInSeconds() default MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL;
|
||||
|
||||
/**
|
||||
* The collection name to use.
|
||||
*
|
||||
* @return name of the collection to store session
|
||||
*/
|
||||
String collectionName() default MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME;
|
||||
String collectionName() default MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME;
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
|
||||
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
|
||||
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
|
||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
||||
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.StringValueResolver;
|
||||
|
||||
@@ -54,9 +54,9 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
||||
private ClassLoader classLoader;
|
||||
|
||||
@Bean
|
||||
public MongoOperationsSessionRepository mongoSessionRepository(MongoOperations mongoOperations) {
|
||||
public MongoIndexedSessionRepository mongoSessionRepository(MongoOperations mongoOperations) {
|
||||
|
||||
MongoOperationsSessionRepository repository = new MongoOperationsSessionRepository(mongoOperations);
|
||||
MongoIndexedSessionRepository repository = new MongoIndexedSessionRepository(mongoOperations);
|
||||
repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds);
|
||||
|
||||
if (this.mongoSessionConverter != null) {
|
||||
@@ -64,7 +64,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
||||
} else {
|
||||
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(),
|
||||
new DeserializingConverter(this.classLoader),
|
||||
Duration.ofSeconds(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL));
|
||||
Duration.ofSeconds(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL));
|
||||
repository.setMongoSessionConverter(mongoSessionConverter);
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
||||
if (attributes != null) {
|
||||
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
|
||||
} else {
|
||||
this.maxInactiveIntervalInSeconds = MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL;
|
||||
this.maxInactiveIntervalInSeconds = MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL;
|
||||
}
|
||||
|
||||
String collectionNameValue = attributes != null ? attributes.getString("collectionName") : "";
|
||||
|
||||
@@ -17,12 +17,8 @@
|
||||
package org.springframework.session.data.mongo;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
@@ -43,25 +39,25 @@ import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
/**
|
||||
* Tests for {@link MongoOperationsSessionRepository}.
|
||||
* Tests for {@link MongoIndexedSessionRepository}.
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Vedran Pavic
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class MongoOperationsSessionRepositoryTest {
|
||||
public class MongoIndexedSessionRepositoryTest {
|
||||
|
||||
@Mock private AbstractMongoSessionConverter converter;
|
||||
|
||||
@Mock private MongoOperations mongoOperations;
|
||||
|
||||
private MongoOperationsSessionRepository repository;
|
||||
private MongoIndexedSessionRepository repository;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
|
||||
this.repository = new MongoOperationsSessionRepository(this.mongoOperations);
|
||||
this.repository = new MongoIndexedSessionRepository(this.mongoOperations);
|
||||
this.repository.setMongoSessionConverter(this.converter);
|
||||
}
|
||||
|
||||
@@ -74,7 +70,7 @@ public class MongoOperationsSessionRepositoryTest {
|
||||
// then
|
||||
assertThat(session.getId()).isNotEmpty();
|
||||
assertThat(session.getMaxInactiveInterval().getSeconds())
|
||||
.isEqualTo(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
.isEqualTo(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -87,7 +83,7 @@ public class MongoOperationsSessionRepositoryTest {
|
||||
// then
|
||||
assertThat(session.getId()).isNotEmpty();
|
||||
assertThat(session.getMaxInactiveInterval().getSeconds())
|
||||
.isEqualTo(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
.isEqualTo(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -103,7 +99,7 @@ public class MongoOperationsSessionRepositoryTest {
|
||||
this.repository.save(session);
|
||||
|
||||
// then
|
||||
verify(this.mongoOperations).save(dbSession, MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME);
|
||||
verify(this.mongoOperations).save(dbSession, MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -113,8 +109,9 @@ public class MongoOperationsSessionRepositoryTest {
|
||||
String sessionId = UUID.randomUUID().toString();
|
||||
Document sessionDocument = new Document();
|
||||
|
||||
given(this.mongoOperations.findById(sessionId, Document.class,
|
||||
MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(sessionDocument);
|
||||
given(
|
||||
this.mongoOperations.findById(sessionId, Document.class, MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME))
|
||||
.willReturn(sessionDocument);
|
||||
|
||||
MongoSession session = new MongoSession();
|
||||
|
||||
@@ -135,8 +132,9 @@ public class MongoOperationsSessionRepositoryTest {
|
||||
String sessionId = UUID.randomUUID().toString();
|
||||
Document sessionDocument = new Document();
|
||||
|
||||
given(this.mongoOperations.findById(sessionId, Document.class,
|
||||
MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(sessionDocument);
|
||||
given(
|
||||
this.mongoOperations.findById(sessionId, Document.class, MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME))
|
||||
.willReturn(sessionDocument);
|
||||
|
||||
MongoSession session = mock(MongoSession.class);
|
||||
|
||||
@@ -149,8 +147,7 @@ public class MongoOperationsSessionRepositoryTest {
|
||||
this.repository.findById(sessionId);
|
||||
|
||||
// then
|
||||
verify(this.mongoOperations).remove(any(Document.class),
|
||||
eq(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME));
|
||||
verify(this.mongoOperations).remove(any(Document.class), eq(MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -162,19 +159,18 @@ public class MongoOperationsSessionRepositoryTest {
|
||||
Document sessionDocument = new Document();
|
||||
sessionDocument.put("id", sessionId);
|
||||
|
||||
MongoSession mongoSession = new MongoSession(sessionId, MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
MongoSession mongoSession = new MongoSession(sessionId, MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
|
||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(mongoSession);
|
||||
given(this.mongoOperations.findById(eq(sessionId), eq(Document.class),
|
||||
eq(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME))).willReturn(sessionDocument);
|
||||
eq(MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME))).willReturn(sessionDocument);
|
||||
|
||||
// when
|
||||
this.repository.deleteById(sessionId);
|
||||
|
||||
// then
|
||||
verify(this.mongoOperations).remove(any(Document.class),
|
||||
eq(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME));
|
||||
verify(this.mongoOperations).remove(any(Document.class), eq(MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -187,7 +183,7 @@ public class MongoOperationsSessionRepositoryTest {
|
||||
|
||||
given(this.converter.getQueryForIndex(anyString(), any(Object.class))).willReturn(mock(Query.class));
|
||||
given(this.mongoOperations.find(any(Query.class), eq(Document.class),
|
||||
eq(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME))).willReturn(Collections.singletonList(document));
|
||||
eq(MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME))).willReturn(Collections.singletonList(document));
|
||||
|
||||
String sessionId = UUID.randomUUID().toString();
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
package org.springframework.session.data.mongo.config.annotation.web.http;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
@@ -33,7 +33,7 @@ import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.index.IndexOperations;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
|
||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
||||
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
/**
|
||||
@@ -71,7 +71,7 @@ public class MongoHttpSessionConfigurationTest {
|
||||
|
||||
registerAndRefresh(DefaultConfiguration.class);
|
||||
|
||||
assertThat(this.context.getBean(MongoOperationsSessionRepository.class)).isNotNull();
|
||||
assertThat(this.context.getBean(MongoIndexedSessionRepository.class)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,7 +79,7 @@ public class MongoHttpSessionConfigurationTest {
|
||||
|
||||
registerAndRefresh(CustomCollectionNameConfiguration.class);
|
||||
|
||||
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
|
||||
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(repository, "collectionName")).isEqualTo(COLLECTION_NAME);
|
||||
@@ -101,7 +101,7 @@ public class MongoHttpSessionConfigurationTest {
|
||||
|
||||
registerAndRefresh(CustomMaxInactiveIntervalInSecondsConfiguration.class);
|
||||
|
||||
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
|
||||
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds"))
|
||||
@@ -113,7 +113,7 @@ public class MongoHttpSessionConfigurationTest {
|
||||
|
||||
registerAndRefresh(CustomMaxInactiveIntervalInSecondsSetConfiguration.class);
|
||||
|
||||
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
|
||||
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds"))
|
||||
@@ -125,7 +125,7 @@ public class MongoHttpSessionConfigurationTest {
|
||||
|
||||
registerAndRefresh(CustomSessionConverterConfiguration.class);
|
||||
|
||||
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
|
||||
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||
AbstractMongoSessionConverter mongoSessionConverter = this.context.getBean(AbstractMongoSessionConverter.class);
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
|
||||
@@ -38,14 +38,14 @@ import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||
import org.springframework.session.Session;
|
||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
||||
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||
import org.springframework.session.data.mongo.MongoSession;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
|
||||
/**
|
||||
* Abstract base class for {@link org.springframework.session.data.mongo.MongoOperationsSessionRepository} tests.
|
||||
* Abstract base class for {@link MongoIndexedSessionRepository} tests.
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Vedran Pavic
|
||||
@@ -57,7 +57,7 @@ abstract public class AbstractMongoRepositoryITest extends AbstractITest {
|
||||
|
||||
protected static final String INDEX_NAME = FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME;
|
||||
|
||||
@Autowired protected MongoOperationsSessionRepository repository;
|
||||
@Autowired protected MongoIndexedSessionRepository repository;
|
||||
|
||||
@Test
|
||||
public void saves() {
|
||||
|
||||
@@ -32,7 +32,7 @@ import org.springframework.session.data.mongo.config.annotation.web.http.EnableM
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link org.springframework.session.data.mongo.MongoOperationsSessionRepository} that use
|
||||
* Integration tests for {@link org.springframework.session.data.mongo.MongoIndexedSessionRepository} that use
|
||||
* {@link JacksonMongoSessionConverter} based session serialization.
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
|
||||
@@ -30,7 +30,7 @@ import org.springframework.session.data.mongo.config.annotation.web.http.EnableM
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link org.springframework.session.data.mongo.MongoOperationsSessionRepository} that use
|
||||
* Integration tests for {@link org.springframework.session.data.mongo.MongoIndexedSessionRepository} that use
|
||||
* {@link JdkMongoSessionConverter} based session serialization.
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
package org.springframework.session.data.mongo.integration;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
||||
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
|
||||
import org.springframework.session.data.mongo.integration.AbstractMongoRepositoryITest.BaseConfig;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@@ -25,7 +25,7 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class TraditionalConfigurationTest extends AbstractClassLoaderTest<MongoOperationsSessionRepository> {
|
||||
public class TraditionalConfigurationTest extends AbstractClassLoaderTest<MongoIndexedSessionRepository> {
|
||||
|
||||
@Configuration
|
||||
@EnableMongoHttpSession
|
||||
|
||||
Reference in New Issue
Block a user