Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Join and Create features #194

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/main/java/seedu/address/logic/commands/CreateCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public CommandResult execute(Model model) {
*
* @param model
*/
private int generateGroupNumber(Model model) {
public int generateGroupNumber(Model model) {
int number = 1;
ReadOnlyAddressBook addressBook = model.getAddressBook();
ObservableList<Group> groups = addressBook.getGroupList();
Expand All @@ -78,4 +78,19 @@ private int generateGroupNumber(Model model) {

return number;
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof CreateCommand)) {
return false;
}

CreateCommand otherCreateCommand = (CreateCommand) other;
return tutorial.equals(otherCreateCommand.tutorial);
}
}
67 changes: 67 additions & 0 deletions src/test/java/seedu/address/logic/commands/CreateCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.tutorial.Tutorial;
import seedu.address.testutil.TypicalGroups;

/**
* Contains integration tests (interaction with the Model) and unit tests for
* {@code CreateCommand}.
*/
public class CreateCommandTest {

@Test
public void execute_validTutorialNumber_success() {
Model model = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs());
CreateCommand createCommand = new CreateCommand(new Tutorial("01"));
String expectedMessage = String.format(CreateCommand.MESSAGE_SUCCESS,
createCommand.generateGroupNumber(model));

CommandResult commandResult = createCommand.execute(model);

assertEquals(expectedMessage, commandResult.getFeedbackToUser());
}

@Test
public void execute_invalidTutorialNumber_throwsCommandException() {
Model model = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs());
assertThrows(IllegalArgumentException.class, () -> new CreateCommand(new Tutorial("40")).execute(model));
}

@Test
public void generateGroupNumberTest() {
Model model = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs());
CreateCommand command = new CreateCommand(new Tutorial("01"));
assertEquals(command.generateGroupNumber(model), model.getFilteredGroupList().size() + 1);
}

@Test
public void equals() {
CreateCommand firstCreateCommand = new CreateCommand(new Tutorial("01"));
CreateCommand secondCreateCommand = new CreateCommand(new Tutorial("02"));

// same object -> returns true
assertTrue(firstCreateCommand.equals(firstCreateCommand));

// same values -> returns true
assertTrue(firstCreateCommand.equals(new CreateCommand(new Tutorial("01"))));

// different types -> returns false
assertFalse(firstCreateCommand.equals(1));

// null -> returns false
assertFalse(firstCreateCommand.equals(null));

// different tutorial number -> returns false
assertFalse(firstCreateCommand.equals(secondCreateCommand));
}
}
108 changes: 108 additions & 0 deletions src/test/java/seedu/address/logic/commands/JoinCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalGroups.GROUP1;
import static seedu.address.testutil.TypicalGroups.GROUP2;
import static seedu.address.testutil.TypicalGroups.GROUP3;
import static seedu.address.testutil.TypicalGroups.GROUP11;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.AMY;
import static seedu.address.testutil.TypicalPersons.BOB;
import static seedu.address.testutil.TypicalPersons.DANIEL;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.testutil.TypicalGroups;

/**
* Contains integration tests (interaction with the Model) and unit tests for
* {@code JoinCommand}.
*/
public class JoinCommandTest {
private Model model;
private Model expectedModel;

@BeforeEach
public void setUp() {
model = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs());
expectedModel = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs());
model.addPerson(AMY);
expectedModel.addPerson(AMY);
}

@Test
public void execute_join_success() {
// person exists, group exists, group is not full, person is not in a group
JoinCommand command = new JoinCommand(AMY.getEmail(), GROUP3.getNumber());
String expectedMessage = String.format(JoinCommand.MESSAGE_JOIN_SUCCESS,
AMY.getName(), GROUP3.getNumber());

expectedModel.addPersonToGroup(AMY, expectedModel.getGroupWithNumber(GROUP3.getNumber()).get());

assertCommandSuccess(command, model, expectedMessage, expectedModel, true);
}

@Test
public void execute_personDoesNotExist_throwsCommandException() {
JoinCommand command = new JoinCommand(BOB.getEmail(), GROUP1.getNumber());
assertCommandFailure(command, model, JoinCommand.MESSAGE_JOIN_EMAIL_NOT_FOUND);
}

@Test
public void execute_groupDoesNotExist_throwsCommandException() {
JoinCommand command = new JoinCommand(AMY.getEmail(), 100);
assertCommandFailure(command, model, JoinCommand.MESSAGE_JOIN_GROUP_NOT_FOUND);
}

@Test
public void execute_personAlreadyInTheGroup_throwsCommandException() {
JoinCommand command = new JoinCommand(ALICE.getEmail(), GROUP1.getNumber());
assertCommandFailure(command, model, JoinCommand.MESSAGE_PERSON_ALREADY_IN_GROUP);
}

@Test
public void execute_personInAnotherGroup_throwsCommandException() {
JoinCommand command = new JoinCommand(DANIEL.getEmail(), GROUP1.getNumber());
assertCommandFailure(command, model, JoinCommand.MESSAGE_PERSON_IN_ANOTHER_GROUP);
}

@Test
public void execute_fullGroup_throwsCommandException() {
JoinCommand command = new JoinCommand(AMY.getEmail(), GROUP11.getNumber());
assertCommandFailure(command, model, JoinCommand.MESSAGE_GROUP_FULL);
}

@Test
public void equals() {
JoinCommand firstJoinCommand = new JoinCommand(AMY.getEmail(), GROUP1.getNumber());
JoinCommand secondJoinCommand = new JoinCommand(AMY.getEmail(), GROUP2.getNumber());
JoinCommand thirdJoinCommand = new JoinCommand(BOB.getEmail(), GROUP1.getNumber());

// same object -> returns true
assertTrue(firstJoinCommand.equals(firstJoinCommand));

// same values -> returns true
JoinCommand firstJoinCommandCopy = new JoinCommand(AMY.getEmail(), GROUP1.getNumber());
assertTrue(firstJoinCommand.equals(firstJoinCommandCopy));

// different types -> returns false
assertFalse(firstJoinCommand.equals(1));

// null -> returns false
assertFalse(firstJoinCommand.equals(null));

// different email -> returns false
assertFalse(firstJoinCommand.equals(thirdJoinCommand));

// different group number -> returns false
assertFalse(firstJoinCommand.equals(secondJoinCommand));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package seedu.address.logic.parser;

import static seedu.address.testutil.Assert.assertThrows;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.CreateCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tutorial.Tutorial;

public class CreateCommandParserTest {

private CreateCommandParser parser = new CreateCommandParser();

@Test
public void parse_validArgs_returnsCreateCommand() {
CommandParserTestUtil.assertParseSuccess(parser, " t/01", new CreateCommand(new Tutorial("01")));
}

@Test
public void parse_missingTutorialPrefix_throwsParseException() {
Assertions.assertThrows(ParseException.class, () -> parser.parse(" 01"));
}

@Test
public void parse_invalidTutorialNumber_throwsParseException() {
assertThrows(ParseException.class, () -> parser.parse(" t/40"));
}

@Test
public void parse_multipleTutorialNumber_throwsParseException() {
assertThrows(ParseException.class, () -> parser.parse(" t/01 t/02"));
}

@Test
public void parse_emptyArgument_throwsParseException() {
Assertions.assertThrows(ParseException.class, () -> parser.parse(""));
}

@Test
public void parse_nullArgument_throwsNullPointerException() {
Assertions.assertThrows(NullPointerException.class, () -> parser.parse(null));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package seedu.address.logic.parser;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP;
import static seedu.address.testutil.Assert.assertThrows;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.JoinCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Email;

public class JoinCommandParserTest {

private JoinCommandParser parser = new JoinCommandParser();

@Test
public void parse_validInput_success() throws ParseException {
String userInput = " " + PREFIX_EMAIL + "[email protected] " + PREFIX_GROUP + "1";
JoinCommand expectedCommand = new JoinCommand(new Email("[email protected]"), 1);
assertEquals(expectedCommand, parser.parse(userInput));
}

@Test
public void parse_missingEmailPrefix_throwsParseException() {
String userInput = " [email protected] " + PREFIX_GROUP + "1";
assertThrows(ParseException.class, () -> parser.parse(userInput));
}

@Test
public void parse_missingGroupPrefix_throwsParseException() {
String userInput = PREFIX_EMAIL + "[email protected] " + "1";
assertThrows(ParseException.class, () -> parser.parse(userInput));
}

@Test
public void parse_invalidEmail_throwsParseException() {
String userInput = PREFIX_EMAIL + "johnd" + " " + PREFIX_GROUP + "1";
assertThrows(ParseException.class, () -> parser.parse(userInput));
}

@Test
public void parse_invalidGroupNumber_throwsParseException() {
String userInput = PREFIX_EMAIL + "[email protected] " + PREFIX_GROUP + "one";
assertThrows(ParseException.class, () -> parser.parse(userInput));
}

@Test
public void parse_missingGroupNumber_throwsParseException() {
String userInput = PREFIX_EMAIL + "[email protected] " + PREFIX_GROUP;
assertThrows(ParseException.class, () -> parser.parse(userInput));
}

@Test
public void parse_missingEmail_throwsParseException() {
String userInput = " " + PREFIX_EMAIL + " " + PREFIX_GROUP + "1";
assertThrows(ParseException.class, () -> parser.parse(userInput));
}

@Test
public void parse_emptyArguments_throwsParseException() {
String userInput = "";
assertThrows(ParseException.class, () -> parser.parse(userInput));
}
}
11 changes: 8 additions & 3 deletions src/test/java/seedu/address/testutil/TypicalGroups.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import seedu.address.model.AddressBook;
import seedu.address.model.group.Group;
import seedu.address.model.group.tasks.TaskList;
import seedu.address.model.person.Person;
import seedu.address.model.tutorial.Tutorial;

/**
Expand All @@ -31,7 +33,7 @@ public class TypicalGroups {
public static final Group GROUP2 = new Group(2, new Tutorial("02"),
Set.of(DANIEL, ELLE, FIONA), new TaskList());
public static final Group GROUP3 = new Group(3, new Tutorial("03"),
Set.of(GEORGE), new TaskList());
new HashSet<>(Set.of(GEORGE)), new TaskList());
public static final Group GROUP4 = new Group(4, new Tutorial("02"),
Set.of(HOON), new TaskList());

Expand Down Expand Up @@ -89,9 +91,12 @@ private TypicalGroups() {} // prevents instantiation
* Returns an {@code AddressBook} with all the typical groups.
*/
public static AddressBook getTypicalAddressBook() {
AddressBook ab = new AddressBook();
AddressBook ab = TypicalPersons.getTypicalAddressBook();
for (Person person : ab.getPersonList()) {
ab.setPerson(person, new PersonBuilder(person).build());
}
for (Group group : getTypicalGroups()) {
ab.addGroup(group);
ab.addGroup(new GroupBuilder(group).build());
}
return ab;
}
Expand Down
Loading