Skip to content
This repository has been archived by the owner on Jul 26, 2019. It is now read-only.

Add unit tests for command classes #77

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/seedu/addressbook/data/person/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public class Address {
* @throws IllegalValueException if given address string is invalid.
*/
public Address(String address, boolean isPrivate) throws IllegalValueException {
String trimmedAddress = address.trim();
this.isPrivate = isPrivate;
if (!isValidAddress(address)) {
if (!isValidAddress(trimmedAddress)) {
throw new IllegalValueException(MESSAGE_ADDRESS_CONSTRAINTS);
}
this.value = address;
this.value = trimmedAddress;
}

/**
Expand Down
147 changes: 147 additions & 0 deletions test/java/seedu/addressbook/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package seedu.addressbook.commands;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Test;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.exception.IllegalValueException;
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.person.UniquePersonList;
import seedu.addressbook.util.TestUtil;

public class AddCommandTest {
private static final List<ReadOnlyPerson> EMPTY_PERSON_LIST = Collections.emptyList();
private static final Set<String> EMPTY_STRING_LIST = Collections.emptySet();

@Test
public void addCommand_invalidName_throwsException() {
final String[] invalidNames = { "", " ", "[]\\[;]" };
for (String name : invalidNames) {
assertConstructingInvalidAddCmdThrowsException(name, Phone.EXAMPLE, true, Email.EXAMPLE, false,
Address.EXAMPLE, true, EMPTY_STRING_LIST);
}
}

@Test
public void addCommand_invalidPhone_throwsException() {
final String[] invalidNumbers = { "", " ", "1234-5678", "[]\\[;]", "abc", "a123", "+651234" };
for (String number : invalidNumbers) {
assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, number, false, Email.EXAMPLE, true,
Address.EXAMPLE, false, EMPTY_STRING_LIST);
}
}

@Test
public void addCommand_invalidEmail_throwsException() {
final String[] invalidEmails = { "", " ", "def.com", "@", "@def", "@def.com", "abc@",
"@invalid@email", "invalid@email!", "!invalid@email" };
for (String email : invalidEmails) {
assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, false, email, false,
Address.EXAMPLE, false, EMPTY_STRING_LIST);
}
}

@Test
public void addCommand_invalidAddress_throwsException() {
final String[] invalidAddresses = { "", " " };
for (String address : invalidAddresses) {
assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE,
true, address, true, EMPTY_STRING_LIST);
}
}

@Test
public void addCommand_invalidTags_throwsException() {
final String[][] invalidTags = { { "" }, { " " }, { "'" }, { "[]\\[;]" }, { "validTag", "" },
{ "", " " } };
for (String[] tags : invalidTags) {
Set<String> tagsToAdd = new HashSet<>(Arrays.asList(tags));
assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE,
true, Address.EXAMPLE, false, tagsToAdd);
}
}

/**
* Asserts that attempting to construct an add command with the supplied
* invalid data throws an IllegalValueException
*/
private void assertConstructingInvalidAddCmdThrowsException(String name, String phone,
boolean isPhonePrivate, String email, boolean isEmailPrivate, String address,
boolean isAddressPrivate, Set<String> tags) {
try {
new AddCommand(name, phone, isPhonePrivate, email, isEmailPrivate, address, isAddressPrivate,
tags);
} catch (IllegalValueException e) {
return;
}
String error = String.format(
"An add command was successfully constructed with invalid input: %s %s %s %s %s %s %s %s",
name, phone, isPhonePrivate, email, isEmailPrivate, address, isAddressPrivate, tags);
fail(error);
}

@Test
public void addCommand_validData_correctlyConstructed() throws Exception {
AddCommand command = new AddCommand(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE, false,
Address.EXAMPLE, true, EMPTY_STRING_LIST);
ReadOnlyPerson p = command.getPerson();

// TODO: add comparison of tags to person.equals and equality methods to
// individual fields that compare privacy to simplify this
assertEquals(Name.EXAMPLE, p.getName().fullName);
assertEquals(Phone.EXAMPLE, p.getPhone().value);
assertTrue(p.getPhone().isPrivate());
assertEquals(Email.EXAMPLE, p.getEmail().value);
assertFalse(p.getEmail().isPrivate());
assertEquals(Address.EXAMPLE, p.getAddress().value);
assertTrue(p.getAddress().isPrivate());
boolean isTagListEmpty = !p.getTags().iterator().hasNext();
assertTrue(isTagListEmpty);
}

@Test
public void addCommand_emptyAddressBook_addressBookContainsPerson() {
Person p = TestUtil.generateTestPerson();
AddCommand command = new AddCommand(p);
AddressBook book = new AddressBook();
command.setData(book, EMPTY_PERSON_LIST);
CommandResult result = command.execute();
UniquePersonList people = book.getAllPersons();

assertTrue(people.contains(p));
assertEquals(1, people.immutableListView().size());
assertFalse(result.getRelevantPersons().isPresent());
assertEquals(String.format(AddCommand.MESSAGE_SUCCESS, p), result.feedbackToUser);
}

@Test
public void addCommand_addressBookAlreadyContainsPerson_addressBookUnmodified() throws Exception {
Person p = TestUtil.generateTestPerson();
AddressBook book = new AddressBook();
book.addPerson(p);
AddCommand command = new AddCommand(p);
command.setData(book, EMPTY_PERSON_LIST);
CommandResult result = command.execute();

assertFalse(result.getRelevantPersons().isPresent());
assertEquals(AddCommand.MESSAGE_DUPLICATE_PERSON, result.feedbackToUser);
UniquePersonList people = book.getAllPersons();
assertTrue(people.contains(p));
assertEquals(1, people.immutableListView().size());
}
}
162 changes: 162 additions & 0 deletions test/java/seedu/addressbook/commands/DeleteCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package seedu.addressbook.commands;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import seedu.addressbook.common.Messages;
import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.exception.IllegalValueException;
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.person.UniquePersonList.PersonNotFoundException;
import seedu.addressbook.data.tag.UniqueTagList;
import seedu.addressbook.ui.Gui;
import seedu.addressbook.util.TestUtil;

public class DeleteCommandTest {

private AddressBook emptyAddressBook;
private AddressBook addressBook;

private List<ReadOnlyPerson> emptyDisplayList;
private List<ReadOnlyPerson> listWithEveryone;
private List<ReadOnlyPerson> listWithSurnameDoe;

@Before
public void setUp() throws Exception {
Person johnDoe = new Person(new Name("John Doe"), new Phone("61234567", false),
new Email("[email protected]", false), new Address("395C Ben Road", false), new UniqueTagList());
Person janeDoe = new Person(new Name("Jane Doe"), new Phone("91234567", false),
new Email("[email protected]", false), new Address("33G Ohm Road", false), new UniqueTagList());
Person samDoe = new Person(new Name("Sam Doe"), new Phone("63345566", false),
new Email("[email protected]", false), new Address("55G Abc Road", false), new UniqueTagList());
Person davidGrant = new Person(new Name("David Grant"), new Phone("61121122", false),
new Email("[email protected]", false), new Address("44H Define Road", false),
new UniqueTagList());

emptyAddressBook = TestUtil.createAddressBook();
addressBook = TestUtil.createAddressBook(johnDoe, janeDoe, davidGrant, samDoe);

emptyDisplayList = TestUtil.createList();

listWithEveryone = TestUtil.createList(johnDoe, janeDoe, davidGrant, samDoe);
listWithSurnameDoe = TestUtil.createList(johnDoe, janeDoe, samDoe);
}

@Test
public void execute_emptyAddressBook_returnsPersonNotFoundMessage() {
assertDeletionFailsDueToNoSuchPerson(1, emptyAddressBook, listWithEveryone);
}

@Test
public void execute_noPersonDisplayed_returnsInvalidIndexMessage() {
assertDeletionFailsDueToInvalidIndex(1, addressBook, emptyDisplayList);
}

@Test
public void execute_targetPersonNotInAddressBook_returnsPersonNotFoundMessage()
throws IllegalValueException {
Person notInAddressBookPerson = new Person(new Name("Not In Book"), new Phone("63331444", false),
new Email("[email protected]", false), new Address("156D Grant Road", false), new UniqueTagList());
List<ReadOnlyPerson> listWithPersonNotInAddressBook = TestUtil.createList(notInAddressBookPerson);

assertDeletionFailsDueToNoSuchPerson(1, addressBook, listWithPersonNotInAddressBook);
}

@Test
public void execute_invalidIndex_returnsInvalidIndexMessage() {
assertDeletionFailsDueToInvalidIndex(0, addressBook, listWithEveryone);
assertDeletionFailsDueToInvalidIndex(-1, addressBook, listWithEveryone);
assertDeletionFailsDueToInvalidIndex(listWithEveryone.size() + 1, addressBook, listWithEveryone);
}

@Test
public void execute_validIndex_personIsDeleted() throws PersonNotFoundException {
assertDeletionSuccessful(1, addressBook, listWithSurnameDoe);
assertDeletionSuccessful(listWithSurnameDoe.size(), addressBook, listWithSurnameDoe);

int middleIndex = (listWithSurnameDoe.size() / 2) + 1;
assertDeletionSuccessful(middleIndex, addressBook, listWithSurnameDoe);
}

/**
* Creates a new delete command.
*
* @param targetVisibleIndex of the person that we want to delete
*/
private DeleteCommand createDeleteCommand(int targetVisibleIndex, AddressBook addressBook,
List<ReadOnlyPerson> displayList) {

DeleteCommand command = new DeleteCommand(targetVisibleIndex);
command.setData(addressBook, displayList);

return command;
}

/**
* Executes the command, and checks that the execution was what we had expected.
*/
private void assertCommandBehaviour(DeleteCommand deleteCommand, String expectedMessage,
AddressBook expectedAddressBook, AddressBook actualAddressBook) {

CommandResult result = deleteCommand.execute();

assertEquals(expectedMessage, result.feedbackToUser);
assertEquals(expectedAddressBook.getAllPersons(), actualAddressBook.getAllPersons());
}

/**
* Asserts that the index is not valid for the given display list.
*/
private void assertDeletionFailsDueToInvalidIndex(int invalidVisibleIndex, AddressBook addressBook,
List<ReadOnlyPerson> displayList) {

String expectedMessage = Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;

DeleteCommand command = createDeleteCommand(invalidVisibleIndex, addressBook, displayList);
assertCommandBehaviour(command, expectedMessage, addressBook, addressBook);
}

/**
* Asserts that the person at the specified index cannot be deleted, because that person
* is not in the address book.
*/
private void assertDeletionFailsDueToNoSuchPerson(int visibleIndex, AddressBook addressBook,
List<ReadOnlyPerson> displayList) {

String expectedMessage = Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK;

DeleteCommand command = createDeleteCommand(visibleIndex, addressBook, displayList);
assertCommandBehaviour(command, expectedMessage, addressBook, addressBook);
}

/**
* Asserts that the person at the specified index can be successfully deleted.
*
* The addressBook passed in will not be modified (no side effects).
*
* @throws PersonNotFoundException if the selected person is not in the address book
*/
private void assertDeletionSuccessful(int targetVisibleIndex, AddressBook addressBook,
List<ReadOnlyPerson> displayList) throws PersonNotFoundException {

ReadOnlyPerson targetPerson = displayList.get(targetVisibleIndex - Gui.DISPLAYED_INDEX_OFFSET);

AddressBook expectedAddressBook = TestUtil.clone(addressBook);
expectedAddressBook.removePerson(targetPerson);
String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS, targetPerson);

AddressBook actualAddressBook = TestUtil.clone(addressBook);

DeleteCommand command = createDeleteCommand(targetVisibleIndex, actualAddressBook, displayList);
assertCommandBehaviour(command, expectedMessage, expectedAddressBook, actualAddressBook);
}
}
63 changes: 63 additions & 0 deletions test/java/seedu/addressbook/commands/FindCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package seedu.addressbook.commands;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Test;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.util.TypicalPersons;

public class FindCommandTest {

private final AddressBook addressBook = new TypicalPersons().getTypicalAddressBook();
private final TypicalPersons td = new TypicalPersons();

@Test
public void execute() throws IllegalValueException {
//same word, same case: matched
assertFindCommandBehavior(new String[]{"Amy"}, Arrays.asList(td.amy));

//same word, different case: not matched
assertFindCommandBehavior(new String[]{"aMy"}, Collections.emptyList());

//partial word: not matched
assertFindCommandBehavior(new String[]{"my"}, Collections.emptyList());

//multiple words: matched
assertFindCommandBehavior(new String[]{"Amy", "Bill", "Candy", "Destiny"},
Arrays.asList(td.amy, td.bill, td.candy));

//repeated keywords: matched
assertFindCommandBehavior(new String[]{"Amy", "Amy"}, Arrays.asList(td.amy));

//Keyword matching a word in address: not matched
assertFindCommandBehavior(new String[]{"Clementi"}, Collections.emptyList());
}

/**
* Executes the find command for the given keywords and verifies
* the result matches the persons in the expectedPersonList exactly.
*/
private void assertFindCommandBehavior(String[] keywords, List<ReadOnlyPerson> expectedPersonList) {
FindCommand command = createFindCommand(keywords);
CommandResult result = command.execute();

assertEquals(Command.getMessageForPersonListShownSummary(expectedPersonList), result.feedbackToUser);
}

private FindCommand createFindCommand(String[] keywords) {
final Set<String> keywordSet = new HashSet<>(Arrays.asList(keywords));
FindCommand command = new FindCommand(keywordSet);
command.setData(addressBook, Collections.emptyList());
return command;
}

}
7 changes: 7 additions & 0 deletions test/java/seedu/addressbook/commands/ViewAllCommandTest.java
Original file line number Diff line number Diff line change
@@ -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.
}
Loading