Skip to content

Commit

Permalink
Overriden equals for editNoteCommand,
Browse files Browse the repository at this point in the history
added EditNoteCommand testcases
  • Loading branch information
Pinran-J committed Oct 27, 2022
1 parent d0d9390 commit 3d66010
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/main/java/seedu/address/logic/commands/EditNoteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ private static Note createEditedNote(Note noteToEdit, EditNoteDescriptor editNot
return new Note(updatedTitle, updatedContent, updatedTags);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EditNoteCommand)) {
return false;
}

// state check
EditNoteCommand e = (EditNoteCommand) other;
return index.equals(e.index)
&& editNoteDescriptor.equals(e.editNoteDescriptor);
}


/**
* Stores the details to edit the note with. Each non-empty field value will replace the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import seedu.address.model.note.TitleContainsKeywordsPredicate;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;
import seedu.address.testutil.EditNoteDescriptorBuilder;
import seedu.address.testutil.EditPersonDescriptorBuilder;

/**
Expand All @@ -46,6 +47,7 @@ public class CommandTestUtil {
public static final String VALID_TITLE_CLUB = "Club";
public static final String VALID_CONTENT_MEETING = "October 3rd";
public static final String VALID_CONTENT_CLUB = "Birthday celebration";
public static final String VALID_TAG_IMPORTANT = "important";

public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY;
public static final String NAME_DESC_BOB = " " + PREFIX_NAME + VALID_NAME_BOB;
Expand All @@ -72,6 +74,9 @@ public class CommandTestUtil {

public static final EditCommand.EditPersonDescriptor DESC_AMY;
public static final EditCommand.EditPersonDescriptor DESC_BOB;
public static final EditNoteCommand.EditNoteDescriptor DESC_MEETING;
public static final EditNoteCommand.EditNoteDescriptor DESC_CLUB;


static {
DESC_AMY = new EditPersonDescriptorBuilder().withName(VALID_NAME_AMY)
Expand All @@ -80,6 +85,10 @@ public class CommandTestUtil {
DESC_BOB = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB)
.withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_BOB).withAddress(VALID_ADDRESS_BOB)
.withBirthday(VALID_BIRTHDAY_BOB).withTags(VALID_TAG_HUSBAND, VALID_TAG_FRIEND).build();
DESC_MEETING = new EditNoteDescriptorBuilder().withTitle(VALID_TITLE_MEETING)
.withContent(VALID_CONTENT_MEETING).withTags(VALID_TAG_IMPORTANT).build();
DESC_CLUB = new EditNoteDescriptorBuilder().withTitle(VALID_TITLE_CLUB)
.withContent(VALID_CONTENT_CLUB).withTags(VALID_TAG_IMPORTANT).build();
}

/**
Expand Down
168 changes: 167 additions & 1 deletion src/test/java/seedu/address/logic/commands/EditNoteCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,169 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;
import static seedu.address.logic.commands.CommandTestUtil.DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.DESC_BOB;
import static seedu.address.logic.commands.CommandTestUtil.DESC_CLUB;
import static seedu.address.logic.commands.CommandTestUtil.DESC_MEETING;
import static seedu.address.logic.commands.CommandTestUtil.VALID_CONTENT_MEETING;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TITLE_CLUB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TITLE_MEETING;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showNoteAtIndex;
import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_NOTE;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_NOTE;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalNotes.getTypicalAddressBook;

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

import org.junit.jupiter.api.Test;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditNoteCommand.EditNoteDescriptor;
import seedu.address.logic.parser.AddCommandParser;
import seedu.address.logic.parser.AddNoteCommandParser;
import seedu.address.logic.parser.CliSyntax;
import seedu.address.logic.parser.EditNoteCommandParser;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.note.Note;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;
import seedu.address.testutil.EditNoteDescriptorBuilder;
import seedu.address.testutil.EditPersonDescriptorBuilder;
import seedu.address.testutil.NoteBuilder;
import seedu.address.testutil.PersonBuilder;

/**
* Contains integration tests (interaction with the Model) and unit tests for EditNoteCommand.
*/
public class EditNoteCommandTest {

private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void execute_allFieldsSpecifiedUnfilteredList_success() {
Note editedNote = new NoteBuilder().build();
EditNoteDescriptor descriptor = new EditNoteDescriptorBuilder(editedNote).build();
EditNoteCommand editNoteCommand = new EditNoteCommand(INDEX_FIRST_NOTE, descriptor);

String expectedMessage = String.format(EditNoteCommand.MESSAGE_EDIT_NOTE_SUCCESS, editedNote);

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
expectedModel.setNote(model.getFilteredNoteList().get(0), editedNote);

assertCommandSuccess(editNoteCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_someFieldsSpecifiedUnfilteredList_success() {
Index indexLastNote = Index.fromOneBased(model.getFilteredNoteList().size());
Note lastNote = model.getFilteredNoteList().get(indexLastNote.getZeroBased());

NoteBuilder noteInList = new NoteBuilder(lastNote);
Note editedNote = noteInList.withTitle(VALID_TITLE_MEETING).withContent(VALID_CONTENT_MEETING).build();

EditNoteCommand.EditNoteDescriptor descriptor = new EditNoteDescriptorBuilder().withTitle(VALID_TITLE_MEETING)
.withContent(VALID_CONTENT_MEETING).build();

EditNoteCommand editNoteCommand = new EditNoteCommand(indexLastNote, descriptor);

String expectedMessage = String.format(EditNoteCommand.MESSAGE_EDIT_NOTE_SUCCESS, editedNote);

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
expectedModel.setNote(lastNote, editedNote);

assertCommandSuccess(editNoteCommand, model, expectedMessage, expectedModel);

}

@Test
public void execute_noFieldSpecifiedUnfilteredList_success() {
EditNoteCommand editNoteCommand = new EditNoteCommand(INDEX_FIRST_NOTE, new EditNoteDescriptor());
Note editedNote = model.getFilteredNoteList().get(INDEX_FIRST_NOTE.getZeroBased());

String expectedMessage = String.format(EditNoteCommand.MESSAGE_EDIT_NOTE_SUCCESS, editedNote);

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());

assertCommandSuccess(editNoteCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_filteredList_success() {
showNoteAtIndex(model, INDEX_FIRST_NOTE);

Note noteInFilteredList = model.getFilteredNoteList().get(INDEX_FIRST_NOTE.getZeroBased());
Note editedNote = new NoteBuilder(noteInFilteredList).withTitle(VALID_TITLE_CLUB).build();
EditNoteCommand editNoteCommand = new EditNoteCommand(INDEX_FIRST_NOTE,
new EditNoteDescriptorBuilder().withTitle(VALID_TITLE_CLUB).build());

String expectedMessage = String.format(EditNoteCommand.MESSAGE_EDIT_NOTE_SUCCESS, editedNote);

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
expectedModel.setNote(model.getFilteredNoteList().get(0), editedNote);

assertCommandSuccess(editNoteCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_duplicateNoteUnfilteredList_failure() {
Note firstNote = model.getFilteredNoteList().get(INDEX_FIRST_NOTE.getZeroBased());
EditNoteDescriptor descriptor = new EditNoteDescriptorBuilder(firstNote).build();
EditNoteCommand editNoteCommand = new EditNoteCommand(INDEX_SECOND_NOTE, descriptor);

assertCommandFailure(editNoteCommand, model, EditNoteCommand.MESSAGE_DUPLICATE_NOTE);
}

@Test
public void execute_duplicateNoteFilteredList_failure() {
showNoteAtIndex(model, INDEX_FIRST_NOTE);

// edit note in filtered note list into a duplicate in address book
Note noteInList = model.getAddressBook().getNoteBook().get(INDEX_SECOND_NOTE.getZeroBased());
EditNoteCommand editNoteCommand = new EditNoteCommand(INDEX_FIRST_NOTE,
new EditNoteDescriptorBuilder(noteInList).build());

assertCommandFailure(editNoteCommand, model, EditNoteCommand.MESSAGE_DUPLICATE_NOTE);
}

@Test
public void execute_invalidNoteIndexUnfilteredList_failure() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredNoteList().size() + 1);
EditNoteDescriptor descriptor = new EditNoteDescriptorBuilder().withTitle(VALID_TITLE_CLUB).build();
EditNoteCommand editNoteCommand = new EditNoteCommand(outOfBoundIndex, descriptor);

assertCommandFailure(editNoteCommand, model, Messages.MESSAGE_INVALID_NOTE_DISPLAYED_INDEX);
}

/**
* Edit filtered note list where index is larger than size of filtered list,
* but smaller than size of address book
*/
@Test
public void execute_invalidNoteIndexFilteredList_failure() {
showNoteAtIndex(model, INDEX_FIRST_NOTE);
Index outOfBoundIndex = INDEX_SECOND_NOTE;
// ensures that outOfBoundIndex is still in bounds of address book note list
assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getNoteBook().size());

EditNoteCommand editNoteCommand = new EditNoteCommand(outOfBoundIndex,
new EditNoteDescriptorBuilder().withTitle(VALID_TITLE_CLUB).build());

assertCommandFailure(editNoteCommand, model, Messages.MESSAGE_INVALID_NOTE_DISPLAYED_INDEX);
}

@Test
public void execute_editNoteWithTag_addsTagIntoTagMapping() throws Exception {
Model model = new ModelManager();
Expand Down Expand Up @@ -143,4 +284,29 @@ public void execute_editNoteWithTag_doesNotDeletesTagFromTagMapping() {
assertTrue(model.getTagMapping().containsKey(tagName));
}

@Test
public void equals() {
final EditNoteCommand standardCommand = new EditNoteCommand(INDEX_FIRST_NOTE, DESC_MEETING);

// same values -> returns true
EditNoteDescriptor copyDescriptor = new EditNoteDescriptor(DESC_MEETING);
EditNoteCommand commandWithSameValues = new EditNoteCommand(INDEX_FIRST_NOTE, copyDescriptor);
assertTrue(standardCommand.equals(commandWithSameValues));

// same object -> returns true
assertTrue(standardCommand.equals(standardCommand));

// null -> returns false
assertFalse(standardCommand.equals(null));

// different types -> returns false
assertFalse(standardCommand.equals(new ClearCommand()));

// different index -> returns false
assertFalse(standardCommand.equals(new EditNoteCommand(INDEX_SECOND_NOTE, DESC_MEETING)));

// different descriptor -> returns false
assertFalse(standardCommand.equals(new EditNoteCommand(INDEX_FIRST_NOTE, DESC_CLUB)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package seedu.address.testutil;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import seedu.address.logic.commands.EditNoteCommand.EditNoteDescriptor;
import seedu.address.model.note.Content;
import seedu.address.model.note.Note;
import seedu.address.model.note.Title;
import seedu.address.model.tag.Tag;

/**
* A utility class to help with building EditNoteDescriptor objects.
*/
public class EditNoteDescriptorBuilder {

private EditNoteDescriptor descriptor;

public EditNoteDescriptorBuilder() {
descriptor = new EditNoteDescriptor();
}

public EditNoteDescriptorBuilder(EditNoteDescriptor descriptor) {
this.descriptor = new EditNoteDescriptor(descriptor);
}

/**
* Returns an {@code EditNoteDescriptor} with fields containing {@code note}'s details
*/
public EditNoteDescriptorBuilder(Note note) {
descriptor = new EditNoteDescriptor();
descriptor.setTitle(note.getTitle());
descriptor.setContent(note.getContent());
descriptor.setTags(note.getTags());
}

/**
* Sets the {@code Title} of the {@code EditNoteDescriptor} that we are building.
*/
public EditNoteDescriptorBuilder withTitle(String title) {
descriptor.setTitle(new Title(title));
return this;
}

/**
* Sets the {@code Content} of the {@code EditNoteDescriptor} that we are building.
*/
public EditNoteDescriptorBuilder withContent(String content) {
descriptor.setContent(new Content(content));
return this;
}

/**
* Parses the {@code tags} into a {@code Set<Tag>} and set it to the {@code EditNoteDescriptor}
* that we are building.
*/
public EditNoteDescriptorBuilder withTags(String... tags) {
Set<Tag> tagSet = Stream.of(tags).map(Tag::new).collect(Collectors.toSet());
descriptor.setTags(tagSet);
return this;
}

public EditNoteDescriptor build() {
return descriptor;
}



}
1 change: 0 additions & 1 deletion src/test/java/seedu/address/testutil/TypicalNotes.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import seedu.address.model.AddressBook;
import seedu.address.model.note.Note;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;

/**
Expand Down

0 comments on commit 3d66010

Please sign in to comment.