From 750d80b2f11438dbed5f17f028ec43989b73bf00 Mon Sep 17 00:00:00 2001 From: huyuxin0429 Date: Sat, 6 Nov 2021 22:55:31 +0800 Subject: [PATCH] Add tests for prescriptions * Prescription test work in progress * Removed model with prescription stub * Add test for prescription * Add test for prescription * Add test for prescription * Add test for prescription * Fix checkstyle * Renamed parser test files * Add tests for Prescription model * Rename packages * Add code quality improvements --- .../prescription/AddPrescriptionCommand.java | 46 +++-- .../DeletePrescriptionCommand.java | 18 +- .../ListPrescriptionsCommand.java | 46 ----- .../logic/parser/AppointmentBookParser.java | 4 +- .../AddPrescriptionCommandParser.java | 25 ++- .../DeletePrescriptionCommandParser.java | 25 +-- .../ListPrescriptionsCommandParser.java | 38 ---- .../docit/model/appointment/Appointment.java | 2 +- .../model/prescription/Prescription.java | 26 +-- .../docit/logic/commands/CommandTestUtil.java | 9 + .../AddPrescriptionCommandTest.java | 163 ++++++++++++++++++ .../DeletePrescriptionCommandTest.java | 105 +++++++++++ .../AddPrescriptionCommandParserTest.java | 72 ++++++++ .../DeletePrescriptionCommandParserTest.java | 75 ++++++++ .../seedu/docit/model/PrescriptionTest.java | 71 ++++++++ .../stubs/appointment/AppointmentStub.java | 13 ++ 16 files changed, 589 insertions(+), 149 deletions(-) delete mode 100644 src/main/java/seedu/docit/logic/commands/prescription/ListPrescriptionsCommand.java delete mode 100644 src/main/java/seedu/docit/logic/parser/prescription/ListPrescriptionsCommandParser.java create mode 100644 src/test/java/seedu/docit/logic/commands/prescriptions/AddPrescriptionCommandTest.java create mode 100644 src/test/java/seedu/docit/logic/commands/prescriptions/DeletePrescriptionCommandTest.java create mode 100644 src/test/java/seedu/docit/logic/parser/prescription/AddPrescriptionCommandParserTest.java create mode 100644 src/test/java/seedu/docit/logic/parser/prescription/DeletePrescriptionCommandParserTest.java create mode 100644 src/test/java/seedu/docit/model/PrescriptionTest.java create mode 100644 src/test/java/seedu/docit/testutil/stubs/appointment/AppointmentStub.java diff --git a/src/main/java/seedu/docit/logic/commands/prescription/AddPrescriptionCommand.java b/src/main/java/seedu/docit/logic/commands/prescription/AddPrescriptionCommand.java index 5aa8f61d159..0e7db4e8e7d 100644 --- a/src/main/java/seedu/docit/logic/commands/prescription/AddPrescriptionCommand.java +++ b/src/main/java/seedu/docit/logic/commands/prescription/AddPrescriptionCommand.java @@ -3,6 +3,7 @@ import static java.util.Objects.requireNonNull; import java.util.List; +import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; @@ -40,9 +41,15 @@ public class AddPrescriptionCommand extends AppointmentCommand { + "Volume: %2$s\nDuration: %3$s"; public static final String MESSAGE_DUPLICATE_MEDICINE = "This medicine already exists in the prescription for this appointment"; - public static final String MESSAGE_FIELD_TOO_LONG = - "Medicine name can only be 20 characters long. Volume field can only be 20 characters long. " - + "Duration field can only be 40 characters long."; + private static final String MESSAGE_FIELD_TOO_LONG = + "Medicine name can only be %1$s characters long. \nVolume field can only be %2$s characters long. " + + "\nDuration field can only be %3$s characters long."; + + public static final String INPUT_TOO_LONG_ERROR_MESSAGE = String.format(MESSAGE_FIELD_TOO_LONG, + Prescription.MEDICINE_CHAR_LENGTH_LIMIT, + Prescription.VOLUME_CHAR_LENGTH_LIMIT, + Prescription.DURATION_CHAR_LENGTH_LIMIT); + private static Logger logger = Logger.getLogger("AddPrescriptionCommand"); private final Index targetAppointmentIndex; @@ -60,9 +67,9 @@ public AddPrescriptionCommand(Index targetAppointmentIndex, String medicine, Str requireNonNull(volume); requireNonNull(duration); this.targetAppointmentIndex = targetAppointmentIndex; - this.volume = volume; - this.medicine = medicine; - this.duration = duration; + this.volume = volume.toLowerCase(); + this.medicine = medicine.toLowerCase(); + this.duration = duration.toLowerCase(); } @Override @@ -76,9 +83,9 @@ public CommandResult execute(Model model) throws CommandException { + Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX); throw new CommandException(Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX); } - + assert (targetAppointmentIndex.getZeroBased() >= 0 + && targetAppointmentIndex.getZeroBased() < lastShownList.size()); Appointment appointmentToMakePrescription = lastShownList.get(targetAppointmentIndex.getZeroBased()); - Prescription prescriptionToAdd = new Prescription(medicine, volume, duration); if (appointmentToMakePrescription.containsPrescription(prescriptionToAdd)) { @@ -87,10 +94,11 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_DUPLICATE_MEDICINE); } - if (volume.length() > 20 || medicine.length() > 20 || duration.length() > 40) { - logger.log(Level.WARNING, "prescription adding error, " - + MESSAGE_FIELD_TOO_LONG); - throw new CommandException(MESSAGE_FIELD_TOO_LONG); + if (volume.length() > Prescription.VOLUME_CHAR_LENGTH_LIMIT + || medicine.length() > Prescription.MEDICINE_CHAR_LENGTH_LIMIT + || duration.length() > Prescription.DURATION_CHAR_LENGTH_LIMIT) { + logger.log(Level.WARNING, INPUT_TOO_LONG_ERROR_MESSAGE); + throw new CommandException(INPUT_TOO_LONG_ERROR_MESSAGE); } model.addPrescription(appointmentToMakePrescription, prescriptionToAdd); @@ -98,4 +106,18 @@ public CommandResult execute(Model model) throws CommandException { logger.log(Level.INFO, "prescription adding success"); return new CommandResult(String.format(MESSAGE_SUCCESS, medicine, volume, duration)); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + AddPrescriptionCommand that = (AddPrescriptionCommand) o; + return Objects.equals(targetAppointmentIndex, that.targetAppointmentIndex) + && Objects.equals(medicine, that.medicine) && Objects.equals(volume, that.volume) + && Objects.equals(duration, that.duration); + } } diff --git a/src/main/java/seedu/docit/logic/commands/prescription/DeletePrescriptionCommand.java b/src/main/java/seedu/docit/logic/commands/prescription/DeletePrescriptionCommand.java index 1a9d9becec3..e8951ab49db 100644 --- a/src/main/java/seedu/docit/logic/commands/prescription/DeletePrescriptionCommand.java +++ b/src/main/java/seedu/docit/logic/commands/prescription/DeletePrescriptionCommand.java @@ -4,6 +4,8 @@ import java.util.List; import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; import seedu.docit.commons.core.Messages; import seedu.docit.commons.core.index.Index; @@ -32,6 +34,9 @@ public class DeletePrescriptionCommand extends AppointmentCommand { public static final String MESSAGE_DELETE_PRESCRIPTION_SUCCESS = "Deleted prescription: \nMedicine: %1$s\n\n" + "from %2$s's appointment."; + private static Logger logger = Logger.getLogger("DeletePrescriptionCommand"); + + private final Index targetAppointmentIndex; private final String targetMedicineName; @@ -46,22 +51,29 @@ public DeletePrescriptionCommand(Index targetAppointmentIndex, String targetMedi } @Override public CommandResult execute(Model model) throws CommandException { + logger.log(Level.INFO, "going to start deleting prescription"); requireNonNull(model); List lastShownList = model.getFilteredAppointmentList(); if (targetAppointmentIndex.getZeroBased() >= lastShownList.size()) { + logger.log(Level.WARNING, "deleting prescription failed, " + + Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX); throw new CommandException(Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX); } + assert (targetAppointmentIndex.getZeroBased() >= 0 + && targetAppointmentIndex.getZeroBased() < lastShownList.size()); Appointment appointmentToTarget = lastShownList.get(targetAppointmentIndex.getZeroBased()); try { model.deletePrescription(appointmentToTarget, targetMedicineName); + logger.log(Level.INFO, "deleting prescription success"); return new CommandResult(String.format(MESSAGE_DELETE_PRESCRIPTION_SUCCESS, targetMedicineName, appointmentToTarget.getPatient().getName())); } catch (MedicineNotFoundException e) { + logger.log(Level.WARNING, "deleting prescription failed, " + + e.getMessage()); throw new CommandException(e.getMessage()); } - } @Override @@ -77,8 +89,4 @@ public boolean equals(Object o) { && Objects.equals(targetMedicineName, that.targetMedicineName); } - @Override - public int hashCode() { - return Objects.hash(targetAppointmentIndex, targetMedicineName); - } } diff --git a/src/main/java/seedu/docit/logic/commands/prescription/ListPrescriptionsCommand.java b/src/main/java/seedu/docit/logic/commands/prescription/ListPrescriptionsCommand.java deleted file mode 100644 index b889cf82e0e..00000000000 --- a/src/main/java/seedu/docit/logic/commands/prescription/ListPrescriptionsCommand.java +++ /dev/null @@ -1,46 +0,0 @@ -package seedu.docit.logic.commands.prescription; - -import static java.util.Objects.requireNonNull; - -import java.util.List; - -import seedu.docit.commons.core.Messages; -import seedu.docit.commons.core.index.Index; -import seedu.docit.logic.commands.AppointmentCommand; -import seedu.docit.logic.commands.CommandResult; -import seedu.docit.logic.commands.exceptions.CommandException; -import seedu.docit.logic.parser.CliSyntax; -import seedu.docit.model.Model; -import seedu.docit.model.appointment.Appointment; - - -public class ListPrescriptionsCommand extends AppointmentCommand { - public static final String COMMAND_WORD = "pl"; - - public static final String MESSAGE_USAGE = "apmt " - + COMMAND_WORD + ": List all prescriptions of an appointment " - + "Parameters: \n" - + CliSyntax.PREFIX_INDEX + "ID OF APPOINTMENT \n" - + "Example: apmt " + COMMAND_WORD + " " - + CliSyntax.PREFIX_INDEX + "1 "; - - public static final String MESSAGE_SUCCESS = "Listed all prescriptions of appointment"; - - private final Index targetAppointmentIndex; - - public ListPrescriptionsCommand(Index targetAppointmentIndex) { - this.targetAppointmentIndex = targetAppointmentIndex; - } - - @Override public CommandResult execute(Model model) throws CommandException { - requireNonNull(model); - List lastShownList = model.getFilteredAppointmentList(); - if (targetAppointmentIndex.getZeroBased() >= lastShownList.size()) { - throw new CommandException(Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX); - } - - Appointment appointmentToTarget = lastShownList.get(targetAppointmentIndex.getZeroBased()); - - return new CommandResult(MESSAGE_SUCCESS + appointmentToTarget.getPrescriptions()); - } -} diff --git a/src/main/java/seedu/docit/logic/parser/AppointmentBookParser.java b/src/main/java/seedu/docit/logic/parser/AppointmentBookParser.java index 54f61a77b35..c7f162d3f97 100644 --- a/src/main/java/seedu/docit/logic/parser/AppointmentBookParser.java +++ b/src/main/java/seedu/docit/logic/parser/AppointmentBookParser.java @@ -44,9 +44,9 @@ public AppointmentCommand parseAppointmentCommand(String commandWord, String arg case SortAppointmentsCommand.COMMAND_WORD: return new SortAppointmentsCommand(); case AddPrescriptionCommand.COMMAND_WORD: - return new AddPrescriptionCommandParser().parse(arguments); + return new AddPrescriptionCommandParser().parseAppointmentCommand(arguments); case DeletePrescriptionCommand.COMMAND_WORD: - return new DeletePrescriptionCommandParser().parse(arguments); + return new DeletePrescriptionCommandParser().parseAppointmentCommand(arguments); default: throw new ParseException(MESSAGE_UNKNOWN_COMMAND); } diff --git a/src/main/java/seedu/docit/logic/parser/prescription/AddPrescriptionCommandParser.java b/src/main/java/seedu/docit/logic/parser/prescription/AddPrescriptionCommandParser.java index 26d189acefe..3ea2b262a91 100644 --- a/src/main/java/seedu/docit/logic/parser/prescription/AddPrescriptionCommandParser.java +++ b/src/main/java/seedu/docit/logic/parser/prescription/AddPrescriptionCommandParser.java @@ -4,10 +4,10 @@ import seedu.docit.commons.core.index.Index; import seedu.docit.logic.commands.prescription.AddPrescriptionCommand; +import seedu.docit.logic.parser.AppointmentParser; import seedu.docit.logic.parser.ArgumentMultimap; import seedu.docit.logic.parser.ArgumentTokenizer; import seedu.docit.logic.parser.CliSyntax; -import seedu.docit.logic.parser.Parser; import seedu.docit.logic.parser.ParserUtil; import seedu.docit.logic.parser.exceptions.ParseException; @@ -15,7 +15,8 @@ /** * Parses input arguments and creates a new AddPrescriptionCommand object */ -public class AddPrescriptionCommandParser implements Parser { +public class AddPrescriptionCommandParser implements AppointmentParser { + public static final String EMPTY_FIELD_ERROR_MESSAGE = "Medicine/Duration/Volume fields cannot be blank."; /** * Parses the given {@code String} of arguments in the context of the AddPrescriptionCommand and returns an @@ -25,30 +26,24 @@ public class AddPrescriptionCommandParser implements Parser { +public class DeletePrescriptionCommandParser implements AppointmentParser { + public static final String EMPTY_MEDICINE_FIELD_ERROR_MESSAGE = "Medicine fields cannot be blank."; /** * Parses the given {@code String} of arguments in the context of the DeletePrescriptionCommand and returns a * DeletePrescriptionCommand object for execution. * * @throws ParseException if the user input does not conform the expected format */ - public DeletePrescriptionCommand parse(String args) throws ParseException { + public DeletePrescriptionCommand parseAppointmentCommand(String args) throws ParseException { Index index; ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, CliSyntax.PREFIX_NAME); - try { - index = ParserUtil.parseIndex(argMultimap.getPreamble()); - } catch (ParseException pe) { - throw new ParseException( - String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeletePrescriptionCommand.MESSAGE_USAGE), pe); - } - if (!ParserUtil.hasAllPrefixes(argMultimap, CliSyntax.PREFIX_NAME) || argMultimap.getPreamble().isEmpty()) { throw new ParseException( String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeletePrescriptionCommand.MESSAGE_USAGE)); } + try { + index = ParserUtil.parseIndex(argMultimap.getPreamble()); + } catch (ParseException pe) { + throw pe; + } + String medicineName = argMultimap.getValue(CliSyntax.PREFIX_NAME).get(); - return new DeletePrescriptionCommand(index, medicineName); + if (medicineName.isBlank()) { + throw new ParseException(EMPTY_MEDICINE_FIELD_ERROR_MESSAGE); + } + return new DeletePrescriptionCommand(index, medicineName); } } diff --git a/src/main/java/seedu/docit/logic/parser/prescription/ListPrescriptionsCommandParser.java b/src/main/java/seedu/docit/logic/parser/prescription/ListPrescriptionsCommandParser.java deleted file mode 100644 index dcbc5cb9b55..00000000000 --- a/src/main/java/seedu/docit/logic/parser/prescription/ListPrescriptionsCommandParser.java +++ /dev/null @@ -1,38 +0,0 @@ -package seedu.docit.logic.parser.prescription; - -import static seedu.docit.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; - -import seedu.docit.commons.core.index.Index; -import seedu.docit.logic.commands.prescription.ListPrescriptionsCommand; -import seedu.docit.logic.parser.ArgumentMultimap; -import seedu.docit.logic.parser.ArgumentTokenizer; -import seedu.docit.logic.parser.CliSyntax; -import seedu.docit.logic.parser.Parser; -import seedu.docit.logic.parser.ParserUtil; -import seedu.docit.logic.parser.exceptions.ParseException; - - -public class ListPrescriptionsCommandParser implements Parser { - /** - * Parses the given {@code String} of arguments in the context of the ListPrescriptionsCommand and returns a - * ListPrescriptionsCommand object for execution. - * - * @throws ParseException if the user input does not conform the expected format - */ - public ListPrescriptionsCommand parse(String args) throws ParseException { - ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(args, CliSyntax.PREFIX_INDEX); - - if (!ParserUtil.hasAllPrefixes(argMultimap, CliSyntax.PREFIX_INDEX) - || !argMultimap.getPreamble().isEmpty()) { - throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, - ListPrescriptionsCommand.MESSAGE_USAGE)); - } - - Index appointmentIndex = ParserUtil.parseIndex(argMultimap.getValue(CliSyntax.PREFIX_INDEX).get()); - - ListPrescriptionsCommand newCommand = new ListPrescriptionsCommand(appointmentIndex); - - return newCommand; - } -} diff --git a/src/main/java/seedu/docit/model/appointment/Appointment.java b/src/main/java/seedu/docit/model/appointment/Appointment.java index 46420c3d9b4..537fed0cf9d 100644 --- a/src/main/java/seedu/docit/model/appointment/Appointment.java +++ b/src/main/java/seedu/docit/model/appointment/Appointment.java @@ -86,7 +86,7 @@ public void addPrescription(Prescription prescription) throws DuplicatePrescript */ public void removePrescription(String medicineName) throws MedicineNotFoundException { if (!this.prescriptions.removeIf(p -> p.hasSameMedicalName( - new Prescription(medicineName, "", "")))) { + new Prescription(medicineName, "default", "default")))) { throw new MedicineNotFoundException(); } Set p = new HashSet<>(); diff --git a/src/main/java/seedu/docit/model/prescription/Prescription.java b/src/main/java/seedu/docit/model/prescription/Prescription.java index 0a26991b4c2..74d9772a5c8 100644 --- a/src/main/java/seedu/docit/model/prescription/Prescription.java +++ b/src/main/java/seedu/docit/model/prescription/Prescription.java @@ -1,8 +1,10 @@ package seedu.docit.model.prescription; -import java.util.Objects; - public class Prescription { + public static final int MEDICINE_CHAR_LENGTH_LIMIT = 20; + public static final int VOLUME_CHAR_LENGTH_LIMIT = 20; + public static final int DURATION_CHAR_LENGTH_LIMIT = 40; + private String medicine; private String volume; private String duration; @@ -14,6 +16,9 @@ public class Prescription { * @param duration Duration of medicine intake */ public Prescription(String medicine, String volume, String duration) { + if (medicine.isBlank() || volume.isBlank() || duration.isBlank()) { + throw new RuntimeException("Medicine cannot be blank. Volume cannot be blank. Duration cannot be blank."); + } this.medicine = medicine.toLowerCase(); this.volume = volume.toLowerCase(); this.duration = duration.toLowerCase(); @@ -32,18 +37,6 @@ public String getVolume() { return volume; } - public boolean medicineContain(String term) { - return medicine.contains(term); - } - - public boolean volumeContain(String term) { - return volume.contains(term); - } - - public boolean durationContain(String term) { - return duration.contains(term); - } - /** * A looser definition of equality where two prescriptions are considered the same if they have the same medicine * name. @@ -72,11 +65,6 @@ public boolean equals(Object o) { && that.getVolume().equals(getVolume()); } - @Override - public int hashCode() { - return Objects.hash(getMedicine(), getVolume(), getDuration()); - } - @Override public String toString() { return "Medicine: " + medicine diff --git a/src/test/java/seedu/docit/logic/commands/CommandTestUtil.java b/src/test/java/seedu/docit/logic/commands/CommandTestUtil.java index cedae4a6079..4ad21f3b0cf 100644 --- a/src/test/java/seedu/docit/logic/commands/CommandTestUtil.java +++ b/src/test/java/seedu/docit/logic/commands/CommandTestUtil.java @@ -95,6 +95,15 @@ public class CommandTestUtil { public static final EditPatientCommand.EditPatientDescriptor DESC_AMY; public static final EditPatientCommand.EditPatientDescriptor DESC_BOB; + public static final String VALID_PRESCRIPTION_MEDICINE = "Penicillin"; + public static final String VALID_PRESCRIPTION_VOLUME = "400 ml"; + public static final String VALID_PRESCRIPTION_DURATION = "2 times a week"; + + public static final String VALID_APPOINTMENT_INDEX = "1"; + + public static final String VALID_PRESCRIPTION_MEDICINE_DESC = + " " + PREFIX_NAME + VALID_PRESCRIPTION_MEDICINE; + static { DESC_AMY = new EditPatientDescriptorBuilder().withName(VALID_NAME_AMY) .withPhone(VALID_PHONE_AMY).withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY) diff --git a/src/test/java/seedu/docit/logic/commands/prescriptions/AddPrescriptionCommandTest.java b/src/test/java/seedu/docit/logic/commands/prescriptions/AddPrescriptionCommandTest.java new file mode 100644 index 00000000000..e7ddd5ec31b --- /dev/null +++ b/src/test/java/seedu/docit/logic/commands/prescriptions/AddPrescriptionCommandTest.java @@ -0,0 +1,163 @@ +package seedu.docit.logic.commands.prescriptions; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static seedu.docit.commons.core.Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX; +import static seedu.docit.logic.commands.prescription.AddPrescriptionCommand.INPUT_TOO_LONG_ERROR_MESSAGE; +import static seedu.docit.logic.commands.prescription.AddPrescriptionCommand.MESSAGE_SUCCESS; +import static seedu.docit.testutil.Assert.assertThrows; +import static seedu.docit.testutil.TypicalAppointments.getTypicalAppointmentList; +import static seedu.docit.testutil.TypicalPatients.getTypicalAddressBook; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +import seedu.docit.commons.core.index.Index; +import seedu.docit.logic.commands.CommandResult; +import seedu.docit.logic.commands.exceptions.CommandException; +import seedu.docit.logic.commands.prescription.AddPrescriptionCommand; +import seedu.docit.model.ArchivedAppointmentBook; +import seedu.docit.model.Model; +import seedu.docit.model.ModelManager; +import seedu.docit.model.UserPrefs; +import seedu.docit.model.appointment.Appointment; +import seedu.docit.model.prescription.Prescription; + +public class AddPrescriptionCommandTest { + private static final String defaultMedicine = "Penicillin"; + private static final String defaultVolume = "400 ml"; + private static final String defaultDuration = "3 times a day"; + private static final Prescription validPrescription = + new Prescription(defaultMedicine, defaultVolume, defaultDuration); + + private Model model = new ModelManager(getTypicalAddressBook(), getTypicalAppointmentList(), + new ArchivedAppointmentBook(), new UserPrefs()); + private final Appointment defaultAppointment = model.getAppointmentBook().getAppointmentList().get(0); + + private String generateString(char character, int length) { + char[] chars = new char[length]; + Arrays.fill(chars, character); + String result = new String(chars); + return result; + } + + @Test + public void constructor_nullPrescription_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> + new AddPrescriptionCommand(null, null, null, null)); + } + + @Test + public void execute_validPrescription_addSuccessfully() throws CommandException { + AddPrescriptionCommand addPrescriptionCommand = + new AddPrescriptionCommand(Index.fromOneBased(2), defaultMedicine, defaultVolume, defaultDuration); + + CommandResult actualCommandResult = addPrescriptionCommand.execute(model); + CommandResult expectedCommandResult = new CommandResult(String.format(MESSAGE_SUCCESS, + defaultMedicine.toLowerCase(), defaultVolume.toLowerCase(), defaultDuration.toLowerCase())); + + assertEquals(actualCommandResult, expectedCommandResult); + + } + + @Test + public void execute_duplicatePrescription_throwsCommandException() { + AddPrescriptionCommand addPrescriptionCommand = + new AddPrescriptionCommand(Index.fromOneBased(1), defaultMedicine, defaultVolume, defaultDuration); + + defaultAppointment.addPrescription(validPrescription); + + assertThrows(CommandException.class, AddPrescriptionCommand.MESSAGE_DUPLICATE_MEDICINE, () -> + addPrescriptionCommand.execute(model)); + + } + + @Test + public void execute_appointmentToAddDoesNotExist_throwsCommandException() { + int maxSize = model.getAppointmentBook().getAppointmentList().size(); + AddPrescriptionCommand invalidAddPrescriptionCommand = + new AddPrescriptionCommand(Index.fromOneBased(maxSize + 1), + defaultMedicine, defaultVolume, defaultDuration); + + assertThrows(CommandException.class, MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX, () -> + invalidAddPrescriptionCommand.execute(model)); + + } + + @Test + public void execute_appointmentToAddMedicineTooLong_throwsCommandException() { + String longMedicineName = generateString('A', Prescription.MEDICINE_CHAR_LENGTH_LIMIT + 1); + AddPrescriptionCommand invalidAddPrescriptionCommand = + new AddPrescriptionCommand(Index.fromOneBased(1), longMedicineName, defaultVolume, defaultDuration); + + assertThrows(CommandException.class, INPUT_TOO_LONG_ERROR_MESSAGE, () -> + invalidAddPrescriptionCommand.execute(model)); + + } + + @Test + public void execute_appointmentToAddVolumeTooLong_throwsCommandException() { + String longVolumeInput = generateString('A', Prescription.VOLUME_CHAR_LENGTH_LIMIT + 1); + AddPrescriptionCommand invalidAddPrescriptionCommand = + new AddPrescriptionCommand(Index.fromOneBased(3), defaultMedicine, longVolumeInput, defaultDuration); + + assertThrows(CommandException.class, INPUT_TOO_LONG_ERROR_MESSAGE, () -> + invalidAddPrescriptionCommand.execute(model)); + + } + + @Test + public void execute_appointmentToAddDurationTooLong_throwsCommandException() { + String longDurationInput = generateString('A', Prescription.DURATION_CHAR_LENGTH_LIMIT + 1); + AddPrescriptionCommand invalidAddPrescriptionCommand = + new AddPrescriptionCommand(Index.fromOneBased(1), defaultMedicine, defaultVolume, longDurationInput); + + assertThrows(CommandException.class, INPUT_TOO_LONG_ERROR_MESSAGE, () -> + invalidAddPrescriptionCommand.execute(model)); + + } + + @Test + public void execute_appointmentToAllFieldsMaxLength_addSuccessfully() throws CommandException { + String longMedicineName = generateString('A', Prescription.MEDICINE_CHAR_LENGTH_LIMIT); + String longVolumeInput = generateString('A', Prescription.VOLUME_CHAR_LENGTH_LIMIT); + String longDurationInput = generateString('A', Prescription.DURATION_CHAR_LENGTH_LIMIT); + AddPrescriptionCommand addPrescriptionCommand = + new AddPrescriptionCommand(Index.fromOneBased(1), longMedicineName, longVolumeInput, longDurationInput); + + CommandResult actualCommandResult = addPrescriptionCommand.execute(model); + CommandResult expectedCommandResult = new CommandResult(String.format(MESSAGE_SUCCESS, + longMedicineName.toLowerCase(), longVolumeInput.toLowerCase(), longDurationInput.toLowerCase())); + + assertEquals(actualCommandResult, expectedCommandResult); + + } + + @Test + public void equals_sameContent_success() { + AddPrescriptionCommand addPrescriptionCommand = new AddPrescriptionCommand(Index.fromOneBased(1), + defaultMedicine, defaultVolume, defaultDuration); + AddPrescriptionCommand addPrescriptionCommandCopy = new AddPrescriptionCommand(Index.fromOneBased(1), + defaultMedicine, defaultVolume, defaultDuration); + assert(addPrescriptionCommand.equals(addPrescriptionCommand)); + assert(addPrescriptionCommand.equals(addPrescriptionCommandCopy)); + } + + @Test + public void equals_differentContent_failure() { + AddPrescriptionCommand addPrescriptionCommand = new AddPrescriptionCommand(Index.fromOneBased(1), + defaultMedicine, defaultVolume, defaultDuration); + AddPrescriptionCommand addPrescriptionCommandNotSame = new AddPrescriptionCommand( + Index.fromOneBased(1), + "medicine", "20 ml", "twice a day"); + assertFalse(addPrescriptionCommand.equals(addPrescriptionCommandNotSame)); + } + + @Test + public void equals_null_failure() { + AddPrescriptionCommand addPrescriptionCommand = new AddPrescriptionCommand(Index.fromOneBased(1), + defaultMedicine, defaultVolume, defaultDuration); + assertFalse(addPrescriptionCommand.equals(null)); + } +} diff --git a/src/test/java/seedu/docit/logic/commands/prescriptions/DeletePrescriptionCommandTest.java b/src/test/java/seedu/docit/logic/commands/prescriptions/DeletePrescriptionCommandTest.java new file mode 100644 index 00000000000..edbf9364af6 --- /dev/null +++ b/src/test/java/seedu/docit/logic/commands/prescriptions/DeletePrescriptionCommandTest.java @@ -0,0 +1,105 @@ +package seedu.docit.logic.commands.prescriptions; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static seedu.docit.commons.core.Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX; +import static seedu.docit.logic.commands.prescription.DeletePrescriptionCommand.MESSAGE_DELETE_PRESCRIPTION_SUCCESS; +import static seedu.docit.testutil.Assert.assertThrows; +import static seedu.docit.testutil.TypicalAppointments.getTypicalAppointmentList; +import static seedu.docit.testutil.TypicalPatients.getTypicalAddressBook; + +import org.junit.jupiter.api.Test; + +import seedu.docit.commons.core.index.Index; +import seedu.docit.logic.commands.CommandResult; +import seedu.docit.logic.commands.exceptions.CommandException; +import seedu.docit.logic.commands.prescription.DeletePrescriptionCommand; +import seedu.docit.model.ArchivedAppointmentBook; +import seedu.docit.model.Model; +import seedu.docit.model.ModelManager; +import seedu.docit.model.UserPrefs; +import seedu.docit.model.appointment.Appointment; +import seedu.docit.model.prescription.Prescription; + + + +public class DeletePrescriptionCommandTest { + private static final String defaultMedicine = "Panadol"; + private static final String defaultVolume = "400 ml"; + private static final String defaultDuration = "3 times a day"; + private static final Prescription validPrescription = + new Prescription(defaultMedicine, defaultVolume, defaultDuration); + + private Model model = new ModelManager(getTypicalAddressBook(), getTypicalAppointmentList(), + new ArchivedAppointmentBook(), new UserPrefs()); + private final Appointment defaultAppointment = model.getAppointmentBook().getAppointmentList().get(0); + + @Test + public void constructor_nullPrescription_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> + new DeletePrescriptionCommand(null, null)); + } + + @Test + public void execute_validPrescription_deleteSuccessfully() throws CommandException { + defaultAppointment.addPrescription(validPrescription); + DeletePrescriptionCommand deletePrescriptionCommand = + new DeletePrescriptionCommand(Index.fromOneBased(1), defaultMedicine); + + CommandResult actualCommandResult = deletePrescriptionCommand.execute(model); + CommandResult expectedCommandResult = new CommandResult(String.format(MESSAGE_DELETE_PRESCRIPTION_SUCCESS, + defaultMedicine.toLowerCase(), defaultAppointment.getPatient().getName())); + + assertEquals(actualCommandResult, expectedCommandResult); + + } + + @Test + public void execute_missingAppointment_throwsCommandException() throws CommandException { + int maxSize = model.getAppointmentBook().getAppointmentList().size(); + DeletePrescriptionCommand deletePrescriptionCommand = + new DeletePrescriptionCommand(Index.fromOneBased(maxSize + 1), defaultMedicine); + + assertThrows(CommandException.class, MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX, () -> + deletePrescriptionCommand.execute(model)); + + } + + @Test + public void execute_missingPrescription_throwsCommandException() throws CommandException { + DeletePrescriptionCommand deletePrescriptionCommand = + new DeletePrescriptionCommand(Index.fromOneBased(1), defaultMedicine); + + assertThrows(CommandException.class, "Medicine name not found in prescription list", () -> + deletePrescriptionCommand.execute(model)); + + } + + @Test + public void equals_sameContent_success() { + DeletePrescriptionCommand deletePrescriptionCommand = new DeletePrescriptionCommand(Index.fromOneBased(1), + defaultMedicine); + DeletePrescriptionCommand deletePrescriptionCommandCopy = new DeletePrescriptionCommand(Index.fromOneBased(1), + defaultMedicine); + assert(deletePrescriptionCommand.equals(deletePrescriptionCommand)); + assert(deletePrescriptionCommand.equals(deletePrescriptionCommandCopy)); + } + + @Test + public void equals_differentContent_failure() { + DeletePrescriptionCommand deletePrescriptionCommand = new DeletePrescriptionCommand(Index.fromOneBased(1), + defaultMedicine); + DeletePrescriptionCommand deletePrescriptionCommandNotSame = new DeletePrescriptionCommand( + Index.fromOneBased(1), + "notMedicine"); + assertFalse(deletePrescriptionCommand.equals(deletePrescriptionCommandNotSame)); + } + + @Test + public void equals_null_failure() { + DeletePrescriptionCommand deletePrescriptionCommand = new DeletePrescriptionCommand(Index.fromOneBased(1), + defaultMedicine); + assertFalse(deletePrescriptionCommand.equals(null)); + } + +} diff --git a/src/test/java/seedu/docit/logic/parser/prescription/AddPrescriptionCommandParserTest.java b/src/test/java/seedu/docit/logic/parser/prescription/AddPrescriptionCommandParserTest.java new file mode 100644 index 00000000000..33dd5723a8c --- /dev/null +++ b/src/test/java/seedu/docit/logic/parser/prescription/AddPrescriptionCommandParserTest.java @@ -0,0 +1,72 @@ +package seedu.docit.logic.parser.prescription; + +import static seedu.docit.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.docit.logic.commands.CommandTestUtil.VALID_APPOINTMENT_INDEX; +import static seedu.docit.logic.commands.CommandTestUtil.VALID_PRESCRIPTION_DURATION; +import static seedu.docit.logic.commands.CommandTestUtil.VALID_PRESCRIPTION_MEDICINE; +import static seedu.docit.logic.commands.CommandTestUtil.VALID_PRESCRIPTION_VOLUME; +import static seedu.docit.logic.parser.AppointmentCommandParserTestUtil.assertParseFailure; +import static seedu.docit.logic.parser.AppointmentCommandParserTestUtil.assertParseSuccess; +import static seedu.docit.logic.parser.CliSyntax.PREFIX_DURATION; +import static seedu.docit.logic.parser.CliSyntax.PREFIX_NAME; +import static seedu.docit.logic.parser.CliSyntax.PREFIX_VOLUME; +import static seedu.docit.logic.parser.prescription.AddPrescriptionCommandParser.EMPTY_FIELD_ERROR_MESSAGE; + +import org.junit.jupiter.api.Test; + +import seedu.docit.commons.core.index.Index; +import seedu.docit.logic.commands.prescription.AddPrescriptionCommand; +import seedu.docit.logic.parser.exceptions.ParseException; + + +public class AddPrescriptionCommandParserTest { + private final AddPrescriptionCommandParser parser = new AddPrescriptionCommandParser(); + + @Test + public void parseAppointmentCommand_allFieldsPresent_success() throws ParseException { + assertParseSuccess(parser, VALID_APPOINTMENT_INDEX + " " + + PREFIX_NAME + VALID_PRESCRIPTION_MEDICINE + " " + + PREFIX_VOLUME + VALID_PRESCRIPTION_VOLUME + " " + + PREFIX_DURATION + VALID_PRESCRIPTION_DURATION, + new AddPrescriptionCommand(Index.fromOneBased(Integer.parseInt(VALID_APPOINTMENT_INDEX)), + VALID_PRESCRIPTION_MEDICINE, + VALID_PRESCRIPTION_VOLUME, + VALID_PRESCRIPTION_DURATION)); + } + + @Test + public void parseAppointmentCommand_allFieldsBlank_failure() throws ParseException { + assertParseFailure(parser, " " + + PREFIX_NAME + " " + + PREFIX_VOLUME + " " + + PREFIX_DURATION, + String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddPrescriptionCommand.MESSAGE_USAGE)); + } + + @Test + public void parseAppointmentCommand_noIndex_failure() throws ParseException { + assertParseFailure(parser, PREFIX_NAME + VALID_PRESCRIPTION_MEDICINE + " " + + PREFIX_VOLUME + VALID_PRESCRIPTION_VOLUME + " " + + PREFIX_DURATION + VALID_PRESCRIPTION_DURATION, + String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddPrescriptionCommand.MESSAGE_USAGE)); + } + + @Test + public void parseAppointmentCommand_noPrefix_failure() throws ParseException { + assertParseFailure(parser, VALID_APPOINTMENT_INDEX + " " + + VALID_PRESCRIPTION_MEDICINE + " " + + VALID_PRESCRIPTION_VOLUME + " " + + VALID_PRESCRIPTION_DURATION, + String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddPrescriptionCommand.MESSAGE_USAGE)); + } + + @Test + public void parseAppointmentCommand_blankEntry_failure() throws ParseException { + assertParseFailure(parser, VALID_APPOINTMENT_INDEX + " " + + PREFIX_NAME + " " + + PREFIX_VOLUME + " " + + PREFIX_DURATION, + EMPTY_FIELD_ERROR_MESSAGE); + } + +} diff --git a/src/test/java/seedu/docit/logic/parser/prescription/DeletePrescriptionCommandParserTest.java b/src/test/java/seedu/docit/logic/parser/prescription/DeletePrescriptionCommandParserTest.java new file mode 100644 index 00000000000..be1c1cd93e1 --- /dev/null +++ b/src/test/java/seedu/docit/logic/parser/prescription/DeletePrescriptionCommandParserTest.java @@ -0,0 +1,75 @@ +package seedu.docit.logic.parser.prescription; + +import static seedu.docit.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.docit.logic.commands.CommandTestUtil.VALID_APPOINTMENT_INDEX; +import static seedu.docit.logic.commands.CommandTestUtil.VALID_PRESCRIPTION_MEDICINE; +import static seedu.docit.logic.parser.AppointmentCommandParserTestUtil.assertParseFailure; +import static seedu.docit.logic.parser.AppointmentCommandParserTestUtil.assertParseSuccess; +import static seedu.docit.logic.parser.CliSyntax.PREFIX_NAME; +import static seedu.docit.logic.parser.prescription.DeletePrescriptionCommandParser.EMPTY_MEDICINE_FIELD_ERROR_MESSAGE; +import static seedu.docit.testutil.TypicalAppointments.getTypicalAppointmentList; +import static seedu.docit.testutil.TypicalPatients.getTypicalAddressBook; + +import org.junit.jupiter.api.Test; + +import seedu.docit.commons.core.index.Index; +import seedu.docit.logic.commands.prescription.DeletePrescriptionCommand; +import seedu.docit.logic.parser.exceptions.ParseException; +import seedu.docit.model.ArchivedAppointmentBook; +import seedu.docit.model.Model; +import seedu.docit.model.ModelManager; +import seedu.docit.model.UserPrefs; +import seedu.docit.model.appointment.Appointment; +import seedu.docit.model.prescription.Prescription; + + + +public class DeletePrescriptionCommandParserTest { + private static final String defaultMedicine = "Panadol"; + private static final String defaultVolume = "400 ml"; + private static final String defaultDuration = "3 times a day"; + private static final Prescription validPrescription = + new Prescription(defaultMedicine, defaultVolume, defaultDuration); + + private Model model = new ModelManager(getTypicalAddressBook(), getTypicalAppointmentList(), + new ArchivedAppointmentBook(), new UserPrefs()); + private final Appointment defaultAppointment = model.getAppointmentBook().getAppointmentList().get(0); + + private final DeletePrescriptionCommandParser parser = new DeletePrescriptionCommandParser(); + + @Test + public void parseAppointmentCommand_allFieldsPresent_success() throws ParseException { + assertParseSuccess(parser, VALID_APPOINTMENT_INDEX + " " + + PREFIX_NAME + VALID_PRESCRIPTION_MEDICINE, + new DeletePrescriptionCommand(Index.fromOneBased(Integer.parseInt(VALID_APPOINTMENT_INDEX)), + VALID_PRESCRIPTION_MEDICINE)); + } + + @Test + public void parseAppointmentCommand_allFieldsBlank_failure() throws ParseException { + assertParseFailure(parser, " " + + PREFIX_NAME, + String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeletePrescriptionCommand.MESSAGE_USAGE)); + } + + @Test + public void parseAppointmentCommand_noIndex_failure() throws ParseException { + assertParseFailure(parser, " " + + PREFIX_NAME + VALID_PRESCRIPTION_MEDICINE, + String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeletePrescriptionCommand.MESSAGE_USAGE)); + } + + @Test + public void parseAppointmentCommand_badIndex_failure() throws ParseException { + assertParseFailure(parser, "notIndex " + + PREFIX_NAME + VALID_PRESCRIPTION_MEDICINE, + "Index is not a non-zero unsigned integer."); + } + + @Test + public void parseAppointmentCommand_blankEntry_failure() throws ParseException { + assertParseFailure(parser, VALID_APPOINTMENT_INDEX + " " + + PREFIX_NAME, + EMPTY_MEDICINE_FIELD_ERROR_MESSAGE); + } +} diff --git a/src/test/java/seedu/docit/model/PrescriptionTest.java b/src/test/java/seedu/docit/model/PrescriptionTest.java new file mode 100644 index 00000000000..e519fbfbde6 --- /dev/null +++ b/src/test/java/seedu/docit/model/PrescriptionTest.java @@ -0,0 +1,71 @@ +package seedu.docit.model; + +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.docit.testutil.Assert.assertThrows; + +import org.junit.jupiter.api.Test; + +import seedu.docit.model.prescription.Prescription; + +public class PrescriptionTest { + private static final String defaultMedicine = "Penicillin"; + private static final String defaultVolume = "400 ml"; + private static final String defaultDuration = "3 times a day"; + + @Test + public void constructor_nullInputs_throwNullPointerException() { + assertThrows(NullPointerException.class, () -> new Prescription(null, null, null)); + } + + @Test + public void constructor_blankInputs_throwEmptyInputException() { + assertThrows(RuntimeException.class, + "Medicine cannot be blank. Volume cannot be blank. Duration cannot be blank.", () -> + new Prescription("", "", "")); + } + + @Test + public void equals_sameObject_success() { + Prescription prescription = new Prescription(defaultMedicine, defaultVolume, defaultDuration); + Prescription prescriptionCopy = new Prescription(defaultMedicine, defaultVolume, defaultDuration); + assertTrue(prescription.equals(prescription)); + assertTrue(prescription.equals(prescriptionCopy)); + } + + @Test + public void equals_null_failure() { + Prescription prescription = new Prescription(defaultMedicine, defaultVolume, defaultDuration); + assertFalse(prescription.equals(null)); + } + + @Test + public void hasSameMedicalName_sameMedicalName_success() { + Prescription prescription = new Prescription(defaultMedicine, defaultVolume, defaultDuration); + Prescription prescriptionTwo = new Prescription(defaultMedicine, "500 ml", "6 days"); + assertTrue(prescription.hasSameMedicalName(prescriptionTwo)); + } + + @Test + public void hasSameMedicalName_differentMedicalName_success() { + Prescription prescription = new Prescription(defaultMedicine, defaultVolume, defaultDuration); + Prescription prescriptionTwo = new Prescription("poison", defaultVolume, defaultDuration); + assertFalse(prescription.hasSameMedicalName(prescriptionTwo)); + } + + @Test + public void toUiFormat_standardInput_correctFormat() { + Prescription prescription = new Prescription(defaultMedicine, defaultVolume, defaultDuration); + assertEquals(prescription.toUiFormat(), defaultMedicine.toLowerCase() + " | " + + defaultVolume + " | " + defaultDuration); + } + + @Test + public void toString_standardInput_correctFormat() { + Prescription prescription = new Prescription(defaultMedicine, defaultVolume, defaultDuration); + assertEquals(prescription.toString(), "Medicine: " + defaultMedicine.toLowerCase() + + ", Volume: " + defaultVolume + + ", Duration: " + defaultDuration); + } +} diff --git a/src/test/java/seedu/docit/testutil/stubs/appointment/AppointmentStub.java b/src/test/java/seedu/docit/testutil/stubs/appointment/AppointmentStub.java new file mode 100644 index 00000000000..c7a16518dab --- /dev/null +++ b/src/test/java/seedu/docit/testutil/stubs/appointment/AppointmentStub.java @@ -0,0 +1,13 @@ +package seedu.docit.testutil.stubs.appointment; + + +import seedu.docit.model.patient.Patient; + + +public class AppointmentStub { + public Patient getPatient() { + throw new AssertionError("This method should not be called."); + } + + +}