forked from nus-cs2103-AY1617S2/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogicManagerTest.java
544 lines (456 loc) · 21.7 KB
/
LogicManagerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package seedu.address.logic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
import static seedu.address.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import com.google.common.eventbus.Subscribe;
import seedu.address.commons.core.EventsCenter;
import seedu.address.commons.events.model.AddressBookChangedEvent;
import seedu.address.commons.events.ui.JumpToListRequestEvent;
import seedu.address.commons.events.ui.ShowHelpRequestEvent;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.SelectCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.ReadOnlyPerson;
import seedu.address.model.tag.Tag;
import seedu.address.model.tag.UniqueTagList;
import seedu.address.storage.StorageManager;
public class LogicManagerTest {
/**
* See https://github.com/junit-team/junit4/wiki/rules#temporaryfolder-rule
*/
@Rule
public TemporaryFolder saveFolder = new TemporaryFolder();
private Model model;
private Logic logic;
//These are for checking the correctness of the events raised
private ReadOnlyAddressBook latestSavedAddressBook;
private boolean helpShown;
private int targetedJumpIndex;
@Subscribe
private void handleLocalModelChangedEvent(AddressBookChangedEvent abce) {
latestSavedAddressBook = new AddressBook(abce.data);
}
@Subscribe
private void handleShowHelpRequestEvent(ShowHelpRequestEvent she) {
helpShown = true;
}
@Subscribe
private void handleJumpToListRequestEvent(JumpToListRequestEvent je) {
targetedJumpIndex = je.targetIndex;
}
@Before
public void setUp() {
model = new ModelManager();
String tempAddressBookFile = saveFolder.getRoot().getPath() + "TempAddressBook.xml";
String tempPreferencesFile = saveFolder.getRoot().getPath() + "TempPreferences.json";
logic = new LogicManager(model, new StorageManager(tempAddressBookFile, tempPreferencesFile));
EventsCenter.getInstance().registerHandler(this);
latestSavedAddressBook = new AddressBook(model.getAddressBook()); // last saved assumed to be up to date
helpShown = false;
targetedJumpIndex = -1; // non yet
}
@After
public void tearDown() {
EventsCenter.clearSubscribers();
}
@Test
public void execute_invalid() {
String invalidCommand = " ";
assertCommandFailure(invalidCommand, String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE));
}
/**
* Executes the command, confirms that a CommandException is not thrown and that the result message is correct.
* Also confirms that both the 'address book' and the 'last shown list' are as specified.
* @see #assertCommandBehavior(boolean, String, String, ReadOnlyAddressBook, List)
*/
private void assertCommandSuccess(String inputCommand, String expectedMessage,
ReadOnlyAddressBook expectedAddressBook,
List<? extends ReadOnlyPerson> expectedShownList) {
assertCommandBehavior(false, inputCommand, expectedMessage, expectedAddressBook, expectedShownList);
}
/**
* Executes the command, confirms that a CommandException is thrown and that the result message is correct.
* Both the 'address book' and the 'last shown list' are verified to be unchanged.
* @see #assertCommandBehavior(boolean, String, String, ReadOnlyAddressBook, List)
*/
private void assertCommandFailure(String inputCommand, String expectedMessage) {
AddressBook expectedAddressBook = new AddressBook(model.getAddressBook());
List<ReadOnlyPerson> expectedShownList = new ArrayList<>(model.getFilteredPersonList());
assertCommandBehavior(true, inputCommand, expectedMessage, expectedAddressBook, expectedShownList);
}
/**
* Executes the command, confirms that the result message is correct
* and that a CommandException is thrown if expected
* and also confirms that the following three parts of the LogicManager object's state are as expected:<br>
* - the internal address book data are same as those in the {@code expectedAddressBook} <br>
* - the backing list shown by UI matches the {@code shownList} <br>
* - {@code expectedAddressBook} was saved to the storage file. <br>
*/
private void assertCommandBehavior(boolean isCommandExceptionExpected, String inputCommand, String expectedMessage,
ReadOnlyAddressBook expectedAddressBook,
List<? extends ReadOnlyPerson> expectedShownList) {
try {
CommandResult result = logic.execute(inputCommand);
assertFalse("CommandException expected but was not thrown.", isCommandExceptionExpected);
assertEquals(expectedMessage, result.feedbackToUser);
} catch (CommandException e) {
assertTrue("CommandException not expected but was thrown.", isCommandExceptionExpected);
assertEquals(expectedMessage, e.getMessage());
}
//Confirm the ui display elements should contain the right data
assertEquals(expectedShownList, model.getFilteredPersonList());
//Confirm the state of data (saved and in-memory) is as expected
assertEquals(expectedAddressBook, model.getAddressBook());
assertEquals(expectedAddressBook, latestSavedAddressBook);
}
@Test
public void execute_unknownCommandWord() {
String unknownCommand = "uicfhmowqewca";
assertCommandFailure(unknownCommand, MESSAGE_UNKNOWN_COMMAND);
}
@Test
public void execute_help() {
assertCommandSuccess("help", HelpCommand.SHOWING_HELP_MESSAGE, new AddressBook(), Collections.emptyList());
assertTrue(helpShown);
}
@Test
public void execute_exit() {
assertCommandSuccess("exit", ExitCommand.MESSAGE_EXIT_ACKNOWLEDGEMENT,
new AddressBook(), Collections.emptyList());
}
@Test
public void execute_clear() throws Exception {
TestDataHelper helper = new TestDataHelper();
model.addPerson(helper.generatePerson(1));
model.addPerson(helper.generatePerson(2));
model.addPerson(helper.generatePerson(3));
assertCommandSuccess("clear", ClearCommand.MESSAGE_SUCCESS, new AddressBook(), Collections.emptyList());
}
@Test
public void execute_add_invalidArgsFormat() {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE);
assertCommandFailure("add wrong args wrong args", expectedMessage);
assertCommandFailure("add Valid Name 12345 e/[email protected] a/valid,address", expectedMessage);
assertCommandFailure("add Valid Name p/12345 [email protected] a/valid, address", expectedMessage);
assertCommandFailure("add Valid Name p/12345 e/[email protected] valid, address", expectedMessage);
}
@Test
public void execute_add_invalidPersonData() {
assertCommandFailure("add []\\[;] p/12345 e/[email protected] a/valid, address",
Name.MESSAGE_NAME_CONSTRAINTS);
assertCommandFailure("add Valid Name p/not_numbers e/[email protected] a/valid, address",
Phone.MESSAGE_PHONE_CONSTRAINTS);
assertCommandFailure("add Valid Name p/12345 e/notAnEmail a/valid, address",
Email.MESSAGE_EMAIL_CONSTRAINTS);
assertCommandFailure("add Valid Name p/12345 e/[email protected] a/valid, address t/invalid_-[.tag",
Tag.MESSAGE_TAG_CONSTRAINTS);
}
@Test
public void execute_add_successful() throws Exception {
// setup expectations
TestDataHelper helper = new TestDataHelper();
Person toBeAdded = helper.adam();
AddressBook expectedAB = new AddressBook();
expectedAB.addPerson(toBeAdded);
// execute command and verify result
assertCommandSuccess(helper.generateAddCommand(toBeAdded),
String.format(AddCommand.MESSAGE_SUCCESS, toBeAdded),
expectedAB,
expectedAB.getPersonList());
}
@Test
public void execute_addDuplicate_notAllowed() throws Exception {
// setup expectations
TestDataHelper helper = new TestDataHelper();
Person toBeAdded = helper.adam();
// setup starting state
model.addPerson(toBeAdded); // person already in internal address book
// execute command and verify result
assertCommandFailure(helper.generateAddCommand(toBeAdded), AddCommand.MESSAGE_DUPLICATE_PERSON);
}
@Test
public void execute_list_showsAllPersons() throws Exception {
// prepare expectations
TestDataHelper helper = new TestDataHelper();
AddressBook expectedAB = helper.generateAddressBook(2);
List<? extends ReadOnlyPerson> expectedList = expectedAB.getPersonList();
// prepare address book state
helper.addToModel(model, 2);
assertCommandSuccess("list",
ListCommand.MESSAGE_SUCCESS,
expectedAB,
expectedList);
}
/**
* Confirms the 'invalid argument index number behaviour' for the given command
* targeting a single person in the shown list, using visible index.
* @param commandWord to test assuming it targets a single person in the last shown list
* based on visible index.
*/
private void assertIncorrectIndexFormatBehaviorForCommand(String commandWord, String expectedMessage)
throws Exception {
assertCommandFailure(commandWord , expectedMessage); //index missing
assertCommandFailure(commandWord + " +1", expectedMessage); //index should be unsigned
assertCommandFailure(commandWord + " -1", expectedMessage); //index should be unsigned
assertCommandFailure(commandWord + " 0", expectedMessage); //index cannot be 0
assertCommandFailure(commandWord + " not_a_number", expectedMessage);
}
/**
* Confirms the 'invalid argument index number behaviour' for the given command
* targeting a single person in the shown list, using visible index.
* @param commandWord to test assuming it targets a single person in the last shown list
* based on visible index.
*/
private void assertIndexNotFoundBehaviorForCommand(String commandWord) throws Exception {
String expectedMessage = MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
TestDataHelper helper = new TestDataHelper();
List<Person> personList = helper.generatePersonList(2);
// set AB state to 2 persons
model.resetData(new AddressBook());
for (Person p : personList) {
model.addPerson(p);
}
assertCommandFailure(commandWord + " 3", expectedMessage);
}
@Test
public void execute_selectInvalidArgsFormat_errorMessageShown() throws Exception {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, SelectCommand.MESSAGE_USAGE);
assertIncorrectIndexFormatBehaviorForCommand("select", expectedMessage);
}
@Test
public void execute_selectIndexNotFound_errorMessageShown() throws Exception {
assertIndexNotFoundBehaviorForCommand("select");
}
@Test
public void execute_select_jumpsToCorrectPerson() throws Exception {
TestDataHelper helper = new TestDataHelper();
List<Person> threePersons = helper.generatePersonList(3);
AddressBook expectedAB = helper.generateAddressBook(threePersons);
helper.addToModel(model, threePersons);
assertCommandSuccess("select 2",
String.format(SelectCommand.MESSAGE_SELECT_PERSON_SUCCESS, 2),
expectedAB,
expectedAB.getPersonList());
assertEquals(1, targetedJumpIndex);
assertEquals(model.getFilteredPersonList().get(1), threePersons.get(1));
}
@Test
public void execute_deleteInvalidArgsFormat_errorMessageShown() throws Exception {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE);
assertIncorrectIndexFormatBehaviorForCommand("delete", expectedMessage);
}
@Test
public void execute_deleteIndexNotFound_errorMessageShown() throws Exception {
assertIndexNotFoundBehaviorForCommand("delete");
}
@Test
public void execute_delete_removesCorrectPerson() throws Exception {
TestDataHelper helper = new TestDataHelper();
List<Person> threePersons = helper.generatePersonList(3);
AddressBook expectedAB = helper.generateAddressBook(threePersons);
expectedAB.removePerson(threePersons.get(1));
helper.addToModel(model, threePersons);
assertCommandSuccess("delete 2",
String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS, threePersons.get(1)),
expectedAB,
expectedAB.getPersonList());
}
@Test
public void execute_find_invalidArgsFormat() {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE);
assertCommandFailure("find ", expectedMessage);
}
@Test
public void execute_find_onlyMatchesFullWordsInNames() throws Exception {
TestDataHelper helper = new TestDataHelper();
Person pTarget1 = helper.generatePersonWithName("bla bla KEY bla");
Person pTarget2 = helper.generatePersonWithName("bla KEY bla bceofeia");
Person p1 = helper.generatePersonWithName("KE Y");
Person p2 = helper.generatePersonWithName("KEYKEYKEY sduauo");
List<Person> fourPersons = helper.generatePersonList(p1, pTarget1, p2, pTarget2);
AddressBook expectedAB = helper.generateAddressBook(fourPersons);
List<Person> expectedList = helper.generatePersonList(pTarget1, pTarget2);
helper.addToModel(model, fourPersons);
assertCommandSuccess("find KEY",
Command.getMessageForPersonListShownSummary(expectedList.size()),
expectedAB,
expectedList);
}
@Test
public void execute_find_isNotCaseSensitive() throws Exception {
TestDataHelper helper = new TestDataHelper();
Person p1 = helper.generatePersonWithName("bla bla KEY bla");
Person p2 = helper.generatePersonWithName("bla KEY bla bceofeia");
Person p3 = helper.generatePersonWithName("key key");
Person p4 = helper.generatePersonWithName("KEy sduauo");
List<Person> fourPersons = helper.generatePersonList(p3, p1, p4, p2);
AddressBook expectedAB = helper.generateAddressBook(fourPersons);
List<Person> expectedList = fourPersons;
helper.addToModel(model, fourPersons);
assertCommandSuccess("find KEY",
Command.getMessageForPersonListShownSummary(expectedList.size()),
expectedAB,
expectedList);
}
@Test
public void execute_find_matchesIfAnyKeywordPresent() throws Exception {
TestDataHelper helper = new TestDataHelper();
Person pTarget1 = helper.generatePersonWithName("bla bla KEY bla");
Person pTarget2 = helper.generatePersonWithName("bla rAnDoM bla bceofeia");
Person pTarget3 = helper.generatePersonWithName("key key");
Person p1 = helper.generatePersonWithName("sduauo");
List<Person> fourPersons = helper.generatePersonList(pTarget1, p1, pTarget2, pTarget3);
AddressBook expectedAB = helper.generateAddressBook(fourPersons);
List<Person> expectedList = helper.generatePersonList(pTarget1, pTarget2, pTarget3);
helper.addToModel(model, fourPersons);
assertCommandSuccess("find key rAnDoM",
Command.getMessageForPersonListShownSummary(expectedList.size()),
expectedAB,
expectedList);
}
/**
* A utility class to generate test data.
*/
class TestDataHelper {
Person adam() throws Exception {
Name name = new Name("Adam Brown");
Phone privatePhone = new Phone("111111");
Email email = new Email("[email protected]");
Address privateAddress = new Address("111, alpha street");
Tag tag1 = new Tag("tag1");
Tag tag2 = new Tag("longertag2");
UniqueTagList tags = new UniqueTagList(tag1, tag2);
return new Person(name, privatePhone, email, privateAddress, tags);
}
/**
* Generates a valid person using the given seed.
* Running this function with the same parameter values guarantees the returned person will have the same state.
* Each unique seed will generate a unique Person object.
*
* @param seed used to generate the person data field values
*/
Person generatePerson(int seed) throws Exception {
return new Person(
new Name("Person " + seed),
new Phone("" + Math.abs(seed)),
new Email(seed + "@email"),
new Address("House of " + seed),
new UniqueTagList(new Tag("tag" + Math.abs(seed)), new Tag("tag" + Math.abs(seed + 1)))
);
}
/** Generates the correct add command based on the person given */
String generateAddCommand(Person p) {
StringBuffer cmd = new StringBuffer();
cmd.append("add ");
cmd.append(p.getName().toString());
cmd.append(" e/").append(p.getEmail());
cmd.append(" p/").append(p.getPhone());
cmd.append(" a/").append(p.getAddress());
UniqueTagList tags = p.getTags();
for (Tag t: tags) {
cmd.append(" t/").append(t.tagName);
}
return cmd.toString();
}
/**
* Generates an AddressBook with auto-generated persons.
*/
AddressBook generateAddressBook(int numGenerated) throws Exception {
AddressBook addressBook = new AddressBook();
addToAddressBook(addressBook, numGenerated);
return addressBook;
}
/**
* Generates an AddressBook based on the list of Persons given.
*/
AddressBook generateAddressBook(List<Person> persons) throws Exception {
AddressBook addressBook = new AddressBook();
addToAddressBook(addressBook, persons);
return addressBook;
}
/**
* Adds auto-generated Person objects to the given AddressBook
* @param addressBook The AddressBook to which the Persons will be added
*/
void addToAddressBook(AddressBook addressBook, int numGenerated) throws Exception {
addToAddressBook(addressBook, generatePersonList(numGenerated));
}
/**
* Adds the given list of Persons to the given AddressBook
*/
void addToAddressBook(AddressBook addressBook, List<Person> personsToAdd) throws Exception {
for (Person p: personsToAdd) {
addressBook.addPerson(p);
}
}
/**
* Adds auto-generated Person objects to the given model
* @param model The model to which the Persons will be added
*/
void addToModel(Model model, int numGenerated) throws Exception {
addToModel(model, generatePersonList(numGenerated));
}
/**
* Adds the given list of Persons to the given model
*/
void addToModel(Model model, List<Person> personsToAdd) throws Exception {
for (Person p: personsToAdd) {
model.addPerson(p);
}
}
/**
* Generates a list of Persons based on the flags.
*/
List<Person> generatePersonList(int numGenerated) throws Exception {
List<Person> persons = new ArrayList<>();
for (int i = 1; i <= numGenerated; i++) {
persons.add(generatePerson(i));
}
return persons;
}
List<Person> generatePersonList(Person... persons) {
return Arrays.asList(persons);
}
/**
* Generates a Person object with given name. Other fields will have some dummy values.
*/
Person generatePersonWithName(String name) throws Exception {
return new Person(
new Name(name),
new Phone("1"),
new Email("1@email"),
new Address("House of 1"),
new UniqueTagList(new Tag("tag"))
);
}
}
}