Skip to content

Commit

Permalink
Refactor test cases to use newly added test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
NereusWB922 committed Oct 29, 2023
1 parent a441caa commit f4022d0
Show file tree
Hide file tree
Showing 14 changed files with 529 additions and 762 deletions.
11 changes: 4 additions & 7 deletions src/main/java/seedu/address/model/field/EditGroupDescriptor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.model.field;

import java.util.Objects;
import java.util.Optional;

import seedu.address.commons.util.CollectionUtil;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down

Large diffs are not rendered by default.

105 changes: 60 additions & 45 deletions src/test/java/seedu/address/logic/commands/CreateGroupCommandTest.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -34,48 +44,53 @@ public void constructor_nullRelativePathAndGroup_throwsNullPointerException() {

@Test
public void execute_createGroup_success() throws CommandException, InvalidPathException {
Map<Id, Group> children = new HashMap<>();
Root root = new Root(children);
Map<Id, Student> 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<Group> 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<Id, Group> children = new HashMap<>();
Root root = new Root(children);
Map<Id, Student> 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<Group> 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<Id, Student> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Id, Group> 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<Student> operation = expectedModel.groupChildOperation(targetAbsolutePath);
operation.addChild(toBeAdded.getId(), toBeAdded);

Student bob = new StudentBuilder()
.withName("Bob")
.withEmail("[email protected]")
.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("[email protected]")
.withPhone("98765432")
.withAddress("311, Clementi Ave 2, #02-25")
.withId("0001Y").build();
Map<Id, Student> studentMap = new HashMap<>();
studentMap.put(duplicatedStudent.getId(), duplicatedStudent);
Group grp = new Group(new ReadOnlyTaskList(), studentMap, new Name("ProfBook"), new GroupId("grp-001"));
Map<Id, Group> 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<Student> 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("[email protected]")
.withPhone("94351253")
.withAddress("123, Jurong West Ave 6, #08-111")
.withId("0001Y").build();
Student bob = new StudentBuilder()
.withName("Bob")
.withEmail("[email protected]")
.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());
}
}
Loading

0 comments on commit f4022d0

Please sign in to comment.