From 81d4a67770472d7d3724fdc1fea605a14b4d0e1f Mon Sep 17 00:00:00 2001 From: Bearypop Date: Fri, 10 Nov 2023 06:03:13 +0800 Subject: [PATCH 1/2] Add tests for Join and Create features --- .../address/logic/commands/CreateCommand.java | 17 ++- .../logic/commands/CreateCommandTest.java | 60 ++++++++++ .../logic/commands/JoinCommandTest.java | 108 ++++++++++++++++++ .../logic/parser/CreateCommandParserTest.java | 46 ++++++++ .../logic/parser/JoinCommandParserTest.java | 66 +++++++++++ .../seedu/address/testutil/TypicalGroups.java | 11 +- 6 files changed, 304 insertions(+), 4 deletions(-) create mode 100644 src/test/java/seedu/address/logic/commands/CreateCommandTest.java create mode 100644 src/test/java/seedu/address/logic/commands/JoinCommandTest.java create mode 100644 src/test/java/seedu/address/logic/parser/CreateCommandParserTest.java create mode 100644 src/test/java/seedu/address/logic/parser/JoinCommandParserTest.java diff --git a/src/main/java/seedu/address/logic/commands/CreateCommand.java b/src/main/java/seedu/address/logic/commands/CreateCommand.java index 2e1705e23f6..85f3fa0c061 100644 --- a/src/main/java/seedu/address/logic/commands/CreateCommand.java +++ b/src/main/java/seedu/address/logic/commands/CreateCommand.java @@ -63,7 +63,7 @@ public CommandResult execute(Model model) { * * @param model */ - private int generateGroupNumber(Model model) { + public int generateGroupNumber(Model model) { int number = 1; ReadOnlyAddressBook addressBook = model.getAddressBook(); ObservableList groups = addressBook.getGroupList(); @@ -78,4 +78,19 @@ private int generateGroupNumber(Model model) { return number; } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof CreateCommand)) { + return false; + } + + CreateCommand otherCreateCommand = (CreateCommand) other; + return tutorial.equals(otherCreateCommand.tutorial); + } } diff --git a/src/test/java/seedu/address/logic/commands/CreateCommandTest.java b/src/test/java/seedu/address/logic/commands/CreateCommandTest.java new file mode 100644 index 00000000000..ae744cc87e6 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/CreateCommandTest.java @@ -0,0 +1,60 @@ +package seedu.address.logic.commands; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.model.tutorial.Tutorial; +import seedu.address.testutil.TypicalGroups; + +/** + * Contains integration tests (interaction with the Model) and unit tests for + * {@code CreateCommand}. + */ +public class CreateCommandTest { + + @Test + public void execute_validTutorialNumber_success() { + Model model = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs()); + CreateCommand createCommand = new CreateCommand(new Tutorial("01")); + String expectedMessage = String.format(CreateCommand.MESSAGE_SUCCESS, + createCommand.generateGroupNumber(model)); + + CommandResult commandResult = createCommand.execute(model); + + assertEquals(expectedMessage, commandResult.getFeedbackToUser()); + } + + @Test + public void execute_invalidTutorialNumber_throwsCommandException() { + Model model = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs()); + assertThrows(IllegalArgumentException.class, () -> new CreateCommand(new Tutorial("40")).execute(model)); + } + + @Test + public void equals() { + CreateCommand firstCreateCommand = new CreateCommand(new Tutorial("01")); + CreateCommand secondCreateCommand = new CreateCommand(new Tutorial("02")); + + // same object -> returns true + assertTrue(firstCreateCommand.equals(firstCreateCommand)); + + // same values -> returns true + assertTrue(firstCreateCommand.equals(new CreateCommand(new Tutorial("01")))); + + // different types -> returns false + assertFalse(firstCreateCommand.equals(1)); + + // null -> returns false + assertFalse(firstCreateCommand.equals(null)); + + // different tutorial number -> returns false + assertFalse(firstCreateCommand.equals(secondCreateCommand)); + } +} diff --git a/src/test/java/seedu/address/logic/commands/JoinCommandTest.java b/src/test/java/seedu/address/logic/commands/JoinCommandTest.java new file mode 100644 index 00000000000..d6a2b8e09e7 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/JoinCommandTest.java @@ -0,0 +1,108 @@ +package seedu.address.logic.commands; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.testutil.TypicalGroups.GROUP1; +import static seedu.address.testutil.TypicalGroups.GROUP2; +import static seedu.address.testutil.TypicalGroups.GROUP3; +import static seedu.address.testutil.TypicalGroups.GROUP11; +import static seedu.address.testutil.TypicalPersons.ALICE; +import static seedu.address.testutil.TypicalPersons.AMY; +import static seedu.address.testutil.TypicalPersons.BOB; +import static seedu.address.testutil.TypicalPersons.DANIEL; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.testutil.TypicalGroups; + +/** + * Contains integration tests (interaction with the Model) and unit tests for + * {@code JoinCommand}. + */ +public class JoinCommandTest { + private Model model; + private Model expectedModel; + + @BeforeEach + public void setUp() { + model = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs()); + expectedModel = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs()); + model.addPerson(AMY); + expectedModel.addPerson(AMY); + } + + @Test + public void execute_join_success() { + // person exists, group exists, group is not full, person is not in a group + JoinCommand command = new JoinCommand(AMY.getEmail(), GROUP3.getNumber()); + String expectedMessage = String.format(JoinCommand.MESSAGE_JOIN_SUCCESS, + AMY.getName(), GROUP3.getNumber()); + + expectedModel.addPersonToGroup(AMY, expectedModel.getGroupWithNumber(GROUP3.getNumber()).get()); + + assertCommandSuccess(command, model, expectedMessage, expectedModel, true); + } + + @Test + public void execute_personDoesNotExist_throwsCommandException() { + JoinCommand command = new JoinCommand(BOB.getEmail(), GROUP1.getNumber()); + assertCommandFailure(command, model, JoinCommand.MESSAGE_JOIN_EMAIL_NOT_FOUND); + } + + @Test + public void execute_groupDoesNotExist_throwsCommandException() { + JoinCommand command = new JoinCommand(AMY.getEmail(), 100); + assertCommandFailure(command, model, JoinCommand.MESSAGE_JOIN_GROUP_NOT_FOUND); + } + + @Test + public void execute_personAlreadyInTheGroup_throwsCommandException() { + JoinCommand command = new JoinCommand(ALICE.getEmail(), GROUP1.getNumber()); + assertCommandFailure(command, model, JoinCommand.MESSAGE_PERSON_ALREADY_IN_GROUP); + } + + @Test + public void execute_personInAnotherGroup_throwsCommandException() { + JoinCommand command = new JoinCommand(DANIEL.getEmail(), GROUP1.getNumber()); + assertCommandFailure(command, model, JoinCommand.MESSAGE_PERSON_IN_ANOTHER_GROUP); + } + + @Test + public void execute_fullGroup_throwsCommandException() { + JoinCommand command = new JoinCommand(AMY.getEmail(), GROUP11.getNumber()); + assertCommandFailure(command, model, JoinCommand.MESSAGE_GROUP_FULL); + } + + @Test + public void equals() { + JoinCommand firstJoinCommand = new JoinCommand(AMY.getEmail(), GROUP1.getNumber()); + JoinCommand secondJoinCommand = new JoinCommand(AMY.getEmail(), GROUP2.getNumber()); + JoinCommand thirdJoinCommand = new JoinCommand(BOB.getEmail(), GROUP1.getNumber()); + + // same object -> returns true + assertTrue(firstJoinCommand.equals(firstJoinCommand)); + + // same values -> returns true + JoinCommand firstJoinCommandCopy = new JoinCommand(AMY.getEmail(), GROUP1.getNumber()); + assertTrue(firstJoinCommand.equals(firstJoinCommandCopy)); + + // different types -> returns false + assertFalse(firstJoinCommand.equals(1)); + + // null -> returns false + assertFalse(firstJoinCommand.equals(null)); + + // different email -> returns false + assertFalse(firstJoinCommand.equals(thirdJoinCommand)); + + // different group number -> returns false + assertFalse(firstJoinCommand.equals(secondJoinCommand)); + } + +} diff --git a/src/test/java/seedu/address/logic/parser/CreateCommandParserTest.java b/src/test/java/seedu/address/logic/parser/CreateCommandParserTest.java new file mode 100644 index 00000000000..a2e12304b72 --- /dev/null +++ b/src/test/java/seedu/address/logic/parser/CreateCommandParserTest.java @@ -0,0 +1,46 @@ +package seedu.address.logic.parser; + +import static seedu.address.testutil.Assert.assertThrows; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import seedu.address.logic.commands.CreateCommand; +import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.tutorial.Tutorial; + +public class CreateCommandParserTest { + + private CreateCommandParser parser = new CreateCommandParser(); + + @Test + public void parse_validArgs_returnsCreateCommand() { + CommandParserTestUtil.assertParseSuccess(parser, " t/01", new CreateCommand(new Tutorial("01"))); + } + + @Test + public void parse_missingTutorialPrefix_throwsParseException() { + Assertions.assertThrows(ParseException.class, () -> parser.parse(" 01")); + } + + @Test + public void parse_invalidTutorialNumber_throwsParseException() { + assertThrows(ParseException.class, () -> parser.parse(" t/40")); + } + + @Test + public void parse_multipleTutorialNumber_throwsParseException() { + assertThrows(ParseException.class, () -> parser.parse(" t/01 t/02")); + } + + @Test + public void parse_emptyArgument_throwsParseException() { + Assertions.assertThrows(ParseException.class, () -> parser.parse("")); + } + + @Test + public void parse_nullArgument_throwsNullPointerException() { + Assertions.assertThrows(NullPointerException.class, () -> parser.parse(null)); + } + +} diff --git a/src/test/java/seedu/address/logic/parser/JoinCommandParserTest.java b/src/test/java/seedu/address/logic/parser/JoinCommandParserTest.java new file mode 100644 index 00000000000..2e05603a564 --- /dev/null +++ b/src/test/java/seedu/address/logic/parser/JoinCommandParserTest.java @@ -0,0 +1,66 @@ +package seedu.address.logic.parser; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; +import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP; +import static seedu.address.testutil.Assert.assertThrows; + +import org.junit.jupiter.api.Test; + +import seedu.address.logic.commands.JoinCommand; +import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.person.Email; + +public class JoinCommandParserTest { + + private JoinCommandParser parser = new JoinCommandParser(); + + @Test + public void parse_validInput_success() throws ParseException { + String userInput = " " + PREFIX_EMAIL + "johnd@u.nus.edu " + PREFIX_GROUP + "1"; + JoinCommand expectedCommand = new JoinCommand(new Email("johnd@u.nus.edu"), 1); + assertEquals(expectedCommand, parser.parse(userInput)); + } + + @Test + public void parse_missingEmailPrefix_throwsParseException() { + String userInput = " johnd@u.nus.edu " + PREFIX_GROUP + "1"; + assertThrows(ParseException.class, () -> parser.parse(userInput)); + } + + @Test + public void parse_missingGroupPrefix_throwsParseException() { + String userInput = PREFIX_EMAIL + "johnd@u.nus.edu " + "1"; + assertThrows(ParseException.class, () -> parser.parse(userInput)); + } + + @Test + public void parse_invalidEmail_throwsParseException() { + String userInput = PREFIX_EMAIL + "johnd" + " " + PREFIX_GROUP + "1"; + assertThrows(ParseException.class, () -> parser.parse(userInput)); + } + + @Test + public void parse_invalidGroupNumber_throwsParseException() { + String userInput = PREFIX_EMAIL + "johnd@u.nus.edu " + PREFIX_GROUP + "one"; + assertThrows(ParseException.class, () -> parser.parse(userInput)); + } + + @Test + public void parse_missingGroupNumber_throwsParseException() { + String userInput = PREFIX_EMAIL + "johnd@u.nus.edu " + PREFIX_GROUP; + assertThrows(ParseException.class, () -> parser.parse(userInput)); + } + + @Test + public void parse_missingEmail_throwsParseException() { + String userInput = " " + PREFIX_EMAIL + " " + PREFIX_GROUP + "1"; + assertThrows(ParseException.class, () -> parser.parse(userInput)); + } + + @Test + public void parse_emptyArguments_throwsParseException() { + String userInput = ""; + assertThrows(ParseException.class, () -> parser.parse(userInput)); + } +} diff --git a/src/test/java/seedu/address/testutil/TypicalGroups.java b/src/test/java/seedu/address/testutil/TypicalGroups.java index e9e655e9ca1..33a76d7ca24 100644 --- a/src/test/java/seedu/address/testutil/TypicalGroups.java +++ b/src/test/java/seedu/address/testutil/TypicalGroups.java @@ -13,12 +13,14 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Set; import seedu.address.model.AddressBook; import seedu.address.model.group.Group; import seedu.address.model.group.tasks.TaskList; +import seedu.address.model.person.Person; import seedu.address.model.tutorial.Tutorial; /** @@ -31,7 +33,7 @@ public class TypicalGroups { public static final Group GROUP2 = new Group(2, new Tutorial("02"), Set.of(DANIEL, ELLE, FIONA), new TaskList()); public static final Group GROUP3 = new Group(3, new Tutorial("03"), - Set.of(GEORGE), new TaskList()); + new HashSet<>(Set.of(GEORGE)), new TaskList()); public static final Group GROUP4 = new Group(4, new Tutorial("02"), Set.of(HOON), new TaskList()); @@ -89,9 +91,12 @@ private TypicalGroups() {} // prevents instantiation * Returns an {@code AddressBook} with all the typical groups. */ public static AddressBook getTypicalAddressBook() { - AddressBook ab = new AddressBook(); + AddressBook ab = TypicalPersons.getTypicalAddressBook(); + for (Person person : ab.getPersonList()) { + ab.setPerson(person, new PersonBuilder(person).build()); + } for (Group group : getTypicalGroups()) { - ab.addGroup(group); + ab.addGroup(new GroupBuilder(group).build()); } return ab; } From 67a90c61f4eafbd25b8979861829a1015394bf3a Mon Sep 17 00:00:00 2001 From: Bearypop Date: Fri, 10 Nov 2023 06:11:52 +0800 Subject: [PATCH 2/2] Add test for generateGroupNumber --- .../seedu/address/logic/commands/CreateCommandTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/seedu/address/logic/commands/CreateCommandTest.java b/src/test/java/seedu/address/logic/commands/CreateCommandTest.java index ae744cc87e6..a017633849c 100644 --- a/src/test/java/seedu/address/logic/commands/CreateCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/CreateCommandTest.java @@ -37,6 +37,13 @@ public void execute_invalidTutorialNumber_throwsCommandException() { assertThrows(IllegalArgumentException.class, () -> new CreateCommand(new Tutorial("40")).execute(model)); } + @Test + public void generateGroupNumberTest() { + Model model = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs()); + CreateCommand command = new CreateCommand(new Tutorial("01")); + assertEquals(command.generateGroupNumber(model), model.getFilteredGroupList().size() + 1); + } + @Test public void equals() { CreateCommand firstCreateCommand = new CreateCommand(new Tutorial("01"));