Search in sources :

Example 1 with ClientSession

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

the class SessionBoundMongoTemplateUnitTests method groupShouldUseProxiedDatabase.

// DATAMONGO-1880
@Test
public void groupShouldUseProxiedDatabase() {
    when(database.runCommand(any(ClientSession.class), any(), eq(Document.class))).thenReturn(new Document("retval", Collections.emptyList()));
    template.group(COLLECTION_NAME, GroupBy.key("firstName"), Person.class);
    verify(database).runCommand(eq(clientSession), any(), eq(Document.class));
}
Also used : ClientSession(com.mongodb.session.ClientSession) Document(org.bson.Document) Test(org.junit.Test)

Example 2 with ClientSession

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

the class SessionBoundMongoTemplateUnitTests method geoNearShouldUseProxiedDatabase.

// DATAMONGO-1880
@Test
public void geoNearShouldUseProxiedDatabase() {
    when(database.runCommand(any(ClientSession.class), any(), eq(Document.class))).thenReturn(new Document("results", Collections.emptyList()));
    template.geoNear(NearQuery.near(new Point(0, 0), Metrics.NEUTRAL), Person.class);
    verify(database).runCommand(eq(clientSession), any(), eq(Document.class));
}
Also used : ClientSession(com.mongodb.session.ClientSession) Point(org.springframework.data.geo.Point) Document(org.bson.Document) Test(org.junit.Test)

Example 3 with ClientSession

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

the class ReactiveClientSessionTests method shouldApplyClientSession.

// DATAMONGO-1880
@Test
public void shouldApplyClientSession() {
    ClientSession session = Mono.from(client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build())).block();
    assertThat(session.getOperationTime()).isNull();
    StepVerifier.create(template.withSession(() -> session).execute(action -> action.findAll(Document.class, "test"))).expectNextCount(1).verifyComplete();
    assertThat(session.getOperationTime()).isNotNull();
    assertThat(session.getServerSession().isClosed()).isFalse();
    session.close();
}
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) ClientSession(com.mongodb.session.ClientSession) Test(org.junit.Test)

Example 4 with ClientSession

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

the class ReactiveClientSessionTests method useMonoInCallback.

// DATAMONGO-1880
@Test
public void useMonoInCallback() {
    ClientSession session = Mono.from(client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build())).block();
    assertThat(session.getOperationTime()).isNull();
    StepVerifier.create(template.withSession(() -> session).execute(action -> action.findOne(new Query(), Document.class, "test"))).expectNextCount(1).verifyComplete();
    assertThat(session.getOperationTime()).isNotNull();
    assertThat(session.getServerSession().isClosed()).isFalse();
    session.close();
}
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)

Example 5 with ClientSession

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

the class ReactiveSessionBoundMongoTemplateUnitTests 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())).thenReturn(collection);
    when(database.getCollection(anyString(), any())).thenReturn(collection);
    when(database.listCollectionNames(any(ClientSession.class))).thenReturn(findPublisher);
    when(database.createCollection(any(ClientSession.class), any(), any())).thenReturn(resultPublisher);
    when(database.runCommand(any(ClientSession.class), any(), any(Class.class))).thenReturn(resultPublisher);
    when(collection.find(any(ClientSession.class))).thenReturn(findPublisher);
    when(collection.find(any(ClientSession.class), any(Document.class))).thenReturn(findPublisher);
    when(collection.find(any(ClientSession.class), any(Class.class))).thenReturn(findPublisher);
    when(collection.find(any(ClientSession.class), any(), any())).thenReturn(findPublisher);
    when(collection.deleteMany(any(ClientSession.class), any(), any())).thenReturn(resultPublisher);
    when(collection.insertOne(any(ClientSession.class), any(Document.class))).thenReturn(resultPublisher);
    when(collection.aggregate(any(ClientSession.class), anyList(), any(Class.class))).thenReturn(aggregatePublisher);
    when(collection.count(any(ClientSession.class), any(), any(CountOptions.class))).thenReturn(resultPublisher);
    when(collection.drop(any(ClientSession.class))).thenReturn(resultPublisher);
    when(collection.findOneAndUpdate(any(ClientSession.class), any(), any(), any())).thenReturn(resultPublisher);
    when(collection.distinct(any(ClientSession.class), any(), any(), any())).thenReturn(distinctPublisher);
    when(collection.updateOne(any(ClientSession.class), any(), any(), any(UpdateOptions.class))).thenReturn(resultPublisher);
    when(collection.updateMany(any(ClientSession.class), any(), any(), any(UpdateOptions.class))).thenReturn(resultPublisher);
    when(collection.dropIndex(any(ClientSession.class), anyString())).thenReturn(resultPublisher);
    when(findPublisher.projection(any())).thenReturn(findPublisher);
    when(findPublisher.limit(anyInt())).thenReturn(findPublisher);
    when(findPublisher.collation(any())).thenReturn(findPublisher);
    when(findPublisher.first()).thenReturn(resultPublisher);
    when(aggregatePublisher.allowDiskUse(anyBoolean())).thenReturn(aggregatePublisher);
    when(aggregatePublisher.useCursor(anyBoolean())).thenReturn(aggregatePublisher);
    factory = new SimpleReactiveMongoDatabaseFactory(client, "foo");
    this.mappingContext = new MongoMappingContext();
    this.converter = new MappingMongoConverter(new NoOpDbRefResolver(), mappingContext);
    this.template = new ReactiveSessionBoundMongoTemplate(clientSession, new ReactiveMongoTemplate(factory, converter));
}
Also used : BsonValueCodec(org.bson.codecs.BsonValueCodec) NoOpDbRefResolver(org.springframework.data.mongodb.core.ReactiveMongoTemplate.NoOpDbRefResolver) ClientSession(com.mongodb.session.ClientSession) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) CountOptions(com.mongodb.client.model.CountOptions) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) Document(org.bson.Document) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateOptions(com.mongodb.client.model.UpdateOptions) ReactiveSessionBoundMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate.ReactiveSessionBoundMongoTemplate) Before(org.junit.Before)

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