From 1cc245301d5978933b7edbeae152a9ed6351c6a8 Mon Sep 17 00:00:00 2001 From: Darren Date: Thu, 19 Oct 2023 12:07:41 +0800 Subject: [PATCH] Add tests --- .../logic/commands/AddAppointmentCommand.java | 2 +- .../address/logic/commands/EditCommand.java | 63 +++---- .../seedu/address/model/person/Person.java | 3 +- .../address/model/util/SampleDataUtil.java | 16 +- .../invalidAndValidPersonAddressBook.json | 45 +++-- .../invalidPersonAddressBook.json | 24 ++- .../duplicatePersonAddressBook.json | 16 +- .../invalidPersonAddressBook.json | 24 ++- .../typicalPersonsAddressBook.json | 36 +++- .../seedu/address/logic/LogicManagerTest.java | 2 +- .../commands/AddAppointmentCommandTest.java | 156 ++++++++++++++++++ .../logic/commands/CommandTestUtil.java | 11 ++ .../logic/commands/EditCommandTest.java | 7 +- .../commands/EditPersonDescriptorTest.java | 3 +- .../logic/parser/AddressBookParserTest.java | 28 ++++ .../address/testutil/AppointmentBuilder.java | 54 ++++++ .../address/testutil/AppointmentUtil.java | 29 ++++ .../testutil/EditPersonDescriptorBuilder.java | 6 +- .../seedu/address/testutil/PersonBuilder.java | 4 +- .../address/testutil/TypicalAppointments.java | 44 +++++ .../address/testutil/TypicalPersons.java | 33 ++-- 21 files changed, 510 insertions(+), 96 deletions(-) create mode 100644 src/test/java/seedu/address/logic/commands/AddAppointmentCommandTest.java create mode 100644 src/test/java/seedu/address/testutil/AppointmentBuilder.java create mode 100644 src/test/java/seedu/address/testutil/AppointmentUtil.java create mode 100644 src/test/java/seedu/address/testutil/TypicalAppointments.java diff --git a/src/main/java/seedu/address/logic/commands/AddAppointmentCommand.java b/src/main/java/seedu/address/logic/commands/AddAppointmentCommand.java index e45521aaf32..5582dd2bcb2 100644 --- a/src/main/java/seedu/address/logic/commands/AddAppointmentCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddAppointmentCommand.java @@ -85,7 +85,7 @@ public boolean equals(Object other) { } AddAppointmentCommand otherAddCommand = (AddAppointmentCommand) other; - return toAdd.equals(otherAddCommand.toAdd); + return toAdd.equals(otherAddCommand.toAdd) && index.equals(otherAddCommand.index); } @Override diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index d3035b22134..ecc14f2c7de 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -111,7 +111,7 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript Age updatedAge = editPersonDescriptor.getAge().orElse(personToEdit.getAge()); BloodType updatedBloodType = editPersonDescriptor.getBloodType().orElse(personToEdit.getBloodType()); Set updatedAllergies = editPersonDescriptor.getAllergies().orElse(personToEdit.getAllergies()); - Boolean updatedisPinned = editPersonDescriptor.getIsPinned().orElse(personToEdit.isPinned()); + Boolean updatedisPinned = personToEdit.isPinned(); UniqueAppointmentList updatedAppointments = personToEdit.getAppointments(); return new Person(updatedName, updatedEmail, updatedPhone, updatedGender, @@ -154,7 +154,6 @@ public static class EditPersonDescriptor { private Age age; private BloodType bloodType; private Set allergies; - private Boolean isPinned; public EditPersonDescriptor() {} @@ -170,7 +169,6 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) { setAge(toCopy.age); setBloodType(toCopy.bloodType); setAllergies(toCopy.allergies); - setIsPinned(toCopy.isPinned); } /** @@ -180,70 +178,52 @@ public boolean isAnyFieldEdited() { return CollectionUtil.isAnyNonNull(name, email, phone, gender, age, bloodType, allergies); } + public Optional getName() { + return Optional.ofNullable(name); + } + public void setName(Name name) { this.name = name; } - public Optional getName() { - return Optional.ofNullable(name); + public Optional getPhone() { + return Optional.ofNullable(phone); } public void setPhone(Phone phone) { this.phone = phone; } - public void setEmail(Email email) { - this.email = email; - } - public Optional getEmail() { return Optional.ofNullable(email); } - - public Optional getPhone() { - return Optional.ofNullable(phone); - } - - public void setGender(Gender gender) { - this.gender = gender; + + public void setEmail(Email email) { + this.email = email; } public Optional getGender() { return Optional.ofNullable(gender); } - - public void setAge(Age age) { - this.age = age; - } - - public void setIsPinned(Boolean isPinned) { - this.isPinned = isPinned; - } - - public Optional getIsPinned() { - return Optional.ofNullable(isPinned); + + public void setGender(Gender gender) { + this.gender = gender; } - - public Optional getAge() { return Optional.ofNullable(age); } - public void setBloodType(BloodType bloodType) { - this.bloodType = bloodType; + public void setAge(Age age) { + this.age = age; } public Optional getBloodType() { return Optional.ofNullable(bloodType); } - /** - * Sets {@code allergies} to this object's {@code tags}. - * A defensive copy of {@code allergies} is used internally. - */ - public void setAllergies(Set allergies) { - this.allergies = (allergies != null) ? new HashSet<>(allergies) : null; + public void setBloodType(BloodType bloodType) { + this.bloodType = bloodType; } /** @@ -255,6 +235,14 @@ public Optional> getAllergies() { return (allergies != null) ? Optional.of(Collections.unmodifiableSet(allergies)) : Optional.empty(); } + /** + * Sets {@code allergies} to this object's {@code tags}. + * A defensive copy of {@code allergies} is used internally. + */ + public void setAllergies(Set allergies) { + this.allergies = (allergies != null) ? new HashSet<>(allergies) : null; + } + @Override public boolean equals(Object other) { if (other == this) { @@ -286,7 +274,6 @@ public String toString() { .add("age", age) .add("bloodType", bloodType) .add("allergies", allergies) - .add("isPinned", isPinned) .toString(); } } diff --git a/src/main/java/seedu/address/model/person/Person.java b/src/main/java/seedu/address/model/person/Person.java index 5f18c6d953b..66514cc5756 100644 --- a/src/main/java/seedu/address/model/person/Person.java +++ b/src/main/java/seedu/address/model/person/Person.java @@ -124,7 +124,8 @@ public boolean equals(Object other) { && age.equals(otherPerson.age) && bloodType.equals(otherPerson.bloodType) && allergies.equals(otherPerson.allergies) - && isPinned == otherPerson.isPinned; + && isPinned == otherPerson.isPinned + && appointments.equals(otherPerson.appointments); } @Override diff --git a/src/main/java/seedu/address/model/util/SampleDataUtil.java b/src/main/java/seedu/address/model/util/SampleDataUtil.java index b27e498db3a..b16cd069859 100644 --- a/src/main/java/seedu/address/model/util/SampleDataUtil.java +++ b/src/main/java/seedu/address/model/util/SampleDataUtil.java @@ -14,6 +14,8 @@ import seedu.address.model.person.Name; import seedu.address.model.person.Person; import seedu.address.model.person.Phone; +import seedu.address.model.person.appointment.Appointment; +import seedu.address.model.person.appointment.DateTime; import seedu.address.model.person.appointment.UniqueAppointmentList; @@ -25,7 +27,7 @@ public static Person[] getSamplePersons() { return new Person[] { new Person(new Name("Alex Yeoh"), new Email("alexyeoh@example.com"), new Phone("87438807"), new Gender("M"), new Age(12), new BloodType("A+"), getAllergySet("Peanuts"), - true, new UniqueAppointmentList()), + true, getAppointmentList(new Appointment(new Name("Eye Exam"), new DateTime("01-01-2001 1200")))), new Person(new Name("Bernice Yu"), new Email("berniceyu@example.com"), new Phone("99272758"), new Gender("F"), new Age(31), new BloodType("B+"), getAllergySet("Dust", "Peanuts"), false, new UniqueAppointmentList()), @@ -53,7 +55,7 @@ public static ReadOnlyAddressBook getSampleAddressBook() { } /** - * Returns a tag set containing the list of strings given. + * Returns an allergy set containing the list of strings given. */ public static Set getAllergySet(String... strings) { return Arrays.stream(strings) @@ -61,4 +63,14 @@ public static Set getAllergySet(String... strings) { .collect(Collectors.toSet()); } + /** + * Returns an appointment list containing the list of appointments given. + */ + public static UniqueAppointmentList getAppointmentList(Appointment... appointments) { + UniqueAppointmentList appointmentList = new UniqueAppointmentList(); + for (Appointment appointment : appointments) { + appointmentList.add(appointment); + } + return appointmentList; + } } diff --git a/src/test/data/JsonAddressBookStorageTest/invalidAndValidPersonAddressBook.json b/src/test/data/JsonAddressBookStorageTest/invalidAndValidPersonAddressBook.json index 6a4d2b7181c..52c3fd6557f 100644 --- a/src/test/data/JsonAddressBookStorageTest/invalidAndValidPersonAddressBook.json +++ b/src/test/data/JsonAddressBookStorageTest/invalidAndValidPersonAddressBook.json @@ -1,13 +1,36 @@ { - "persons": [ { - "name": "Valid Person", - "phone": "9482424", - "email": "hans@example.com", - "address": "4th street" - }, { - "name": "Person With Invalid Phone Field", - "phone": "948asdf2424", - "email": "hans@example.com", - "address": "4th street" - } ] + "persons": [ + { + "name": "Valid Person", + "phone": "9482424", + "email": "hans@example.com", + "gender": "M", + "age": "18", + "bloodType": "A+", + "allergies": ["Peanuts"], + "isPinned": true, + "appointments": [ + { + "title": "Eye Examination", + "dateTime": "18-09-2023 1800" + } + ] + }, + { + "name": "Person With Invalid Phone Field", + "phone": "948asdf2424", + "email": "hans@example.com", + "gender": "M", + "age": "18", + "bloodType": "A+", + "allergies": ["Peanuts"], + "isPinned": true, + "appointments": [ + { + "title": "Eye Examination", + "dateTime": "18-09-2023 1800" + } + ] + } + ] } diff --git a/src/test/data/JsonAddressBookStorageTest/invalidPersonAddressBook.json b/src/test/data/JsonAddressBookStorageTest/invalidPersonAddressBook.json index ccd21f7d1a9..acdd740c4cd 100644 --- a/src/test/data/JsonAddressBookStorageTest/invalidPersonAddressBook.json +++ b/src/test/data/JsonAddressBookStorageTest/invalidPersonAddressBook.json @@ -1,8 +1,20 @@ { - "persons": [ { - "name": "Person with invalid name field: Ha!ns Mu@ster", - "phone": "9482424", - "email": "hans@example.com", - "address": "4th street" - } ] + "persons": [ + { + "name": "Person with invalid name field: Ha!ns Mu@ster", + "phone": "9482424", + "email": "hans@example.com", + "gender": "M", + "age": "18", + "bloodType": "A+", + "allergies": ["Peanuts"], + "isPinned": true, + "appointments": [ + { + "title": "Eye Examination", + "dateTime": "18-09-2023 1800" + } + ] + } + ] } diff --git a/src/test/data/JsonSerializableAddressBookTest/duplicatePersonAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/duplicatePersonAddressBook.json index 2a34dfda87b..8e0d965a1f2 100644 --- a/src/test/data/JsonSerializableAddressBookTest/duplicatePersonAddressBook.json +++ b/src/test/data/JsonSerializableAddressBookTest/duplicatePersonAddressBook.json @@ -9,7 +9,13 @@ "gender": "F", "age": 20, "allergies": ["Chocolate"], - "isPinned": true + "isPinned": true, + "appointments": [ + { + "title": "Eye Examination", + "dateTime": "18-09-2023 1800" + } + ] }, { "name": "Alice Pauline", @@ -19,7 +25,13 @@ "gender": "F", "age": 20, "allergies": ["Chocolate"], - "isPinned": true + "isPinned": true, + "appointments": [ + { + "title": "Eye Examination", + "dateTime": "18-09-2023 1800" + } + ] } ] } diff --git a/src/test/data/JsonSerializableAddressBookTest/invalidPersonAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/invalidPersonAddressBook.json index ad3f135ae42..c1c67020589 100644 --- a/src/test/data/JsonSerializableAddressBookTest/invalidPersonAddressBook.json +++ b/src/test/data/JsonSerializableAddressBookTest/invalidPersonAddressBook.json @@ -1,8 +1,20 @@ { - "persons": [ { - "name": "Hans Muster", - "phone": "9482424", - "email": "invalid@email!3e", - "address": "4th street" - } ] + "persons": [ + { + "name": "Hans Muster", + "phone": "9482424", + "email": "invalid@email!3e", + "gender": "M", + "age": "18", + "bloodType": "A+", + "allergies": ["Peanuts"], + "isPinned": true, + "appointments": [ + { + "title": "Eye Examination", + "dateTime": "18-09-2023 1800" + } + ] + } + ] } diff --git a/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json index b4f283c9559..174bf9e068b 100644 --- a/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json +++ b/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json @@ -9,7 +9,13 @@ "gender": "F", "age": 20, "allergies": ["Chocolate"], - "isPinned": true + "isPinned": true, + "appointments": [ + { + "title": "Eye Exam", + "dateTime": "01-01-2001 1200" + } + ] }, { "name": "Benson Meier", @@ -19,7 +25,13 @@ "gender": "M", "age": 15, "allergies": ["Pollen", "Soil"], - "isPinned": false + "isPinned": false, + "appointments": [ + { + "title": "Vaccination", + "dateTime": "11-09-2001 1200" + } + ] }, { "name": "Carl Kurz", @@ -29,7 +41,13 @@ "gender": "M", "age": 24, "allergies": ["Dogs"], - "isPinned": false + "isPinned": false, + "appointments": [ + { + "title": "Colonoscopy", + "dateTime": "21-12-2001 1200" + } + ] }, { "name": "Daniel Meier", @@ -39,7 +57,8 @@ "gender": "M", "age": 26, "allergies": ["Cats"], - "isPinned": false + "isPinned": false, + "appointments": [] }, { "name": "Elle Meyer", @@ -49,7 +68,8 @@ "gender": "F", "age": 27, "allergies": ["Light"], - "isPinned": false + "isPinned": false, + "appointments": [] }, { "name": "Fiona Kunz", @@ -59,7 +79,8 @@ "gender": "F", "age": 29, "allergies": [], - "isPinned": false + "isPinned": false, + "appointments": [] }, { "name": "George Best", @@ -69,7 +90,8 @@ "gender": "M", "age": 30, "allergies": [], - "isPinned": false + "isPinned": false, + "appointments": [] } ] } diff --git a/src/test/java/seedu/address/logic/LogicManagerTest.java b/src/test/java/seedu/address/logic/LogicManagerTest.java index 684b52961da..09e0f4c386d 100644 --- a/src/test/java/seedu/address/logic/LogicManagerTest.java +++ b/src/test/java/seedu/address/logic/LogicManagerTest.java @@ -49,7 +49,7 @@ public class LogicManagerTest { @BeforeEach public void setUp() { JsonAddressBookStorage addressBookStorage = - new JsonAddressBookStorage(temporaryFolder.resolve("addressBook.json")); + new JsonAddressBookStorage(temporaryFolder.resolve("medbook.json")); JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(temporaryFolder.resolve("userPrefs.json")); StorageManager storage = new StorageManager(addressBookStorage, userPrefsStorage); logic = new LogicManager(model, storage); diff --git a/src/test/java/seedu/address/logic/commands/AddAppointmentCommandTest.java b/src/test/java/seedu/address/logic/commands/AddAppointmentCommandTest.java new file mode 100644 index 00000000000..8db75368837 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/AddAppointmentCommandTest.java @@ -0,0 +1,156 @@ +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.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex; +import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; +import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; + +import org.junit.jupiter.api.Test; + +import seedu.address.commons.core.index.Index; +import seedu.address.commons.util.ToStringBuilder; +import seedu.address.logic.Messages; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.model.person.Person; +import seedu.address.model.person.appointment.Appointment; +import seedu.address.model.person.appointment.UniqueAppointmentList; +import seedu.address.testutil.AppointmentBuilder; +import seedu.address.testutil.PersonBuilder; + +public class AddAppointmentCommandTest { + + private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + + @Test + public void constructor_nullPerson_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> new AddAppointmentCommand(INDEX_FIRST_PERSON, null)); + } + + @Test + public void execute_validIndexUnfilteredList_success() { + Appointment validAppointment = new AppointmentBuilder().build(); + AddAppointmentCommand addAppointmentCommand = new AddAppointmentCommand(INDEX_FIRST_PERSON, validAppointment); + + Person personToAddAppointment = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + UniqueAppointmentList newAppointmentList = new UniqueAppointmentList(); + newAppointmentList.setAppointments(personToAddAppointment.getAppointments()); + newAppointmentList.add(validAppointment); + + Person editedPerson = new PersonBuilder(personToAddAppointment).withAppointments(newAppointmentList).build(); + + String expectedMessage = String.format(AddAppointmentCommand.MESSAGE_SUCCESS, + Messages.format(validAppointment, editedPerson)); + + ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson); + + assertCommandSuccess(addAppointmentCommand, model, expectedMessage, expectedModel); + } + + + @Test + public void execute_invalidIndexUnfilteredList_throwsCommandException() { + Appointment validAppointment = new AppointmentBuilder().build(); + Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1); + AddAppointmentCommand addAppointmentCommand = new AddAppointmentCommand(outOfBoundIndex, validAppointment); + + assertCommandFailure(addAppointmentCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + + @Test + public void execute_validIndexFilteredList_success() { + showPersonAtIndex(model, INDEX_FIRST_PERSON); + + Appointment validAppointment = new AppointmentBuilder().build(); + AddAppointmentCommand addAppointmentCommand = new AddAppointmentCommand(INDEX_FIRST_PERSON, validAppointment); + + Person personToAddAppointment = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + UniqueAppointmentList newAppointmentList = new UniqueAppointmentList(); + newAppointmentList.setAppointments(personToAddAppointment.getAppointments()); + newAppointmentList.add(validAppointment); + + Person editedPerson = new PersonBuilder(personToAddAppointment).withAppointments(newAppointmentList).build(); + + String expectedMessage = String.format(AddAppointmentCommand.MESSAGE_SUCCESS, + Messages.format(validAppointment, editedPerson)); + + ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + showPersonAtIndex(expectedModel, INDEX_FIRST_PERSON); + expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson); + + assertCommandSuccess(addAppointmentCommand, model, expectedMessage, expectedModel); + } + + @Test + public void execute_invalidIndexFilteredList_throwsCommandException() { + showPersonAtIndex(model, INDEX_FIRST_PERSON); + + Appointment validAppointment = new AppointmentBuilder().build(); + Index outOfBoundIndex = INDEX_SECOND_PERSON; + // ensures that outOfBoundIndex is still in bounds of address book list + assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size()); + + AddAppointmentCommand addAppointmentCommand = new AddAppointmentCommand(outOfBoundIndex, validAppointment); + + assertCommandFailure(addAppointmentCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + + @Test + public void execute_duplicateAppointment_throwsCommandException() { + Appointment validAppointment = new AppointmentBuilder().build(); + AddAppointmentCommand addAppointmentCommand = new AddAppointmentCommand(INDEX_FIRST_PERSON, validAppointment); + + Person personToAddAppointment = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + UniqueAppointmentList newAppointmentList = personToAddAppointment.getAppointments(); + newAppointmentList.add(validAppointment); + + Person editedPerson = new PersonBuilder(personToAddAppointment).withAppointments(newAppointmentList).build(); + + model.setPerson(model.getFilteredPersonList().get(0), editedPerson); + + assertThrows(CommandException.class, AddAppointmentCommand.MESSAGE_DUPLICATE_APPOINTMENT, () -> addAppointmentCommand.execute(model)); + } + + @Test + public void equals() { + Appointment eyeExam = new AppointmentBuilder().withTitle("Eye Exam").build(); + Appointment earExam = new AppointmentBuilder().withTitle("Ear Exam").build(); + + final AddAppointmentCommand standardCommand = new AddAppointmentCommand(INDEX_FIRST_PERSON, eyeExam); + + // 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 AddAppointmentCommand(INDEX_SECOND_PERSON, eyeExam))); + + // different descriptor -> returns false + assertFalse(standardCommand.equals(new AddAppointmentCommand(INDEX_FIRST_PERSON, earExam))); + } + + @Test + public void toStringMethod() { + Index targetIndex = Index.fromOneBased(1); + Appointment appointment = new AppointmentBuilder().build(); + AddAppointmentCommand addAppointmentCommand = new AddAppointmentCommand(targetIndex, appointment); + String expected = new ToStringBuilder(addAppointmentCommand) + .add("toAdd", appointment) + .toString(); + assertEquals(expected, addAppointmentCommand.toString()); + } +} diff --git a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java index f4c502fdb18..0e11e17fd4f 100644 --- a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java +++ b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java @@ -19,8 +19,11 @@ import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.AddressBook; import seedu.address.model.Model; +import seedu.address.model.person.Name; import seedu.address.model.person.NameContainsKeywordsPredicate; import seedu.address.model.person.Person; +import seedu.address.model.person.appointment.Appointment; +import seedu.address.model.person.appointment.DateTime; import seedu.address.testutil.EditPersonDescriptorBuilder; /** @@ -43,6 +46,14 @@ public class CommandTestUtil { public static final String VALID_ALLERGY_DUST = "Dust"; public static final String VALID_ALLERGY_PEANUTS = "Peanuts"; + public static final String VALID_TITLE_THYROID_CHECK = "Thyroid Check"; + public static final String VALID_TITLE_SLEEP_STUDY = "Sleep Study"; + public static final String VALID_DATETIME_THYROID_CHECK = "01-01-2001 1200"; + public static final String VALID_DATETIME_SLEEP_STUDY = "11-09-2001 1200"; + public static final Appointment VALID_APPOINTMENT_THYROID_CHECK = new Appointment(new Name(VALID_TITLE_THYROID_CHECK), + new DateTime(VALID_DATETIME_THYROID_CHECK)); + public static final Appointment VALID_APPOINTMENT_SLEEP_STUDY = new Appointment(new Name(VALID_TITLE_SLEEP_STUDY), new DateTime(VALID_DATETIME_SLEEP_STUDY)); + public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY; public static final String NAME_DESC_BOB = " " + PREFIX_NAME + VALID_NAME_BOB; public static final String EMAIL_DESC_AMY = " " + PREFIX_EMAIL + VALID_EMAIL_AMY; diff --git a/src/test/java/seedu/address/logic/commands/EditCommandTest.java b/src/test/java/seedu/address/logic/commands/EditCommandTest.java index eb86f71709c..1d4d95f3931 100644 --- a/src/test/java/seedu/address/logic/commands/EditCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/EditCommandTest.java @@ -38,7 +38,8 @@ public class EditCommandTest { @Test public void execute_allFieldsSpecifiedUnfilteredList_success() { - Person editedPerson = new PersonBuilder().build(); + Person personToEdit = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + Person editedPerson = new PersonBuilder().withIsPinned(personToEdit.isPinned()).withAppointments(personToEdit.getAppointments()).build(); EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(editedPerson).build(); EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor); @@ -58,7 +59,7 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() { PersonBuilder personInList = new PersonBuilder(lastPerson); Person editedPerson = personInList.withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB) - .withAllergies(VALID_ALLERGY_DUST).build(); + .withAllergies(VALID_ALLERGY_DUST).withIsPinned(lastPerson.isPinned()).withAppointments(lastPerson.getAppointments()).build(); EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB) .withPhone(VALID_PHONE_BOB).withAllergies(VALID_ALLERGY_DUST).build(); @@ -90,7 +91,7 @@ public void execute_filteredList_success() { Person personInFilteredList = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); - Person editedPerson = new PersonBuilder(personInFilteredList).withName(VALID_NAME_BOB).build(); + Person editedPerson = new PersonBuilder(personInFilteredList).withName(VALID_NAME_BOB).withIsPinned(personInFilteredList.isPinned()).withAppointments(personInFilteredList.getAppointments()).build(); EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build()); diff --git a/src/test/java/seedu/address/logic/commands/EditPersonDescriptorTest.java b/src/test/java/seedu/address/logic/commands/EditPersonDescriptorTest.java index 931ac279cea..4fc9ca65a86 100644 --- a/src/test/java/seedu/address/logic/commands/EditPersonDescriptorTest.java +++ b/src/test/java/seedu/address/logic/commands/EditPersonDescriptorTest.java @@ -77,8 +77,7 @@ public void toStringMethod() { + editPersonDescriptor.getGender().orElse(null) + ", age=" + editPersonDescriptor.getAge().orElse(null) + ", bloodType=" + editPersonDescriptor.getBloodType().orElse(null) + ", allergies=" - + editPersonDescriptor.getAllergies().orElse(null) + ", isPinned=" - + editPersonDescriptor.getIsPinned().orElse(null) + "}"; + + editPersonDescriptor.getAllergies().orElse(null) + "}"; assertEquals(expected, editPersonDescriptor.toString()); } } diff --git a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java index 5a1ab3dbc0c..8a0b63d9d28 100644 --- a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java @@ -13,6 +13,7 @@ import org.junit.jupiter.api.Test; +import seedu.address.logic.commands.AddAppointmentCommand; import seedu.address.logic.commands.AddCommand; import seedu.address.logic.commands.ClearCommand; import seedu.address.logic.commands.DeleteCommand; @@ -22,9 +23,14 @@ import seedu.address.logic.commands.FindCommand; import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ListCommand; +import seedu.address.logic.commands.PinCommand; +import seedu.address.logic.commands.UnpinCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.person.NameContainsKeywordsPredicate; import seedu.address.model.person.Person; +import seedu.address.model.person.appointment.Appointment; +import seedu.address.testutil.AppointmentBuilder; +import seedu.address.testutil.AppointmentUtil; import seedu.address.testutil.EditPersonDescriptorBuilder; import seedu.address.testutil.PersonBuilder; import seedu.address.testutil.PersonUtil; @@ -88,6 +94,28 @@ public void parseCommand_list() throws Exception { assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD + " 3") instanceof ListCommand); } + @Test + public void parseCommand_pin() throws Exception { + PinCommand command = (PinCommand) parser.parseCommand( + PinCommand.COMMAND_WORD + " " + INDEX_FIRST_PERSON.getOneBased()); + assertEquals(new PinCommand(INDEX_FIRST_PERSON), command); + } + + @Test + public void parseCommand_unpin() throws Exception { + UnpinCommand command = (UnpinCommand) parser.parseCommand( + UnpinCommand.COMMAND_WORD + " " + INDEX_FIRST_PERSON.getOneBased()); + assertEquals(new UnpinCommand(INDEX_FIRST_PERSON), command); + } + + @Test + public void parseCommand_addAppointment() throws Exception { + Appointment appointment = new AppointmentBuilder().build(); + AddAppointmentCommand command = (AddAppointmentCommand) parser + .parseCommand(AppointmentUtil.getAddAppointmentCommand(appointment)); + assertEquals(new AddAppointmentCommand(INDEX_FIRST_PERSON, appointment), command); + } + @Test public void parseCommand_unrecognisedInput_throwsParseException() { assertThrows(ParseException.class, String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE), () diff --git a/src/test/java/seedu/address/testutil/AppointmentBuilder.java b/src/test/java/seedu/address/testutil/AppointmentBuilder.java new file mode 100644 index 00000000000..9f5762d6a8b --- /dev/null +++ b/src/test/java/seedu/address/testutil/AppointmentBuilder.java @@ -0,0 +1,54 @@ +package seedu.address.testutil; + +import seedu.address.model.person.Name; +import seedu.address.model.person.appointment.Appointment; +import seedu.address.model.person.appointment.DateTime; + +/** + * A utility class to help with building Appointment objects. + */ +public class AppointmentBuilder { + + public static final String DEFAULT_TITLE = "Nose Exam"; + public static final String DEFAULT_DATETIME = "27-10-2010 1200"; + + private Name title; + private DateTime dateTime; + + /** + * Creates a {@code AppoointmentBuilder} with the default details. + */ + public AppointmentBuilder() { + title = new Name(DEFAULT_TITLE); + dateTime = new DateTime(DEFAULT_DATETIME); + } + + /** + * InitializesAppointment with the data of {@code personToCopy}. + */ + public AppointmentBuilder(Appointment appointmentToCopy) { + title = appointmentToCopy.getTitle(); + dateTime = appointmentToCopy.getDateTime(); + } + + /** + * Sets the {@code Name} of the {@code Person} that we are building. + */ + public AppointmentBuilder withTitle(String title) { + this.title = new Name(title); + return this; + } + + /** + * Sets the {@code DateTime} of the {@code Appointment} that we are building. + */ + public AppointmentBuilder withDateTime(String dateTime) { + this.dateTime = new DateTime(dateTime); + return this; + } + + public Appointment build() { + return new Appointment(title, dateTime); + } + +} diff --git a/src/test/java/seedu/address/testutil/AppointmentUtil.java b/src/test/java/seedu/address/testutil/AppointmentUtil.java new file mode 100644 index 00000000000..b8499aefa77 --- /dev/null +++ b/src/test/java/seedu/address/testutil/AppointmentUtil.java @@ -0,0 +1,29 @@ +package seedu.address.testutil; + +import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE; +import static seedu.address.logic.parser.CliSyntax.PREFIX_TITLE; + +import seedu.address.logic.commands.AddAppointmentCommand; +import seedu.address.model.person.appointment.Appointment; +/** + * A utility class for Appointment. + */ +public class AppointmentUtil { + + /** + * Returns an add command string for adding the {@code Appointment}. + */ + public static String getAddAppointmentCommand(Appointment Appointment) { + return AddAppointmentCommand.COMMAND_WORD + " 1 " + getAppointmentDetails(Appointment); + } + + /** + * Returns the part of command string for the given {@code Appointment}'s details. + */ + public static String getAppointmentDetails(Appointment Appointment) { + StringBuilder sb = new StringBuilder(); + sb.append(PREFIX_TITLE + Appointment.getTitle().fullName + " "); + sb.append(PREFIX_DATE + Appointment.getDateTime().toString()); + return sb.toString(); + } +} diff --git a/src/test/java/seedu/address/testutil/EditPersonDescriptorBuilder.java b/src/test/java/seedu/address/testutil/EditPersonDescriptorBuilder.java index 6caeeb756e2..4c26fbe83b7 100644 --- a/src/test/java/seedu/address/testutil/EditPersonDescriptorBuilder.java +++ b/src/test/java/seedu/address/testutil/EditPersonDescriptorBuilder.java @@ -1,8 +1,6 @@ package seedu.address.testutil; import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; import seedu.address.logic.commands.EditCommand.EditPersonDescriptor; import seedu.address.model.person.Age; @@ -13,6 +11,7 @@ import seedu.address.model.person.Name; import seedu.address.model.person.Person; import seedu.address.model.person.Phone; +import seedu.address.model.util.SampleDataUtil; /** * A utility class to help with building EditPersonDescriptor objects. @@ -41,7 +40,6 @@ public EditPersonDescriptorBuilder(Person person) { descriptor.setAge(person.getAge()); descriptor.setBloodType(person.getBloodType()); descriptor.setAllergies(person.getAllergies()); - descriptor.setIsPinned(person.isPinned()); } /** @@ -97,7 +95,7 @@ public EditPersonDescriptorBuilder withBloodType(String bloodType) { * that we are building. */ public EditPersonDescriptorBuilder withAllergies(String... allergies) { - Set allergySet = Stream.of(allergies).map(Allergy::new).collect(Collectors.toSet()); + Set allergySet = SampleDataUtil.getAllergySet(allergies); descriptor.setAllergies(allergySet); return this; } diff --git a/src/test/java/seedu/address/testutil/PersonBuilder.java b/src/test/java/seedu/address/testutil/PersonBuilder.java index 1ee1b6afdd0..52588505052 100644 --- a/src/test/java/seedu/address/testutil/PersonBuilder.java +++ b/src/test/java/seedu/address/testutil/PersonBuilder.java @@ -134,8 +134,8 @@ public PersonBuilder withIsPinned(boolean isPinned) { /** * Sets the {@code appointments} of the {@code Person} that we are building. */ - public PersonBuilder withAppointments(UniqueAppointmentList appointments) { - this.appointments = appointments; + public PersonBuilder withAppointments(UniqueAppointmentList appointment) { + this.appointments = appointment; return this; } diff --git a/src/test/java/seedu/address/testutil/TypicalAppointments.java b/src/test/java/seedu/address/testutil/TypicalAppointments.java new file mode 100644 index 00000000000..92c7c15de0b --- /dev/null +++ b/src/test/java/seedu/address/testutil/TypicalAppointments.java @@ -0,0 +1,44 @@ +package seedu.address.testutil; + +import static seedu.address.logic.commands.CommandTestUtil.VALID_DATETIME_SLEEP_STUDY; +import static seedu.address.logic.commands.CommandTestUtil.VALID_DATETIME_THYROID_CHECK; +import static seedu.address.logic.commands.CommandTestUtil.VALID_TITLE_SLEEP_STUDY; +import static seedu.address.logic.commands.CommandTestUtil.VALID_TITLE_THYROID_CHECK; + +import seedu.address.model.person.appointment.Appointment; +import seedu.address.model.person.appointment.UniqueAppointmentList; +/** + * A utility class containing a list of {@code Appointment} objects to be used in tests. + */ +public class TypicalAppointments { + + public static final Appointment EYE_EXAM = new AppointmentBuilder().withTitle("Eye Exam") + .withDateTime("01-01-2001 1200").build(); + public static final Appointment VACCINATION = new AppointmentBuilder().withTitle("Vaccination") + .withDateTime("11-09-2001 1200").build(); + public static final Appointment COLONOSCOPY = new AppointmentBuilder().withTitle("Colonoscopy").withDateTime("21-12-2001 1200").build(); + + // Manually added + public static final Appointment BIOPSY = new AppointmentBuilder().withTitle("Biopsy") + .withDateTime("01-01-2001 1200").build(); + public static final Appointment STD_TEST = new AppointmentBuilder().withTitle("STD Test") + .withDateTime("17-08-1964 1900").build(); + + // Manually added - Appointment's details found in {@code CommandTestUtil} + public static final Appointment SLEEP_STUDY = new AppointmentBuilder().withTitle(VALID_TITLE_SLEEP_STUDY) + .withDateTime(VALID_DATETIME_SLEEP_STUDY).build(); + public static final Appointment THYROID_CHECK = new AppointmentBuilder().withTitle(VALID_TITLE_THYROID_CHECK) + .withDateTime(VALID_DATETIME_THYROID_CHECK).build(); + + private TypicalAppointments() {} // prevents instantiation + + public static UniqueAppointmentList getTypicalAppointments() { + UniqueAppointmentList ab = new UniqueAppointmentList(); + ab.add(EYE_EXAM); + ab.add(VACCINATION); + ab.add(COLONOSCOPY); + return ab; + } + + +} diff --git a/src/test/java/seedu/address/testutil/TypicalPersons.java b/src/test/java/seedu/address/testutil/TypicalPersons.java index c4c6b6e7bb0..e02cbed5f2d 100644 --- a/src/test/java/seedu/address/testutil/TypicalPersons.java +++ b/src/test/java/seedu/address/testutil/TypicalPersons.java @@ -32,47 +32,57 @@ public class TypicalPersons { .withGender("F") .withAge(20) .withBloodType("AB+") - .withAllergies("Chocolate").withIsPinned(true).build(); + .withAllergies("Chocolate") + .withIsPinned(true) + .withAppointments(TypicalAppointments.getTypicalAppointments()).build(); public static final Person BENSON = new PersonBuilder().withName("Benson Meier") .withEmail("johnd@example.com") .withPhone("98765432") .withGender("M") .withAge(15) .withBloodType("B-") - .withAllergies("Pollen", "Soil").build(); + .withAllergies("Pollen", "Soil"). + withIsPinned(false). + withAppointments(TypicalAppointments.getTypicalAppointments()).build(); public static final Person CARL = new PersonBuilder().withName("Carl Kurz") .withEmail("heinz@example.com") .withPhone("95352563") .withGender("M") .withAge(24) .withBloodType("AB-") - .withAllergies("Dogs").build(); + .withAllergies("Dogs") + .withIsPinned(false). + withAppointments(TypicalAppointments.getTypicalAppointments()).build(); public static final Person DANIEL = new PersonBuilder().withName("Daniel Meier") .withEmail("cornelia@example.com") .withPhone("87652533") .withGender("M") .withAge(26) .withBloodType("AB+") - .withAllergies("Cats").build(); + .withAllergies("Cats") + .withIsPinned(false).build(); public static final Person ELLE = new PersonBuilder().withName("Elle Meyer") .withPhone("9482224") .withEmail("werner@example.com") .withGender("F") .withAge(27) .withBloodType("A-") - .withAllergies("Light").build(); + .withAllergies("Light") + .withIsPinned(false).build(); public static final Person FIONA = new PersonBuilder().withName("Fiona Kunz") .withEmail("lydia@example.com") .withPhone("9482427") .withGender("F") .withAge(29) - .withBloodType("B+").build(); + .withBloodType("B+") + .withIsPinned(false).build(); public static final Person GEORGE = new PersonBuilder().withName("George Best") .withEmail("anna@example.com") .withPhone("9482442") .withGender("M") .withAge(30) - .withBloodType("O+").build(); + .withBloodType("O+") + .withIsPinned(false).build(); // Manually added public static final Person HOON = new PersonBuilder().withName("Hoon Meier") @@ -80,13 +90,15 @@ public class TypicalPersons { .withPhone("8482424") .withGender("M") .withAge(30) - .withBloodType("A+").build(); + .withBloodType("A+") + .withIsPinned(true).build(); public static final Person IDA = new PersonBuilder().withName("Ida Mueller") .withEmail("hans@example.com") .withPhone("8482131") .withGender("M") .withAge(33) - .withBloodType("B+").build(); + .withBloodType("B+") + .withIsPinned(false).build(); // Manually added - Person's details found in {@code CommandTestUtil} public static final Person AMY = new PersonBuilder().withName(VALID_NAME_AMY) @@ -95,7 +107,8 @@ public class TypicalPersons { .withGender(VALID_GENDER_AMY) .withAge(VALID_AGE_AMY) .withBloodType(VALID_BLOODTYPE_AMY) - .withAllergies(VALID_ALLERGY_DUST).build(); + .withAllergies(VALID_ALLERGY_DUST). + build(); public static final Person BOB = new PersonBuilder().withName(VALID_NAME_BOB) .withEmail(VALID_EMAIL_BOB) .withPhone(VALID_PHONE_BOB)