|
| 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