Skip to content

Commit 5dce7fa

Browse files
authored
Merge pull request AY2324S2-CS2103T-T08-1#234 from jovantanyk/fix-addvisitcommand
Fix AddVisitCommand and Storage bug. Init Visit TestUtils and Test Cases
2 parents 65cd725 + 167c5c6 commit 5dce7fa

19 files changed

+1076
-64
lines changed

src/main/java/seedu/address/logic/LogicManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
4848

4949
CommandResult commandResult;
5050
model.updateFilteredPersonList(Model.PREDICATE_SHOW_ALL_PERSONS);
51+
model.updateFilteredVisitList(Model.PREDICATE_SHOW_ALL_VISITS);
5152
Command command = immuniMateParser.parseCommand(commandText);
5253
commandResult = command.execute(model);
5354

src/main/java/seedu/address/logic/commands/AddVisitCommand.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import seedu.address.logic.Messages;
1212
import seedu.address.logic.commands.exceptions.CommandException;
1313
import seedu.address.model.Model;
14+
import seedu.address.model.person.Nric;
15+
import seedu.address.model.person.Person;
1416
import seedu.address.model.visit.Visit;
1517

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

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

3942
private final Visit toAdd;
4043

@@ -50,9 +53,14 @@ public AddVisitCommand(Visit visit) {
5053
@Override
5154
public CommandResult execute(Model model) throws CommandException {
5255
requireNonNull(model);
53-
56+
Nric patientNric = toAdd.getNric();
57+
Person patient = Person.createPersonWithNric(patientNric);
58+
// Guard clauses to ensure NRIC is valid and Visit is not duplicate
59+
if (!model.hasPerson(patient)) {
60+
throw new CommandException(MESSAGE_INVALID_VISIT);
61+
}
5462
if (model.hasVisit(toAdd)) {
55-
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
63+
throw new CommandException(MESSAGE_DUPLICATE_VISIT);
5664
}
5765
//TODO: Update patient symptom and diagnosis to reflect latest visit!
5866
model.addVisit(toAdd);

src/main/java/seedu/address/logic/parser/AddVisitCommandParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ public AddVisitCommand parse(String args) throws ParseException {
4242
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NRIC, PREFIX_DATEOFVISIT,
4343
PREFIX_SYMPTOM, PREFIX_DIAGNOSIS, PREFIX_STATUS);
4444
Nric nric = ParserUtil.parseNric(argMultimap.getValue(PREFIX_NRIC).get());
45+
46+
4547
DateOfVisit dov = ParserUtil.parseDateOfVisit(argMultimap.getValue(PREFIX_DATEOFVISIT).get());
4648
Symptom symptom = ParserUtil.parseSymptom(argMultimap.getValue(PREFIX_SYMPTOM).get());
4749
Diagnosis diagnosis = ParserUtil.parseDiagnosis(argMultimap.getValue(PREFIX_DIAGNOSIS).get());
4850
Status status = ParserUtil.parseStatus(argMultimap.getValue(PREFIX_STATUS).get());
49-
//TODO (later): assersion to make sure optional values don't generate errors
51+
5052
Visit visit = new Visit(nric, dov, symptom, diagnosis, status);
5153

5254
return new AddVisitCommand(visit);

src/main/java/seedu/address/model/util/SampleDataUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public static Person[] getSamplePersons() {
4444

4545
public static Visit[] getSampleVisits() {
4646
return new Visit[] {
47-
new Visit(new Nric("T0245123C"), new DateOfVisit("2023-01-02"),
47+
new Visit(new Nric("T0234567C"), new DateOfVisit("2023-01-02"),
4848
new Symptom("Dying"), new Diagnosis("Cancer"), new Status("UNWELL")),
49-
new Visit(new Nric("T0245123C"), new DateOfVisit("2023-02-25"),
49+
new Visit(new Nric("T0234567C"), new DateOfVisit("2023-02-25"),
5050
new Symptom("Throat Pain"), new Diagnosis("Cancer"), new Status("HEALTHY")),
5151
new Visit(new Nric("S9234568N"), new DateOfVisit("2023-01-02"),
5252
new Symptom("Headache"), new Diagnosis("COVID"), new Status("UNWELL")),

src/main/java/seedu/address/model/visit/Visit.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@ public Status getStatus() {
5151
return this.status;
5252
}
5353

54-
/**
55-
* Returns true if both persons have the same nric.
56-
* This defines a weaker notion of equality between two persons.
57-
*/
58-
public boolean isSamePerson(Visit otherPerson) {
59-
if (otherPerson == this) {
60-
return true;
61-
}
62-
63-
return otherPerson != null && otherPerson.getNric().equals(getNric());
64-
}
65-
6654
/**
6755
* Returns true if the person has all mandatory fields.
6856
*/

src/main/java/seedu/address/ui/CommandBox.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ private void navigateCommandHistory(int direction) {
6262
}
6363

6464
// Guard Clause for initial key input. Shows first command without skipping it.
65-
if (historyIndex == 0 && isFirstPress) {
65+
if (historyIndex == 0 && isFirstPress && direction == 1) {
6666
commandTextField.setText(commandHistory.get(historyIndex));
67-
System.out.println("HistoryIndex: " + historyIndex + " NextCommand: " + commandHistory.get(historyIndex));
6867
isFirstPress = false;
6968
return;
7069
}
@@ -80,7 +79,6 @@ private void navigateCommandHistory(int direction) {
8079
} else if (historyIndex >= commandHistory.size()) {
8180
historyIndex = commandHistory.size();
8281
}
83-
System.out.println("HistoryIndex: " + historyIndex + " NextCommand: " + commandHistory.get(historyIndex));
8482
// Set the commandTextField's text to the command at the new history index
8583
commandTextField.setText(commandHistory.get(historyIndex));
8684
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package seedu.address.logic.commands;
2+
3+
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
4+
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
5+
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;
6+
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
import seedu.address.logic.Messages;
11+
import seedu.address.model.Model;
12+
import seedu.address.model.ModelManager;
13+
import seedu.address.model.UserPrefs;
14+
import seedu.address.model.person.Person;
15+
import seedu.address.testutil.PersonBuilder;
16+
17+
/**
18+
* Contains integration tests (interaction with the Model) for {@code AddCommand}.
19+
*/
20+
public class AddVisitCommandIntegrationTest {
21+
22+
private Model model;
23+
24+
@BeforeEach
25+
public void setUp() {
26+
model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
27+
}
28+
29+
@Test
30+
public void execute_newVisit_success() {
31+
Person validPerson = new PersonBuilder().build();
32+
33+
Model expectedModel = new ModelManager(model.getImmuniMate(), new UserPrefs());
34+
expectedModel.addPerson(validPerson);
35+
36+
assertCommandSuccess(new CreateCommand(validPerson), model,
37+
String.format(CreateCommand.MESSAGE_SUCCESS, Messages.format(validPerson)),
38+
expectedModel);
39+
}
40+
41+
@Test
42+
public void execute_duplicatePerson_throwsCommandException() {
43+
Person personInList = model.getImmuniMate().getPersonList().get(0);
44+
assertCommandFailure(new CreateCommand(personInList), model,
45+
CreateCommand.MESSAGE_DUPLICATE_PERSON);
46+
}
47+
48+
}
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
import static seedu.address.testutil.Assert.assertThrows;
8+
import static seedu.address.testutil.TypicalPersons.ALICE;
9+
10+
import java.nio.file.Path;
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.function.Predicate;
14+
15+
import org.junit.jupiter.api.Test;
16+
17+
import javafx.collections.ObservableList;
18+
import seedu.address.commons.core.GuiSettings;
19+
import seedu.address.logic.Messages;
20+
import seedu.address.logic.commands.exceptions.CommandException;
21+
import seedu.address.model.ImmuniMate;
22+
import seedu.address.model.Model;
23+
import seedu.address.model.ReadOnlyImmuniMate;
24+
import seedu.address.model.ReadOnlyUserPrefs;
25+
import seedu.address.model.person.Person;
26+
import seedu.address.model.visit.Visit;
27+
import seedu.address.testutil.PersonBuilder;
28+
29+
30+
// TODO Adjust Test Cases
31+
public class AddVisitCommandTest {
32+
33+
@Test
34+
public void constructor_nullPerson_throwsNullPointerException() {
35+
assertThrows(NullPointerException.class, () -> new CreateCommand(null));
36+
}
37+
38+
@Test
39+
public void execute_personAcceptedByModel_addSuccessful() throws Exception {
40+
ModelStubAcceptingPersonAdded modelStub = new ModelStubAcceptingPersonAdded();
41+
Person validPerson = new PersonBuilder().build();
42+
43+
CommandResult commandResult = new CreateCommand(validPerson).execute(modelStub);
44+
45+
assertEquals(String.format(CreateCommand.MESSAGE_SUCCESS, Messages.format(validPerson)),
46+
commandResult.getFeedbackToUser());
47+
assertEquals(Arrays.asList(validPerson), modelStub.personsAdded);
48+
}
49+
50+
@Test
51+
public void execute_duplicatePerson_throwsCommandException() {
52+
Person validPerson = new PersonBuilder().build();
53+
CreateCommand createCommand = new CreateCommand(validPerson);
54+
ModelStub modelStub = new ModelStubWithPerson(validPerson);
55+
56+
assertThrows(CommandException.class,
57+
CreateCommand.MESSAGE_DUPLICATE_PERSON, () -> createCommand.execute(modelStub));
58+
}
59+
60+
@Test
61+
public void equals() {
62+
Person alice = new PersonBuilder().withName("Alice").build();
63+
Person bob = new PersonBuilder().withName("Bob").build();
64+
CreateCommand addAliceCommand = new CreateCommand(alice);
65+
CreateCommand addBobCommand = new CreateCommand(bob);
66+
67+
// same object -> returns true
68+
assertTrue(addAliceCommand.equals(addAliceCommand));
69+
70+
// same values -> returns true
71+
CreateCommand addAliceCommandCopy = new CreateCommand(alice);
72+
assertTrue(addAliceCommand.equals(addAliceCommandCopy));
73+
74+
// different types -> returns false
75+
assertFalse(addAliceCommand.equals(1));
76+
77+
// null -> returns false
78+
assertFalse(addAliceCommand.equals(null));
79+
80+
// different person -> returns false
81+
assertFalse(addAliceCommand.equals(addBobCommand));
82+
}
83+
84+
@Test
85+
public void toStringMethod() {
86+
CreateCommand createCommand = new CreateCommand(ALICE);
87+
String expected = CreateCommand.class.getCanonicalName() + "{toAdd=" + ALICE + "}";
88+
assertEquals(expected, createCommand.toString());
89+
}
90+
91+
/**
92+
* A default model stub that have all the methods failing.
93+
*/
94+
private class ModelStub implements Model {
95+
@Override
96+
public void setUserPrefs(ReadOnlyUserPrefs userPrefs) {
97+
throw new AssertionError("This method should not be called.");
98+
}
99+
100+
@Override
101+
public ReadOnlyUserPrefs getUserPrefs() {
102+
throw new AssertionError("This method should not be called.");
103+
}
104+
105+
@Override
106+
public GuiSettings getGuiSettings() {
107+
throw new AssertionError("This method should not be called.");
108+
}
109+
110+
@Override
111+
public void setGuiSettings(GuiSettings guiSettings) {
112+
throw new AssertionError("This method should not be called.");
113+
}
114+
115+
@Override
116+
public Path getImmunimateFilePath() {
117+
throw new AssertionError("This method should not be called.");
118+
}
119+
120+
@Override
121+
public void setImmunimateFilePath(Path immuniMateFilePath) {
122+
throw new AssertionError("This method should not be called.");
123+
}
124+
125+
@Override
126+
public void addPerson(Person person) {
127+
throw new AssertionError("This method should not be called.");
128+
}
129+
130+
@Override
131+
public void addVisit(Visit visit) {
132+
133+
}
134+
135+
@Override
136+
public void setImmuniMate(ReadOnlyImmuniMate immuniMate) {
137+
throw new AssertionError("This method should not be called.");
138+
}
139+
140+
@Override
141+
public ReadOnlyImmuniMate getImmuniMate() {
142+
throw new AssertionError("This method should not be called.");
143+
}
144+
145+
@Override
146+
public boolean hasPerson(Person person) {
147+
throw new AssertionError("This method should not be called.");
148+
}
149+
150+
@Override
151+
public boolean hasVisit(Visit visit) {
152+
throw new AssertionError("This method should not be called.");
153+
}
154+
155+
@Override
156+
public void deletePerson(Person target) {
157+
throw new AssertionError("This method should not be called.");
158+
}
159+
160+
@Override
161+
public void deleteVisit(Visit target) {
162+
throw new AssertionError("This method should not be called.");
163+
}
164+
165+
@Override
166+
public void setPerson(Person target, Person editedPerson) {
167+
throw new AssertionError("This method should not be called.");
168+
}
169+
170+
@Override
171+
public void setVisit(Visit target, Visit editedVisit) {
172+
throw new AssertionError("This method should not be called.");
173+
}
174+
175+
@Override
176+
public ObservableList<Person> getFilteredPersonList() {
177+
throw new AssertionError("This method should not be called.");
178+
}
179+
180+
@Override
181+
public ObservableList<Visit> getFilteredVisitList() {
182+
throw new AssertionError("This method should not be called.");
183+
}
184+
185+
@Override
186+
public void updateFilteredPersonList(Predicate<Person> predicate) {
187+
throw new AssertionError("This method should not be called.");
188+
}
189+
190+
@Override
191+
public void updateFilteredVisitList(Predicate<Visit> predicate) {
192+
throw new AssertionError("This method should not be called.");
193+
}
194+
}
195+
196+
/**
197+
* A Model stub that contains a single person.
198+
*/
199+
private class ModelStubWithPerson extends ModelStub {
200+
private final Person person;
201+
202+
ModelStubWithPerson(Person person) {
203+
requireNonNull(person);
204+
this.person = person;
205+
}
206+
207+
@Override
208+
public boolean hasPerson(Person person) {
209+
requireNonNull(person);
210+
return this.person.isSamePerson(person);
211+
}
212+
}
213+
214+
/**
215+
* A Model stub that always accept the person being added.
216+
*/
217+
private class ModelStubAcceptingPersonAdded extends ModelStub {
218+
final ArrayList<Person> personsAdded = new ArrayList<>();
219+
220+
@Override
221+
public boolean hasPerson(Person person) {
222+
requireNonNull(person);
223+
return personsAdded.stream().anyMatch(person::isSamePerson);
224+
}
225+
226+
@Override
227+
public void addPerson(Person person) {
228+
requireNonNull(person);
229+
personsAdded.add(person);
230+
}
231+
232+
@Override
233+
public ReadOnlyImmuniMate getImmuniMate() {
234+
return new ImmuniMate();
235+
}
236+
}
237+
238+
}

0 commit comments

Comments
 (0)