diff --git a/src/test/java/seedu/address/logic/commands/FindByEmailCommandTest.java b/src/test/java/seedu/address/logic/commands/FindByEmailCommandTest.java new file mode 100644 index 00000000000..709c98b39f1 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/FindByEmailCommandTest.java @@ -0,0 +1,95 @@ +package seedu.address.logic.commands; + +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.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import seedu.address.model.person.EmailContainsKeywordsPredicate; +import static seedu.address.testutil.TypicalPersons.CARL; +import static seedu.address.testutil.TypicalPersons.ELLE; +import static seedu.address.testutil.TypicalPersons.FIONA; +import static seedu.address.testutil.TypicalPersons.INA; +import static seedu.address.testutil.TypicalPersons.REG; +import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.jupiter.api.Test; + +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.model.person.ContactContainsKeywordsPredicate; +import seedu.address.model.person.NameContainsKeywordsPredicate; + +/** + * Contains integration tests (interaction with the Model) for {@code FindByContactCommand}. + */ +public class FindByEmailCommandTest { + private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + + @Test + public void equals() { + EmailContainsKeywordsPredicate firstPredicate = + new EmailContainsKeywordsPredicate(Collections.singletonList("first")); + EmailContainsKeywordsPredicate secondPredicate = + new EmailContainsKeywordsPredicate(Collections.singletonList("second")); + + FindByEmailCommand findFirstCommand = new FindByEmailCommand(firstPredicate); + FindByEmailCommand findSecondCommand = new FindByEmailCommand(secondPredicate); + + // same object -> returns true + assertTrue(findFirstCommand.equals(findFirstCommand)); + + // same values -> returns true + FindByEmailCommand findFirstCommandCopy = new FindByEmailCommand(firstPredicate); + assertTrue(findFirstCommand.equals(findFirstCommandCopy)); + + // different types -> returns false + assertFalse(findFirstCommand.equals(1)); + + // null -> returns false + assertFalse(findFirstCommand.equals(null)); + + // different person -> returns false + assertFalse(findFirstCommand.equals(findSecondCommand)); + } + + @Test + public void execute_zeroKeywords_noPersonFound() { + String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0); + EmailContainsKeywordsPredicate predicate = preparePredicate(" "); + FindByEmailCommand command = new FindByEmailCommand(predicate); + expectedModel.updateFilteredPersonList(predicate); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Collections.emptyList(), model.getFilteredPersonList()); + } + + @Test + public void execute_multipleKeywords_multiplePersonsFound() { + String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 2); + EmailContainsKeywordsPredicate predicate = preparePredicate("regina@example.com"); + FindByEmailCommand command = new FindByEmailCommand(predicate); + expectedModel.updateFilteredPersonList(predicate); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(REG, INA), model.getFilteredPersonList()); + } + + @Test + public void toStringMethod() { + NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword")); + FindByNameCommand findCommand = new FindByNameCommand(predicate); + String expected = FindByNameCommand.class.getCanonicalName() + "{predicate=" + predicate + "}"; + assertEquals(expected, findCommand.toString()); + } + + /** + * Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}. + */ + private EmailContainsKeywordsPredicate preparePredicate(String userInput) { + return new EmailContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+"))); + } +} \ No newline at end of file diff --git a/src/test/java/seedu/address/testutil/TypicalPersons.java b/src/test/java/seedu/address/testutil/TypicalPersons.java index fec76fb7129..77a42acd75f 100644 --- a/src/test/java/seedu/address/testutil/TypicalPersons.java +++ b/src/test/java/seedu/address/testutil/TypicalPersons.java @@ -48,6 +48,12 @@ public class TypicalPersons { public static final Person IDA = new PersonBuilder().withName("Ida Mueller").withPhone("8482131") .withEmail("hans@example.com").withAddress("chicago ave").build(); + // same emails for FindByEmailCommandTest + public static final Person REG = new PersonBuilder().withName("Reg Loo").withPhone("8482424") + .withEmail("regina@example.com").withAddress("little india").build(); + public static final Person INA = new PersonBuilder().withName("Ina Crust").withPhone("8482131") + .withEmail("regina@example.com").withAddress("chicago ave").build(); + // Manually added - Person's details found in {@code CommandTestUtil} public static final Person AMY = new PersonBuilder().withName(VALID_NAME_AMY).withPhone(VALID_PHONE_AMY) .withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY).withTags(VALID_TAG_FRIEND).build(); @@ -71,6 +77,6 @@ public static AddressBook getTypicalAddressBook() { } public static List getTypicalPersons() { - return new ArrayList<>(Arrays.asList(ALICE, BENSON, CARL, DANIEL, ELLE, FIONA, GEORGE)); + return new ArrayList<>(Arrays.asList(ALICE, BENSON, CARL, DANIEL, ELLE, FIONA, GEORGE, REG, INA)); } }