Search in sources :

Example 6 with ClientSession

use of com.mongodb.session.ClientSession in project spring-data-mongodb by spring-projects.

the class ClientSessionTests method shouldApplyClientSession.

// DATAMONGO-1880
@Test
public void shouldApplyClientSession() {
    ClientSession session = client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
    assertThat(session.getOperationTime()).isNull();
    Document doc = template.withSession(() -> session).execute(action -> action.findOne(new Query(), Document.class, "test"));
    assertThat(doc).isNotNull();
    assertThat(session.getOperationTime()).isNotNull();
    assertThat(session.getServerSession().isClosed()).isFalse();
    session.close();
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) ClientSession(com.mongodb.session.ClientSession) Document(org.bson.Document) Test(org.junit.Test)

Example 7 with ClientSession

use of com.mongodb.session.ClientSession in project spring-data-mongodb by spring-projects.

the class SessionAwareMethodInterceptorUnitTests method proxyFactoryOnDatabaseWithSessionInArgumentListProceedsWithExecution.

// DATAMONGO-1880
@Test
public void proxyFactoryOnDatabaseWithSessionInArgumentListProceedsWithExecution() {
    ClientSession yetAnotherSession = mock(ClientSession.class);
    database.drop(yetAnotherSession);
    verify(targetDatabase).drop(eq(yetAnotherSession));
}
Also used : ClientSession(com.mongodb.session.ClientSession) Test(org.junit.Test)

Example 8 with ClientSession

use of com.mongodb.session.ClientSession in project spring-data-mongodb by spring-projects.

the class SessionAwareMethodInterceptorUnitTests method proxyFactoryOnCollectionWithSessionInArgumentListProceedsWithExecution.

// DATAMONGO-1880
@Test
public void proxyFactoryOnCollectionWithSessionInArgumentListProceedsWithExecution() {
    ClientSession yetAnotherSession = mock(ClientSession.class);
    collection.find(yetAnotherSession);
    verify(targetCollection).find(eq(yetAnotherSession));
}
Also used : ClientSession(com.mongodb.session.ClientSession) Test(org.junit.Test)

Example 9 with ClientSession

use of com.mongodb.session.ClientSession in project spring-data-mongodb by spring-projects.

the class SessionBoundMongoTemplateUnitTests method setUp.

@Before
public void setUp() {
    when(client.getDatabase(anyString())).thenReturn(database);
    when(codecRegistry.get(any(Class.class))).thenReturn(new BsonValueCodec());
    when(database.getCodecRegistry()).thenReturn(codecRegistry);
    when(database.getCollection(anyString(), any())).thenReturn(collection);
    when(database.listCollectionNames(any(ClientSession.class))).thenReturn(mongoIterable);
    when(collection.find(any(ClientSession.class), any(), any())).thenReturn(findIterable);
    when(collection.aggregate(any(ClientSession.class), anyList(), any())).thenReturn(aggregateIterable);
    when(collection.distinct(any(ClientSession.class), any(), any(), any())).thenReturn(distinctIterable);
    when(collection.mapReduce(any(ClientSession.class), any(), any(), any())).thenReturn(mapReduceIterable);
    when(findIterable.iterator()).thenReturn(cursor);
    when(aggregateIterable.collation(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.allowDiskUse(anyBoolean())).thenReturn(aggregateIterable);
    when(aggregateIterable.batchSize(anyInt())).thenReturn(aggregateIterable);
    when(aggregateIterable.map(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.useCursor(anyBoolean())).thenReturn(aggregateIterable);
    when(aggregateIterable.into(any())).thenReturn(Collections.emptyList());
    when(mongoIterable.iterator()).thenReturn(cursor);
    when(distinctIterable.map(any())).thenReturn(distinctIterable);
    when(distinctIterable.into(any())).thenReturn(Collections.emptyList());
    when(mapReduceIterable.sort(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.filter(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.map(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.iterator()).thenReturn(cursor);
    when(cursor.hasNext()).thenReturn(false);
    when(findIterable.projection(any())).thenReturn(findIterable);
    factory = new SimpleMongoDbFactory(client, "foo");
    this.mappingContext = new MongoMappingContext();
    this.converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext);
    this.template = new SessionBoundMongoTemplate(clientSession, new MongoTemplate(factory, converter));
}
Also used : BsonValueCodec(org.bson.codecs.BsonValueCodec) ClientSession(com.mongodb.session.ClientSession) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Before(org.junit.Before)

Example 10 with ClientSession

use of com.mongodb.session.ClientSession in project spring-data-mongodb by spring-projects.

the class ReactiveClientSessionTests method reusesClientSessionInSessionScopedCallback.

// DATAMONGO-1880
@Test
public void reusesClientSessionInSessionScopedCallback() {
    ClientSession session = Mono.from(client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build())).block();
    CountingSessionSupplier sessionSupplier = new CountingSessionSupplier(session);
    ReactiveSessionScoped sessionScoped = template.withSession(sessionSupplier);
    sessionScoped.execute(action -> action.findOne(new Query(), Document.class, "test")).blockFirst();
    assertThat(sessionSupplier.getInvocationCount()).isEqualTo(1);
    sessionScoped.execute(action -> action.findOne(new Query(), Document.class, "test")).blockFirst();
    assertThat(sessionSupplier.getInvocationCount()).isEqualTo(1);
}
Also used : Document(org.bson.Document) StepVerifier(reactor.test.StepVerifier) TestRule(org.junit.rules.TestRule) ReplicaSet(org.springframework.data.mongodb.test.util.ReplicaSet) Mono(reactor.core.publisher.Mono) Test(org.junit.Test) MongoClients(com.mongodb.reactivestreams.client.MongoClients) Supplier(java.util.function.Supplier) ClientSession(com.mongodb.session.ClientSession) Query(org.springframework.data.mongodb.core.query.Query) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ClientSessionOptions(com.mongodb.ClientSessionOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MongoVersionRule(org.springframework.data.mongodb.test.util.MongoVersionRule) Assertions(org.assertj.core.api.Assertions) Version(org.springframework.data.util.Version) ClassRule(org.junit.ClassRule) Before(org.junit.Before) Query(org.springframework.data.mongodb.core.query.Query) ClientSession(com.mongodb.session.ClientSession) Test(org.junit.Test)

Aggregations

ClientSession (com.mongodb.session.ClientSession)10 Test (org.junit.Test)8 Document (org.bson.Document)7 Before (org.junit.Before)5 Query (org.springframework.data.mongodb.core.query.Query)4 ClientSessionOptions (com.mongodb.ClientSessionOptions)3 MongoClient (com.mongodb.reactivestreams.client.MongoClient)3 MongoClients (com.mongodb.reactivestreams.client.MongoClients)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Supplier (java.util.function.Supplier)3 Assertions (org.assertj.core.api.Assertions)3 ClassRule (org.junit.ClassRule)3 TestRule (org.junit.rules.TestRule)3 MongoVersionRule (org.springframework.data.mongodb.test.util.MongoVersionRule)3 ReplicaSet (org.springframework.data.mongodb.test.util.ReplicaSet)3 Version (org.springframework.data.util.Version)3 Mono (reactor.core.publisher.Mono)3 StepVerifier (reactor.test.StepVerifier)3 BsonValueCodec (org.bson.codecs.BsonValueCodec)2 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)2