|
| 1 | +/* |
| 2 | + * Copyright © 2024 XDEV Software (https://xdev.software) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package software.xdev.spring.data.eclipse.store.integration.isolated.tests.transactions; |
| 17 | + |
| 18 | +import java.math.BigDecimal; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Assertions; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.test.context.ContextConfiguration; |
| 25 | +import org.springframework.transaction.PlatformTransactionManager; |
| 26 | +import org.springframework.transaction.support.TransactionTemplate; |
| 27 | + |
| 28 | +import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations; |
| 29 | + |
| 30 | + |
| 31 | +@IsolatedTestAnnotations |
| 32 | +@ContextConfiguration(classes = {TransactionsTestConfiguration.class}) |
| 33 | +class TransactionsTest |
| 34 | +{ |
| 35 | + private final TransactionsTestConfiguration configuration; |
| 36 | + // single TransactionTemplate shared amongst all methods in this instance |
| 37 | + private final TransactionTemplate transactionTemplate; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + public TransactionsTest( |
| 41 | + final TransactionsTestConfiguration configuration, |
| 42 | + final PlatformTransactionManager transactionManager) |
| 43 | + { |
| 44 | + this.configuration = configuration; |
| 45 | + this.transactionTemplate = new TransactionTemplate(transactionManager); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void accountTransaction_Working(final AccountRepository repository) |
| 50 | + { |
| 51 | + final Account account1 = new Account(BigDecimal.TEN); |
| 52 | + final Account account2 = new Account(BigDecimal.ZERO); |
| 53 | + repository.saveAll(List.of(account1, account2)); |
| 54 | + |
| 55 | + this.transactionTemplate.execute( |
| 56 | + status -> |
| 57 | + { |
| 58 | + account1.setBalance(account1.getBalance().subtract(BigDecimal.ONE)); |
| 59 | + repository.save(account1); |
| 60 | + |
| 61 | + account2.setBalance(account2.getBalance().subtract(BigDecimal.ONE)); |
| 62 | + repository.save(account2); |
| 63 | + return null; |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + Assertions.assertEquals(BigDecimal.valueOf(9.0), repository.findById(account1.getId()).get().getBalance()); |
| 68 | + Assertions.assertEquals(BigDecimal.ONE, repository.findById(account2.getId()).get().getBalance()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void accountTransaction_UnexpectedError(final AccountRepository repository) |
| 73 | + { |
| 74 | + final Account account1 = new Account(BigDecimal.TEN); |
| 75 | + final Account account2 = new Account(BigDecimal.ZERO); |
| 76 | + repository.saveAll(List.of(account1, account2)); |
| 77 | + |
| 78 | + this.transactionTemplate.execute( |
| 79 | + status -> |
| 80 | + { |
| 81 | + account1.setBalance(account1.getBalance().subtract(BigDecimal.ONE)); |
| 82 | + repository.save(account1); |
| 83 | + |
| 84 | + throw new RuntimeException("Unexpected error"); |
| 85 | + } |
| 86 | + ); |
| 87 | + |
| 88 | + Assertions.assertEquals(BigDecimal.TEN, repository.findById(account1.getId()).get().getBalance()); |
| 89 | + Assertions.assertEquals(BigDecimal.ZERO, repository.findById(account2.getId()).get().getBalance()); |
| 90 | + } |
| 91 | +} |
0 commit comments