Search in sources :

Example 1 with ClientSession

use of com.mongodb.reactivestreams.client.ClientSession in project mongo-java-driver by mongodb.

the class MongoCollectionImplTest method testCountDocuments.

@Test
public void testCountDocuments() {
    CountOptions options = new CountOptions().collation(Collation.builder().locale("de").build());
    assertAll("countDocuments", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments((Bson) null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments(filter, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments((ClientSession) null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments(clientSession, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments(clientSession, filter, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments(null, filter)), () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments(null, filter, options))), () -> {
        Publisher<Long> expected = mongoOperationPublisher.countDocuments(null, new BsonDocument(), new CountOptions());
        assertPublisherIsTheSameAs(expected, collection.countDocuments(), "Default");
    }, () -> {
        Publisher<Long> expected = mongoOperationPublisher.countDocuments(null, filter, new CountOptions());
        assertPublisherIsTheSameAs(expected, collection.countDocuments(filter), "With filter");
    }, () -> {
        Publisher<Long> expected = mongoOperationPublisher.countDocuments(null, filter, options);
        assertPublisherIsTheSameAs(expected, collection.countDocuments(filter, options), "With filter & options");
    }, () -> {
        Publisher<Long> expected = mongoOperationPublisher.countDocuments(clientSession, new BsonDocument(), new CountOptions());
        assertPublisherIsTheSameAs(expected, collection.countDocuments(clientSession), "With client session");
    }, () -> {
        Publisher<Long> expected = mongoOperationPublisher.countDocuments(clientSession, filter, new CountOptions());
        assertPublisherIsTheSameAs(expected, collection.countDocuments(clientSession, filter), "With client session & filter");
    }, () -> {
        Publisher<Long> expected = mongoOperationPublisher.countDocuments(clientSession, filter, options);
        assertPublisherIsTheSameAs(expected, collection.countDocuments(clientSession, filter, options), "With client session, filter & options");
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.reactivestreams.client.ClientSession) CountOptions(com.mongodb.client.model.CountOptions) EstimatedDocumentCountOptions(com.mongodb.client.model.EstimatedDocumentCountOptions) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 2 with ClientSession

use of com.mongodb.reactivestreams.client.ClientSession in project mongo-java-driver by mongodb.

the class MongoCollectionImplTest method testFind.

@Test
public void testFind() {
    assertAll("find", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.find((Bson) null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.find((Class<?>) null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.find(filter, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.find(clientSession, (Bson) null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.find(clientSession, (Class<?>) null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.find(clientSession, filter, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.find(null, filter)), () -> assertThrows(IllegalArgumentException.class, () -> collection.find((ClientSession) null, Document.class)), () -> assertThrows(IllegalArgumentException.class, () -> collection.find(null, filter, Document.class))), () -> {
        FindPublisher<Document> expected = new FindPublisherImpl<>(null, mongoOperationPublisher, new BsonDocument());
        assertPublisherIsTheSameAs(expected, collection.find(), "Default");
    }, () -> {
        FindPublisher<Document> expected = new FindPublisherImpl<>(null, mongoOperationPublisher, filter);
        assertPublisherIsTheSameAs(expected, collection.find(filter), "With filter");
    }, () -> {
        FindPublisher<BsonDocument> expected = new FindPublisherImpl<>(null, mongoOperationPublisher.withDocumentClass(BsonDocument.class), filter);
        assertPublisherIsTheSameAs(expected, collection.find(filter, BsonDocument.class), "With filter & result class");
    }, () -> {
        FindPublisher<Document> expected = new FindPublisherImpl<>(clientSession, mongoOperationPublisher, new BsonDocument());
        assertPublisherIsTheSameAs(expected, collection.find(clientSession), "With client session");
    }, () -> {
        FindPublisher<Document> expected = new FindPublisherImpl<>(clientSession, mongoOperationPublisher, filter);
        assertPublisherIsTheSameAs(expected, collection.find(clientSession, filter), "With client session & filter");
    }, () -> {
        FindPublisher<BsonDocument> expected = new FindPublisherImpl<>(clientSession, mongoOperationPublisher.withDocumentClass(BsonDocument.class), filter);
        assertPublisherIsTheSameAs(expected, collection.find(clientSession, filter, BsonDocument.class), "With client session, filter & result class");
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.reactivestreams.client.ClientSession) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 3 with ClientSession

use of com.mongodb.reactivestreams.client.ClientSession in project mongo-java-driver by mongodb.

the class MongoClientImplTest method testStartSession.

@Test
void testStartSession() {
    ServerDescription serverDescription = ServerDescription.builder().address(new ServerAddress()).state(ServerConnectionState.CONNECTED).maxWireVersion(8).build();
    MongoClientImpl mongoClient = createMongoClient();
    Cluster cluster = mongoClient.getCluster();
    when(cluster.getCurrentDescription()).thenReturn(new ClusterDescription(ClusterConnectionMode.SINGLE, ClusterType.STANDALONE, singletonList(serverDescription)));
    ServerSessionPool serverSessionPool = mock(ServerSessionPool.class);
    ClientSessionHelper clientSessionHelper = new ClientSessionHelper(mongoClient, serverSessionPool);
    assertAll("Start Session Tests", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> mongoClient.startSession(null))), () -> {
        Mono<ClientSession> expected = clientSessionHelper.createClientSessionMono(ClientSessionOptions.builder().build(), OPERATION_EXECUTOR);
        assertPublisherIsTheSameAs(expected, mongoClient.startSession(), "Default");
    }, () -> {
        ClientSessionOptions options = ClientSessionOptions.builder().causallyConsistent(true).defaultTransactionOptions(TransactionOptions.builder().readConcern(ReadConcern.LINEARIZABLE).build()).build();
        Mono<ClientSession> expected = clientSessionHelper.createClientSessionMono(options, OPERATION_EXECUTOR);
        assertPublisherIsTheSameAs(expected, mongoClient.startSession(options), "with options");
    });
}
Also used : ServerSessionPool(com.mongodb.internal.session.ServerSessionPool) ServerDescription(com.mongodb.connection.ServerDescription) ClientSession(com.mongodb.reactivestreams.client.ClientSession) ServerAddress(com.mongodb.ServerAddress) Cluster(com.mongodb.internal.connection.Cluster) ClusterDescription(com.mongodb.connection.ClusterDescription) ClientSessionOptions(com.mongodb.ClientSessionOptions) Test(org.junit.jupiter.api.Test)

Example 4 with ClientSession

use of com.mongodb.reactivestreams.client.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();
    // 
    template.withSession(() -> session).execute(action -> action.findOne(new Query(), Document.class, COLLECTION_NAME)).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    assertThat(session.getOperationTime()).isNotNull();
    assertThat(session.getServerSession().isClosed()).isFalse();
    session.close();
}
Also used : Document(org.bson.Document) BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) Mono(reactor.core.publisher.Mono) ClientSession(com.mongodb.reactivestreams.client.ClientSession) Supplier(java.util.function.Supplier) Client(org.springframework.data.mongodb.test.util.Client) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) MongoClient(com.mongodb.reactivestreams.client.MongoClient) Test(org.junit.jupiter.api.Test) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ClientSessionOptions(com.mongodb.ClientSessionOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.assertj.core.api.Assertions) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) MongoClientExtension(org.springframework.data.mongodb.test.util.MongoClientExtension) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) Query(org.springframework.data.mongodb.core.query.Query) ClientSession(com.mongodb.reactivestreams.client.ClientSession) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 5 with ClientSession

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

the class ReactiveClientSessionTests method countInTransactionShouldReturnCount.

// DATAMONGO-2001
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
public void countInTransactionShouldReturnCount() {
    ClientSession session = Mono.from(client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build())).block();
    template.withSession(() -> session).execute(action -> {
        session.startTransaction();
        return // 
        action.insert(new Document("_id", "id-2").append("value", "in transaction"), COLLECTION_NAME).then(// 
        action.count(query(where("value").is("in transaction")), Document.class, COLLECTION_NAME)).flatMap(it -> Mono.from(session.commitTransaction()).then(Mono.just(it)));
    }).as(// 
    StepVerifier::create).expectNext(// 
    1L).verifyComplete();
    template.withSession(() -> session).execute(action -> {
        session.startTransaction();
        return // 
        action.insert(new Document("value", "in transaction"), COLLECTION_NAME).then(// 
        action.count(query(where("value").is("foo")), Document.class, COLLECTION_NAME)).flatMap(it -> Mono.from(session.commitTransaction()).then(Mono.just(it)));
    }).as(// 
    StepVerifier::create).expectNext(// 
    0L).verifyComplete();
}
Also used : Document(org.bson.Document) BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) Mono(reactor.core.publisher.Mono) ClientSession(com.mongodb.reactivestreams.client.ClientSession) Supplier(java.util.function.Supplier) Client(org.springframework.data.mongodb.test.util.Client) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) MongoClient(com.mongodb.reactivestreams.client.MongoClient) Test(org.junit.jupiter.api.Test) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ClientSessionOptions(com.mongodb.ClientSessionOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.assertj.core.api.Assertions) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) MongoClientExtension(org.springframework.data.mongodb.test.util.MongoClientExtension) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) ClientSession(com.mongodb.reactivestreams.client.ClientSession) Document(org.bson.Document) StepVerifier(reactor.test.StepVerifier) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Aggregations

ClientSession (com.mongodb.reactivestreams.client.ClientSession)14 Document (org.bson.Document)11 Mono (reactor.core.publisher.Mono)10 Test (org.junit.jupiter.api.Test)9 ClientSessionOptions (com.mongodb.ClientSessionOptions)7 MongoClient (com.mongodb.reactivestreams.client.MongoClient)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Assertions (org.assertj.core.api.Assertions)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)6 Criteria (org.springframework.data.mongodb.core.query.Criteria)6 Query (org.springframework.data.mongodb.core.query.Query)6 Client (org.springframework.data.mongodb.test.util.Client)6 EnableIfMongoServerVersion (org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion)6 EnableIfReplicaSetAvailable (org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable)6 MongoClientExtension (org.springframework.data.mongodb.test.util.MongoClientExtension)6 MongoTestUtils (org.springframework.data.mongodb.test.util.MongoTestUtils)6 StepVerifier (reactor.test.StepVerifier)6 Publisher (org.reactivestreams.Publisher)5 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)4