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");
});
}
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");
});
}
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");
});
}
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();
}
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();
}
Aggregations