diff --git a/test/java/seedu/addressbook/commands/ViewAllCommandTest.java b/test/java/seedu/addressbook/commands/ViewAllCommandTest.java new file mode 100644 index 000000000..fc1ee66fa --- /dev/null +++ b/test/java/seedu/addressbook/commands/ViewAllCommandTest.java @@ -0,0 +1,7 @@ +package seedu.addressbook.commands; + +public class ViewAllCommandTest { + // ViewAllCommand is tested together with ViewCommand in ViewCommandTest. + // This is because they function similarly but ViewCommand hides private information. + // They are tested with same test data input. +} diff --git a/test/java/seedu/addressbook/commands/ViewCommandTest.java b/test/java/seedu/addressbook/commands/ViewCommandTest.java new file mode 100644 index 000000000..3408463c1 --- /dev/null +++ b/test/java/seedu/addressbook/commands/ViewCommandTest.java @@ -0,0 +1,154 @@ +package seedu.addressbook.commands; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +import org.junit.Test; + +import seedu.addressbook.common.Messages; +import seedu.addressbook.data.AddressBook; +import seedu.addressbook.data.person.Address; +import seedu.addressbook.data.person.Email; +import seedu.addressbook.data.person.Name; +import seedu.addressbook.data.person.Person; +import seedu.addressbook.data.person.Phone; +import seedu.addressbook.data.person.ReadOnlyPerson; +import seedu.addressbook.data.tag.UniqueTagList; +import seedu.addressbook.util.TestUtil; +import seedu.addressbook.util.TypicalPersons; + +public class ViewCommandTest { + private TypicalPersons td = new TypicalPersons(); + + private AddressBook typicalAddressBook = td.getTypicalAddressBook(); + private AddressBook emptyAddressBook = TestUtil.createAddressBook(); + private List emptyPersonList = Collections.emptyList(); + private List listWithAllTypicalPersons = Arrays.asList(td.getTypicalPersons()); + private List listWithSomeTypicalPersons = Arrays.asList(td.amy, td.candy, td.dan); + + @Test + public void execute_invalidIndex_returnsInvalidIndexMessage() { + // empty addressbook + assertViewErrorInvalidIndex(emptyAddressBook, emptyPersonList, 1); + + // non-empty addressbook + assertViewErrorInvalidIndex(typicalAddressBook, listWithAllTypicalPersons, -1); + assertViewErrorInvalidIndex(typicalAddressBook, listWithAllTypicalPersons, 0); + assertViewErrorInvalidIndex(typicalAddressBook, listWithAllTypicalPersons, + listWithAllTypicalPersons.size() + 1); + } + + @Test + public void execute_personNotInAddressBook_returnsPersonNotInAddressBookMessage() throws Exception { + // generate list with person not in addressbook, add to list + ReadOnlyPerson stranger = new Person(new Name("me"), + new Phone("123", true), + new Email("some@hey.go", true), + new Address("nus", false), + new UniqueTagList(Collections.emptySet())); + List listWithExtraPerson + = new ArrayList(listWithAllTypicalPersons); + listWithExtraPerson.add(stranger); + + // empty addressbook + assertViewErrorPersonNotInAddressBook(emptyAddressBook, listWithExtraPerson, 1); + + // non-empty addressbook + assertViewErrorPersonNotInAddressBook(typicalAddressBook, listWithExtraPerson, + listWithExtraPerson.size()); + } + + @Test + public void execute_validIndex_returnsPersonDetails() { + // person with no private information + assertViewSuccess(typicalAddressBook, listWithAllTypicalPersons, 1); + + // person with some private information + assertViewSuccess(typicalAddressBook, listWithAllTypicalPersons, 2); + + // person with all private information + assertViewSuccess(typicalAddressBook, listWithAllTypicalPersons, 4); + + // Addressbook has more people than the list. + // This can happen when a command causes the list to show only a sub-set of persons(e.g. FindCommand). + assertViewSuccess(typicalAddressBook, listWithSomeTypicalPersons, 1); + } + + /** + * Asserts that the details of person at specific index cannot be retrieved due to + * invalid index. + */ + private void assertViewErrorInvalidIndex(AddressBook addressBook, List relevantPersons, + int targetVisibleIndex) { + assertViewError(addressBook, relevantPersons, targetVisibleIndex, + Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + + /** + * Asserts that the details of person at specific index cannot be retrieved due to + * person not existing in the addressbook. + */ + private void assertViewErrorPersonNotInAddressBook(AddressBook addressBook, List relevantPersons, + int targetVisibleIndex) { + assertViewError(addressBook, relevantPersons, targetVisibleIndex, + Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK); + } + + /** + * Asserts that both a ViewCommand and a ViewAllCommand can retrieve from + * the {@code addressBook} details of the person at the given {@code targetVisibleIndex} + * in the given {@code relevantPersons} list. + * + * @param targetVisibleIndex one-indexed position of the target person in the list + */ + private void assertViewSuccess(AddressBook addressBook, List relevantPersons, + int targetVisibleIndex) { + // get person to be viewed (targetVisibleIndex - 1 because index is one-indexed) + ReadOnlyPerson personToBeViewed = relevantPersons.get(targetVisibleIndex - 1); + + String expectedMessage = String.format(ViewCommand.MESSAGE_VIEW_PERSON_DETAILS, + personToBeViewed.getAsTextHidePrivate()); + assertViewBehavior(new ViewCommand(targetVisibleIndex), addressBook, relevantPersons, expectedMessage); + + expectedMessage = String.format(ViewAllCommand.MESSAGE_VIEW_PERSON_DETAILS, + personToBeViewed.getAsTextShowAll()); + assertViewBehavior(new ViewAllCommand(targetVisibleIndex), addressBook, relevantPersons, expectedMessage); + } + + /** + * Asserts that the Viewcommand and ViewAllcommand reports the given error for the given input. + */ + private static void assertViewError(AddressBook addressBook, List relevantPersons, + int targetVisibleIndex, String expectedMessage) { + assertViewBehavior(new ViewCommand(targetVisibleIndex), addressBook, relevantPersons, expectedMessage); + assertViewBehavior(new ViewAllCommand(targetVisibleIndex), addressBook, relevantPersons, expectedMessage); + } + + /** + * Executes the test command for the given addressbook data. + * Checks that ViewCommand and ViewAllCommand exhibits the correct command behavior, namely: + * 1. The feedback message of the CommandResult it returns matches expectedMessage. + * 2. The CommandResult it returns has no relevant persons. + * 3. The original addressbook data is not modified after executing ViewCommand and ViewAllCommand. + */ + private static void assertViewBehavior(Command viewCommand, AddressBook addressBook, + List relevantPersons, String expectedMessage) { + AddressBook expectedAddressBook = TestUtil.clone(addressBook); + + viewCommand.setData(addressBook, relevantPersons); + CommandResult result = viewCommand.execute(); + + // feedback message is as expected and there are no relevant persons returned. + assertEquals(expectedMessage, result.feedbackToUser); + assertEquals(Optional.empty(), result.getRelevantPersons()); + + // addressbook was not modified. + assertEquals(expectedAddressBook.getAllPersons(), addressBook.getAllPersons()); + } + +} diff --git a/test/java/seedu/addressbook/util/TestUtil.java b/test/java/seedu/addressbook/util/TestUtil.java index 8909c437b..cda6c44db 100644 --- a/test/java/seedu/addressbook/util/TestUtil.java +++ b/test/java/seedu/addressbook/util/TestUtil.java @@ -29,13 +29,16 @@ public class TestUtil { /** * Creates an address book containing the given persons. - * @throws DuplicatePersonException if two or more given persons are the same */ - public static AddressBook createAddressBook(Person... persons) throws DuplicatePersonException { + public static AddressBook createAddressBook(Person... persons) { AddressBook addressBook = new AddressBook(); for (Person person : persons) { - addressBook.addPerson(person); + try { + addressBook.addPerson(person); + } catch (DuplicatePersonException e) { + throw new AssertionError(e); + } } return addressBook; diff --git a/test/java/seedu/addressbook/util/TypicalPersons.java b/test/java/seedu/addressbook/util/TypicalPersons.java index 64e0d99c8..7c03f6fa5 100644 --- a/test/java/seedu/addressbook/util/TypicalPersons.java +++ b/test/java/seedu/addressbook/util/TypicalPersons.java @@ -7,6 +7,7 @@ import seedu.addressbook.data.person.Name; import seedu.addressbook.data.person.Person; import seedu.addressbook.data.person.Phone; +import seedu.addressbook.data.tag.Tag; import seedu.addressbook.data.tag.UniqueTagList; /** @@ -14,16 +15,18 @@ */ public class TypicalPersons { - public Person amy, bill, candy; + public Person amy, bill, candy, dan; public TypicalPersons() { try { amy = new Person(new Name("Amy Buck"), new Phone("91119111", false), new Email("ab@gmail.com", false), new Address("1 Clementi Road", false), new UniqueTagList()); bill = new Person(new Name("Bill Clint"), new Phone("92229222", false), new Email("bc@gmail.com", false), - new Address("2 Clementi Road", false), new UniqueTagList()); - candy = new Person(new Name("Candy Destiny"), new Phone("93339333", false), - new Email("cd@gmail.com", false), new Address("3 Clementi Road", false), new UniqueTagList()); + new Address("2 Clementi Road", true), new UniqueTagList()); + candy = new Person(new Name("Candy Destiny"), new Phone("93339333", true), + new Email("cd@gmail.com", false), new Address("3 Clementi Road", true), new UniqueTagList()); + dan = new Person(new Name("Dan Smith"), new Phone("1234556", true), new Email("ss@tt.com", true), + new Address("NUS", true), new UniqueTagList(new Tag("Test"))); } catch (IllegalValueException e) { e.printStackTrace(); assert false : "not possible"; @@ -41,7 +44,7 @@ private void loadAddressBookWithSampleData(AddressBook ab) { } public Person[] getTypicalPersons() { - return new Person[]{amy, bill, candy}; + return new Person[]{amy, bill, candy, dan}; } public AddressBook getTypicalAddressBook() {