Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-T08-1#234 from jovantanyk/fix-add…
Browse files Browse the repository at this point in the history
…visitcommand

Fix AddVisitCommand and Storage bug. Init Visit TestUtils and Test Cases
  • Loading branch information
NatLeong authored Apr 13, 2024
2 parents 65cd725 + 167c5c6 commit 5dce7fa
Show file tree
Hide file tree
Showing 19 changed files with 1,076 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public CommandResult execute(String commandText) throws CommandException, ParseE

CommandResult commandResult;
model.updateFilteredPersonList(Model.PREDICATE_SHOW_ALL_PERSONS);
model.updateFilteredVisitList(Model.PREDICATE_SHOW_ALL_VISITS);
Command command = immuniMateParser.parseCommand(commandText);
commandResult = command.execute(model);

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/seedu/address/logic/commands/AddVisitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;
import seedu.address.model.visit.Visit;

/**
Expand All @@ -34,7 +36,8 @@ public class AddVisitCommand extends Command {
+ PREFIX_STATUS + "PENDING";

public static final String MESSAGE_SUCCESS = "New Visit added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This visit already exists in the system";
public static final String MESSAGE_DUPLICATE_VISIT = "This visit already exists in the system";
public static final String MESSAGE_INVALID_VISIT = "The NRIC supplied does not link to any existing Patient";

private final Visit toAdd;

Expand All @@ -50,9 +53,14 @@ public AddVisitCommand(Visit visit) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

Nric patientNric = toAdd.getNric();
Person patient = Person.createPersonWithNric(patientNric);
// Guard clauses to ensure NRIC is valid and Visit is not duplicate
if (!model.hasPerson(patient)) {
throw new CommandException(MESSAGE_INVALID_VISIT);
}
if (model.hasVisit(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
throw new CommandException(MESSAGE_DUPLICATE_VISIT);
}
//TODO: Update patient symptom and diagnosis to reflect latest visit!
model.addVisit(toAdd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ public AddVisitCommand parse(String args) throws ParseException {
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NRIC, PREFIX_DATEOFVISIT,
PREFIX_SYMPTOM, PREFIX_DIAGNOSIS, PREFIX_STATUS);
Nric nric = ParserUtil.parseNric(argMultimap.getValue(PREFIX_NRIC).get());


DateOfVisit dov = ParserUtil.parseDateOfVisit(argMultimap.getValue(PREFIX_DATEOFVISIT).get());
Symptom symptom = ParserUtil.parseSymptom(argMultimap.getValue(PREFIX_SYMPTOM).get());
Diagnosis diagnosis = ParserUtil.parseDiagnosis(argMultimap.getValue(PREFIX_DIAGNOSIS).get());
Status status = ParserUtil.parseStatus(argMultimap.getValue(PREFIX_STATUS).get());
//TODO (later): assersion to make sure optional values don't generate errors

Visit visit = new Visit(nric, dov, symptom, diagnosis, status);

return new AddVisitCommand(visit);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public static Person[] getSamplePersons() {

public static Visit[] getSampleVisits() {
return new Visit[] {
new Visit(new Nric("T0245123C"), new DateOfVisit("2023-01-02"),
new Visit(new Nric("T0234567C"), new DateOfVisit("2023-01-02"),
new Symptom("Dying"), new Diagnosis("Cancer"), new Status("UNWELL")),
new Visit(new Nric("T0245123C"), new DateOfVisit("2023-02-25"),
new Visit(new Nric("T0234567C"), new DateOfVisit("2023-02-25"),
new Symptom("Throat Pain"), new Diagnosis("Cancer"), new Status("HEALTHY")),
new Visit(new Nric("S9234568N"), new DateOfVisit("2023-01-02"),
new Symptom("Headache"), new Diagnosis("COVID"), new Status("UNWELL")),
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/seedu/address/model/visit/Visit.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,6 @@ public Status getStatus() {
return this.status;
}

/**
* Returns true if both persons have the same nric.
* This defines a weaker notion of equality between two persons.
*/
public boolean isSamePerson(Visit otherPerson) {
if (otherPerson == this) {
return true;
}

return otherPerson != null && otherPerson.getNric().equals(getNric());
}

/**
* Returns true if the person has all mandatory fields.
*/
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/seedu/address/ui/CommandBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ private void navigateCommandHistory(int direction) {
}

// Guard Clause for initial key input. Shows first command without skipping it.
if (historyIndex == 0 && isFirstPress) {
if (historyIndex == 0 && isFirstPress && direction == 1) {
commandTextField.setText(commandHistory.get(historyIndex));
System.out.println("HistoryIndex: " + historyIndex + " NextCommand: " + commandHistory.get(historyIndex));
isFirstPress = false;
return;
}
Expand All @@ -80,7 +79,6 @@ private void navigateCommandHistory(int direction) {
} else if (historyIndex >= commandHistory.size()) {
historyIndex = commandHistory.size();
}
System.out.println("HistoryIndex: " + historyIndex + " NextCommand: " + commandHistory.get(historyIndex));
// Set the commandTextField's text to the command at the new history index
commandTextField.setText(commandHistory.get(historyIndex));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package seedu.address.logic.commands;

import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Person;
import seedu.address.testutil.PersonBuilder;

/**
* Contains integration tests (interaction with the Model) for {@code AddCommand}.
*/
public class AddVisitCommandIntegrationTest {

private Model model;

@BeforeEach
public void setUp() {
model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
}

@Test
public void execute_newVisit_success() {
Person validPerson = new PersonBuilder().build();

Model expectedModel = new ModelManager(model.getImmuniMate(), new UserPrefs());
expectedModel.addPerson(validPerson);

assertCommandSuccess(new CreateCommand(validPerson), model,
String.format(CreateCommand.MESSAGE_SUCCESS, Messages.format(validPerson)),
expectedModel);
}

@Test
public void execute_duplicatePerson_throwsCommandException() {
Person personInList = model.getImmuniMate().getPersonList().get(0);
assertCommandFailure(new CreateCommand(personInList), model,
CreateCommand.MESSAGE_DUPLICATE_PERSON);
}

}
238 changes: 238 additions & 0 deletions src/test/java/seedu/address/logic/commands/AddVisitCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
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.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalPersons.ALICE;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Predicate;

import org.junit.jupiter.api.Test;

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.ImmuniMate;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyImmuniMate;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.person.Person;
import seedu.address.model.visit.Visit;
import seedu.address.testutil.PersonBuilder;


// TODO Adjust Test Cases
public class AddVisitCommandTest {

@Test
public void constructor_nullPerson_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new CreateCommand(null));
}

@Test
public void execute_personAcceptedByModel_addSuccessful() throws Exception {
ModelStubAcceptingPersonAdded modelStub = new ModelStubAcceptingPersonAdded();
Person validPerson = new PersonBuilder().build();

CommandResult commandResult = new CreateCommand(validPerson).execute(modelStub);

assertEquals(String.format(CreateCommand.MESSAGE_SUCCESS, Messages.format(validPerson)),
commandResult.getFeedbackToUser());
assertEquals(Arrays.asList(validPerson), modelStub.personsAdded);
}

@Test
public void execute_duplicatePerson_throwsCommandException() {
Person validPerson = new PersonBuilder().build();
CreateCommand createCommand = new CreateCommand(validPerson);
ModelStub modelStub = new ModelStubWithPerson(validPerson);

assertThrows(CommandException.class,
CreateCommand.MESSAGE_DUPLICATE_PERSON, () -> createCommand.execute(modelStub));
}

@Test
public void equals() {
Person alice = new PersonBuilder().withName("Alice").build();
Person bob = new PersonBuilder().withName("Bob").build();
CreateCommand addAliceCommand = new CreateCommand(alice);
CreateCommand addBobCommand = new CreateCommand(bob);

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

// same values -> returns true
CreateCommand addAliceCommandCopy = new CreateCommand(alice);
assertTrue(addAliceCommand.equals(addAliceCommandCopy));

// different types -> returns false
assertFalse(addAliceCommand.equals(1));

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

// different person -> returns false
assertFalse(addAliceCommand.equals(addBobCommand));
}

@Test
public void toStringMethod() {
CreateCommand createCommand = new CreateCommand(ALICE);
String expected = CreateCommand.class.getCanonicalName() + "{toAdd=" + ALICE + "}";
assertEquals(expected, createCommand.toString());
}

/**
* A default model stub that have all the methods failing.
*/
private class ModelStub implements Model {
@Override
public void setUserPrefs(ReadOnlyUserPrefs userPrefs) {
throw new AssertionError("This method should not be called.");
}

@Override
public ReadOnlyUserPrefs getUserPrefs() {
throw new AssertionError("This method should not be called.");
}

@Override
public GuiSettings getGuiSettings() {
throw new AssertionError("This method should not be called.");
}

@Override
public void setGuiSettings(GuiSettings guiSettings) {
throw new AssertionError("This method should not be called.");
}

@Override
public Path getImmunimateFilePath() {
throw new AssertionError("This method should not be called.");
}

@Override
public void setImmunimateFilePath(Path immuniMateFilePath) {
throw new AssertionError("This method should not be called.");
}

@Override
public void addPerson(Person person) {
throw new AssertionError("This method should not be called.");
}

@Override
public void addVisit(Visit visit) {

}

@Override
public void setImmuniMate(ReadOnlyImmuniMate immuniMate) {
throw new AssertionError("This method should not be called.");
}

@Override
public ReadOnlyImmuniMate getImmuniMate() {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean hasPerson(Person person) {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean hasVisit(Visit visit) {
throw new AssertionError("This method should not be called.");
}

@Override
public void deletePerson(Person target) {
throw new AssertionError("This method should not be called.");
}

@Override
public void deleteVisit(Visit target) {
throw new AssertionError("This method should not be called.");
}

@Override
public void setPerson(Person target, Person editedPerson) {
throw new AssertionError("This method should not be called.");
}

@Override
public void setVisit(Visit target, Visit editedVisit) {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Person> getFilteredPersonList() {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Visit> getFilteredVisitList() {
throw new AssertionError("This method should not be called.");
}

@Override
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
}

@Override
public void updateFilteredVisitList(Predicate<Visit> predicate) {
throw new AssertionError("This method should not be called.");
}
}

/**
* A Model stub that contains a single person.
*/
private class ModelStubWithPerson extends ModelStub {
private final Person person;

ModelStubWithPerson(Person person) {
requireNonNull(person);
this.person = person;
}

@Override
public boolean hasPerson(Person person) {
requireNonNull(person);
return this.person.isSamePerson(person);
}
}

/**
* A Model stub that always accept the person being added.
*/
private class ModelStubAcceptingPersonAdded extends ModelStub {
final ArrayList<Person> personsAdded = new ArrayList<>();

@Override
public boolean hasPerson(Person person) {
requireNonNull(person);
return personsAdded.stream().anyMatch(person::isSamePerson);
}

@Override
public void addPerson(Person person) {
requireNonNull(person);
personsAdded.add(person);
}

@Override
public ReadOnlyImmuniMate getImmuniMate() {
return new ImmuniMate();
}
}

}
Loading

0 comments on commit 5dce7fa

Please sign in to comment.