|
| 1 | +package com.javatodev.finance.service; |
| 2 | + |
| 3 | +import com.javatodev.finance.exception.EntityNotFoundException; |
| 4 | +import com.javatodev.finance.exception.GlobalErrorCode; |
| 5 | +import com.javatodev.finance.exception.InsufficientFundsException; |
| 6 | +import com.javatodev.finance.model.TransactionType; |
| 7 | +import com.javatodev.finance.model.dto.BankAccount; |
| 8 | +import com.javatodev.finance.model.dto.UtilityAccount; |
| 9 | +import com.javatodev.finance.model.dto.request.FundTransferRequest; |
| 10 | +import com.javatodev.finance.model.dto.request.UtilityPaymentRequest; |
| 11 | +import com.javatodev.finance.model.dto.response.FundTransferResponse; |
| 12 | +import com.javatodev.finance.model.dto.response.UtilityPaymentResponse; |
| 13 | +import com.javatodev.finance.model.entity.BankAccountEntity; |
| 14 | +import com.javatodev.finance.model.entity.TransactionEntity; |
| 15 | +import com.javatodev.finance.repository.BankAccountRepository; |
| 16 | +import com.javatodev.finance.repository.TransactionRepository; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.mockito.ArgumentCaptor; |
| 20 | +import java.math.BigDecimal; |
| 21 | +import java.util.Optional; |
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
| 23 | +import static org.mockito.Mockito.*; |
| 24 | + |
| 25 | +class TransactionServiceTest { |
| 26 | + private AccountService accountService; |
| 27 | + private BankAccountRepository bankAccountRepository; |
| 28 | + private TransactionRepository transactionRepository; |
| 29 | + private TransactionService transactionService; |
| 30 | + |
| 31 | + @BeforeEach |
| 32 | + void setUp() { |
| 33 | + accountService = mock(AccountService.class); |
| 34 | + bankAccountRepository = mock(BankAccountRepository.class); |
| 35 | + transactionRepository = mock(TransactionRepository.class); |
| 36 | + transactionService = new TransactionService(accountService, bankAccountRepository, transactionRepository); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void fundTransfer_success() { |
| 41 | + FundTransferRequest request = new FundTransferRequest(); |
| 42 | + request.setFromAccount("A1"); |
| 43 | + request.setToAccount("A2"); |
| 44 | + request.setAmount(BigDecimal.valueOf(100)); |
| 45 | + |
| 46 | + BankAccount from = new BankAccount(); |
| 47 | + from.setNumber("A1"); |
| 48 | + from.setActualBalance(BigDecimal.valueOf(200)); |
| 49 | + BankAccount to = new BankAccount(); |
| 50 | + to.setNumber("A2"); |
| 51 | + to.setActualBalance(BigDecimal.valueOf(50)); |
| 52 | + |
| 53 | + when(accountService.readBankAccount("A1")).thenReturn(from); |
| 54 | + when(accountService.readBankAccount("A2")).thenReturn(to); |
| 55 | + BankAccountEntity fromEntity = new BankAccountEntity(); |
| 56 | + fromEntity.setNumber("A1"); |
| 57 | + fromEntity.setActualBalance(BigDecimal.valueOf(200)); |
| 58 | + BankAccountEntity toEntity = new BankAccountEntity(); |
| 59 | + toEntity.setNumber("A2"); |
| 60 | + toEntity.setActualBalance(BigDecimal.valueOf(50)); |
| 61 | + when(bankAccountRepository.findByNumber("A1")).thenReturn(Optional.of(fromEntity)); |
| 62 | + when(bankAccountRepository.findByNumber("A2")).thenReturn(Optional.of(toEntity)); |
| 63 | + |
| 64 | + FundTransferResponse response = transactionService.fundTransfer(request); |
| 65 | + assertNotNull(response.getTransactionId()); |
| 66 | + assertEquals("Transaction successfully completed", response.getMessage()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void fundTransfer_insufficientFunds() { |
| 71 | + FundTransferRequest request = new FundTransferRequest(); |
| 72 | + request.setFromAccount("A1"); |
| 73 | + request.setToAccount("A2"); |
| 74 | + request.setAmount(BigDecimal.valueOf(300)); |
| 75 | + BankAccount from = new BankAccount(); |
| 76 | + from.setNumber("A1"); |
| 77 | + from.setActualBalance(BigDecimal.valueOf(200)); |
| 78 | + BankAccount to = new BankAccount(); |
| 79 | + to.setNumber("A2"); |
| 80 | + to.setActualBalance(BigDecimal.valueOf(50)); |
| 81 | + when(accountService.readBankAccount("A1")).thenReturn(from); |
| 82 | + when(accountService.readBankAccount("A2")).thenReturn(to); |
| 83 | + assertThrows(InsufficientFundsException.class, () -> transactionService.fundTransfer(request)); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void fundTransfer_accountNotFound() { |
| 88 | + FundTransferRequest request = new FundTransferRequest(); |
| 89 | + request.setFromAccount("A1"); |
| 90 | + request.setToAccount("A2"); |
| 91 | + request.setAmount(BigDecimal.valueOf(100)); |
| 92 | + when(accountService.readBankAccount("A1")).thenThrow(EntityNotFoundException.class); |
| 93 | + assertThrows(EntityNotFoundException.class, () -> transactionService.fundTransfer(request)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void utilPayment_success() { |
| 98 | + UtilityPaymentRequest request = new UtilityPaymentRequest(); |
| 99 | + request.setAccount("A1"); |
| 100 | + request.setProviderId(1L); |
| 101 | + request.setAmount(BigDecimal.valueOf(50)); |
| 102 | + request.setReferenceNumber("REF123"); |
| 103 | + BankAccount from = new BankAccount(); |
| 104 | + from.setNumber("A1"); |
| 105 | + from.setActualBalance(BigDecimal.valueOf(100)); |
| 106 | + UtilityAccount utility = new UtilityAccount(); |
| 107 | + utility.setId(1L); |
| 108 | + when(accountService.readBankAccount("A1")).thenReturn(from); |
| 109 | + when(accountService.readUtilityAccount(1L)).thenReturn(utility); |
| 110 | + BankAccountEntity fromEntity = new BankAccountEntity(); |
| 111 | + fromEntity.setNumber("A1"); |
| 112 | + fromEntity.setActualBalance(BigDecimal.valueOf(100)); |
| 113 | + fromEntity.setAvailableBalance(BigDecimal.valueOf(100)); |
| 114 | + when(bankAccountRepository.findByNumber("A1")).thenReturn(Optional.of(fromEntity)); |
| 115 | + UtilityPaymentResponse response = transactionService.utilPayment(request); |
| 116 | + assertNotNull(response.getTransactionId()); |
| 117 | + assertEquals("Utility payment successfully completed", response.getMessage()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void utilPayment_insufficientFunds() { |
| 122 | + UtilityPaymentRequest request = new UtilityPaymentRequest(); |
| 123 | + request.setAccount("A1"); |
| 124 | + request.setProviderId(1L); |
| 125 | + request.setAmount(BigDecimal.valueOf(150)); |
| 126 | + BankAccount from = new BankAccount(); |
| 127 | + from.setNumber("A1"); |
| 128 | + from.setActualBalance(BigDecimal.valueOf(100)); |
| 129 | + when(accountService.readBankAccount("A1")).thenReturn(from); |
| 130 | + assertThrows(InsufficientFundsException.class, () -> transactionService.utilPayment(request)); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void utilPayment_accountNotFound() { |
| 135 | + UtilityPaymentRequest request = new UtilityPaymentRequest(); |
| 136 | + request.setAccount("A1"); |
| 137 | + request.setProviderId(1L); |
| 138 | + request.setAmount(BigDecimal.valueOf(50)); |
| 139 | + when(accountService.readBankAccount("A1")).thenThrow(EntityNotFoundException.class); |
| 140 | + assertThrows(EntityNotFoundException.class, () -> transactionService.utilPayment(request)); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void internalFundTransfer_success() { |
| 145 | + BankAccount from = new BankAccount(); |
| 146 | + from.setNumber("A1"); |
| 147 | + from.setActualBalance(BigDecimal.valueOf(200)); |
| 148 | + BankAccount to = new BankAccount(); |
| 149 | + to.setNumber("A2"); |
| 150 | + to.setActualBalance(BigDecimal.valueOf(50)); |
| 151 | + BankAccountEntity fromEntity = new BankAccountEntity(); |
| 152 | + fromEntity.setNumber("A1"); |
| 153 | + fromEntity.setActualBalance(BigDecimal.valueOf(200)); |
| 154 | + fromEntity.setAvailableBalance(BigDecimal.valueOf(200)); |
| 155 | + BankAccountEntity toEntity = new BankAccountEntity(); |
| 156 | + toEntity.setNumber("A2"); |
| 157 | + toEntity.setActualBalance(BigDecimal.valueOf(50)); |
| 158 | + toEntity.setAvailableBalance(BigDecimal.valueOf(50)); |
| 159 | + when(bankAccountRepository.findByNumber("A1")).thenReturn(Optional.of(fromEntity)); |
| 160 | + when(bankAccountRepository.findByNumber("A2")).thenReturn(Optional.of(toEntity)); |
| 161 | + String transactionId = transactionService.internalFundTransfer(from, to, BigDecimal.valueOf(100)); |
| 162 | + assertNotNull(transactionId); |
| 163 | + verify(bankAccountRepository, times(2)).save(any(BankAccountEntity.class)); |
| 164 | + verify(transactionRepository, times(2)).save(any(TransactionEntity.class)); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void internalFundTransfer_entityNotFound() { |
| 169 | + BankAccount from = new BankAccount(); |
| 170 | + from.setNumber("A1"); |
| 171 | + BankAccount to = new BankAccount(); |
| 172 | + to.setNumber("A2"); |
| 173 | + when(bankAccountRepository.findByNumber("A1")).thenReturn(Optional.empty()); |
| 174 | + assertThrows(EntityNotFoundException.class, () -> transactionService.internalFundTransfer(from, to, BigDecimal.valueOf(100))); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + void validateBalance_throwsException() { |
| 179 | + BankAccount account = new BankAccount(); |
| 180 | + account.setNumber("A1"); |
| 181 | + account.setActualBalance(BigDecimal.valueOf(50)); |
| 182 | + assertThrows(InsufficientFundsException.class, () -> { |
| 183 | + transactionService.fundTransfer(new FundTransferRequest("A1", "A2", BigDecimal.valueOf(100))); |
| 184 | + }); |
| 185 | + } |
| 186 | +} |
| 187 | + |
0 commit comments