diff --git a/src/main/java/seedu/address/logic/commands/FilterCommand.java b/src/main/java/seedu/address/logic/commands/FilterCommand.java index bf5f8fda3d6..c98b7b2c141 100644 --- a/src/main/java/seedu/address/logic/commands/FilterCommand.java +++ b/src/main/java/seedu/address/logic/commands/FilterCommand.java @@ -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 . @@ -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. */ @@ -122,8 +151,8 @@ public void setTags(Set tags) { * if modification is attempted. * Returns {@code Optional#empty()} if {@code tags} is null. */ - public Optional> getTags() { - return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty(); + public Set getTags() { + return (tags != null) ? Collections.unmodifiableSet(tags) : null; } public boolean test(Person person) { @@ -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 diff --git a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java index 643a1d08069..c96621b760c 100644 --- a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java +++ b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java @@ -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. @@ -60,6 +61,10 @@ 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) @@ -67,6 +72,12 @@ public class CommandTestUtil { 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(); } /** diff --git a/src/test/java/seedu/address/logic/commands/FilterCommandTest.java b/src/test/java/seedu/address/logic/commands/FilterCommandTest.java new file mode 100644 index 00000000000..5d5ed68d4f1 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/FilterCommandTest.java @@ -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)); + } +} \ No newline at end of file diff --git a/src/test/java/seedu/address/logic/commands/PersonFilterTest.java b/src/test/java/seedu/address/logic/commands/PersonFilterTest.java new file mode 100644 index 00000000000..3d54c967283 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/PersonFilterTest.java @@ -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()); + } +} \ No newline at end of file diff --git a/src/test/java/seedu/address/testutil/PersonFilterBuilder.java b/src/test/java/seedu/address/testutil/PersonFilterBuilder.java new file mode 100644 index 00000000000..6a178b7ad13 --- /dev/null +++ b/src/test/java/seedu/address/testutil/PersonFilterBuilder.java @@ -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} and set it to the {@code EditPersonDescriptor} + * that we are building. + */ + public PersonFilterBuilder withTags(String... tags) { + Set tagSet = Stream.of(tags).map(Tag::new).collect(Collectors.toSet()); + filter.setTags(tagSet); + return this; + } + + public FilterCommand.PersonFilter build() { + return filter; + } +}