From cb0a3f90302b312d9e20161285faeb38a32bbe2d Mon Sep 17 00:00:00 2001 From: yooplo Date: Thu, 10 Oct 2024 15:22:46 +0800 Subject: [PATCH] Add test cases for finding contacts by email --- .../commands/FindByEmailCommandTest.java | 95 +++++++++++++++++++ .../address/testutil/TypicalPersons.java | 8 +- 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/test/java/seedu/address/logic/commands/FindByEmailCommandTest.java 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)); } }