From f4022d034de66d32716e863c477628c29136ecb4 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 30 Oct 2023 02:35:15 +0800 Subject: [PATCH] Refactor test cases to use newly added test utils --- .../model/field/EditGroupDescriptor.java | 11 +- .../seedu/address/model/task/Deadline.java | 2 +- .../commands/CreateDeadlineCommandTest.java | 335 +++++------------- .../commands/CreateGroupCommandTest.java | 105 +++--- .../commands/CreateStudentCommandTest.java | 152 ++++---- .../logic/commands/CreateTodoCommandTest.java | 218 +++++------- ...DeleteForStudentsAndGroupsCommandTest.java | 197 +++++----- .../logic/commands/DeleteTaskCommandTest.java | 46 +-- .../logic/commands/EditCommandTest.java | 108 +++--- .../logic/commands/MarkCommandTest.java | 54 +-- .../logic/commands/UnmarkCommandTest.java | 54 +-- .../address/storage/JsonAdaptedGroupTest.java | 4 +- .../address/storage/JsonAdaptedRootTest.java | 3 +- .../storage/JsonAdaptedStudentTest.java | 2 +- 14 files changed, 529 insertions(+), 762 deletions(-) diff --git a/src/main/java/seedu/address/model/field/EditGroupDescriptor.java b/src/main/java/seedu/address/model/field/EditGroupDescriptor.java index 02b2376edf6..8ec022658e4 100644 --- a/src/main/java/seedu/address/model/field/EditGroupDescriptor.java +++ b/src/main/java/seedu/address/model/field/EditGroupDescriptor.java @@ -1,5 +1,6 @@ package seedu.address.model.field; +import java.util.Objects; import java.util.Optional; import seedu.address.commons.util.CollectionUtil; @@ -90,13 +91,9 @@ public boolean equals(Object other) { } EditGroupDescriptor otherEditGroupDescriptor = (EditGroupDescriptor) other; - if (this.name == null || otherEditGroupDescriptor.name == null) { - return false; - } - if (this.id == null || otherEditGroupDescriptor.id == null) { - return false; - } - return this.name.equals(otherEditGroupDescriptor.name) && this.id.equals(otherEditGroupDescriptor.id); + + return Objects.equals(this.name, otherEditGroupDescriptor.name) + && Objects.equals(this.id, otherEditGroupDescriptor.id); } /** diff --git a/src/main/java/seedu/address/model/task/Deadline.java b/src/main/java/seedu/address/model/task/Deadline.java index d0c3de793e0..86e9c69d4f8 100644 --- a/src/main/java/seedu/address/model/task/Deadline.java +++ b/src/main/java/seedu/address/model/task/Deadline.java @@ -12,7 +12,7 @@ */ public class Deadline extends Task { private static final DateTimeFormatter OUTPUT_DATETIME_FORMATTER = - DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy h:mm a"); + DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy hh:mm a"); private final LocalDateTime dueBy; /** diff --git a/src/test/java/seedu/address/logic/commands/CreateDeadlineCommandTest.java b/src/test/java/seedu/address/logic/commands/CreateDeadlineCommandTest.java index 3f2ba8181cd..b53cd333884 100644 --- a/src/test/java/seedu/address/logic/commands/CreateDeadlineCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/CreateDeadlineCommandTest.java @@ -1,313 +1,148 @@ 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.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static seedu.address.logic.commands.CreateDeadlineCommand.MESSAGE_SUCCESS; -import static seedu.address.logic.commands.CreateDeadlineCommand.MESSAGE_SUCCESS_ALL_GROUPS; -import static seedu.address.logic.commands.CreateDeadlineCommand.MESSAGE_SUCCESS_ALL_STUDENTS; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; import static seedu.address.testutil.Assert.assertThrows; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.ChildOperation; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.TaskOperation; import seedu.address.model.UserPrefs; -import seedu.address.model.id.GroupId; -import seedu.address.model.id.Id; import seedu.address.model.path.AbsolutePath; import seedu.address.model.path.RelativePath; import seedu.address.model.path.exceptions.InvalidPathException; import seedu.address.model.path.exceptions.UnsupportedPathOperationException; import seedu.address.model.profbook.Group; -import seedu.address.model.profbook.Name; import seedu.address.model.profbook.Root; import seedu.address.model.profbook.Student; import seedu.address.model.task.Deadline; -import seedu.address.model.task.ReadOnlyTaskList; -import seedu.address.model.task.Task; -import seedu.address.testutil.GroupBuilder; -import seedu.address.testutil.StudentBuilder; +import seedu.address.testutil.TypicalGroups; +import seedu.address.testutil.TypicalRoots; +import seedu.address.testutil.TypicalStudents; +import seedu.address.testutil.TypicalTasks; class CreateDeadlineCommandTest { + private Model model; + private Model expectedModel; + private AbsolutePath rootPath = CommandTestUtil.getValidRootAbsolutePath(); + private Deadline toBeAdded = TypicalTasks.DEADLINE_1; + + @BeforeEach + public void setup() { + model = new ModelManager(rootPath, new Root(TypicalRoots.PROFBOOK_WITH_TWO_GROUPS), new UserPrefs()); + expectedModel = new ModelManager(rootPath, new Root(TypicalRoots.PROFBOOK_WITH_TWO_GROUPS), new UserPrefs()); + } + + @Test + public void constructor_nullArgs_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> new CreateDeadlineCommand(null, null)); + } + @Test public void execute_deadlineForStudentAccepted_addSuccessful() throws InvalidPathException, UnsupportedPathOperationException, CommandException { - AbsolutePath currPath = new AbsolutePath("~/grp-001/"); - Map studentMap = new HashMap<>(); - Student calissa = new StudentBuilder() - .withName("Calissa") - .withEmail("callissa@example.com") - .withPhone("94351253") - .withAddress("123, Jurong West Ave 6, #08-111") - .withId("0012Y").withTaskList(new ArrayList<>()).build(); - studentMap.put(calissa.getId(), calissa); - Group grp = new Group(new ReadOnlyTaskList(), studentMap, new Name("Group1"), new GroupId("grp-001")); - Map groups = new HashMap<>(); - groups.put(new GroupId("grp-001"), grp); - Root root = new Root(groups); - - LocalDateTime duedate = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline = new Deadline("Assignment 3", duedate); - - assertFalse(calissa.contains(deadline)); - RelativePath path = new RelativePath("0012Y"); - AbsolutePath absolutePath = currPath.resolve(path); - - Model model = new ModelManager(currPath, root, new UserPrefs()); + Student alice = TypicalStudents.ALICE; + Group aliceGroup = TypicalGroups.GROUP_ONE; - TaskOperation target = model.taskOperation(absolutePath); + RelativePath groupPath = new RelativePath(aliceGroup.getId().toString()); + RelativePath alicePath = new RelativePath(alice.getId().toString()); + AbsolutePath absoluteTargetPath = rootPath.resolve(groupPath).resolve(alicePath); - CreateDeadlineCommand command = new CreateDeadlineCommand(absolutePath, deadline); - CommandResult runCommand = command.execute(model); + // Add task to expectedModel + TaskOperation operation = expectedModel.taskOperation(absoluteTargetPath); + operation.addTask(toBeAdded); - CommandResult returnStatement = - new CommandResult(String.format(MESSAGE_SUCCESS, deadline)); + CreateDeadlineCommand command = new CreateDeadlineCommand(absoluteTargetPath, toBeAdded); + String expectedMessage = String.format(CreateDeadlineCommand.MESSAGE_SUCCESS, + toBeAdded); - assertEquals(runCommand, returnStatement); - - assertTrue(target.hasTask(deadline)); - //assertTrue(calissa.contains(deadline)); + assertCommandSuccess(command, model, expectedMessage, expectedModel); } @Test public void execute_deadlineForAllStudentsInGroupAccepted_addSuccessful() throws InvalidPathException, CommandException { - AbsolutePath currPath = new AbsolutePath("~"); - Map studentMap = new HashMap<>(); - Student alice = new StudentBuilder() - .withName("Alice") - .withEmail("alice@example.com") - .withPhone("94351253") - .withAddress("123, Jurong West Ave 6, #08-111") - .withId("0001Y").withTaskList(new ArrayList<>()).build(); - Student bob = new StudentBuilder() - .withName("Bob") - .withEmail("johnd@example.com") - .withPhone("98765432") - .withAddress("311, Clementi Ave 2, #02-25") - .withId("0002Y").withTaskList(new ArrayList<>()).build(); - studentMap.put(alice.getId(), alice); - studentMap.put(bob.getId(), bob); - Group grp = new Group(new ReadOnlyTaskList(), studentMap, new Name("AmazingGroup"), new GroupId("grp-003")); - Map groups = new HashMap<>(); - groups.put(new GroupId("grp-003"), grp); - Root root = new Root(groups); - - LocalDateTime duedate = LocalDateTime.parse("2023-03-03T00:00"); - Deadline deadline = new Deadline("Assignment 1", duedate); - - assertFalse(alice.contains(deadline)); - assertFalse(bob.contains(deadline)); - - RelativePath path = new RelativePath("grp-003"); - AbsolutePath absolutePath = currPath.resolve(path); - - CreateDeadlineCommand command = new CreateDeadlineCommand(absolutePath, deadline, "allStu"); - Model model = new ModelManager(currPath, root, new UserPrefs()); - CommandResult runCommand = command.execute(model); - - // assertTrue(alice.contains(deadline)); - // assertTrue(bob.contains(deadline)); - - CommandResult returnStatement = - new CommandResult(MESSAGE_SUCCESS_ALL_STUDENTS); - - assertEquals(runCommand, returnStatement); - } + Group targetGroup = TypicalGroups.GROUP_ONE; - @Test - public void execute_deadlineForAllGroupsInRootAccepted_addSuccessful() - throws InvalidPathException, CommandException { - AbsolutePath currPath = new AbsolutePath("~"); - Map studentMap = new HashMap<>(); - List list1 = new ArrayList<>(); - List list2 = new ArrayList<>(); - ReadOnlyTaskList taskList1 = new ReadOnlyTaskList(list1); - ReadOnlyTaskList taskList2 = new ReadOnlyTaskList(list2); - Group grp1 = new Group(taskList1, studentMap, new Name("Amazing"), new GroupId("grp-001")); - Group grp2 = new Group(taskList2, studentMap, new Name("AmazingGroup"), new GroupId("grp-002")); - Map groups = new HashMap<>(); - groups.put(new GroupId("grp-001"), grp1); - groups.put(new GroupId("grp-002"), grp2); - Root root = new Root(groups); - - LocalDateTime duedate = LocalDateTime.parse("2023-12-03T23:58"); - Deadline deadline = new Deadline("Assignment 3", duedate); - - RelativePath path = new RelativePath("~"); - AbsolutePath absolutePath = currPath.resolve(path); - - assertFalse(grp1.contains(deadline)); - assertFalse(grp2.contains(deadline)); - - CreateDeadlineCommand command = new CreateDeadlineCommand(absolutePath, deadline, "allGrp"); - Model model = new ModelManager(currPath, root, new UserPrefs()); - CommandResult runCommand = command.execute(model); - - //assertTrue(grp1.contains(deadline)); - //assertTrue(grp2.contains(deadline)); - - CommandResult returnStatement = - new CommandResult(MESSAGE_SUCCESS_ALL_GROUPS); - - assertEquals(runCommand, returnStatement); - } + RelativePath groupPath = new RelativePath(targetGroup.getId().toString()); + AbsolutePath absoluteTargetPath = rootPath.resolve(groupPath); - @Test - public void execute_duplicateDeadline_throwsCommandException() throws InvalidPathException { - AbsolutePath currPath = new AbsolutePath("~/grp-001/"); + ChildOperation operation = expectedModel.groupChildOperation(absoluteTargetPath); + operation.addTaskToAllChildren(toBeAdded, 1); - Map groups = new HashMap<>(); - Group grp = new GroupBuilder().build(); - groups.put(new GroupId("grp-001"), grp); - Root root = new Root(groups); + CreateDeadlineCommand command = new CreateDeadlineCommand(absoluteTargetPath, toBeAdded, "allStu"); + String expectedMessage = CreateDeadlineCommand.MESSAGE_SUCCESS_ALL_STUDENTS; - Model model = new ModelManager(currPath, root, new UserPrefs()); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + } - RelativePath path = new RelativePath("~/grp-001/0001Y"); - AbsolutePath absolutePath = currPath.resolve(path); - Deadline deadline = new Deadline("Assignment 3", LocalDateTime.parse("2023-12-03T23:59")); + @Test + public void execute_deadlineForAllGroupsInRootAccepted_addSuccessful() + throws InvalidPathException, CommandException { + ChildOperation operation = expectedModel.rootChildOperation(); + operation.addTaskToAllChildren(toBeAdded, 1); - CreateDeadlineCommand createDeadlineCommand = new CreateDeadlineCommand(absolutePath, deadline); + CreateDeadlineCommand command = new CreateDeadlineCommand(rootPath, toBeAdded, "allGrp"); + String expectedMessage = CreateDeadlineCommand.MESSAGE_SUCCESS_ALL_GROUPS; - assertThrows(CommandException.class, - CreateDeadlineCommand.MESSAGE_DUPLICATE_DEADLINE_TASK, ( - ) -> createDeadlineCommand.execute(model) - ); + assertCommandSuccess(command, model, expectedMessage, expectedModel); } @Test - void equals_sameInstanceForStudentDirectory_success() throws InvalidPathException { - AbsolutePath path = new AbsolutePath("~/grp-001/0001Y"); - LocalDateTime duedate = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline = new Deadline("Assignment 1", duedate); - CreateDeadlineCommand command = new CreateDeadlineCommand(path, deadline); + public void execute_duplicateDeadline_throwsCommandException() throws InvalidPathException { + Student alice = TypicalStudents.ALICE; + Group aliceGroup = TypicalGroups.GROUP_ONE; - assertEquals(command, command); - } + RelativePath groupPath = new RelativePath(aliceGroup.getId().toString()); + RelativePath alicePath = new RelativePath(alice.getId().toString()); + AbsolutePath absoluteTargetPath = rootPath.resolve(groupPath).resolve(alicePath); - @Test - void equals_sameInstanceForGroupDirectory_success() throws InvalidPathException { - AbsolutePath path = new AbsolutePath("~/grp-001"); - LocalDateTime duedate = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline = new Deadline("Assignment 1", duedate); - CreateDeadlineCommand command = new CreateDeadlineCommand(path, deadline); + // Add task to model first + TaskOperation operation = model.taskOperation(absoluteTargetPath); + operation.addTask(toBeAdded); - assertEquals(command, command); - } + CreateDeadlineCommand command = new CreateDeadlineCommand(absoluteTargetPath, toBeAdded); + String expectedMessage = CreateDeadlineCommand.MESSAGE_DUPLICATE_DEADLINE_TASK; - @Test - void equals_sameCommandForStudentDirectory_success() throws InvalidPathException { - AbsolutePath path1 = new AbsolutePath("~/grp-001/0001Y"); - LocalDateTime duedate1 = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline1 = new Deadline("Assignment 1", duedate1); - CreateDeadlineCommand command1 = new CreateDeadlineCommand(path1, deadline1); - - AbsolutePath path2 = new AbsolutePath("~/grp-001/0001Y"); - LocalDateTime duedate2 = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline2 = new Deadline("Assignment 1", duedate2); - CreateDeadlineCommand command2 = new CreateDeadlineCommand(path2, deadline2); - - assertEquals(command1, command2); + assertCommandFailure(command, model, expectedMessage); } @Test - void equals_sameCommandForGroupDirectory_success() throws InvalidPathException { - AbsolutePath path1 = new AbsolutePath("~/grp-001"); - LocalDateTime duedate1 = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline1 = new Deadline("Assignment 1", duedate1); - CreateDeadlineCommand command1 = new CreateDeadlineCommand(path1, deadline1); - - AbsolutePath path2 = new AbsolutePath("~/grp-001"); - LocalDateTime duedate2 = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline2 = new Deadline("Assignment 1", duedate2); - CreateDeadlineCommand command2 = new CreateDeadlineCommand(path2, deadline2); - - assertEquals(command1, command2); - } + public void equals() { + CreateDeadlineCommand createDeadlineCommand1 = new CreateDeadlineCommand(rootPath, TypicalTasks.DEADLINE_1); + CreateDeadlineCommand createDeadlineCommand2 = new CreateDeadlineCommand(rootPath, TypicalTasks.DEADLINE_2); - @Test - void equals_differentDescriptionForStudentDirectory_fail() throws InvalidPathException { - AbsolutePath path1 = new AbsolutePath("~/grp-001/0001Y"); - LocalDateTime duedate1 = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline1 = new Deadline("Assignment 1", duedate1); - CreateDeadlineCommand command1 = new CreateDeadlineCommand(path1, deadline1); - - AbsolutePath path2 = new AbsolutePath("~/grp-001/0001Y"); - LocalDateTime duedate2 = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline2 = new Deadline("Assignment 2", duedate2); - CreateDeadlineCommand command2 = new CreateDeadlineCommand(path2, deadline2); - - assertNotEquals(command1, command2); - } + // same object -> returns true + assertEquals(createDeadlineCommand1, createDeadlineCommand1); - @Test - void equals_differentPathForStudentDirectory_fail() throws InvalidPathException { - AbsolutePath path1 = new AbsolutePath("~/grp-001/0001Y"); - LocalDateTime duedate1 = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline1 = new Deadline("Assignment 1", duedate1); - CreateDeadlineCommand command1 = new CreateDeadlineCommand(path1, deadline1); - - AbsolutePath path2 = new AbsolutePath("~/grp-001/0002Y"); - LocalDateTime duedate2 = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline2 = new Deadline("Assignment 1", duedate2); - CreateDeadlineCommand command2 = new CreateDeadlineCommand(path2, deadline2); - - assertNotEquals(command1, command2); - } + // same values -> returns true + CreateDeadlineCommand createDeadlineCommand1Copy = new CreateDeadlineCommand(rootPath, TypicalTasks.DEADLINE_1); + assertEquals(createDeadlineCommand1, createDeadlineCommand1Copy); - @Test - void equals_differentDueDateForStudentDirectory_fail() throws InvalidPathException { - AbsolutePath path1 = new AbsolutePath("~/grp-001/0001Y"); - LocalDateTime duedate1 = LocalDateTime.parse("2023-12-03T23:58"); - Deadline deadline1 = new Deadline("Assignment 1", duedate1); - CreateDeadlineCommand command1 = new CreateDeadlineCommand(path1, deadline1); - - AbsolutePath path2 = new AbsolutePath("~/grp-001/0001Y"); - LocalDateTime duedate2 = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline2 = new Deadline("Assignment 1", duedate2); - CreateDeadlineCommand command2 = new CreateDeadlineCommand(path2, deadline2); - - assertNotEquals(command1, command2); - } + // different types -> returns false + assertNotEquals(1, createDeadlineCommand1); - @Test - void equals_differentFieldsForStudentDirectory_fail() throws InvalidPathException { - AbsolutePath path1 = new AbsolutePath("~/grp-001/0001Y"); - LocalDateTime duedate1 = LocalDateTime.parse("2023-12-03T23:58"); - Deadline deadline1 = new Deadline("Assignment 1", duedate1); - CreateDeadlineCommand command1 = new CreateDeadlineCommand(path1, deadline1); - - AbsolutePath path2 = new AbsolutePath("~/grp-001/0001Y"); - LocalDateTime duedate2 = LocalDateTime.parse("2023-12-03T23:59"); - Deadline deadline2 = new Deadline("Assignment 2", duedate2); - CreateDeadlineCommand command2 = new CreateDeadlineCommand(path2, deadline2); - - assertNotEquals(command1, command2); + // null -> returns false + assertNotEquals(null, createDeadlineCommand1); + + // different values + assertNotEquals(createDeadlineCommand1, createDeadlineCommand2); } @Test - void toString_sameStringForStudentDirectory_success() throws InvalidPathException { - AbsolutePath path = new AbsolutePath("~/grp-001/0001Y"); - LocalDateTime duedate = LocalDateTime.parse("2023-12-03T23:58"); - Deadline deadline = new Deadline("Assignment 1", duedate); - CreateDeadlineCommand command = new CreateDeadlineCommand(path, deadline); - String expected = "seedu.address.logic.commands.CreateDeadlineCommand{" - + "toCreateDeadline=" - + "[D][ ] Assignment 1(by: 2023-12-03 23:58)}"; - - assertEquals(expected, command.toString()); + public void toStringMethod() { + CreateDeadlineCommand createDeadlineCommand = new CreateDeadlineCommand(rootPath, TypicalTasks.DEADLINE_1); + String expected = CreateDeadlineCommand.class.getCanonicalName() + + "{toCreateDeadline=" + TypicalTasks.DEADLINE_1 + "}"; + assertEquals(expected, createDeadlineCommand.toString()); } } diff --git a/src/test/java/seedu/address/logic/commands/CreateGroupCommandTest.java b/src/test/java/seedu/address/logic/commands/CreateGroupCommandTest.java index d98e79c294e..6a9863f9aa1 100644 --- a/src/test/java/seedu/address/logic/commands/CreateGroupCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/CreateGroupCommandTest.java @@ -1,31 +1,41 @@ package seedu.address.logic.commands; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; import static seedu.address.logic.commands.CreateGroupCommand.MESSAGE_DUPLICATE_GROUP; import static seedu.address.logic.commands.CreateGroupCommand.MESSAGE_SUCCESS; import static seedu.address.testutil.Assert.assertThrows; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; - +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.ChildOperation; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; -import seedu.address.model.id.GroupId; -import seedu.address.model.id.Id; import seedu.address.model.path.AbsolutePath; +import seedu.address.model.path.RelativePath; import seedu.address.model.path.exceptions.InvalidPathException; import seedu.address.model.profbook.Group; -import seedu.address.model.profbook.Name; import seedu.address.model.profbook.Root; -import seedu.address.model.profbook.Student; -import seedu.address.model.task.ReadOnlyTaskList; +import seedu.address.testutil.RootBuilder; +import seedu.address.testutil.TypicalGroups; public class CreateGroupCommandTest { + private Model model; + private Model expectedModel; + private AbsolutePath rootPath = CommandTestUtil.getValidRootAbsolutePath(); + private Group toBeAdded = TypicalGroups.GROUP_TWO; + + @BeforeEach + public void setup() { + Root root = new RootBuilder().withGroup(TypicalGroups.GROUP_ONE).build(); + model = new ModelManager(rootPath, new Root(root), new UserPrefs()); + expectedModel = new ModelManager(rootPath, new Root(root), new UserPrefs()); + } @Test public void constructor_nullRelativePathAndGroup_throwsNullPointerException() { @@ -34,48 +44,53 @@ public void constructor_nullRelativePathAndGroup_throwsNullPointerException() { @Test public void execute_createGroup_success() throws CommandException, InvalidPathException { - Map children = new HashMap<>(); - Root root = new Root(children); - Map students = new HashMap<>(); - Group group = new Group(new ReadOnlyTaskList(new ArrayList<>()), students, - new Name("Group1"), new GroupId("grp-001")); - AbsolutePath currPath = new AbsolutePath("~/"); - Model model = new ModelManager(currPath, root, new UserPrefs()); - - AbsolutePath target = new AbsolutePath("~/grp-001"); - CreateGroupCommand createGroupCommand = new CreateGroupCommand(target, group); - CommandResult successCommandResult = new CommandResult(String.format(MESSAGE_SUCCESS, group)); - - assertEquals(successCommandResult, createGroupCommand.execute(model)); + RelativePath groupTwo = new RelativePath(toBeAdded.getId().toString()); + AbsolutePath groupTwoAbsolutePath = rootPath.resolve(groupTwo); + + ChildOperation operation = expectedModel.rootChildOperation(); + operation.addChild(toBeAdded.getId(), toBeAdded); + expectedModel.updateList(); + + CreateGroupCommand command = new CreateGroupCommand(groupTwoAbsolutePath, toBeAdded); + String expectedMessage = String.format(MESSAGE_SUCCESS, toBeAdded); + + assertCommandSuccess(command, model, expectedMessage, expectedModel); } @Test public void execute_duplicateGroup_throwCommandException() throws InvalidPathException { - Map children = new HashMap<>(); - Root root = new Root(children); - Map students = new HashMap<>(); - Group group = new Group(new ReadOnlyTaskList(new ArrayList<>()), - students, new Name("Group1"), new GroupId("grp-001")); - root.addChild(group.getId(), group); - AbsolutePath currPath = new AbsolutePath("~/"); - AbsolutePath target = new AbsolutePath("~/grp-001"); - Model model = new ModelManager(currPath, root, new UserPrefs()); - CreateGroupCommand createGroupCommand = new CreateGroupCommand(target, group); - - assertThrows(CommandException.class, MESSAGE_DUPLICATE_GROUP, () -> createGroupCommand.execute(model)); + RelativePath groupTwo = new RelativePath(toBeAdded.getId().toString()); + AbsolutePath groupTwoAbsolutePath = rootPath.resolve(groupTwo); + + ChildOperation operation = model.rootChildOperation(); + operation.addChild(toBeAdded.getId(), toBeAdded); + model.updateList(); + + CreateGroupCommand command = new CreateGroupCommand(groupTwoAbsolutePath, toBeAdded); + String expectedMessage = MESSAGE_DUPLICATE_GROUP; + + assertCommandFailure(command, model, expectedMessage); } @Test - public void equals_sameInstance_success() throws InvalidPathException { - ReadOnlyTaskList taskList = new ReadOnlyTaskList(new ArrayList<>()); - Map students = new HashMap<>(); - Name name = new Name("Group 1"); - GroupId id = new GroupId("grp-001"); - Group group = new Group(taskList, students, name, id); - - AbsolutePath target = new AbsolutePath("~/grp-001"); - CreateGroupCommand createGroupCommand = new CreateGroupCommand(target, group); - CreateGroupCommand duplicateCreateGroupCommand = new CreateGroupCommand(target, group); - assertEquals(createGroupCommand, duplicateCreateGroupCommand); + public void equals() { + CreateGroupCommand createGroupCommand1 = new CreateGroupCommand(rootPath, TypicalGroups.GROUP_ONE); + CreateGroupCommand createGroupCommand2 = new CreateGroupCommand(rootPath, TypicalGroups.GROUP_TWO); + + // same object -> returns true + assertEquals(createGroupCommand1, createGroupCommand1); + + // same values -> returns true + CreateGroupCommand createGroupCommand1Copy = new CreateGroupCommand(rootPath, TypicalGroups.GROUP_ONE); + assertEquals(createGroupCommand1, createGroupCommand1Copy); + + // different types -> returns false + assertNotEquals(1, createGroupCommand1); + + // null -> returns false + assertNotEquals(null, createGroupCommand1); + + // different values + assertNotEquals(createGroupCommand1, createGroupCommand2); } } diff --git a/src/test/java/seedu/address/logic/commands/CreateStudentCommandTest.java b/src/test/java/seedu/address/logic/commands/CreateStudentCommandTest.java index 2ee6e42340a..d303147e56e 100644 --- a/src/test/java/seedu/address/logic/commands/CreateStudentCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/CreateStudentCommandTest.java @@ -2,142 +2,110 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.logic.commands.CreateStudentCommand.MESSAGE_DUPLICATE_STUDENT; +import static seedu.address.logic.commands.CreateStudentCommand.MESSAGE_SUCCESS; import static seedu.address.testutil.Assert.assertThrows; -import static seedu.address.testutil.TypicalStudents.ALICE; - -import java.util.HashMap; -import java.util.Map; +import static seedu.address.testutil.TypicalGroups.GROUP_ONE; +import static seedu.address.testutil.TypicalRoots.PROFBOOK_WITH_TWO_GROUPS; +import static seedu.address.testutil.TypicalStudents.KAREN; +import static seedu.address.testutil.TypicalStudents.LEO; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import seedu.address.logic.Messages; -import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.ChildOperation; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; -import seedu.address.model.id.GroupId; -import seedu.address.model.id.Id; import seedu.address.model.path.AbsolutePath; +import seedu.address.model.path.RelativePath; import seedu.address.model.path.exceptions.InvalidPathException; import seedu.address.model.profbook.Group; -import seedu.address.model.profbook.Name; import seedu.address.model.profbook.Root; import seedu.address.model.profbook.Student; -import seedu.address.model.task.ReadOnlyTaskList; -import seedu.address.testutil.GroupBuilder; -import seedu.address.testutil.StudentBuilder; class CreateStudentCommandTest { - private final Student validStudent = new StudentBuilder().build(); - - @Test - public void constructor_nullPersonNullPath_throwsNullPointerException() { - assertThrows(NullPointerException.class, () -> new CreateStudentCommand(null, null)); + private Model model; + private Model expectedModel; + private AbsolutePath rootPath = CommandTestUtil.getValidRootAbsolutePath(); + private Student toBeAdded = KAREN; + private Group targetGroup = GROUP_ONE; + private RelativePath groupPath; + private RelativePath studentPath; + private AbsolutePath targetAbsolutePath; + + + @BeforeEach + public void setup() throws InvalidPathException { + Root root = PROFBOOK_WITH_TWO_GROUPS; + model = new ModelManager(rootPath, new Root(root), new UserPrefs()); + expectedModel = new ModelManager(rootPath, new Root(root), new UserPrefs()); + + groupPath = new RelativePath(targetGroup.getId().toString()); + studentPath = new RelativePath(toBeAdded.getId().toString()); + targetAbsolutePath = rootPath.resolve(groupPath).resolve(studentPath); } @Test - public void constructor_nullPerson_throwsNullPointerException() { + public void constructor_nullArgs_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> new CreateStudentCommand(null, null)); assertThrows(NullPointerException.class, ( - ) -> new CreateStudentCommand(new AbsolutePath("~/grp-001/0001Y"), null)); - } - - @Test - public void constructor_nullPath_throwsNullPointerException() { - assertThrows(NullPointerException.class, () -> new CreateStudentCommand(null, validStudent)); + ) -> new CreateStudentCommand(new AbsolutePath("~/grp-001/0001Y"), null)); + assertThrows(NullPointerException.class, () -> new CreateStudentCommand(null, KAREN)); } @Test void execute_studentAcceptedByGroup_success() throws Exception { - AbsolutePath currPath = new AbsolutePath("~/grp-001/"); - - Map groups = new HashMap<>(); - Group grp = new GroupBuilder().build(); - groups.put(new GroupId("grp-001"), grp); - Root root = new Root(groups); - - AbsolutePath path = new AbsolutePath("~/grp-001/0002Y"); + ChildOperation operation = expectedModel.groupChildOperation(targetAbsolutePath); + operation.addChild(toBeAdded.getId(), toBeAdded); - Student bob = new StudentBuilder() - .withName("Bob") - .withEmail("bobthebuilder@example.com") - .withPhone("98765432") - .withAddress("311, Clementi Ave 2, #02-25") - .withId("0002Y").build(); + CreateStudentCommand command = new CreateStudentCommand(targetAbsolutePath, toBeAdded); + String expectedMessage = String.format(MESSAGE_SUCCESS, Messages.format(toBeAdded)); - CreateStudentCommand createStudentCommand = new CreateStudentCommand(path, bob); - Model model = new ModelManager(currPath, root, new UserPrefs()); - CommandResult commandResult = createStudentCommand.execute(model); - - assertEquals(String.format(CreateStudentCommand.MESSAGE_SUCCESS, Messages.format(bob)), - commandResult.getFeedbackToUser()); + assertCommandSuccess(command, model, expectedMessage, expectedModel); } @Test public void execute_duplicateStudent_throwsCommandException() throws InvalidPathException { - AbsolutePath currPath = new AbsolutePath("~/grp-001/"); - Student duplicatedStudent = new StudentBuilder() - .withName("alice") - .withEmail("aliceinwonderland@example.com") - .withPhone("98765432") - .withAddress("311, Clementi Ave 2, #02-25") - .withId("0001Y").build(); - Map studentMap = new HashMap<>(); - studentMap.put(duplicatedStudent.getId(), duplicatedStudent); - Group grp = new Group(new ReadOnlyTaskList(), studentMap, new Name("ProfBook"), new GroupId("grp-001")); - Map groups = new HashMap<>(); - groups.put(new GroupId("grp-001"), grp); - Root root = new Root(groups); - - AbsolutePath path = new AbsolutePath("~/grp-001/0001Y"); - - CreateStudentCommand createStudentCommand = new CreateStudentCommand(path, validStudent); - Model model = new ModelManager(currPath, root, new UserPrefs()); - assertThrows(CommandException.class, - CreateStudentCommand.MESSAGE_DUPLICATE_STUDENT, ( - ) -> createStudentCommand.execute(model) - ); + ChildOperation operation = model.groupChildOperation(targetAbsolutePath); + operation.addChild(toBeAdded.getId(), toBeAdded); + + CreateStudentCommand command = new CreateStudentCommand(targetAbsolutePath, toBeAdded); + String expectedMessage = MESSAGE_DUPLICATE_STUDENT; + + assertCommandFailure(command, model, expectedMessage); } @Test - void testEquals() throws InvalidPathException { - AbsolutePath path = new AbsolutePath("~/grp-001"); - Student alice = new StudentBuilder() - .withName("Alice") - .withEmail("alice@example.com") - .withPhone("94351253") - .withAddress("123, Jurong West Ave 6, #08-111") - .withId("0001Y").build(); - Student bob = new StudentBuilder() - .withName("Bob") - .withEmail("johnd@example.com") - .withPhone("98765432") - .withAddress("311, Clementi Ave 2, #02-25") - .withId("0002Y").build(); - CreateStudentCommand createAliceCommand = new CreateStudentCommand(path, alice); - CreateStudentCommand createBobCommand = new CreateStudentCommand(path, bob); + public void equals() { + CreateStudentCommand createStudentCommand1 = new CreateStudentCommand(targetAbsolutePath, KAREN); + CreateStudentCommand createStudentCommand2 = new CreateStudentCommand(targetAbsolutePath, LEO); // same object -> returns true - assertEquals(createAliceCommand, createAliceCommand); + assertEquals(createStudentCommand1, createStudentCommand1); // same values -> returns true - CreateStudentCommand createAliceCommandCopy = new CreateStudentCommand(path, alice); - assertEquals(createAliceCommand, createAliceCommandCopy); + CreateStudentCommand createStudentCommand1Copy = new CreateStudentCommand(targetAbsolutePath, KAREN); + assertEquals(createStudentCommand1, createStudentCommand1Copy); // different types -> returns false - assertNotEquals(1, createAliceCommand); + assertNotEquals(1, createStudentCommand1); // null -> returns false - assertNotEquals(null, createAliceCommand); + assertNotEquals(null, createStudentCommand1); - // different person -> returns false - assertNotEquals(createAliceCommand, createBobCommand); + // different values + assertNotEquals(createStudentCommand1, createStudentCommand2); } @Test - void toString_sameString_success() throws InvalidPathException { - AbsolutePath path = new AbsolutePath("~/grp-001"); - CreateStudentCommand createStudentCommand = new CreateStudentCommand(path, ALICE); - String expected = CreateStudentCommand.class.getCanonicalName() + "{toCreateStudent=" + ALICE + "}"; + public void toStringMethod() { + CreateStudentCommand createStudentCommand = new CreateStudentCommand(targetAbsolutePath, KAREN); + String expected = CreateStudentCommand.class.getCanonicalName() + + "{toCreateStudent=" + KAREN + "}"; assertEquals(expected, createStudentCommand.toString()); } } diff --git a/src/test/java/seedu/address/logic/commands/CreateTodoCommandTest.java b/src/test/java/seedu/address/logic/commands/CreateTodoCommandTest.java index 4f74ad62fda..3d943ddfa46 100644 --- a/src/test/java/seedu/address/logic/commands/CreateTodoCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/CreateTodoCommandTest.java @@ -1,40 +1,50 @@ 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.assertNotEquals; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.logic.commands.CreateTodoCommand.MESSAGE_DUPLICATE_TODO_TASK_STUDENT; import static seedu.address.logic.commands.CreateTodoCommand.MESSAGE_SUCCESS; import static seedu.address.logic.commands.CreateTodoCommand.MESSAGE_SUCCESS_ALL_GROUPS; import static seedu.address.logic.commands.CreateTodoCommand.MESSAGE_SUCCESS_ALL_STUDENTS; import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalTasks.TODO_1; +import static seedu.address.testutil.TypicalTasks.TODO_2; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.ChildOperation; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.TaskOperation; import seedu.address.model.UserPrefs; -import seedu.address.model.id.GroupId; -import seedu.address.model.id.Id; import seedu.address.model.path.AbsolutePath; +import seedu.address.model.path.RelativePath; import seedu.address.model.path.exceptions.InvalidPathException; import seedu.address.model.path.exceptions.UnsupportedPathOperationException; import seedu.address.model.profbook.Group; -import seedu.address.model.profbook.Name; import seedu.address.model.profbook.Root; import seedu.address.model.profbook.Student; -import seedu.address.model.task.ReadOnlyTaskList; -import seedu.address.model.task.Task; import seedu.address.model.task.ToDo; -import seedu.address.testutil.StudentBuilder; +import seedu.address.testutil.TypicalGroups; +import seedu.address.testutil.TypicalRoots; +import seedu.address.testutil.TypicalStudents; public class CreateTodoCommandTest { + private Model model; + private Model expectedModel; + private AbsolutePath rootPath = CommandTestUtil.getValidRootAbsolutePath(); + private ToDo toBeAdded = TODO_1; + + @BeforeEach + public void setup() { + model = new ModelManager(rootPath, new Root(TypicalRoots.PROFBOOK_WITH_TWO_GROUPS), new UserPrefs()); + expectedModel = new ModelManager(rootPath, new Root(TypicalRoots.PROFBOOK_WITH_TWO_GROUPS), new UserPrefs()); + } + @Test public void constructor_nullRelativePathAndTodo_throwsNullPointerException() { @@ -44,153 +54,99 @@ public void constructor_nullRelativePathAndTodo_throwsNullPointerException() { @Test public void execute_todoForAllStudentsInGroupAccepted_addSuccessful() throws InvalidPathException, CommandException { - AbsolutePath currPath = new AbsolutePath("~"); - Map studentMap = new HashMap<>(); - Student alice = new StudentBuilder() - .withName("Alice") - .withEmail("alice@example.com") - .withPhone("94351253") - .withAddress("123, Jurong West Ave 6, #08-111") - .withId("0001Y").withTaskList(new ArrayList<>()).build(); - Student bob = new StudentBuilder() - .withName("Bob") - .withEmail("johnd@example.com") - .withPhone("98765432") - .withAddress("311, Clementi Ave 2, #02-25") - .withId("0002Y").withTaskList(new ArrayList<>()).build(); - studentMap.put(alice.getId(), alice); - studentMap.put(bob.getId(), bob); - Group grp = new Group(new ReadOnlyTaskList(), studentMap, new Name("AmazingGroup"), new GroupId("grp-003")); - Map groups = new HashMap<>(); - groups.put(new GroupId("grp-003"), grp); - Root root = new Root(groups); - - ToDo todo = new ToDo("Assignment 1"); - - assertFalse(alice.contains(todo)); - assertFalse(bob.contains(todo)); - - AbsolutePath path = new AbsolutePath("~/grp-003"); - - CreateTodoCommand command = new CreateTodoCommand(path, todo, "allStu"); - Model model = new ModelManager(currPath, root, new UserPrefs()); - CommandResult runCommand = command.execute(model); - - //assertTrue(alice.contains(todo)); - //assertTrue(bob.contains(todo)); - - CommandResult returnStatement = - new CommandResult(MESSAGE_SUCCESS_ALL_STUDENTS); - - assertEquals(runCommand, returnStatement); - } + Group targetGroup = TypicalGroups.GROUP_ONE; - @Test - public void execute_deadlineForAllGroupsInRootAccepted_addSuccessful() - throws InvalidPathException, CommandException { - AbsolutePath currPath = new AbsolutePath("~"); - Map studentMap = new HashMap<>(); - List list1 = new ArrayList<>(); - List list2 = new ArrayList<>(); - ReadOnlyTaskList taskList1 = new ReadOnlyTaskList(list1); - ReadOnlyTaskList taskList2 = new ReadOnlyTaskList(list2); - Group grp1 = new Group(taskList1, studentMap, new Name("Amazing"), new GroupId("grp-001")); - Group grp2 = new Group(taskList2, studentMap, new Name("AmazingGroup"), new GroupId("grp-002")); - Map groups = new HashMap<>(); - groups.put(new GroupId("grp-001"), grp1); - groups.put(new GroupId("grp-002"), grp2); - Root root = new Root(groups); - - ToDo todo = new ToDo("Assignment 3"); + RelativePath groupPath = new RelativePath(targetGroup.getId().toString()); + AbsolutePath absoluteTargetPath = rootPath.resolve(groupPath); - AbsolutePath path = new AbsolutePath("~"); + ChildOperation operation = expectedModel.groupChildOperation(absoluteTargetPath); + operation.addTaskToAllChildren(toBeAdded, 1); - assertFalse(grp1.contains(todo)); - assertFalse(grp2.contains(todo)); + CreateTodoCommand command = new CreateTodoCommand(absoluteTargetPath, toBeAdded, "allStu"); + String expectedMessage = MESSAGE_SUCCESS_ALL_STUDENTS; - CreateTodoCommand command = new CreateTodoCommand(path, todo, "allGrp"); - Model model = new ModelManager(currPath, root, new UserPrefs()); - CommandResult runCommand = command.execute(model); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + } - //assertTrue(grp1.contains(todo)); - //assertTrue(grp2.contains(todo)); + @Test + public void execute_deadlineForAllGroupsInRootAccepted_addSuccessful() + throws InvalidPathException, CommandException { + ChildOperation operation = expectedModel.rootChildOperation(); + operation.addTaskToAllChildren(toBeAdded, 1); - CommandResult returnStatement = - new CommandResult(MESSAGE_SUCCESS_ALL_GROUPS); + CreateTodoCommand command = new CreateTodoCommand(rootPath, toBeAdded, "allGrp"); + String expectedMessage = MESSAGE_SUCCESS_ALL_GROUPS; - assertEquals(runCommand, returnStatement); + assertCommandSuccess(command, model, expectedMessage, expectedModel); } @Test public void execute_createTodoTask_success() throws CommandException, InvalidPathException, UnsupportedPathOperationException { - ToDo todo = new ToDo("Todo read book"); - Map children = new HashMap<>(); - Root root = new Root(children); - Map students = new HashMap<>(); - Group group = new Group(new ReadOnlyTaskList(new ArrayList<>()), students, - new Name("Group1"), new GroupId("grp-001")); - root.addChild(group.getId(), group); - - AbsolutePath currPath = new AbsolutePath("~/"); - Model model = new ModelManager(currPath, root, new UserPrefs()); - AbsolutePath target = new AbsolutePath("~/grp-001"); - CreateTodoCommand createTodoCommand = new CreateTodoCommand(target, todo); - - CommandResult successCommandResult = new CommandResult(String.format(MESSAGE_SUCCESS, target)); - assertEquals(successCommandResult, createTodoCommand.execute(model)); + Student alice = TypicalStudents.ALICE; + Group aliceGroup = TypicalGroups.GROUP_ONE; + + RelativePath groupPath = new RelativePath(aliceGroup.getId().toString()); + RelativePath alicePath = new RelativePath(alice.getId().toString()); + AbsolutePath absoluteTargetPath = rootPath.resolve(groupPath).resolve(alicePath); + + // Add task to expectedModel + TaskOperation operation = expectedModel.taskOperation(absoluteTargetPath); + operation.addTask(toBeAdded); + + CreateTodoCommand command = new CreateTodoCommand(absoluteTargetPath, toBeAdded); + String expectedMessage = String.format(MESSAGE_SUCCESS, absoluteTargetPath); + + assertCommandSuccess(command, model, expectedMessage, expectedModel); } @Test public void execute_duplicateTodoTask_throwCommandException() throws InvalidPathException, UnsupportedPathOperationException { - ToDo todo = new ToDo("Todo read book"); - Map children = new HashMap<>(); - Root root = new Root(children); - Map students = new HashMap<>(); - Group group = new Group(new ReadOnlyTaskList(new ArrayList<>()), students, - new Name("Group1"), new GroupId("grp-001")); - root.addChild(group.getId(), group); + Student alice = TypicalStudents.ALICE; + Group aliceGroup = TypicalGroups.GROUP_ONE; - AbsolutePath currPath = new AbsolutePath("~/"); - Model model = new ModelManager(currPath, root, new UserPrefs()); + RelativePath groupPath = new RelativePath(aliceGroup.getId().toString()); + RelativePath alicePath = new RelativePath(alice.getId().toString()); + AbsolutePath absoluteTargetPath = rootPath.resolve(groupPath).resolve(alicePath); - AbsolutePath target = new AbsolutePath("~/grp-001"); - TaskOperation taskOperation = model.taskOperation(target); - taskOperation.addTask(todo); + // Add task to model first + TaskOperation operation = model.taskOperation(absoluteTargetPath); + operation.addTask(toBeAdded); - CreateTodoCommand createTodoCommand = new CreateTodoCommand(target, todo); + CreateTodoCommand command = new CreateTodoCommand(absoluteTargetPath, toBeAdded); + String expectedMessage = MESSAGE_DUPLICATE_TODO_TASK_STUDENT; - assertThrows(CommandException.class, - CreateTodoCommand.MESSAGE_DUPLICATE_TODO_TASK_STUDENT, () -> createTodoCommand.execute(model)); + assertCommandFailure(command, model, expectedMessage); } @Test - public void equals_sameInstance_success() throws InvalidPathException { - ToDo todo = new ToDo("Todo test"); - AbsolutePath target = new AbsolutePath("~/grp-001"); - CreateTodoCommand createTodoCommand = new CreateTodoCommand(target, todo); - CreateTodoCommand duplicateCreateTodoCommand = new CreateTodoCommand(target, todo); - assertEquals(createTodoCommand, duplicateCreateTodoCommand); - } + public void equals() { + CreateTodoCommand createTodoCommand1 = new CreateTodoCommand(rootPath, TODO_1); + CreateTodoCommand createTodoCommand2 = new CreateTodoCommand(rootPath, TODO_2); - @Test - public void equals_differentTodoTask_fail() throws InvalidPathException { - ToDo todoTest1 = new ToDo("Todo test1"); - ToDo todoTest2 = new ToDo("Todo test2"); - AbsolutePath target = new AbsolutePath("~/grp-001"); - CreateTodoCommand createTodoCommand1 = new CreateTodoCommand(target, todoTest1); - CreateTodoCommand createTodoCommand2 = new CreateTodoCommand(target, todoTest2); + // same object -> returns true + assertEquals(createTodoCommand1, createTodoCommand1); + + // same values -> returns true + CreateTodoCommand createDeadlineCommand1Copy = new CreateTodoCommand(rootPath, TODO_1); + assertEquals(createTodoCommand1, createDeadlineCommand1Copy); + + // different types -> returns false + assertNotEquals(1, createTodoCommand1); + + // null -> returns false + assertNotEquals(null, createTodoCommand1); + + // different values assertNotEquals(createTodoCommand1, createTodoCommand2); } @Test - public void toString_validateOutputString_correctStringRepresentation() throws InvalidPathException { - ToDo todo = new ToDo("Todo test"); - AbsolutePath target = new AbsolutePath("~/grp-001"); - CreateTodoCommand createTodoCommand = new CreateTodoCommand(target, todo); - String expected = "seedu.address.logic.commands.CreateTodoCommand{toCreateTodo=" - + "[T][ ] Todo test}"; - assertEquals(expected, createTodoCommand.toString()); + public void toStringMethod() { + CreateTodoCommand createDeadlineCommand = new CreateTodoCommand(rootPath, TODO_1); + String expected = CreateTodoCommand.class.getCanonicalName() + + "{toCreateTodo=" + TODO_1 + "}"; + assertEquals(expected, createDeadlineCommand.toString()); } } diff --git a/src/test/java/seedu/address/logic/commands/DeleteForStudentsAndGroupsCommandTest.java b/src/test/java/seedu/address/logic/commands/DeleteForStudentsAndGroupsCommandTest.java index dd2faf67f36..daef4ecfb89 100644 --- a/src/test/java/seedu/address/logic/commands/DeleteForStudentsAndGroupsCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/DeleteForStudentsAndGroupsCommandTest.java @@ -1,188 +1,151 @@ 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.assertNotEquals; -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.logic.commands.DeleteForStudentsAndGroupsCommand.MESSAGE_INCORRECT_DIRECTORY_ERROR; +import static seedu.address.logic.commands.DeleteForStudentsAndGroupsCommand.MESSAGE_NO_SUCH_STUDENT_OR_GROUP; +import static seedu.address.logic.commands.DeleteForStudentsAndGroupsCommand.MESSAGE_SUCCESS_FOR_GROUP; +import static seedu.address.logic.commands.DeleteForStudentsAndGroupsCommand.MESSAGE_SUCCESS_FOR_STUDENT; import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalGroups.GROUP_ONE; +import static seedu.address.testutil.TypicalGroups.GROUP_TWO; +import static seedu.address.testutil.TypicalStudents.ELLE; +import static seedu.address.testutil.TypicalStudents.FIONA; -import java.util.HashMap; -import java.util.Map; - +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import seedu.address.logic.Messages; import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.ChildOperation; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; -import seedu.address.model.id.GroupId; -import seedu.address.model.id.Id; -import seedu.address.model.id.StudentId; import seedu.address.model.path.AbsolutePath; +import seedu.address.model.path.RelativePath; import seedu.address.model.path.exceptions.InvalidPathException; import seedu.address.model.profbook.Group; import seedu.address.model.profbook.Root; import seedu.address.model.profbook.Student; -import seedu.address.testutil.GroupBuilder; -import seedu.address.testutil.StudentBuilder; +import seedu.address.testutil.RootBuilder; class DeleteForStudentsAndGroupsCommandTest { + private Model model; + private Model expectedModel; + private AbsolutePath rootPath = CommandTestUtil.getValidRootAbsolutePath(); + + @BeforeEach + public void setup() { + Root root = new RootBuilder().withGroup(GROUP_ONE).build(); + model = new ModelManager(rootPath, new Root(root), new UserPrefs()); + expectedModel = new ModelManager(rootPath, new Root(root), new UserPrefs()); + } + public void constructor_nullPath_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> new DeleteForStudentsAndGroupsCommand(null)); } @Test void execute_deleteStudent_success() throws InvalidPathException, CommandException { - AbsolutePath currPath = new AbsolutePath("~/grp-001/"); + Student toBeDeleted = ELLE; + Group elleGroup = GROUP_ONE; - Map groups = new HashMap<>(); - Group grp = new GroupBuilder().build(); - groups.put(grp.getId(), grp); - Root root = new Root(groups); + RelativePath groupPath = new RelativePath(elleGroup.getId().toString()); + RelativePath stuPath = new RelativePath(toBeDeleted.getId().toString()); + AbsolutePath targetAbsolutePath = rootPath.resolve(groupPath).resolve(stuPath); - AbsolutePath path = new AbsolutePath("~/grp-001/0001Y"); - Student stu = new StudentBuilder().build(); - StudentId studentId = new StudentId("0001Y"); - assertTrue(grp.hasChild(studentId)); + ChildOperation operation = expectedModel.groupChildOperation(targetAbsolutePath); + operation.deleteChild(toBeDeleted.getId()); - DeleteForStudentsAndGroupsCommand command = new DeleteForStudentsAndGroupsCommand(path); - Model model = new ModelManager(currPath, root, new UserPrefs()); - CommandResult commandResult = command.execute(model); + DeleteForStudentsAndGroupsCommand command = new DeleteForStudentsAndGroupsCommand(targetAbsolutePath); + String expectedMessage = String.format(MESSAGE_SUCCESS_FOR_STUDENT, Messages.format(toBeDeleted)); - assertFalse(root.hasChild(studentId)); - - assertEquals(String.format(DeleteForStudentsAndGroupsCommand.MESSAGE_SUCCESS_FOR_STUDENT, Messages.format(stu)), - commandResult.getFeedbackToUser()); + assertCommandSuccess(command, model, expectedMessage, expectedModel); } @Test void execute_deleteGroup_success() throws InvalidPathException, CommandException { - AbsolutePath currPath = new AbsolutePath("~/"); - - Map groups = new HashMap<>(); - Group grp = new GroupBuilder().build(); - groups.put(new GroupId("grp-001"), grp); - Root root = new Root(groups); + Group toBeDeleted = GROUP_ONE; - AbsolutePath path = new AbsolutePath("~/grp-001/"); + RelativePath groupPath = new RelativePath(toBeDeleted.getId().toString()); + AbsolutePath targetAbsolutePath = rootPath.resolve(groupPath); - DeleteForStudentsAndGroupsCommand command = new DeleteForStudentsAndGroupsCommand(path); - Model model = new ModelManager(currPath, root, new UserPrefs()); - CommandResult commandResult = command.execute(model); + ChildOperation operation = expectedModel.rootChildOperation(); + operation.deleteChild(toBeDeleted.getId()); + expectedModel.updateList(); - GroupId groupId = new GroupId("grp-001"); - assertFalse(root.hasChild(groupId)); + DeleteForStudentsAndGroupsCommand command = new DeleteForStudentsAndGroupsCommand(targetAbsolutePath); + String expectedMessage = String.format(MESSAGE_SUCCESS_FOR_GROUP, Messages.format(toBeDeleted)); - assertEquals(String.format(DeleteForStudentsAndGroupsCommand.MESSAGE_SUCCESS_FOR_GROUP, Messages.format(grp)), - commandResult.getFeedbackToUser()); + assertCommandSuccess(command, model, expectedMessage, expectedModel); } @Test public void execute_noSuchStudent_throwsCommandException() throws InvalidPathException { - AbsolutePath currPath = new AbsolutePath("~/grp-001/"); + Student studentNotInGroupOne = FIONA; - Map groups = new HashMap<>(); - Group grp = new GroupBuilder().build(); - groups.put(new GroupId("grp-001"), grp); - Root root = new Root(groups); + RelativePath groupPath = new RelativePath(GROUP_ONE.getId().toString()); + RelativePath stuPath = new RelativePath(studentNotInGroupOne.getId().toString()); + AbsolutePath targetAbsolutePath = rootPath.resolve(groupPath).resolve(stuPath); - AbsolutePath path = new AbsolutePath("~/grp-001/0002Y"); + DeleteForStudentsAndGroupsCommand command = new DeleteForStudentsAndGroupsCommand(targetAbsolutePath); + String expectedMessage = MESSAGE_NO_SUCH_STUDENT_OR_GROUP; - DeleteForStudentsAndGroupsCommand command = new DeleteForStudentsAndGroupsCommand(path); - Model model = new ModelManager(currPath, root, new UserPrefs()); - assertThrows(CommandException.class, - DeleteForStudentsAndGroupsCommand.MESSAGE_NO_SUCH_STUDENT_OR_GROUP, ( - ) -> command.execute(model) - ); + assertCommandFailure(command, model, expectedMessage); } @Test public void execute_noSuchGroup_throwsCommandException() throws InvalidPathException { - AbsolutePath currPath = new AbsolutePath("~/"); + Group groupNotInRoot = GROUP_TWO; - Map groups = new HashMap<>(); - Group grp = new GroupBuilder().build(); - groups.put(new GroupId("grp-001"), grp); - Root root = new Root(groups); + RelativePath groupPath = new RelativePath(groupNotInRoot.getId().toString()); + AbsolutePath targetAbsolutePath = rootPath.resolve(groupPath); - AbsolutePath path = new AbsolutePath("~/grp-002/"); + DeleteForStudentsAndGroupsCommand command = new DeleteForStudentsAndGroupsCommand(targetAbsolutePath); + String expectedMessage = MESSAGE_NO_SUCH_STUDENT_OR_GROUP; - DeleteForStudentsAndGroupsCommand command = new DeleteForStudentsAndGroupsCommand(path); - Model model = new ModelManager(currPath, root, new UserPrefs()); - assertThrows(CommandException.class, - DeleteForStudentsAndGroupsCommand.MESSAGE_NO_SUCH_STUDENT_OR_GROUP, ( - ) -> command.execute(model) - ); + assertCommandFailure(command, model, expectedMessage); } @Test public void execute_incorrectDirectory_throwsCommandException() throws InvalidPathException { - AbsolutePath currPath = new AbsolutePath("~/"); - - Map groups = new HashMap<>(); - Group grp = new GroupBuilder().build(); - groups.put(new GroupId("grp-001"), grp); - Root root = new Root(groups); + DeleteForStudentsAndGroupsCommand command = new DeleteForStudentsAndGroupsCommand(rootPath); + String expectedMessage = MESSAGE_INCORRECT_DIRECTORY_ERROR; - AbsolutePath path = new AbsolutePath("~/"); - - DeleteForStudentsAndGroupsCommand command = new DeleteForStudentsAndGroupsCommand(path); - Model model = new ModelManager(currPath, root, new UserPrefs()); - assertThrows(CommandException.class, - DeleteForStudentsAndGroupsCommand.MESSAGE_INCORRECT_DIRECTORY_ERROR, ( - ) -> command.execute(model) - ); + assertCommandFailure(command, model, expectedMessage); } @Test - void testEquals() throws InvalidPathException { - AbsolutePath pathGrp001 = new AbsolutePath("~/grp-001"); - AbsolutePath pathGrp002 = new AbsolutePath("~/grp-002"); - AbsolutePath pathStu001 = new AbsolutePath("~/grp-001/0001Y"); - AbsolutePath pathStu002 = new AbsolutePath("~/grp-002/0002Y"); - - DeleteForStudentsAndGroupsCommand deleteG001 = new DeleteForStudentsAndGroupsCommand(pathGrp001); - DeleteForStudentsAndGroupsCommand deleteG002 = new DeleteForStudentsAndGroupsCommand(pathGrp002); - DeleteForStudentsAndGroupsCommand deleteS001 = new DeleteForStudentsAndGroupsCommand(pathStu001); - DeleteForStudentsAndGroupsCommand deleteS002 = new DeleteForStudentsAndGroupsCommand(pathStu002); - - // same object(Group) -> returns true - assertEquals(deleteG001, deleteG001); - // same object(Student) -> returns true - assertEquals(deleteS001, deleteS001); - - // same values(Group) -> returns true - DeleteForStudentsAndGroupsCommand deleteG001Copy = new DeleteForStudentsAndGroupsCommand(pathGrp001); - assertEquals(deleteG001, deleteG001Copy); - // same values(Student) -> returns true - DeleteForStudentsAndGroupsCommand deleteS001Copy = new DeleteForStudentsAndGroupsCommand(pathStu001); - assertEquals(deleteS001, deleteS001Copy); + public void equals() throws InvalidPathException { + DeleteForStudentsAndGroupsCommand command1 = new DeleteForStudentsAndGroupsCommand(rootPath); + DeleteForStudentsAndGroupsCommand command2 = new DeleteForStudentsAndGroupsCommand( + new AbsolutePath("~/grp-001")); + + // same object -> returns true + assertEquals(command1, command1); + + // same values -> returns true + DeleteForStudentsAndGroupsCommand command1Copy = new DeleteForStudentsAndGroupsCommand(rootPath); + assertEquals(command1, command1Copy); // different types -> returns false - assertNotEquals(1, deleteG001); - assertNotEquals(1, deleteS001); + assertNotEquals(1, command1); + // null -> returns false - assertNotEquals(null, deleteG001); - assertNotEquals(null, deleteS001); - - // different group -> returns false - assertNotEquals(deleteG001, deleteG002); - // different student -> returns false - assertNotEquals(deleteS001, deleteS002); - // different area to delete -> returns false - assertNotEquals(deleteG001, deleteS001); - // different student -> returns false - assertNotEquals(deleteS001, deleteS002); + assertNotEquals(null, command1); + + // different values + assertNotEquals(command1, command2); } @Test - void toString_sameString_success() throws InvalidPathException { - AbsolutePath path = new AbsolutePath("~/grp-001/0001Y"); - DeleteForStudentsAndGroupsCommand command = new DeleteForStudentsAndGroupsCommand(path); + public void toStringMethod() { + DeleteForStudentsAndGroupsCommand command = new DeleteForStudentsAndGroupsCommand(rootPath); String expected = DeleteForStudentsAndGroupsCommand.class.getCanonicalName() - + "{toDeleteStudentOrGroup=" + path + "}"; + + "{toDeleteStudentOrGroup=" + rootPath + "}"; assertEquals(expected, command.toString()); } } diff --git a/src/test/java/seedu/address/logic/commands/DeleteTaskCommandTest.java b/src/test/java/seedu/address/logic/commands/DeleteTaskCommandTest.java index 27490fce5f9..6bead14f666 100644 --- a/src/test/java/seedu/address/logic/commands/DeleteTaskCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/DeleteTaskCommandTest.java @@ -4,69 +4,73 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.logic.commands.DeleteTaskCommand.MESSAGE_INVALID_INDEX; +import static seedu.address.testutil.TypicalGroups.GROUP_ONE; import static seedu.address.testutil.TypicalIndexes.FIRST_INDEX; import static seedu.address.testutil.TypicalIndexes.SECOND_INDEX; +import static seedu.address.testutil.TypicalTasks.TASK_LIST_1; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import seedu.address.commons.core.index.Index; -import seedu.address.logic.Messages; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.TaskOperation; import seedu.address.model.UserPrefs; import seedu.address.model.path.AbsolutePath; +import seedu.address.model.path.RelativePath; import seedu.address.model.path.exceptions.InvalidPathException; +import seedu.address.model.profbook.Group; import seedu.address.model.profbook.Root; import seedu.address.model.task.Task; -import seedu.address.model.util.SampleProfBook; +import seedu.address.testutil.GroupBuilder; +import seedu.address.testutil.RootBuilder; public class DeleteTaskCommandTest { private Model actualModel; private Model expectedModel; - private TaskOperation taskOperation; + private AbsolutePath rootPath = CommandTestUtil.getValidRootAbsolutePath(); + private AbsolutePath displayPath; @BeforeEach public void setup() throws InvalidPathException { - AbsolutePath currentPath = new AbsolutePath("~/"); - Root root1 = SampleProfBook.getRoot(); - Root root2 = SampleProfBook.getRoot(); - actualModel = new ModelManager(currentPath, root1, new UserPrefs()); - - expectedModel = new ModelManager(currentPath, root2, new UserPrefs()); + Group groupOneWithTasks = new GroupBuilder(GROUP_ONE).withTaskList(TASK_LIST_1).build(); + Root root = new RootBuilder().withGroup(groupOneWithTasks).build(); + actualModel = new ModelManager(rootPath, new Root(root), new UserPrefs()); + expectedModel = new ModelManager(rootPath, new Root(root), new UserPrefs()); // Display task - AbsolutePath displayPath = new AbsolutePath("~/grp-002/"); + RelativePath groupPath = new RelativePath(groupOneWithTasks.getId().toString()); + displayPath = rootPath.resolve(groupPath); + actualModel.setDisplayPath(displayPath); actualModel.showTaskList(); expectedModel.setDisplayPath(displayPath); expectedModel.showTaskList(); - - taskOperation = expectedModel.taskOperation(displayPath); } @Test public void execute_validIndex_success() { - Task taskToDelete = taskOperation.getTask(FIRST_INDEX.getOneBased()); - DeleteTaskCommand deleteCommand = new DeleteTaskCommand(FIRST_INDEX); + TaskOperation operation = expectedModel.taskOperation(displayPath); - String expectedMessage = String.format(DeleteTaskCommand.MESSAGE_DELETE_TASK_SUCCESS, - taskToDelete.toString()); - - taskOperation.deleteTask(FIRST_INDEX.getOneBased()); + Task deletedTask = operation.deleteTask(FIRST_INDEX.getOneBased()); expectedModel.updateList(); + DeleteTaskCommand deleteCommand = new DeleteTaskCommand(FIRST_INDEX); + String expectedMessage = String.format(DeleteTaskCommand.MESSAGE_DELETE_TASK_SUCCESS, + deletedTask.toString()); + assertCommandSuccess(deleteCommand, actualModel, expectedMessage, expectedModel); } @Test public void execute_invalidIndex_throwsCommandException() { - Index outOfBoundIndex = Index.fromOneBased(taskOperation.getTaskListSize() + 1); + TaskOperation operation = actualModel.taskOperation(displayPath); + Index outOfBoundIndex = Index.fromOneBased(operation.getTaskListSize() + 1); DeleteTaskCommand deleteCommand = new DeleteTaskCommand(outOfBoundIndex); - assertCommandFailure(deleteCommand, actualModel, - Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX, expectedModel); + assertCommandFailure(deleteCommand, actualModel, MESSAGE_INVALID_INDEX); } @Test diff --git a/src/test/java/seedu/address/logic/commands/EditCommandTest.java b/src/test/java/seedu/address/logic/commands/EditCommandTest.java index ff44a61b5d6..31e9e16045f 100644 --- a/src/test/java/seedu/address/logic/commands/EditCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/EditCommandTest.java @@ -2,26 +2,40 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.logic.commands.EditCommand.ERROR_MESSAGE_NO_SUCH_GROUP; +import static seedu.address.logic.commands.EditCommand.MESSAGE_INCORRECT_DIRECTORY_ERROR; import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalGroups.GROUP_ONE; +import static seedu.address.testutil.TypicalGroups.GROUP_TWO; -import java.util.HashMap; -import java.util.Map; - +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; import seedu.address.model.field.EditGroupDescriptor; import seedu.address.model.field.EditStudentDescriptor; -import seedu.address.model.id.Id; import seedu.address.model.path.AbsolutePath; +import seedu.address.model.path.RelativePath; import seedu.address.model.path.exceptions.InvalidPathException; import seedu.address.model.profbook.Group; +import seedu.address.model.profbook.Name; import seedu.address.model.profbook.Root; +import seedu.address.testutil.RootBuilder; public class EditCommandTest { + private Model model; + private Model expectedModel; + private AbsolutePath rootPath = CommandTestUtil.getValidRootAbsolutePath(); + + @BeforeEach + public void setup() { + Root root = new RootBuilder().withGroup(GROUP_ONE).build(); + model = new ModelManager(rootPath, new Root(root), new UserPrefs()); + expectedModel = new ModelManager(rootPath, new Root(root), new UserPrefs()); + } @Test public void constructor_nullRelativePathAndEditGroupDescriptor_throwsNullPointerException() { @@ -34,53 +48,63 @@ public void constructor_nullRelativePathAndEditStudentDescriptor_throwsNullPoint } @Test - public void equals_differentRelativePath_fail() throws InvalidPathException { - AbsolutePath path1 = new AbsolutePath("~/grp-001"); - AbsolutePath path2 = new AbsolutePath("~/grp-002"); - EditGroupDescriptor editGroupDescriptor = new EditGroupDescriptor(); - EditCommand firstEditCommand = new EditCommand(path1, editGroupDescriptor); - EditCommand secondEditCommand = new EditCommand(path2, editGroupDescriptor); - - assertNotEquals(firstEditCommand, secondEditCommand); - } + public void execute_noSuchGroup_throwCommandException() throws InvalidPathException { + Group groupNotInRoot = GROUP_TWO; - @Test - public void toString_validateOutputString_correctStringRepresentation() throws InvalidPathException { - AbsolutePath path = new AbsolutePath("~/grp-001"); - EditGroupDescriptor editGroupDescriptor = new EditGroupDescriptor(); - EditCommand firstEditCommand = new EditCommand(path, editGroupDescriptor); - - String expected = "seedu.address.logic.commands.EditCommand{toEdit=seedu.address.model" - + ".field.EditGroupDescriptor{name=null, id=null}}"; - assertEquals(expected, firstEditCommand.toString()); + RelativePath groupPath = new RelativePath(groupNotInRoot.getId().toString()); + AbsolutePath targetAbsolutePath = rootPath.resolve(groupPath); + + EditGroupDescriptor editStudentDescriptor = new EditGroupDescriptor(); + EditCommand command = new EditCommand(targetAbsolutePath, editStudentDescriptor); + + String expectedMessage = ERROR_MESSAGE_NO_SUCH_GROUP; + + assertCommandFailure(command, model, expectedMessage); } @Test - public void execute_noSuchGroup_throwCommandException() throws InvalidPathException { - Map children = new HashMap<>(); - Root root = new Root(children); - AbsolutePath currPath = new AbsolutePath("~/"); - AbsolutePath path = new AbsolutePath("~/grp-001"); - Model model = new ModelManager(currPath, root, new UserPrefs()); - + public void execute_invalidDirectory_throwCommandException() throws InvalidPathException { EditStudentDescriptor editStudentDescriptor = new EditStudentDescriptor(); - EditCommand editCommand = new EditCommand(path, editStudentDescriptor); + EditCommand command = new EditCommand(rootPath, editStudentDescriptor); + + String expectedMessage = MESSAGE_INCORRECT_DIRECTORY_ERROR; - assertThrows(CommandException.class, EditCommand.MESSAGE_NO_SUCH_PATH, () -> editCommand.execute(model)); + assertCommandFailure(command, model, expectedMessage); } @Test - public void execute_invalidDirectory_throwCommandException() throws InvalidPathException { - Map children = new HashMap<>(); - Root root = new Root(children); - AbsolutePath currPath = new AbsolutePath("~/"); - AbsolutePath path = new AbsolutePath("~/"); - Model model = new ModelManager(currPath, root, new UserPrefs()); + public void equals() throws InvalidPathException { + EditGroupDescriptor editGroupDescriptor1 = new EditGroupDescriptor(); + EditGroupDescriptor editGroupDescriptor2 = new EditGroupDescriptor(); + editGroupDescriptor1.setName(new Name("Group 123")); + editGroupDescriptor2.setName(new Name("Group 321")); + EditCommand command1 = new EditCommand(rootPath, editGroupDescriptor1); + EditCommand command2 = new EditCommand(rootPath, editGroupDescriptor2); - EditStudentDescriptor editStudentDescriptor = new EditStudentDescriptor(); - EditCommand editCommand = new EditCommand(path, editStudentDescriptor); + // same object -> returns true + assertEquals(command1, command1); + + // same values -> returns true + EditCommand command1Copy = new EditCommand(rootPath, editGroupDescriptor1); + assertEquals(command1, command1Copy); - assertThrows(CommandException.class, - EditCommand.MESSAGE_INCORRECT_DIRECTORY_ERROR, () -> editCommand.execute(model)); + // different types -> returns false + assertNotEquals(1, command1); + + // null -> returns false + assertNotEquals(null, command1); + + // different values + assertNotEquals(command1, command2); + } + + @Test + public void toStringMethod() { + EditGroupDescriptor editGroupDescriptor1 = new EditGroupDescriptor(); + editGroupDescriptor1.setName(new Name("Group 123")); + EditCommand command = new EditCommand(rootPath, editGroupDescriptor1); + String expected = EditCommand.class.getCanonicalName() + + "{toEdit=" + editGroupDescriptor1 + "}"; + assertEquals(expected, command.toString()); } } diff --git a/src/test/java/seedu/address/logic/commands/MarkCommandTest.java b/src/test/java/seedu/address/logic/commands/MarkCommandTest.java index cb0bcd74af9..c5e91660978 100644 --- a/src/test/java/seedu/address/logic/commands/MarkCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/MarkCommandTest.java @@ -4,72 +4,74 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.logic.commands.MarkCommand.MESSAGE_INVALID_INDEX; +import static seedu.address.logic.commands.MarkCommand.MESSAGE_MARK_TASK_SUCCESS; +import static seedu.address.testutil.TypicalGroups.GROUP_ONE; import static seedu.address.testutil.TypicalIndexes.FIRST_INDEX; import static seedu.address.testutil.TypicalIndexes.SECOND_INDEX; +import static seedu.address.testutil.TypicalTasks.TASK_LIST_1; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import seedu.address.commons.core.index.Index; -import seedu.address.logic.Messages; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.TaskOperation; import seedu.address.model.UserPrefs; import seedu.address.model.path.AbsolutePath; +import seedu.address.model.path.RelativePath; import seedu.address.model.path.exceptions.InvalidPathException; +import seedu.address.model.profbook.Group; import seedu.address.model.profbook.Root; import seedu.address.model.task.Task; -import seedu.address.model.util.SampleProfBook; +import seedu.address.testutil.GroupBuilder; +import seedu.address.testutil.RootBuilder; public class MarkCommandTest { - private Model actualModel; - private Model expectedModel; - - private TaskOperation taskOperation; + private AbsolutePath rootPath = CommandTestUtil.getValidRootAbsolutePath(); + private AbsolutePath displayPath; @BeforeEach public void setup() throws InvalidPathException { - AbsolutePath currentPath = new AbsolutePath("~/"); - Root root1 = SampleProfBook.getRoot(); - Root root2 = SampleProfBook.getRoot(); - actualModel = new ModelManager(currentPath, root1, new UserPrefs()); - - expectedModel = new ModelManager(currentPath, root2, new UserPrefs()); + Group groupOneWithTasks = new GroupBuilder(GROUP_ONE).withTaskList(TASK_LIST_1).build(); + Root root = new RootBuilder().withGroup(groupOneWithTasks).build(); + actualModel = new ModelManager(rootPath, new Root(root), new UserPrefs()); + expectedModel = new ModelManager(rootPath, new Root(root), new UserPrefs()); // Display task - AbsolutePath displayPath = new AbsolutePath("~/grp-002/"); + RelativePath groupPath = new RelativePath(groupOneWithTasks.getId().toString()); + displayPath = rootPath.resolve(groupPath); + actualModel.setDisplayPath(displayPath); actualModel.showTaskList(); expectedModel.setDisplayPath(displayPath); expectedModel.showTaskList(); - - taskOperation = expectedModel.taskOperation(displayPath); } @Test public void execute_validIndex_success() { - Task taskToMark = taskOperation.markTask(FIRST_INDEX.getOneBased()); - MarkCommand markCommand = new MarkCommand(FIRST_INDEX); + TaskOperation operation = expectedModel.taskOperation(displayPath); - String expectedMessage = String.format(MarkCommand.MESSAGE_MARK_TASK_SUCCESS, - taskToMark.toString()); - - taskOperation.markTask(FIRST_INDEX.getOneBased()); + Task markedTask = operation.markTask(FIRST_INDEX.getOneBased()); expectedModel.updateList(); - assertCommandSuccess(markCommand, actualModel, expectedMessage, expectedModel); + MarkCommand command = new MarkCommand(FIRST_INDEX); + String expectedMessage = String.format(MESSAGE_MARK_TASK_SUCCESS, + markedTask.toString()); + + assertCommandSuccess(command, actualModel, expectedMessage, expectedModel); } @Test public void execute_invalidIndex_throwsCommandException() { - Index outOfBoundIndex = Index.fromOneBased(taskOperation.getTaskListSize() + 1); - MarkCommand markCommand = new MarkCommand(outOfBoundIndex); + TaskOperation operation = actualModel.taskOperation(displayPath); + Index outOfBoundIndex = Index.fromOneBased(operation.getTaskListSize() + 1); + MarkCommand deleteCommand = new MarkCommand(outOfBoundIndex); - assertCommandFailure(markCommand, actualModel, - Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX, expectedModel); + assertCommandFailure(deleteCommand, actualModel, MESSAGE_INVALID_INDEX); } @Test diff --git a/src/test/java/seedu/address/logic/commands/UnmarkCommandTest.java b/src/test/java/seedu/address/logic/commands/UnmarkCommandTest.java index 522ffe69d5a..c0dcc63ff83 100644 --- a/src/test/java/seedu/address/logic/commands/UnmarkCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/UnmarkCommandTest.java @@ -4,72 +4,74 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.logic.commands.UnmarkCommand.MESSAGE_INVALID_INDEX; +import static seedu.address.logic.commands.UnmarkCommand.MESSAGE_MARK_TASK_SUCCESS; +import static seedu.address.testutil.TypicalGroups.GROUP_ONE; import static seedu.address.testutil.TypicalIndexes.FIRST_INDEX; import static seedu.address.testutil.TypicalIndexes.SECOND_INDEX; +import static seedu.address.testutil.TypicalTasks.TASK_LIST_1; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import seedu.address.commons.core.index.Index; -import seedu.address.logic.Messages; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.TaskOperation; import seedu.address.model.UserPrefs; import seedu.address.model.path.AbsolutePath; +import seedu.address.model.path.RelativePath; import seedu.address.model.path.exceptions.InvalidPathException; +import seedu.address.model.profbook.Group; import seedu.address.model.profbook.Root; import seedu.address.model.task.Task; -import seedu.address.model.util.SampleProfBook; +import seedu.address.testutil.GroupBuilder; +import seedu.address.testutil.RootBuilder; public class UnmarkCommandTest { - private Model actualModel; - private Model expectedModel; - - private TaskOperation taskOperation; + private AbsolutePath rootPath = CommandTestUtil.getValidRootAbsolutePath(); + private AbsolutePath displayPath; @BeforeEach public void setup() throws InvalidPathException { - AbsolutePath currentPath = new AbsolutePath("~/"); - Root root1 = SampleProfBook.getRoot(); - Root root2 = SampleProfBook.getRoot(); - actualModel = new ModelManager(currentPath, root1, new UserPrefs()); - - expectedModel = new ModelManager(currentPath, root2, new UserPrefs()); + Group groupOneWithTasks = new GroupBuilder(GROUP_ONE).withTaskList(TASK_LIST_1).build(); + Root root = new RootBuilder().withGroup(groupOneWithTasks).build(); + actualModel = new ModelManager(rootPath, new Root(root), new UserPrefs()); + expectedModel = new ModelManager(rootPath, new Root(root), new UserPrefs()); // Display task - AbsolutePath displayPath = new AbsolutePath("~/grp-002/"); + RelativePath groupPath = new RelativePath(groupOneWithTasks.getId().toString()); + displayPath = rootPath.resolve(groupPath); + actualModel.setDisplayPath(displayPath); actualModel.showTaskList(); expectedModel.setDisplayPath(displayPath); expectedModel.showTaskList(); - - taskOperation = expectedModel.taskOperation(displayPath); } @Test public void execute_validIndex_success() { - Task taskToUnmark = taskOperation.unmarkTask(FIRST_INDEX.getOneBased()); - UnmarkCommand unmarkCommand = new UnmarkCommand(FIRST_INDEX); + TaskOperation operation = expectedModel.taskOperation(displayPath); - String expectedMessage = String.format(UnmarkCommand.MESSAGE_MARK_TASK_SUCCESS, - taskToUnmark.toString()); - - taskOperation.unmarkTask(FIRST_INDEX.getOneBased()); + Task unmarkedTask = operation.unmarkTask(FIRST_INDEX.getOneBased()); expectedModel.updateList(); - assertCommandSuccess(unmarkCommand, actualModel, expectedMessage, expectedModel); + UnmarkCommand command = new UnmarkCommand(FIRST_INDEX); + String expectedMessage = String.format(MESSAGE_MARK_TASK_SUCCESS, + unmarkedTask.toString()); + + assertCommandSuccess(command, actualModel, expectedMessage, expectedModel); } @Test public void execute_invalidIndex_throwsCommandException() { - Index outOfBoundIndex = Index.fromOneBased(taskOperation.getTaskListSize() + 1); - UnmarkCommand unmarkCommand = new UnmarkCommand(outOfBoundIndex); + TaskOperation operation = actualModel.taskOperation(displayPath); + Index outOfBoundIndex = Index.fromOneBased(operation.getTaskListSize() + 1); + UnmarkCommand deleteCommand = new UnmarkCommand(outOfBoundIndex); - assertCommandFailure(unmarkCommand, actualModel, - Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX, expectedModel); + assertCommandFailure(deleteCommand, actualModel, MESSAGE_INVALID_INDEX); } @Test diff --git a/src/test/java/seedu/address/storage/JsonAdaptedGroupTest.java b/src/test/java/seedu/address/storage/JsonAdaptedGroupTest.java index 80fc312b56a..d8cb381e6c8 100644 --- a/src/test/java/seedu/address/storage/JsonAdaptedGroupTest.java +++ b/src/test/java/seedu/address/storage/JsonAdaptedGroupTest.java @@ -20,14 +20,14 @@ public void toModelType_validGroupDetails_returnsGroup() throws Exception { @Test public void toModelType_emptyTask_returnsGroup() throws Exception { - Group grp = new GroupBuilder().buildEmptyTask(); + Group grp = new GroupBuilder().build(); JsonAdaptedGroup group = new JsonAdaptedGroup(grp); assertEquals(grp, group.toModelType()); } @Test public void toModelType_emptyStudent_returnsGroup() throws Exception { - Group grp = new GroupBuilder().buildWithEmptyStudent(); + Group grp = new GroupBuilder().build(); JsonAdaptedGroup group = new JsonAdaptedGroup(grp); assertEquals(grp, group.toModelType()); diff --git a/src/test/java/seedu/address/storage/JsonAdaptedRootTest.java b/src/test/java/seedu/address/storage/JsonAdaptedRootTest.java index d9f5637109f..fd5935bc404 100644 --- a/src/test/java/seedu/address/storage/JsonAdaptedRootTest.java +++ b/src/test/java/seedu/address/storage/JsonAdaptedRootTest.java @@ -6,12 +6,13 @@ import seedu.address.model.profbook.Root; import seedu.address.testutil.RootBuilder; +import seedu.address.testutil.TypicalGroups; public class JsonAdaptedRootTest { @Test public void toModelType_validGroupDetails_returnsGroup() throws Exception { - Root root = new RootBuilder().withGroup(); + Root root = new RootBuilder().withGroup(TypicalGroups.GROUP_ONE).build(); System.out.println(root); JsonAdaptedRoot jsonRoot = new JsonAdaptedRoot(root); assertEquals(root, jsonRoot.toModelType()); diff --git a/src/test/java/seedu/address/storage/JsonAdaptedStudentTest.java b/src/test/java/seedu/address/storage/JsonAdaptedStudentTest.java index 5e904f94ea4..3749bcd9fdf 100644 --- a/src/test/java/seedu/address/storage/JsonAdaptedStudentTest.java +++ b/src/test/java/seedu/address/storage/JsonAdaptedStudentTest.java @@ -19,7 +19,7 @@ public void toModelType_validStudentDetails_returnsStudent() throws Exception { @Test public void toModelType_emptyTask_returnsStudent() throws Exception { - Student grp = new StudentBuilder().buildEmptyTask(); + Student grp = new StudentBuilder().build(); JsonAdaptedStudent student = new JsonAdaptedStudent(grp); assertEquals(grp, student.toModelType()); }