Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DeleteNoteCommandTest #107

Merged
merged 24 commits into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f03339c
Merge pull request #2 from AY1920S1-CS2103T-F12-3/master
shiweing Sep 30, 2019
59b651d
Merge pull request #3 from AY1920S1-CS2103T-F12-3/master
shiweing Sep 30, 2019
7133161
Merge pull request #4 from AY1920S1-CS2103T-F12-3/master
shiweing Sep 30, 2019
7acba76
Add changes from team repo (#5)
shiweing Sep 30, 2019
d6dd340
Merge pull request #6 from from AY1920S1-CS2103T-F12-3/master
shiweing Oct 3, 2019
1e6da83
Merge and resolve conflicts from upstream
shiweing Oct 3, 2019
e0a4438
Add newline after EOF for README and AboutUs
shiweing Oct 3, 2019
beda0a8
Merge pull request #9 from AY1920S1-CS2103T-F12-3/main
shiweing Oct 3, 2019
7f7a6f7
Fix Yehez image name
shiweing Oct 3, 2019
aebd5fd
Merge PR #10 from AY1920S1-CS2103T-F12-3/main
shiweing Oct 8, 2019
72ed368
Updates from upstream (#14)
shiweing Oct 13, 2019
8f6c38b
Fix merge conflict
shiweing Oct 17, 2019
a6665fd
Fix merge conflicts from upstream
shiweing Oct 17, 2019
3fa5eb6
Fix merge conflicts
shiweing Oct 17, 2019
ff2154d
Merge remote-tracking branch 'upstream/master'
shiweing Oct 17, 2019
feca022
Fix merge conflict from pulling from upstream
shiweing Oct 18, 2019
49b4ac9
Add missing files to master from upstream
shiweing Oct 19, 2019
ade641a
Remove dummy view
shiweing Oct 19, 2019
00f90b0
Add valid note id tests for DeleteNoteCommand
shiweing Oct 18, 2019
a03ad08
Implement invalid note id test for DeleteNoteCommand
shiweing Oct 18, 2019
abe9652
Add findNote() in Model
shiweing Oct 18, 2019
22a29f1
Implement DeleteNoteCommand execution with model.findNote()
shiweing Oct 18, 2019
6caa677
Fix checkstyle errors
shiweing Oct 18, 2019
36b6984
Fix ModelStub error
shiweing Oct 19, 2019
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
14 changes: 5 additions & 9 deletions src/main/java/tagline/logic/commands/note/DeleteNoteCommand.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package tagline.logic.commands.note;

import static java.util.Objects.requireNonNull;
import static tagline.model.note.NoteModel.PREDICATE_SHOW_ALL_NOTES;

import java.util.List;
import java.util.Optional;

import tagline.commons.core.Messages;
import tagline.logic.commands.CommandResult;
import tagline.logic.commands.exceptions.CommandException;
import tagline.model.Model;
import tagline.model.note.Note;
import tagline.model.note.NoteId;
import tagline.model.note.NoteIdEqualsTargetIdPredicate;

/**
* Deletes a note identified using it's index.
Expand All @@ -37,18 +35,16 @@ public DeleteNoteCommand(NoteId noteId) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
NoteIdEqualsTargetIdPredicate predicate = new NoteIdEqualsTargetIdPredicate(targetId);
model.updateFilteredNoteList(predicate);
List<Note> filteredList = model.getFilteredNoteList();

if (filteredList.size() < 1) {
Optional<Note> noteFound = model.findNote(targetId);

if (noteFound.isEmpty()) {
throw new CommandException(Messages.MESSAGE_INVALID_NOTE_INDEX);
}

Note noteToDelete = filteredList.get(0);
Note noteToDelete = noteFound.get();

model.deleteNote(noteToDelete);
model.updateFilteredNoteList(PREDICATE_SHOW_ALL_NOTES);
return new CommandResult(String.format(MESSAGE_SUCCESS, noteToDelete), CommandResult.ViewType.NOTE);
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/tagline/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tagline.model.contact.ContactId;
import tagline.model.contact.ReadOnlyAddressBook;
import tagline.model.note.Note;
import tagline.model.note.NoteId;
import tagline.model.note.ReadOnlyNoteBook;

/**
Expand Down Expand Up @@ -142,6 +143,12 @@ public interface Model {
*/
void deleteNote(Note target);

/**
* Finds a {@code Note} in the note book based on the {@code noteId}.
* @return Optional object if corresponding note is found, empty otherwise
*/
public Optional<Note> findNote(NoteId noteId);

/**
* Returns an unmodifiable view of the filtered note list
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/tagline/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tagline.model.contact.ReadOnlyAddressBook;
import tagline.model.note.Note;
import tagline.model.note.NoteBook;
import tagline.model.note.NoteId;
import tagline.model.note.NoteManager;
import tagline.model.note.ReadOnlyNoteBook;

Expand Down Expand Up @@ -190,6 +191,11 @@ public void deleteNote(Note target) {
noteManager.deleteNote(target);
}

@Override
public Optional<Note> findNote(NoteId noteId) {
return noteManager.findNote(noteId);
}

//=========== Filtered Note List Accessors =============================================================

/**
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/tagline/model/note/NoteBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;

import java.util.List;
import java.util.Optional;

import javafx.collections.ObservableList;

Expand Down Expand Up @@ -72,6 +73,14 @@ public void addNote(Note p) {
notes.add(p);
}

/**
* Finds a {@code Note} based on the {@code noteId}.
* @return Optional object if corresponding note is found, empty otherwise
*/
public Optional<Note> findNote(NoteId noteId) {
return notes.findNote(noteId);
}

/**
* Replaces the given note {@code target} in the list with {@code editedNote}.
* {@code target} must exist in the address book.
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/tagline/model/note/NoteManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static tagline.commons.util.CollectionUtil.requireAllNonNull;

import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.logging.Logger;

Expand Down Expand Up @@ -121,6 +122,11 @@ public void setNote(Note target, Note editedNote) {
noteBook.setNote(target, editedNote);
}

@Override
public Optional<Note> findNote(NoteId noteId) {
return noteBook.findNote(noteId);
}

//=========== Filtered Note List Accessors =============================================================

/**
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/tagline/model/note/NoteModel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tagline.model.note;

import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Predicate;

import javafx.collections.ObservableList;
Expand Down Expand Up @@ -76,6 +77,12 @@ public interface NoteModel {
*/
void setNote(Note target, Note editedNote);

/**
* Finds a {@code Note} based on the {@code noteId}.
* @return Optional object if corresponding note is found, empty otherwise
*/
public Optional<Note> findNote(NoteId noteId);

/** Returns an unmodifiable view of the filtered note list */
ObservableList<Note> getFilteredNoteList();

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/tagline/model/note/UniqueNoteList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
Expand Down Expand Up @@ -48,6 +49,23 @@ public void add(Note toAdd) {
internalList.add(toAdd);
}

/**
* Find a note by id.
*
* @param id of the note
* @return an optional object which implies whether the corresponding note is found or not.
*/
public Optional<Note> findNote(NoteId id) {
var it = iterator();
while (it.hasNext()) {
Note currentNote = it.next();
if (currentNote.getNoteId().equals(id)) {
return Optional.of(currentNote);
}
}
return Optional.empty();
}

/**
* Replaces the note {@code target} in the list with {@code editedNote}.
* {@code target} must exist in the list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import tagline.model.contact.ContactId;
import tagline.model.contact.ReadOnlyAddressBook;
import tagline.model.note.Note;
import tagline.model.note.NoteId;
import tagline.model.note.ReadOnlyNoteBook;

public class CreateContactCommandTest {
Expand Down Expand Up @@ -200,6 +201,11 @@ public void setNote(Note target, Note editedNote) {
throw new AssertionError("This method should not be called.");
}

@Override
public Optional<Note> findNote(NoteId noteId) {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Note> getFilteredNoteList() {
throw new AssertionError("This method should not be called.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static tagline.logic.commands.CommandTestUtil.assertCommandFailure;
import static tagline.logic.commands.CommandTestUtil.assertCommandSuccess;
import static tagline.testutil.TypicalContacts.getTypicalAddressBook;
import static tagline.testutil.TypicalIndexes.INDEX_FIRST_CONTACT;
import static tagline.testutil.TypicalIndexes.INDEX_FIRST;

import org.junit.jupiter.api.Test;

Expand All @@ -32,7 +32,7 @@ public class DeleteContactCommandTest {

@Test
public void execute_validContactId_success() {
Contact contactToDelete = model.getAddressBook().getContactList().get(INDEX_FIRST_CONTACT.getZeroBased());
Contact contactToDelete = model.getAddressBook().getContactList().get(INDEX_FIRST.getZeroBased());
DeleteContactCommand deleteContactCommand = new DeleteContactCommand(contactToDelete.getContactId());

String expectedMessage = String.format(DeleteContactCommand.MESSAGE_DELETE_CONTACT_SUCCESS, contactToDelete);
Expand All @@ -46,7 +46,7 @@ public void execute_validContactId_success() {

@Test
public void execute_validContactIdButUnfiltered_success() {
Contact contactToDelete = model.getAddressBook().getContactList().get(INDEX_FIRST_CONTACT.getZeroBased());
Contact contactToDelete = model.getAddressBook().getContactList().get(INDEX_FIRST.getZeroBased());
DeleteContactCommand deleteContactCommand = new DeleteContactCommand(contactToDelete.getContactId());

String expectedMessage = String.format(DeleteContactCommand.MESSAGE_DELETE_CONTACT_SUCCESS, contactToDelete);
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/tagline/logic/commands/EditContactCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import static tagline.logic.commands.CommandTestUtil.assertCommandFailure;
import static tagline.logic.commands.CommandTestUtil.assertCommandSuccess;
import static tagline.testutil.TypicalContacts.getTypicalAddressBook;
import static tagline.testutil.TypicalIndexes.INDEX_FIRST_CONTACT;
import static tagline.testutil.TypicalIndexes.INDEX_SECOND_CONTACT;
import static tagline.testutil.TypicalIndexes.INDEX_FIRST;
import static tagline.testutil.TypicalIndexes.INDEX_SECOND;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -40,7 +40,7 @@ public class EditContactCommandTest {

@Test
public void execute_allFieldsSpecified_success() {
Contact originalContact = model.getAddressBook().getContactList().get(INDEX_FIRST_CONTACT.getZeroBased());
Contact originalContact = model.getAddressBook().getContactList().get(INDEX_FIRST.getZeroBased());
Contact editedContact = new ContactBuilder().withId(originalContact.getContactId().toInteger()).build();

EditContactDescriptor descriptor = new EditContactDescriptorBuilder(editedContact).build();
Expand Down Expand Up @@ -80,7 +80,7 @@ public void execute_someFieldsSpecified_success() {

@Test
public void execute_noFieldSpecified_success() {
Contact editedContact = model.getFilteredContactList().get(INDEX_FIRST_CONTACT.getZeroBased());
Contact editedContact = model.getFilteredContactList().get(INDEX_FIRST.getZeroBased());
EditContactCommand editContactCommand =
new EditContactCommand(editedContact.getContactId(), new EditContactDescriptor());

Expand All @@ -95,8 +95,8 @@ public void execute_noFieldSpecified_success() {
@Test
public void execute_duplicateContact_failure() {
var contactList = model.getAddressBook().getContactList();
Contact firstContact = contactList.get(INDEX_FIRST_CONTACT.getZeroBased());
Contact secondContact = contactList.get(INDEX_SECOND_CONTACT.getZeroBased());
Contact firstContact = contactList.get(INDEX_FIRST.getZeroBased());
Contact secondContact = contactList.get(INDEX_SECOND.getZeroBased());

EditContactDescriptor descriptor = new EditContactDescriptorBuilder(firstContact).build();
EditContactCommand editContactCommand = new EditContactCommand(secondContact.getContactId(), descriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static tagline.logic.commands.CommandTestUtil.assertCommandSuccess;
import static tagline.logic.commands.CommandTestUtil.showContactAtIndex;
import static tagline.testutil.TypicalContacts.getTypicalAddressBook;
import static tagline.testutil.TypicalIndexes.INDEX_FIRST_CONTACT;
import static tagline.testutil.TypicalIndexes.INDEX_FIRST;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -38,7 +38,7 @@ public void execute_listIsNotFiltered_showsSameList() {

@Test
public void execute_listIsFiltered_showsEverything() {
showContactAtIndex(model, INDEX_FIRST_CONTACT);
showContactAtIndex(model, INDEX_FIRST);
assertCommandSuccess(new ListContactCommand(), model, ListContactCommand.MESSAGE_SUCCESS,
LIST_CONTACT_COMMAND_VIEW_TYPE, expectedModel);
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/tagline/logic/commands/NoteCommandTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static tagline.logic.parser.note.NoteCliSyntax.PREFIX_CONTENT;
import static tagline.logic.parser.note.NoteCliSyntax.PREFIX_TITLE;
import static tagline.testutil.Assert.assertThrows;

import java.util.ArrayList;
import java.util.List;

import tagline.logic.commands.exceptions.CommandException;
import tagline.model.Model;
import tagline.model.note.Note;
import tagline.model.note.NoteBook;
import tagline.model.note.NoteId;

/**
* Contains helper methods for testing commands.
Expand Down Expand Up @@ -44,6 +51,8 @@ public class NoteCommandTestUtil {
public static final String PREAMBLE_WHITESPACE = "\t \r \n";
public static final String PREAMBLE_NON_EMPTY = "NonEmptyPreamble";

public static final NoteId NON_EXISTING_NOTE_ID = new NoteId(99999);

/**
* Executes the given {@code command}, confirms that <br>
* - the returned {@link CommandResult} matches {@code expectedCommandResult} <br>
Expand All @@ -69,4 +78,21 @@ public static void assertCommandSuccess(Command command, Model actualModel, Stri
CommandResult expectedCommandResult = new CommandResult(expectedMessage);
assertCommandSuccess(command, actualModel, expectedCommandResult, expectedModel);
}

/**
* Executes the given {@code command}, confirms that <br>
* - a {@code CommandException} is thrown <br>
* - the CommandException message matches {@code expectedMessage} <br>
* - the note book, filtered note list and selected contact in {@code actualModel} remain unchanged
*/
public static void assertCommandFailure(Command command, Model actualModel, String expectedMessage) {
// we are unable to defensively copy the model for comparison later, so we can
// only do so by copying its components.
NoteBook expectedNoteBook = new NoteBook(actualModel.getNoteBook());
List<Note> expectedFilteredList = new ArrayList<>(actualModel.getFilteredNoteList());

assertThrows(CommandException.class, expectedMessage, () -> command.execute(actualModel));
assertEquals(expectedNoteBook, actualModel.getNoteBook());
assertEquals(expectedFilteredList, actualModel.getFilteredNoteList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import tagline.model.contact.ContactId;
import tagline.model.contact.ReadOnlyAddressBook;
import tagline.model.note.Note;
import tagline.model.note.NoteId;
import tagline.model.note.NoteModel;
import tagline.model.note.ReadOnlyNoteBook;
import tagline.testutil.NoteBuilder;
Expand Down Expand Up @@ -146,6 +147,11 @@ public void setNote(Note target, Note editedNote) {
throw new AssertionError("This method should not be called.");
}

@Override
public Optional<Note> findNote(NoteId noteId) {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Note> getFilteredNoteList() {
throw new AssertionError("This method should not be called.");
Expand Down Expand Up @@ -313,6 +319,11 @@ public void setNote(Note target, Note editedNote) {
throw new AssertionError("This method should not be called.");
}

@Override
public Optional<Note> findNote(NoteId noteId) {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Note> getFilteredNoteList() {
throw new AssertionError("This method should not be called.");
Expand Down
Loading