Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add junit test for loan class #195

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/test/java/seedu/address/model/person/LoanTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package seedu.address.model.person;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.TypicalLoans.ACTIVE_NON_OVERDUE_LOAN;
import static seedu.address.testutil.TypicalLoans.INACTIVE_LOAN;

import java.math.BigDecimal;

import org.junit.jupiter.api.Test;

import seedu.address.commons.util.DateUtil;
import seedu.address.testutil.LoanBuilder;

public class LoanTest {

@Test
public void equals() {
// same object -> returns true
assertTrue(ACTIVE_NON_OVERDUE_LOAN.equals(ACTIVE_NON_OVERDUE_LOAN));

// null -> returns false
assertFalse(ACTIVE_NON_OVERDUE_LOAN.equals(null));

// same name, all other attributes different -> returns true
Loan editedActiveNonOverdueLoan = new LoanBuilder(ACTIVE_NON_OVERDUE_LOAN).withValue(INACTIVE_LOAN.getValue())
.withStartDate(INACTIVE_LOAN.getStartDate()).withReturnDate(INACTIVE_LOAN.getReturnDate())
.withIsReturned(INACTIVE_LOAN.isReturned()).withAssignee(INACTIVE_LOAN.getAssignee()).build();
assertTrue(ACTIVE_NON_OVERDUE_LOAN.equals(editedActiveNonOverdueLoan));

// different name, all other attributes same -> returns false
editedActiveNonOverdueLoan = new LoanBuilder(ACTIVE_NON_OVERDUE_LOAN).withId(6969).build();
assertFalse(ACTIVE_NON_OVERDUE_LOAN.equals(editedActiveNonOverdueLoan));
}

@Test
public void isValidValue() {
// value is zero -> returns false
assertFalse(Loan.isValidValue(BigDecimal.ZERO));

// value is negative -> returns false
assertFalse(Loan.isValidValue(new BigDecimal("-1.00")));

// value is positive -> returns true
assertTrue(Loan.isValidValue(new BigDecimal("1.00")));

// value is very large number -> returns true
assertTrue(Loan.isValidValue(new BigDecimal("11235638206.00")));
}

@Test
public void markAsReturned() {
Loan loanCopy = new LoanBuilder(ACTIVE_NON_OVERDUE_LOAN).build();
loanCopy.markAsReturned();
assertTrue(loanCopy.isReturned());
}

@Test
public void unmarkAsReturned() {
Loan loanCopy = new LoanBuilder(INACTIVE_LOAN).build();
loanCopy.unmarkAsReturned();
assertFalse(loanCopy.isReturned());
}

@Test
public void toStringMethod() {
String expected = String.format("$%.2f, %s, %s",
ACTIVE_NON_OVERDUE_LOAN.getValue(),
DateUtil.format(ACTIVE_NON_OVERDUE_LOAN.getStartDate()),
DateUtil.format(ACTIVE_NON_OVERDUE_LOAN.getReturnDate()));
assertEquals(expected, ACTIVE_NON_OVERDUE_LOAN.toString());

expected = String.format("$%.2f, %s, %s (Returned)",
INACTIVE_LOAN.getValue(),
DateUtil.format(INACTIVE_LOAN.getStartDate()),
DateUtil.format(INACTIVE_LOAN.getReturnDate()));
assertEquals(expected, INACTIVE_LOAN.toString());
}

}
133 changes: 133 additions & 0 deletions src/test/java/seedu/address/testutil/LoanBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package seedu.address.testutil;

import java.math.BigDecimal;
import java.util.Date;

import seedu.address.commons.util.DateUtil;
import seedu.address.model.person.Loan;
import seedu.address.model.person.Person;

/**
* A utility class to help with building Person objects.
*/
public class LoanBuilder {

public static final int DEFAULT_ID = 9999;
public static final BigDecimal DEFAULT_VALUE = new BigDecimal("100.00");
public static final String DEFAULT_START_DATE = "2020-01-01";
public static final String DEFAULT_RETURN_DATE = "2030-02-01";
public static final boolean DEFAULT_IS_RETURNED = false;
public static final Person DEFAULT_ASSIGNEE = new PersonBuilder().build();

private int id;
private BigDecimal value;
private Date startDate;
private Date returnDate;
private boolean isReturned;
private Person assignee;

/**
* Creates a {@code LoanBuilder} with the default details.
*/
public LoanBuilder() {
id = DEFAULT_ID;
value = DEFAULT_VALUE;
isReturned = DEFAULT_IS_RETURNED;
assignee = DEFAULT_ASSIGNEE;
try {
startDate = DateUtil.parse(DEFAULT_START_DATE);
returnDate = DateUtil.parse(DEFAULT_RETURN_DATE);
} catch (Exception e) {
System.out.println("THIS SHOULD NOT HAPPEN");
}
}

/**
* Initializes the LoanBuilder with the data of {@code loanToCopy}.
*/
public LoanBuilder(Loan loan) {
id = loan.getId();
value = loan.getValue();
startDate = loan.getStartDate();
returnDate = loan.getReturnDate();
isReturned = loan.isReturned();
assignee = loan.getAssignee();
}

/**
* Sets the {@code id} of the {@code Loan} that we are building.
*/
public LoanBuilder withId(int id) {
this.id = id;
return this;
}

/**
* Parses the {@code value} of the {@code Loan} that we are building.
*/
public LoanBuilder withValue(BigDecimal value) {
this.value = value;
return this;
}

/**
* Sets the {@code startDate} of the {@code Loan} that we are building.
*/
public LoanBuilder withStartDate(String startDate) {
try {
this.startDate = DateUtil.parse(startDate);
} catch (Exception e) {
System.out.println("THIS SHOULD NOT HAPPEN");
}
return this;
}

/**
* Sets the {@code startDate} of the {@code Loan} that we are building.
*/
public LoanBuilder withStartDate(Date startDate) {
this.startDate = startDate;
return this;
}

/**
* Sets the {@code returnDate} of the {@code Loan} that we are building.
*/
public LoanBuilder withReturnDate(String returnDate) {
try {
this.returnDate = DateUtil.parse(returnDate);
} catch (Exception e) {
System.out.println("THIS SHOULD NOT HAPPEN");
}
return this;
}

/**
* Sets the {@code returnDate} of the {@code Loan} that we are building.
*/
public LoanBuilder withReturnDate(Date returnDate) {
this.returnDate = returnDate;
return this;
}

/**
* Sets the {@code isReturned} of the {@code Loan} that we are building.
*/
public LoanBuilder withIsReturned(boolean isReturned) {
this.isReturned = isReturned;
return this;
}

/**
* Sets the {@code assignee} of the {@code Loan} that we are building.
*/
public LoanBuilder withAssignee(Person assignee) {
this.assignee = assignee;
return this;
}

public Loan build() {
return new Loan(id, value, startDate, returnDate, isReturned, assignee);
}

}
32 changes: 32 additions & 0 deletions src/test/java/seedu/address/testutil/TypicalLoans.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package seedu.address.testutil;

import static seedu.address.testutil.TypicalPersons.BOB;
import static seedu.address.testutil.TypicalPersons.CARL;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import seedu.address.model.person.Loan;

/**
* A utility class containing a list of {@code Person} objects to be used in tests.
*/
public class TypicalLoans {

public static final Loan ACTIVE_NON_OVERDUE_LOAN = new LoanBuilder().build();
public static final Loan ACTIVE_OVERDUE_LOAN = new LoanBuilder().withId(9998)
.withValue(new BigDecimal("1000.00")).withStartDate("2020-06-01").withReturnDate("2021-01-01")
.withAssignee(BOB).build();
public static final Loan INACTIVE_LOAN = new LoanBuilder().withId(9997)
.withValue(new BigDecimal("1123.00")).withStartDate("2020-06-01").withReturnDate("2040-12-01")
.withAssignee(CARL).withIsReturned(true).build();

private TypicalLoans() {} // prevents instantiation

public static List<Loan> getTypicalLoans() {
return new ArrayList<>(Arrays.asList(ACTIVE_NON_OVERDUE_LOAN, ACTIVE_OVERDUE_LOAN, INACTIVE_LOAN));
}

}
Loading