Adjust reference from transaction to recurring transaction

- Make the reference nullable
- Add the reference when a transaction gets created by a recurring transaction
- Add overload method createTransaction as the reference is optional
- Fix integration tests name so they get picked up by surefire
- Add a integration test for the recurringTransaction -> transaction creation
This commit is contained in:
2019-03-08 19:37:06 +01:00
parent b6318c65d4
commit 24e9dcda35
6 changed files with 91 additions and 7 deletions

View File

@@ -380,6 +380,7 @@ public class RecurringTransactionService {
recurringTransaction.getToAccount().getKey(), recurringTransaction.getToAccount().getKey(),
recurringTransaction.getAmount(), recurringTransaction.getAmount(),
LocalDate.now().format(DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat())), LocalDate.now().format(DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat())),
recurringTransaction.getDescription()); recurringTransaction.getDescription(),
recurringTransaction);
} }
} }

View File

@@ -4,6 +4,7 @@ import de.financer.ResponseReason;
import de.financer.config.FinancerConfig; import de.financer.config.FinancerConfig;
import de.financer.dba.TransactionRepository; import de.financer.dba.TransactionRepository;
import de.financer.model.Account; import de.financer.model.Account;
import de.financer.model.RecurringTransaction;
import de.financer.model.Transaction; import de.financer.model.Transaction;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -54,7 +55,16 @@ public class TransactionService {
} }
@Transactional(propagation = Propagation.REQUIRED) @Transactional(propagation = Propagation.REQUIRED)
public ResponseReason createTransaction(String fromAccountKey, String toAccountKey, Long amount, String date, String description) { public ResponseReason createTransaction(String fromAccountKey, String toAccountKey, Long amount, String date,
String description)
{
return this.createTransaction(fromAccountKey, toAccountKey, amount, date, description, null);
}
@Transactional(propagation = Propagation.REQUIRED)
public ResponseReason createTransaction(String fromAccountKey, String toAccountKey, Long amount, String date,
String description, RecurringTransaction recurringTransaction
) {
final Account fromAccount = this.accountService.getAccountByKey(fromAccountKey); final Account fromAccount = this.accountService.getAccountByKey(fromAccountKey);
final Account toAccount = this.accountService.getAccountByKey(toAccountKey); final Account toAccount = this.accountService.getAccountByKey(toAccountKey);
ResponseReason response = validateParameters(fromAccount, toAccount, amount, date); ResponseReason response = validateParameters(fromAccount, toAccount, amount, date);
@@ -65,7 +75,7 @@ public class TransactionService {
} }
try { try {
final Transaction transaction = buildTransaction(fromAccount, toAccount, amount, description, date); final Transaction transaction = buildTransaction(fromAccount, toAccount, amount, description, date, recurringTransaction);
fromAccount.setCurrentBalance(fromAccount.getCurrentBalance() + (this.ruleService fromAccount.setCurrentBalance(fromAccount.getCurrentBalance() + (this.ruleService
.getMultiplierFromAccount(fromAccount) * amount)); .getMultiplierFromAccount(fromAccount) * amount));
@@ -95,10 +105,14 @@ public class TransactionService {
* @param amount the transaction amount * @param amount the transaction amount
* @param description the description of the transaction * @param description the description of the transaction
* @param date the date of the transaction * @param date the date of the transaction
* @param recurringTransaction the recurring transaction that caused the creation of this transaction, may be
* <code>null</code>
* *
* @return the build {@link Transaction} instance * @return the build {@link Transaction} instance
*/ */
private Transaction buildTransaction(Account fromAccount, Account toAccount, Long amount, String description, String date) { private Transaction buildTransaction(Account fromAccount, Account toAccount, Long amount, String description,
String date, RecurringTransaction recurringTransaction
) {
final Transaction transaction = new Transaction(); final Transaction transaction = new Transaction();
transaction.setFromAccount(fromAccount); transaction.setFromAccount(fromAccount);
@@ -106,6 +120,7 @@ public class TransactionService {
transaction.setAmount(amount); transaction.setAmount(amount);
transaction.setDescription(description); transaction.setDescription(description);
transaction.setDate(LocalDate.parse(date, DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat()))); transaction.setDate(LocalDate.parse(date, DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat())));
transaction.setRecurringTransaction(recurringTransaction);
return transaction; return transaction;
} }

View File

@@ -37,7 +37,7 @@ CREATE TABLE "transaction" ( --escape keyword "transaction"
"date" DATE NOT NULL, --escape keyword "date" "date" DATE NOT NULL, --escape keyword "date"
description VARCHAR(1000), description VARCHAR(1000),
amount BIGINT NOT NULL, amount BIGINT NOT NULL,
recurring_transaction_id BIGINT NOT NULL, recurring_transaction_id BIGINT,
CONSTRAINT fk_transaction_from_account FOREIGN KEY (from_account_id) REFERENCES account (id), CONSTRAINT fk_transaction_from_account FOREIGN KEY (from_account_id) REFERENCES account (id),
CONSTRAINT fk_transaction_to_account FOREIGN KEY (to_account_id) REFERENCES account (id), CONSTRAINT fk_transaction_to_account FOREIGN KEY (to_account_id) REFERENCES account (id),

View File

@@ -26,7 +26,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@AutoConfigureMockMvc @AutoConfigureMockMvc
@TestPropertySource( @TestPropertySource(
locations = "classpath:application-integrationtest.properties") locations = "classpath:application-integrationtest.properties")
public class AccountControllerIntegration_getAllTest { public class AccountController_getAllIntegrationTest {
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;

View File

@@ -26,7 +26,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@AutoConfigureMockMvc @AutoConfigureMockMvc
@TestPropertySource( @TestPropertySource(
locations = "classpath:application-integrationtest.properties") locations = "classpath:application-integrationtest.properties")
public class RecurringTransactionServiceIntegration_createRecurringTransactionTest { public class RecurringTransactionService_createRecurringTransactionIntegrationTest {
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;

View File

@@ -0,0 +1,68 @@
package de.financer.controller.integration;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.financer.FinancerApplication;
import de.financer.model.RecurringTransaction;
import de.financer.model.Transaction;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import java.util.List;
import java.util.Optional;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = FinancerApplication.class)
@AutoConfigureMockMvc
@TestPropertySource(
locations = "classpath:application-integrationtest.properties")
public class RecurringTransactionService_createTransactionIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Test
public void test_createTransaction() throws Exception {
final MvcResult mvcResultAll = this.mockMvc.perform(get("/recurringTransactions/getAll")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
final List<RecurringTransaction> allRecurringTransactions = this.objectMapper
.readValue(mvcResultAll.getResponse().getContentAsByteArray(), new TypeReference<List<RecurringTransaction>>() {});
final Optional<RecurringTransaction> optionalRecurringTransaction = allRecurringTransactions.stream().findFirst();
if (!optionalRecurringTransaction.isPresent()) {
Assert.fail("No recurring transaction found!");
}
this.mockMvc.perform(get("/recurringTransactions/createTransaction")
.param("recurringTransactionId", optionalRecurringTransaction.get().getId().toString()))
.andExpect(status().isOk())
.andReturn();
final MvcResult mvcResultAllTransactions = this.mockMvc.perform(get("/transactions/getAll")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
final List<Transaction> allTransactions = this.objectMapper
.readValue(mvcResultAllTransactions.getResponse().getContentAsByteArray(), new TypeReference<List<Transaction>>() {});
Assert.assertEquals(1, allTransactions.size());
}
}