From 0377d155010bdbc7bde79722b6102462b50fed89 Mon Sep 17 00:00:00 2001 From: effe Date: Tue, 10 Sep 2024 14:00:43 -0400 Subject: [PATCH] test: ExpenseService unit tests --- .../munera/services/ExpenseService.java | 9 +- .../munera/services/ExpenseServiceTest.java | 90 +++++++++++++++++++ 2 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/application/munera/services/ExpenseServiceTest.java diff --git a/src/main/java/com/application/munera/services/ExpenseService.java b/src/main/java/com/application/munera/services/ExpenseService.java index bed2480..94d03d2 100644 --- a/src/main/java/com/application/munera/services/ExpenseService.java +++ b/src/main/java/com/application/munera/services/ExpenseService.java @@ -192,17 +192,18 @@ public class ExpenseService { totalExpenses.addAll(bothExpenses); // Include these regardless of isPaid status // Fetch expenses where you are the payer (you owe money), filtered by year - List payerExpenses = expenseRepository.findExpensesByPayerAndYear(loggedInPerson.getId(), yearValue); - for (Expense expense : payerExpenses) { + List beneficiaryExpenses = expenseRepository.findExpensesByBeneficiaryAndYear(loggedInPerson.getId(), yearValue); + for (Expense expense : beneficiaryExpenses) { if (!totalExpenses.contains(expense)) totalExpenses.add(expense); } // Fetch expenses where you are the beneficiary and not paid (amount owed to you), filtered by year - List beneficiaryExpenses = expenseRepository.findExpensesByBeneficiaryAndYear(loggedInPerson.getId(), yearValue); - for (Expense expense : beneficiaryExpenses) { + List payerExpenses = expenseRepository.findExpensesByPayerAndYear(loggedInPerson.getId(), yearValue); + for (Expense expense : payerExpenses) { if (Boolean.FALSE.equals(expense.getIsPaid()) && !totalExpenses.contains(expense)) totalExpenses.add(expense); } return totalExpenses; } + // ================================ // Private methods // ================================ diff --git a/src/test/java/com/application/munera/services/ExpenseServiceTest.java b/src/test/java/com/application/munera/services/ExpenseServiceTest.java new file mode 100644 index 0000000..0b2e126 --- /dev/null +++ b/src/test/java/com/application/munera/services/ExpenseServiceTest.java @@ -0,0 +1,90 @@ +package com.application.munera.services; + +import com.application.munera.data.Expense; +import com.application.munera.data.Person; +import com.application.munera.repositories.ExpenseRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import java.time.Year; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +class ExpenseServiceTest { + @Mock + private ExpenseRepository expenseRepository; + + @Mock + private Person loggedInPerson; + + @InjectMocks + private ExpenseService expenseService; + + private Year year; + + @BeforeEach + public void setUp() { + year = Year.of(2023); + when(loggedInPerson.getId()).thenReturn(1L); + } + + @Test + public void testFetchExpensesForDashboard_NoExpenses() { + when(expenseRepository.findExpensesByPayerAndBeneficiaryAndYear(loggedInPerson.getId(), year.getValue())) + .thenReturn(new ArrayList<>()); + when(expenseRepository.findExpensesByBeneficiaryAndYear(loggedInPerson.getId(), year.getValue())) + .thenReturn(new ArrayList<>()); + when(expenseRepository.findExpensesByPayerAndYear(loggedInPerson.getId(), year.getValue())) + .thenReturn(new ArrayList<>()); + + List result = expenseService.fetchExpensesForDashboard(loggedInPerson, year); + + assertEquals(0, result.size(), "Expected no expenses to be fetched"); + } + + @Test + void testFetchExpensesForDashboard_WithSelfExpenses() { + Expense selfExpense = new Expense(); // Create a dummy Expense object + List bothExpenses = List.of(selfExpense); + when(expenseRepository.findExpensesByPayerAndBeneficiaryAndYear(loggedInPerson.getId(), year.getValue())) + .thenReturn(bothExpenses); + when(expenseRepository.findExpensesByBeneficiaryAndYear(loggedInPerson.getId(), year.getValue())) + .thenReturn(new ArrayList<>()); + when(expenseRepository.findExpensesByPayerAndYear(loggedInPerson.getId(), year.getValue())) + .thenReturn(new ArrayList<>()); + + List result = expenseService.fetchExpensesForDashboard(loggedInPerson, year); + + assertEquals(1, result.size(), "Expected one self-expense to be fetched"); + assertEquals(selfExpense, result.get(0), "Expected the self-expense to match"); + } + + @Test + void testFetchExpensesForDashboard_WithUnpaidExpenses() { + Expense unpaidExpense = new Expense(); + unpaidExpense.setIsPaid(false); + when(expenseRepository.findExpensesByPayerAndBeneficiaryAndYear(loggedInPerson.getId(), year.getValue())) + .thenReturn(new ArrayList<>()); + when(expenseRepository.findExpensesByBeneficiaryAndYear(loggedInPerson.getId(), year.getValue())) + .thenReturn(new ArrayList<>()); + when(expenseRepository.findExpensesByPayerAndYear(loggedInPerson.getId(), year.getValue())) + .thenReturn(List.of(unpaidExpense)); + + List result = expenseService.fetchExpensesForDashboard(loggedInPerson, year); + + assertEquals(1, result.size(), "Expected one unpaid expense to be fetched"); + assertEquals(unpaidExpense, result.getFirst(), "Expected the unpaid expense to match"); + } +} \ No newline at end of file