test: ExpenseService unit tests
This commit is contained in:
parent
3b2fd229a5
commit
0377d15501
2 changed files with 95 additions and 4 deletions
|
@ -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<Expense> payerExpenses = expenseRepository.findExpensesByPayerAndYear(loggedInPerson.getId(), yearValue);
|
||||
for (Expense expense : payerExpenses) {
|
||||
List<Expense> 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<Expense> beneficiaryExpenses = expenseRepository.findExpensesByBeneficiaryAndYear(loggedInPerson.getId(), yearValue);
|
||||
for (Expense expense : beneficiaryExpenses) {
|
||||
List<Expense> 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
|
||||
// ================================
|
||||
|
|
|
@ -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<Expense> 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<Expense> 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<Expense> 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<Expense> 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");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue