|
| 1 | +package io.github.kaiso.relmongo.tests; |
| 2 | + |
| 3 | +import io.github.kaiso.relmongo.data.model.Car; |
| 4 | +import io.github.kaiso.relmongo.data.model.DrivingLicense; |
| 5 | +import io.github.kaiso.relmongo.data.model.House; |
| 6 | +import io.github.kaiso.relmongo.data.model.Passport; |
| 7 | +import io.github.kaiso.relmongo.data.model.Person; |
| 8 | +import io.github.kaiso.relmongo.data.model.State; |
| 9 | +import io.github.kaiso.relmongo.data.repository.PersonRepository; |
| 10 | +import io.github.kaiso.relmongo.tests.common.AbstractBaseTest; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Assertions; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Disabled; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.context.annotation.Bean; |
| 18 | +import org.springframework.context.annotation.Configuration; |
| 19 | +import org.springframework.data.mongodb.MongoDbFactory; |
| 20 | +import org.springframework.data.mongodb.MongoTransactionException; |
| 21 | +import org.springframework.data.mongodb.MongoTransactionManager; |
| 22 | +import org.springframework.data.mongodb.core.MongoOperations; |
| 23 | +import org.springframework.stereotype.Service; |
| 24 | +import org.springframework.test.context.ContextConfiguration; |
| 25 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 26 | +import org.springframework.transaction.annotation.Propagation; |
| 27 | +import org.springframework.transaction.annotation.Transactional; |
| 28 | + |
| 29 | +@ContextConfiguration(classes = { TransactionsTest.class }) |
| 30 | +public class TransactionsTest extends AbstractBaseTest { |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private TransactionalBean transactionalBean; |
| 34 | + |
| 35 | + @Service |
| 36 | + public static class TransactionalBean { |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private PersonRepository repository; |
| 40 | + |
| 41 | + @Autowired |
| 42 | + private MongoOperations mongoOperations; |
| 43 | + |
| 44 | + public void before() { |
| 45 | + |
| 46 | + mongoOperations.dropCollection(DrivingLicense.class); |
| 47 | + mongoOperations.dropCollection(Passport.class); |
| 48 | + mongoOperations.dropCollection(Person.class); |
| 49 | + mongoOperations.dropCollection(House.class); |
| 50 | + mongoOperations.dropCollection(Car.class); |
| 51 | + mongoOperations.dropCollection(State.class); |
| 52 | + |
| 53 | + // create collection |
| 54 | + mongoOperations.createCollection(Person.class); |
| 55 | + } |
| 56 | + |
| 57 | + @Transactional(propagation = Propagation.REQUIRES_NEW) |
| 58 | + public void savePerson(Person person) { |
| 59 | + repository.save(person); |
| 60 | + } |
| 61 | + |
| 62 | + @Transactional(propagation = Propagation.REQUIRES_NEW) |
| 63 | + public void savePersonAndDropCollection(Person person) { |
| 64 | + repository.save(person); |
| 65 | + repository.count(); |
| 66 | + mongoOperations.dropCollection(Person.class); |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @Configuration |
| 72 | + @EnableTransactionManagement |
| 73 | + public static class TransactionConfig { |
| 74 | + |
| 75 | + @Bean |
| 76 | + MongoTransactionManager transactionManager(MongoDbFactory dbFactory) { |
| 77 | + return new MongoTransactionManager(dbFactory); |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + @BeforeEach |
| 83 | + public void beforeEach() { |
| 84 | + transactionalBean.before(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + @Disabled("this test works only on replicaset environment") |
| 89 | + public void shouldSaveInTransaction() { |
| 90 | + |
| 91 | + Person person = new Person(); |
| 92 | + person.setName("Dave"); |
| 93 | + person.setEmail("dave@mail.com"); |
| 94 | + transactionalBean.savePerson(person); |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + @Disabled("this test works only on replicaset environment") |
| 100 | + public void shouldSaveInTransaction_excepttion() { |
| 101 | + Assertions.assertThrows(MongoTransactionException.class, () -> { |
| 102 | + Person person = new Person(); |
| 103 | + person.setName("Dave"); |
| 104 | + person.setEmail("dave@mail.com"); |
| 105 | + transactionalBean.savePersonAndDropCollection(person); |
| 106 | + }); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments