Skip to content

Commit

Permalink
Create JUnit tests for PersonFilter and FilterCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Fliprun committed Oct 12, 2023
1 parent 4b7abbd commit 4e171da
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 9 deletions.
47 changes: 38 additions & 9 deletions src/main/java/seedu/address/logic/commands/FilterCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ public CommandResult execute(Model model) {
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof FilterCommand)) {
return false;
}

FilterCommand otherFilterCommand = (FilterCommand) other;
return personFilter.equals(otherFilterCommand.personFilter);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("personFilter", personFilter)
.toString();
}

/**
* Stores the details to filter the contacts by. Contacts will be filtered by each non-empty field .
Expand All @@ -70,6 +91,14 @@ public static class PersonFilter {

public PersonFilter() {}

public PersonFilter(PersonFilter toCopy) {
setName(toCopy.name);
setPhone(toCopy.phone);
setEmail(toCopy.email);
setAddress(toCopy.address);
setTags(toCopy.tags);
}

/**
* Returns true if at least one field is edited.
*/
Expand Down Expand Up @@ -122,8 +151,8 @@ public void setTags(Set<Tag> tags) {
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code tags} is null.
*/
public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
public Set<Tag> getTags() {
return (tags != null) ? Collections.unmodifiableSet(tags) : null;
}

public boolean test(Person person) {
Expand Down Expand Up @@ -162,16 +191,16 @@ public boolean equals(Object other) {
}

// instanceof handles nulls
if (!(other instanceof PersonFilter)) {
if (!(other instanceof FilterCommand)) {
return false;
}

PersonFilter otherPersonFilterDescriptor = (PersonFilter) other;
return Objects.equals(name, otherPersonFilterDescriptor.name)
&& Objects.equals(phone, otherPersonFilterDescriptor.phone)
&& Objects.equals(email, otherPersonFilterDescriptor.email)
&& Objects.equals(address, otherPersonFilterDescriptor.address)
&& Objects.equals(tags, otherPersonFilterDescriptor.tags);
PersonFilter otherPersonFilter = (PersonFilter) other;
return name.equals(otherPersonFilter.getName())
&& phone.equals(otherPersonFilter.getPhone())
&& email.equals(otherPersonFilter.getEmail())
&& address.equals(otherPersonFilter.getAddress())
&& tags.equals(otherPersonFilter.getTags());
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/seedu/address/logic/commands/CommandTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;
import seedu.address.testutil.EditPersonDescriptorBuilder;
import seedu.address.testutil.PersonFilterBuilder;

/**
* Contains helper methods for testing commands.
Expand Down Expand Up @@ -60,13 +61,23 @@ public class CommandTestUtil {
public static final EditCommand.EditPersonDescriptor DESC_AMY;
public static final EditCommand.EditPersonDescriptor DESC_BOB;

public static final FilterCommand.PersonFilter FILTER_AMY;

public static final FilterCommand.PersonFilter FILTER_BOB;

static {
DESC_AMY = new EditPersonDescriptorBuilder().withName(VALID_NAME_AMY)
.withPhone(VALID_PHONE_AMY).withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY)
.withTags(VALID_TAG_FRIEND).build();
DESC_BOB = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB)
.withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_BOB).withAddress(VALID_ADDRESS_BOB)
.withTags(VALID_TAG_HUSBAND, VALID_TAG_FRIEND).build();
FILTER_AMY = new PersonFilterBuilder().withName(VALID_NAME_AMY)
.withPhone(VALID_PHONE_AMY).withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY)
.withTags(VALID_TAG_FRIEND).build();
FILTER_BOB = new PersonFilterBuilder().withName(VALID_NAME_BOB)
.withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_BOB).withAddress(VALID_ADDRESS_BOB)
.withTags(VALID_TAG_HUSBAND, VALID_TAG_FRIEND).build();
}

/**
Expand Down
76 changes: 76 additions & 0 deletions src/test/java/seedu/address/logic/commands/FilterCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package seedu.address.logic.commands;

import org.junit.jupiter.api.Test;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.testutil.PersonFilterBuilder;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;
import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW;
import static seedu.address.logic.commands.CommandTestUtil.*;
import static seedu.address.testutil.TypicalPersons.*;

/**
* Contains integration tests (interaction with the Model) for {@code FilterCommand}.
*/
class FilterCommandTest {

private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void execute_oneField_multiplePersonsDisplayed() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
FilterCommand.PersonFilter filter = new PersonFilterBuilder().withPhone("948").build();
FilterCommand command = new FilterCommand(filter);
expectedModel.updateFilteredPersonList(filter::test);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(ELLE, FIONA, GEORGE), model.getFilteredPersonList());
}
@Test
public void execute_multipleKeywords_onePersonFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 1);
FilterCommand.PersonFilter filter = new PersonFilterBuilder().withName("carl").withEmail("heinz").build();
FilterCommand command = new FilterCommand(filter);
expectedModel.updateFilteredPersonList(filter::test);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(CARL), model.getFilteredPersonList());
}
@Test
public void toStringMethod() {
FilterCommand.PersonFilter filter = new PersonFilterBuilder().build();
FilterCommand filterCommand = new FilterCommand(filter);
String expected = FilterCommand.class.getCanonicalName() + "{personFilter=" + filter + "}";
assertEquals(expected, filterCommand.toString());
}

@Test
public void equals() {
FilterCommand.PersonFilter firstFilter =
new FilterCommand.PersonFilter(FILTER_AMY);
FilterCommand.PersonFilter secondFilter =
new FilterCommand.PersonFilter(FILTER_BOB);

FilterCommand filterFirstCommand = new FilterCommand(firstFilter);
FilterCommand filterSecondCommand = new FilterCommand(secondFilter);

// same object -> returns true
assertTrue(filterFirstCommand.equals(filterFirstCommand));

// same values -> returns true
FilterCommand filterFirstCommandCopy = new FilterCommand(firstFilter);
assertTrue(filterFirstCommand.equals(filterFirstCommandCopy));

// different types -> returns false
assertFalse(filterFirstCommand.equals(1));

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

// different person -> returns false
assertFalse(filterFirstCommand.equals(filterSecondCommand));
}
}
57 changes: 57 additions & 0 deletions src/test/java/seedu/address/logic/commands/PersonFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package seedu.address.logic.commands;

import org.junit.jupiter.api.Test;
import seedu.address.logic.commands.FilterCommand.PersonFilter;
import seedu.address.testutil.PersonFilterBuilder;

import static org.junit.jupiter.api.Assertions.*;
import static seedu.address.logic.commands.CommandTestUtil.*;

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

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

// different types -> returns false
assertFalse(FILTER_AMY.equals(5));

// different values -> returns false
assertFalse(FILTER_AMY.equals(DESC_BOB));

// different name -> returns false
PersonFilter editedAmy = new PersonFilterBuilder(FILTER_AMY).withName(VALID_NAME_BOB).build();
assertFalse(FILTER_AMY.equals(editedAmy));

// different phone -> returns false
editedAmy = new PersonFilterBuilder(FILTER_AMY).withPhone(VALID_PHONE_BOB).build();
assertFalse(FILTER_AMY.equals(editedAmy));

// different email -> returns false
editedAmy = new PersonFilterBuilder(FILTER_AMY).withEmail(VALID_EMAIL_BOB).build();
assertFalse(FILTER_AMY.equals(editedAmy));

// different address -> returns false
editedAmy = new PersonFilterBuilder(FILTER_AMY).withAddress(VALID_ADDRESS_BOB).build();
assertFalse(FILTER_AMY.equals(editedAmy));

// different tags -> returns false
editedAmy = new PersonFilterBuilder(FILTER_AMY).withTags(VALID_TAG_HUSBAND).build();
assertFalse(FILTER_AMY.equals(editedAmy));
}

@Test
public void toStringMethod() {
PersonFilter personFilter = new PersonFilter();
String expected = PersonFilter.class.getCanonicalName() + "{name="
+ personFilter.getName() + ", phone="
+ personFilter.getPhone() + ", email="
+ personFilter.getEmail() + ", address="
+ personFilter.getAddress() + ", tags="
+ personFilter.getTags() + "}";
assertEquals(expected, personFilter.toString());
}
}
81 changes: 81 additions & 0 deletions src/test/java/seedu/address/testutil/PersonFilterBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package seedu.address.testutil;

import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.FilterCommand;
import seedu.address.model.person.*;
import seedu.address.model.tag.Tag;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PersonFilterBuilder {

private FilterCommand.PersonFilter filter;

public PersonFilterBuilder() {
filter = new FilterCommand.PersonFilter();
}

public PersonFilterBuilder(FilterCommand.PersonFilter filter) {
this.filter = new FilterCommand.PersonFilter(filter);
}

/**
* Returns an {@code EditPersonDescriptor} with fields containing {@code person}'s details
*/
public PersonFilterBuilder(Person person) {
filter = new FilterCommand.PersonFilter();
filter.setName(person.getName().fullName);
filter.setPhone(person.getPhone().value);
filter.setEmail(person.getEmail().value);
filter.setAddress(person.getAddress().value);
filter.setTags(person.getTags());
}

/**
* Sets the {@code Name} of the {@code EditPersonDescriptor} that we are building.
*/
public PersonFilterBuilder withName(String name) {
filter.setName(name);
return this;
}

/**
* Sets the {@code Phone} of the {@code EditPersonDescriptor} that we are building.
*/
public PersonFilterBuilder withPhone(String phone) {
filter.setPhone(phone);
return this;
}

/**
* Sets the {@code Email} of the {@code EditPersonDescriptor} that we are building.
*/
public PersonFilterBuilder withEmail(String email) {
filter.setEmail(email);
return this;
}

/**
* Sets the {@code Address} of the {@code EditPersonDescriptor} that we are building.
*/
public PersonFilterBuilder withAddress(String address) {
filter.setAddress(address);
return this;
}

/**
* Parses the {@code tags} into a {@code Set<Tag>} and set it to the {@code EditPersonDescriptor}
* that we are building.
*/
public PersonFilterBuilder withTags(String... tags) {
Set<Tag> tagSet = Stream.of(tags).map(Tag::new).collect(Collectors.toSet());
filter.setTags(tagSet);
return this;
}

public FilterCommand.PersonFilter build() {
return filter;
}
}

0 comments on commit 4e171da

Please sign in to comment.