Skip to content

Commit

Permalink
Merge pull request #34 from Alfred-Goh02/branch-J-Unit
Browse files Browse the repository at this point in the history
Add JUnit test Validator and Manager
  • Loading branch information
Alfred-Goh02 authored Oct 12, 2024
2 parents 8418614 + 76c039f commit c29adf5
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/seedu/budgetbuddy/commands/AddExpenseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@ public static boolean isCommand(String command) {
public void execute() {
ExpenseManager.addExpense(new Expense(description, amount, date, category));
}

public String getDescription() {
return description;
}

public double getAmount() {
return amount;
}

public LocalDate getDate() {
return date;
}

public Category getCategory() {
return category;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public IncorrectCommand(String feedbackToUser) {
public void execute () {
Ui.displayToUser(feedbackToUser);
}

public String getFeedbackToUser() {
return feedbackToUser;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package seedu.budgetbuddy.transaction.income;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class IncomeManagerTest {

private IncomeManager incomeManager;
private ArrayList<Income> incomes;
private int numberOfIncomes;

@BeforeEach
void initializeTestContent() {
incomes = new ArrayList<>();
numberOfIncomes = 0;
incomeManager = new IncomeManager(incomes, numberOfIncomes);
}

@Test
void addIncome_validIncome_incomeAddedSuccessfully() {
Income income = new Income("Salary", 5000, LocalDate.of(2024, 10, 1));
incomeManager.addIncome(income);

assertEquals(1, incomeManager.getNumberOfIncomes());
assertEquals(income, incomeManager.getIncomes().get(0));
}

@Test
void deleteIncome_validIndex_incomeDeletedSuccessfully() {
Income income = new Income("Salary", 5000, LocalDate.of(2024, 10, 1));
incomeManager.addIncome(income);

// Now deleting the income at index 0 (since it's the first item)
incomeManager.deleteIncome(0);

assertEquals(0, incomeManager.getNumberOfIncomes());
assertTrue(incomeManager.getIncomes().isEmpty());
}

@Test
void listIncomes_withIncomes_displaysIncomes() {
Income income1 = new Income("Salary", 5000, LocalDate.of(2024, 10, 1));
Income income2 = new Income("Bonus", 1000, LocalDate.of(2024, 10, 15));
incomeManager.addIncome(income1);
incomeManager.addIncome(income2);

// Test output to UI, this can be validated through mocking the Ui class
incomeManager.listIncomes();
// assert the UI displays correct output
}

@Test
void displayIncomeWithMonth_emptyMonth_displaysNoIncomeMessage() {
YearMonth month = YearMonth.of(2024, 9);
incomeManager.displayIncomeWithMonth(month);

// assert the UI displays the empty message
}

@Test
void getYearMonthFromDate_validDate_returnsCorrectYearMonth() {
LocalDate date = LocalDate.of(2024, 10, 1);
YearMonth expected = YearMonth.of(2024, 10);
assertEquals(expected, IncomeManager.getYearMonthFromDate(date));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package seedu.budgetbuddy.validators;

import org.junit.jupiter.api.Test;
import seedu.budgetbuddy.commands.AddExpenseCommand;
import seedu.budgetbuddy.commands.Command;
import seedu.budgetbuddy.commands.IncorrectCommand;
import seedu.budgetbuddy.transaction.expense.Category;

import java.time.LocalDate;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class AddExpenseValidatorTest {

@Test
void processCommand_invalidAmount_returnsIncorrectCommand() {
Command result = AddExpenseValidator.processCommand("add expense Lunch a/abc d/12/10/2024 c/FOOD");

// Check if result is an IncorrectCommand and compare the message
assertTrue(result instanceof IncorrectCommand);
String message = ((IncorrectCommand) result).getFeedbackToUser();
assertEquals("Invalid amount format. Amount should be a positive number.", message);
}

@Test
void processCommand_noDescription_returnsIncorrectCommand() {
Command result = AddExpenseValidator.processCommand("add expense a/100 d/12/10/2024 c/FOOD");

// Check if result is an IncorrectCommand and compare the message
assertTrue(result instanceof IncorrectCommand);
String message = ((IncorrectCommand) result).getFeedbackToUser();
assertEquals("Description cannot be empty.", message);
}

@Test
void processCommand_invalidDate_returnsIncorrectCommand() {
Command result = AddExpenseValidator.processCommand("add expense Lunch a/100 d/invalid_date c/FOOD");

// Check if result is an IncorrectCommand and compare the message
assertTrue(result instanceof IncorrectCommand);
String message = ((IncorrectCommand) result).getFeedbackToUser();
assertEquals("Invalid date format. Use d/dd/MM/yyyy.", message);
}

@Test
void processCommand_noAmount_returnsIncorrectCommand() {
Command result = AddExpenseValidator.processCommand("add expense Lunch d/12/10/2024 c/FOOD");

// Check if result is an IncorrectCommand and compare the message
assertTrue(result instanceof IncorrectCommand);
String message = ((IncorrectCommand) result).getFeedbackToUser();
assertEquals("Amount not entered.", message);
}

@Test
void processCommand_negativeAmount_returnsIncorrectCommand() {
Command result = AddExpenseValidator.processCommand("add expense Lunch a/-50 d/12/10/2024 c/FOOD");

// Check if result is an IncorrectCommand and compare the message
assertTrue(result instanceof IncorrectCommand);
String message = ((IncorrectCommand) result).getFeedbackToUser();
assertEquals("Amount must be a positive value.", message);
}


@Test
void processCommand_validCommand_returnsAddExpenseCommand() {
Command result = AddExpenseValidator.processCommand("add expense Lunch a/100 d/12/10/2024 c/FOOD");

// Check if the result is an AddExpenseCommand (a valid command)
assertTrue(result instanceof AddExpenseCommand);
AddExpenseCommand command = (AddExpenseCommand) result;

// Verify the details of the created command
assertEquals("Lunch", command.getDescription());
assertEquals(100, command.getAmount());
assertEquals(LocalDate.of(2024, 10, 12), command.getDate());
assertEquals(Category.FOOD, command.getCategory());
}
}

0 comments on commit c29adf5

Please sign in to comment.