diff --git a/src/main/java/de/financer/service/RecurringTransactionService.java b/src/main/java/de/financer/service/RecurringTransactionService.java index 53cd68d..fd9a477 100644 --- a/src/main/java/de/financer/service/RecurringTransactionService.java +++ b/src/main/java/de/financer/service/RecurringTransactionService.java @@ -380,6 +380,7 @@ public class RecurringTransactionService { recurringTransaction.getToAccount().getKey(), recurringTransaction.getAmount(), LocalDate.now().format(DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat())), - recurringTransaction.getDescription()); + recurringTransaction.getDescription(), + recurringTransaction); } } diff --git a/src/main/java/de/financer/service/TransactionService.java b/src/main/java/de/financer/service/TransactionService.java index 7c5012e..ff49233 100644 --- a/src/main/java/de/financer/service/TransactionService.java +++ b/src/main/java/de/financer/service/TransactionService.java @@ -4,6 +4,7 @@ import de.financer.ResponseReason; import de.financer.config.FinancerConfig; import de.financer.dba.TransactionRepository; import de.financer.model.Account; +import de.financer.model.RecurringTransaction; import de.financer.model.Transaction; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -54,7 +55,16 @@ public class TransactionService { } @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 toAccount = this.accountService.getAccountByKey(toAccountKey); ResponseReason response = validateParameters(fromAccount, toAccount, amount, date); @@ -65,7 +75,7 @@ public class TransactionService { } 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 .getMultiplierFromAccount(fromAccount) * amount)); @@ -95,10 +105,14 @@ public class TransactionService { * @param amount the transaction amount * @param description the description of the transaction * @param date the date of the transaction + * @param recurringTransaction the recurring transaction that caused the creation of this transaction, may be + * null * * @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(); transaction.setFromAccount(fromAccount); @@ -106,6 +120,7 @@ public class TransactionService { transaction.setAmount(amount); transaction.setDescription(description); transaction.setDate(LocalDate.parse(date, DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat()))); + transaction.setRecurringTransaction(recurringTransaction); return transaction; } diff --git a/src/main/resources/database/hsqldb/V1_0_0__init.sql b/src/main/resources/database/hsqldb/V1_0_0__init.sql index 1cfbe37..88b281a 100644 --- a/src/main/resources/database/hsqldb/V1_0_0__init.sql +++ b/src/main/resources/database/hsqldb/V1_0_0__init.sql @@ -37,7 +37,7 @@ CREATE TABLE "transaction" ( --escape keyword "transaction" "date" DATE NOT NULL, --escape keyword "date" description VARCHAR(1000), 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_to_account FOREIGN KEY (to_account_id) REFERENCES account (id), diff --git a/src/test/java/de/financer/controller/integration/AccountControllerIntegration_getAllTest.java b/src/test/java/de/financer/controller/integration/AccountController_getAllIntegrationTest.java similarity index 97% rename from src/test/java/de/financer/controller/integration/AccountControllerIntegration_getAllTest.java rename to src/test/java/de/financer/controller/integration/AccountController_getAllIntegrationTest.java index f34631e..cc1390c 100644 --- a/src/test/java/de/financer/controller/integration/AccountControllerIntegration_getAllTest.java +++ b/src/test/java/de/financer/controller/integration/AccountController_getAllIntegrationTest.java @@ -26,7 +26,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @AutoConfigureMockMvc @TestPropertySource( locations = "classpath:application-integrationtest.properties") -public class AccountControllerIntegration_getAllTest { +public class AccountController_getAllIntegrationTest { @Autowired private MockMvc mockMvc; diff --git a/src/test/java/de/financer/controller/integration/RecurringTransactionServiceIntegration_createRecurringTransactionTest.java b/src/test/java/de/financer/controller/integration/RecurringTransactionService_createRecurringTransactionIntegrationTest.java similarity index 97% rename from src/test/java/de/financer/controller/integration/RecurringTransactionServiceIntegration_createRecurringTransactionTest.java rename to src/test/java/de/financer/controller/integration/RecurringTransactionService_createRecurringTransactionIntegrationTest.java index 436e07c..4cb8119 100644 --- a/src/test/java/de/financer/controller/integration/RecurringTransactionServiceIntegration_createRecurringTransactionTest.java +++ b/src/test/java/de/financer/controller/integration/RecurringTransactionService_createRecurringTransactionIntegrationTest.java @@ -26,7 +26,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @AutoConfigureMockMvc @TestPropertySource( locations = "classpath:application-integrationtest.properties") -public class RecurringTransactionServiceIntegration_createRecurringTransactionTest { +public class RecurringTransactionService_createRecurringTransactionIntegrationTest { @Autowired private MockMvc mockMvc; diff --git a/src/test/java/de/financer/controller/integration/RecurringTransactionService_createTransactionIntegrationTest.java b/src/test/java/de/financer/controller/integration/RecurringTransactionService_createTransactionIntegrationTest.java new file mode 100644 index 0000000..c19d396 --- /dev/null +++ b/src/test/java/de/financer/controller/integration/RecurringTransactionService_createTransactionIntegrationTest.java @@ -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 allRecurringTransactions = this.objectMapper + .readValue(mvcResultAll.getResponse().getContentAsByteArray(), new TypeReference>() {}); + final Optional 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 allTransactions = this.objectMapper + .readValue(mvcResultAllTransactions.getResponse().getContentAsByteArray(), new TypeReference>() {}); + + Assert.assertEquals(1, allTransactions.size()); + } +}