Search in sources :

Example 11 with EnableIfReplicaSetAvailable

use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable in project spring-data-mongodb by spring-projects.

the class DefaultMessageListenerContainerTests method shouldReceiveMessagesWhenAddingRequestToAlreadyStartedContainer.

// DATAMONGO-1803
@Test
@EnableIfReplicaSetAvailable
public void shouldReceiveMessagesWhenAddingRequestToAlreadyStartedContainer() throws InterruptedException {
    MessageListenerContainer container = new DefaultMessageListenerContainer(template);
    container.start();
    Document unexpected = new Document("_id", "id-1").append("value", "foo");
    collection.insertOne(unexpected);
    Subscription subscription = container.register(new ChangeStreamRequest(messageListener, options()), Document.class);
    awaitSubscription(subscription, TIMEOUT);
    Document expected = new Document("_id", "id-2").append("value", "bar");
    collection.insertOne(expected);
    awaitMessages(messageListener, 1, TIMEOUT);
    container.stop();
    assertThat(messageListener.getMessages().stream().map(Message::getBody).collect(Collectors.toList())).containsExactly(expected);
}
Also used : Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) Test(org.junit.jupiter.api.Test)

Example 12 with EnableIfReplicaSetAvailable

use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable in project spring-data-mongodb by spring-projects.

the class SimpleMongoRepositoryTests method existsShouldBePossibleInTransaction.

// DATAMONGO-2130
@Test
@EnableIfReplicaSetAvailable
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
void existsShouldBePossibleInTransaction() {
    MongoTransactionManager txmgr = new MongoTransactionManager(template.getMongoDbFactory());
    TransactionTemplate tt = new TransactionTemplate(txmgr);
    tt.afterPropertiesSet();
    boolean exists = tt.execute(status -> {
        Person sample = new Person();
        sample.setLastname("Matthews");
        repository.save(sample);
        return repository.existsById(sample.getId());
    });
    assertThat(exists).isTrue();
}
Also used : MongoTransactionManager(org.springframework.data.mongodb.MongoTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Person(org.springframework.data.mongodb.repository.Person) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Example 13 with EnableIfReplicaSetAvailable

use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable in project spring-data-mongodb by spring-projects.

the class SimpleMongoRepositoryTests method countShouldBePossibleInTransaction.

// DATAMONGO-2130
@Test
@EnableIfReplicaSetAvailable
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
void countShouldBePossibleInTransaction() {
    MongoTransactionManager txmgr = new MongoTransactionManager(template.getMongoDbFactory());
    TransactionTemplate tt = new TransactionTemplate(txmgr);
    tt.afterPropertiesSet();
    long countPreTx = repository.count();
    long count = tt.execute(status -> {
        Person sample = new Person();
        sample.setLastname("Matthews");
        repository.save(sample);
        return repository.count();
    });
    assertThat(count).isEqualTo(countPreTx + 1);
}
Also used : MongoTransactionManager(org.springframework.data.mongodb.MongoTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Person(org.springframework.data.mongodb.repository.Person) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)13 EnableIfReplicaSetAvailable (org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable)13 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)8 VersionedPerson (org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson)8 Disposable (reactor.core.Disposable)8 StepVerifier (reactor.test.StepVerifier)8 EnableIfMongoServerVersion (org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion)6 Document (org.bson.Document)4 Disabled (org.junit.jupiter.api.Disabled)4 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)3 BsonDocument (org.bson.BsonDocument)2 MongoTransactionManager (org.springframework.data.mongodb.MongoTransactionManager)2 Person (org.springframework.data.mongodb.repository.Person)2 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)2 MongoClient (com.mongodb.client.MongoClient)1 MongoCollection (com.mongodb.client.MongoCollection)1 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1