forked from nus-cs2103-AY2122S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from HongshuW/branch-testing
AddClientCommandParserTest class: test orders
- Loading branch information
Showing
1 changed file
with
54 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,29 +4,40 @@ | |
import static org.junit.jupiter.api.Assertions.fail; | ||
import static seedu.address.testutil.Assert.assertThrows; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.AddClientCommand; | ||
import seedu.address.logic.commands.AddProductCommand; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.client.Address; | ||
import seedu.address.model.client.Client; | ||
import seedu.address.model.client.Email; | ||
import seedu.address.model.client.PhoneNumber; | ||
import seedu.address.model.commons.ID; | ||
import seedu.address.model.commons.Name; | ||
import seedu.address.model.order.Order; | ||
import seedu.address.model.product.Quantity; | ||
|
||
public class AddClientCommandParserTest { | ||
private final AddClientCommandParser parser = new AddClientCommandParser(new ModelStub()); | ||
|
||
@Test | ||
public void parse_nullArgs_throwsNullPointerException() { | ||
AddClientCommandParser parser = new AddClientCommandParser(new ModelStub()); | ||
assertThrows(NullPointerException.class, () -> parser.parse(null)); | ||
} | ||
|
||
@Test | ||
public void parse_compulsoryFieldsMissing_throwsParseException() { | ||
AddClientCommandParser parser = new AddClientCommandParser(new ModelStub()); | ||
|
||
// empty args | ||
assertThrows(ParseException.class, () -> parser.parse("")); | ||
assertThrows(ParseException.class, () -> parser.parse(" ")); | ||
|
@@ -47,6 +58,7 @@ public void parse_compulsoryFieldsMissing_throwsParseException() { | |
|
||
@Test | ||
public void parse_invalidAttributes_throwsParseException() { | ||
AddClientCommandParser parser = new AddClientCommandParser(new ModelStub()); | ||
// invalid phone number | ||
assertThrows(ParseException.class, () -> parser.parse("name -pn phone")); | ||
assertThrows(ParseException.class, () -> parser.parse( | ||
|
@@ -57,45 +69,76 @@ public void parse_invalidAttributes_throwsParseException() { | |
assertThrows(ParseException.class, () -> parser.parse("name -pn 43532510 -e @email")); | ||
assertThrows(ParseException.class, () -> parser.parse( | ||
"name -pn 43532510 -e @email -a Singapore")); | ||
|
||
// invalid orders | ||
assertThrows(ParseException.class, () -> parser.parse("name -pn 2342532 -o -o ")); | ||
assertThrows(ParseException.class, () -> parser.parse("name -pn 2342532 -o -100 0 10/27")); | ||
assertThrows(ParseException.class, () -> parser.parse("name -pn 2342532 -o 0 -10 2021/10/27")); | ||
assertThrows(ParseException.class, () -> parser.parse("name -pn 2342532 -o 0 1 2021/15/20")); | ||
assertThrows(ParseException.class, () -> parser.parse("name -pn 2342532 -o 0 1 ")); | ||
} | ||
|
||
@Test | ||
public void parse_allFieldsPresent_success() { | ||
ModelStub model = new ModelStub(); | ||
AddClientCommandParser parser = new AddClientCommandParser(model); | ||
// add a product | ||
String addProductResult = ""; | ||
try { | ||
AddProductCommandParser addProductCommandParser = new AddProductCommandParser(); | ||
AddProductCommand addProductCommand = addProductCommandParser.parse("pen -$ 10 -q 10"); | ||
addProductResult = addProductCommand.execute(model).getFeedbackToUser(); | ||
} catch (ParseException | CommandException e) { | ||
fail(); | ||
} | ||
// information of a client. | ||
Name name = new Name("John Doe"); | ||
PhoneNumber phoneNumber = new PhoneNumber("12345678"); | ||
Email email = new Email("[email protected]"); | ||
Address address = new Address("Singapore"); | ||
Set<Order> orders = new HashSet<>(); | ||
AddClientCommand.AddClientDescriptor descriptor = | ||
new AddClientCommand.AddClientDescriptor(name, phoneNumber); | ||
|
||
// name and phone number are provided | ||
testValidAttributes("John Doe -pn 12345678", descriptor); | ||
testValidAttributes("John Doe -pn 12345678", descriptor, parser, model); | ||
|
||
// name, phone number and email are provided | ||
descriptor.setEmail(email); | ||
testValidAttributes("John Doe -pn 12345678 -e [email protected]", descriptor); | ||
testValidAttributes("John Doe -pn 12345678 -e [email protected]", descriptor, parser, model); | ||
|
||
// name, phone number, email and address are provided | ||
descriptor.setAddress(address); | ||
testValidAttributes("John Doe -pn 12345678 -e [email protected] -a Singapore", descriptor); | ||
testValidAttributes("John Doe -pn 12345678 -e [email protected] -a Singapore", descriptor, parser, model); | ||
|
||
// name, phone number and address are provided | ||
descriptor.setEmail(null); | ||
testValidAttributes("John Doe -pn 12345678 -a Singapore", descriptor); | ||
testValidAttributes("John Doe -pn 12345678 -a Singapore", descriptor, parser, model); | ||
|
||
// name, phone number and orders are provided | ||
descriptor.setAddress(null); | ||
String idString = addProductResult | ||
.substring(addProductResult.indexOf("ID: "), addProductResult.indexOf("; Name")) | ||
.substring(4); | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d"); | ||
Order order = new Order(new ID(Integer.parseInt(idString)), new Quantity("1"), | ||
LocalDate.parse("2021/10/27", formatter)); | ||
orders.add(order); | ||
descriptor.setOrders(orders); | ||
testValidAttributes("John Doe -pn 12345678 -o " + idString + " 1 2021/10/27", descriptor, parser, model); | ||
} | ||
|
||
private void testValidAttributes(String args, AddClientCommand.AddClientDescriptor descriptor) { | ||
private void testValidAttributes(String args, AddClientCommand.AddClientDescriptor descriptor, | ||
AddClientCommandParser parser, Model model) { | ||
try { | ||
CommandResult actualResult = parser.parse(args).execute(new ModelStub()); | ||
CommandResult expectedResult = new AddClientCommand(descriptor).execute(new ModelStub()); | ||
CommandResult actualResult = parser.parse(args).execute(model); | ||
CommandResult expectedResult = new AddClientCommand(descriptor).execute(model); | ||
// compare the feedback to user excluding the id. | ||
String actualString = actualResult.getFeedbackToUser(); | ||
actualString = actualString.substring(actualString.indexOf("Name")); | ||
String expectedString = expectedResult.getFeedbackToUser(); | ||
expectedString = expectedString.substring(expectedString.indexOf("Name")); | ||
assertEquals(expectedString, actualString); | ||
// assertEquals(expectedResult.isShowHelp(), actualResult.isShowHelp()); | ||
// assertEquals(expectedResult.isExit(), actualResult.isExit()); | ||
} catch (ParseException | CommandException e) { | ||
fail(); | ||
} | ||
|