|
|
|
|
@@ -1,174 +0,0 @@
|
|
|
|
|
package de.financer.service;
|
|
|
|
|
|
|
|
|
|
import de.financer.ResponseReason;
|
|
|
|
|
import de.financer.config.FinancerConfig;
|
|
|
|
|
import de.financer.dba.TransactionRepository;
|
|
|
|
|
import de.financer.model.Account;
|
|
|
|
|
import org.junit.Assert;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
import org.mockito.InjectMocks;
|
|
|
|
|
import org.mockito.Mock;
|
|
|
|
|
import org.mockito.Mockito;
|
|
|
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
|
|
|
|
|
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
|
|
|
public class TransactionService_createTransactionTest {
|
|
|
|
|
@InjectMocks
|
|
|
|
|
private TransactionService classUnderTest;
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private AccountService accountService;
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private RuleService ruleService;
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private PeriodService periodService;
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private AccountStatisticService accountStatisticService;
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private TransactionRepository transactionRepository;
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private FinancerConfig financerConfig;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() {
|
|
|
|
|
Mockito.when(this.financerConfig.getDateFormat()).thenReturn("dd.MM.yyyy");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test_createTransaction_FROM_AND_TO_ACCOUNT_NOT_FOUND() {
|
|
|
|
|
// Arrange
|
|
|
|
|
// Nothing to do, if we do not instruct the account service instance to return anything the accounts
|
|
|
|
|
// will not be found.
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
final ResponseReason response = this.classUnderTest.createTransaction("account.invalid", "account.invalid", 150L, "24.02.2019", "XXX", false, null, null);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.assertEquals(ResponseReason.FROM_AND_TO_ACCOUNT_NOT_FOUND, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test_createTransaction_TO_ACCOUNT_NOT_FOUND() {
|
|
|
|
|
// Arrange
|
|
|
|
|
Mockito.when(this.accountService.getAccountByKey(Mockito.anyString())).thenReturn(createAccount(), null);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
final ResponseReason response = this.classUnderTest.createTransaction("account.from", "account.invalid", 150L, "24.02.2019", "XXX", false, null, null);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.assertEquals(ResponseReason.TO_ACCOUNT_NOT_FOUND, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test_createTransaction_FROM_ACCOUNT_NOT_FOUND() {
|
|
|
|
|
// Arrange
|
|
|
|
|
Mockito.when(this.accountService.getAccountByKey(Mockito.anyString())).thenReturn(null, createAccount());
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
final ResponseReason response = this.classUnderTest.createTransaction("account.invalid", "account.to", 150L, "24.02.2019", "XXX", false, null, null);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.assertEquals(ResponseReason.FROM_ACCOUNT_NOT_FOUND, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test_createTransaction_INVALID_BOOKING_ACCOUNTS() {
|
|
|
|
|
// Arrange
|
|
|
|
|
Mockito.when(this.accountService.getAccountByKey(Mockito.anyString())).thenReturn(createAccount(), createAccount());
|
|
|
|
|
Mockito.when(this.ruleService.isValidBooking(Mockito.any(Account.class), Mockito.any(Account.class))).thenReturn(Boolean.FALSE);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
final ResponseReason response = this.classUnderTest.createTransaction("account.from", "account.to", 150L, "24.02.2019", "XXX", false, null, null);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.assertEquals(ResponseReason.INVALID_BOOKING_ACCOUNTS, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test_createTransaction_MISSING_AMOUNT() {
|
|
|
|
|
// Arrange
|
|
|
|
|
Mockito.when(this.accountService.getAccountByKey(Mockito.anyString())).thenReturn(createAccount(), createAccount());
|
|
|
|
|
Mockito.when(this.ruleService.isValidBooking(Mockito.any(Account.class), Mockito.any(Account.class))).thenReturn(Boolean.TRUE);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
final ResponseReason response = this.classUnderTest.createTransaction("account.from", "account.to", null, "24.02.2019", "XXX", false, null, null);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.assertEquals(ResponseReason.MISSING_AMOUNT, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test_createTransaction_AMOUNT_ZERO() {
|
|
|
|
|
// Arrange
|
|
|
|
|
Mockito.when(this.accountService.getAccountByKey(Mockito.anyString())).thenReturn(createAccount(), createAccount());
|
|
|
|
|
Mockito.when(this.ruleService.isValidBooking(Mockito.any(Account.class), Mockito.any(Account.class))).thenReturn(Boolean.TRUE);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
final ResponseReason response = this.classUnderTest.createTransaction("account.from", "account.to", 0L, "24.02.2019", "XXX", false, null, null);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.assertEquals(ResponseReason.AMOUNT_ZERO, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test_createTransaction_MISSING_DATE() {
|
|
|
|
|
// Arrange
|
|
|
|
|
Mockito.when(this.accountService.getAccountByKey(Mockito.anyString())).thenReturn(createAccount(), createAccount());
|
|
|
|
|
Mockito.when(this.ruleService.isValidBooking(Mockito.any(Account.class), Mockito.any(Account.class))).thenReturn(Boolean.TRUE);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
final ResponseReason response = this.classUnderTest.createTransaction("account.from", "account.to", 125L, null, "XXX", false, null, null);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.assertEquals(ResponseReason.MISSING_DATE, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test_createTransaction_INVALID_DATE_FORMAT() {
|
|
|
|
|
// Arrange
|
|
|
|
|
Mockito.when(this.accountService.getAccountByKey(Mockito.anyString())).thenReturn(createAccount(), createAccount());
|
|
|
|
|
Mockito.when(this.ruleService.isValidBooking(Mockito.any(Account.class), Mockito.any(Account.class))).thenReturn(Boolean.TRUE);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
final ResponseReason response = this.classUnderTest.createTransaction("account.from", "account.to", 125L, "2019-01-01", "XXX", false, null, null);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.assertEquals(ResponseReason.INVALID_DATE_FORMAT, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test_createTransaction_OK() {
|
|
|
|
|
// Arrange
|
|
|
|
|
final Account fromAccount = Mockito.mock(Account.class);
|
|
|
|
|
final Account toAccount = Mockito.mock(Account.class);
|
|
|
|
|
Mockito.when(this.accountService.getAccountByKey(Mockito.anyString())).thenReturn(fromAccount, toAccount);
|
|
|
|
|
Mockito.when(this.ruleService.isValidBooking(Mockito.any(), Mockito.any())).thenReturn(Boolean.TRUE);
|
|
|
|
|
Mockito.when(this.ruleService.getMultiplierFromAccount(Mockito.any())).thenReturn(-1L);
|
|
|
|
|
Mockito.when(this.ruleService.getMultiplierToAccount(Mockito.any())).thenReturn(1L);
|
|
|
|
|
Mockito.when(fromAccount.getCurrentBalance()).thenReturn(0L);
|
|
|
|
|
Mockito.when(toAccount.getCurrentBalance()).thenReturn(0L);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
final ResponseReason response = this.classUnderTest.createTransaction("account.from", "account.to", 125L, "24.02.2019", "XXX", false, null, null);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.assertEquals(ResponseReason.CREATED, response);
|
|
|
|
|
Mockito.verify(fromAccount, Mockito.times(1)).setCurrentBalance((long) -125);
|
|
|
|
|
Mockito.verify(toAccount, Mockito.times(1)).setCurrentBalance(125L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Account createAccount() {
|
|
|
|
|
final Account account = new Account();
|
|
|
|
|
|
|
|
|
|
account.setCurrentBalance(0L);
|
|
|
|
|
|
|
|
|
|
return account;
|
|
|
|
|
}
|
|
|
|
|
}
|