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 Test Coverage for non-trivial paths #540

Merged
merged 5 commits into from
Nov 13, 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
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/delivery/Note.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public boolean equals(Object other) {
}

public static boolean isValid(String test) {
return !test.isEmpty() && test.matches("^[A-Za-z0-9\\s]+$");
return !test.isBlank() && test.matches("^[A-Za-z0-9\\s]+$");
}
}
3 changes: 1 addition & 2 deletions src/main/java/seedu/address/storage/JsonAdaptedCustomer.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ public Customer toModelType() throws IllegalValueException {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
throw new IllegalValueException("Customer ID should only contain numbers, "
+ "and it should be more than 0");
throw new IllegalValueException("ID must be a positive integer and less than 2147483648");
}

return new Customer(modelCustomerId, modelName, modelPhone, modelEmail, modelAddress);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/seedu/address/storage/JsonAdaptedDelivery.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public Delivery toModelType(Optional<ReadOnlyBook<Customer>> customerBook) throw
try {
customerIdInt = Integer.parseInt(customerId);
} catch (NumberFormatException e) {
throw new IllegalValueException("Customer ID should only contain numbers");
throw new IllegalValueException("ID must be a positive integer and less than 2147483648");
}
Optional<Customer> c = customerBook.get().getById(customerIdInt);
if (c.isEmpty()) {
Expand Down Expand Up @@ -167,11 +167,11 @@ public Delivery toModelType(Optional<ReadOnlyBook<Customer>> customerBook) throw
try {
modelDeliveryId = Integer.parseInt(deliveryId);
} catch (NumberFormatException e) {
throw new IllegalValueException("Delivery ID should only contain numbers");
throw new IllegalValueException("ID must be a positive integer and less than 2147483648");
}

if (modelDeliveryId <= 0) {
throw new IllegalValueException("Delivery ID must be an integer more than 0.");
throw new IllegalValueException("ID must be a positive integer and less than 2147483648");
}

return new Delivery(
Expand Down
68 changes: 61 additions & 7 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,26 @@ public void execute_validCommand_success() throws Exception {
}

@Test
public void execute_storageThrowsIoException_throwsCommandException() {
assertCommandFailureForExceptionFromStorage(DUMMY_IO_EXCEPTION, String.format(
public void execute_storageAddressBookThrowsIoException_throwsCommandException() {
assertCommandFailureForExceptionFromAddressBookStorage(DUMMY_IO_EXCEPTION, String.format(
LogicManager.FILE_OPS_ERROR_FORMAT, DUMMY_IO_EXCEPTION.getMessage()));
}

@Test
public void execute_storageThrowsAdException_throwsCommandException() {
assertCommandFailureForExceptionFromStorage(DUMMY_AD_EXCEPTION, String.format(
public void execute_storageAddressBookThrowsAdException_throwsCommandException() {
assertCommandFailureForExceptionFromAddressBookStorage(DUMMY_AD_EXCEPTION, String.format(
LogicManager.FILE_OPS_PERMISSION_ERROR_FORMAT, DUMMY_AD_EXCEPTION.getMessage()));
}

@Test
public void execute_storageDeliveryBookThrowsIoException_throwsCommandException() {
assertCommandFailureForExceptionFromDeliveryBookStorage(DUMMY_IO_EXCEPTION, String.format(
LogicManager.FILE_OPS_ERROR_FORMAT, DUMMY_IO_EXCEPTION.getMessage()));
}

@Test
public void execute_storageDeliveryBookThrowsAdException_throwsCommandException() {
assertCommandFailureForExceptionFromDeliveryBookStorage(DUMMY_AD_EXCEPTION, String.format(
LogicManager.FILE_OPS_PERMISSION_ERROR_FORMAT, DUMMY_AD_EXCEPTION.getMessage()));
}

Expand Down Expand Up @@ -157,12 +169,12 @@ private void assertCommandFailure(String inputCommand, Class<? extends Throwable
}

/**
* Tests the Logic component's handling of an {@code IOException} thrown by the Storage component.
* Tests the Logic component's handling of an {@code IOException} thrown by the Storage component for AddressBook.
*
* @param e the exception to be thrown by the Storage component
* @param e the exception to be thrown by the Storage component for AddressBook
* @param expectedMessage the message expected inside exception thrown by the Logic component
*/
private void assertCommandFailureForExceptionFromStorage(IOException e, String expectedMessage) {
private void assertCommandFailureForExceptionFromAddressBookStorage(IOException e, String expectedMessage) {
Path prefPath = temporaryFolder.resolve("ExceptionUserPrefs.json");

// Inject LogicManager with an AddressBookStorage that throws the IOException e when saving
Expand Down Expand Up @@ -237,4 +249,46 @@ public void getLoginStatus() {

assertEquals(logic.getLoginStatus(), model.getLoginStatus());
}

/**
* Tests the Logic component's handling of an {@code IOException} thrown by the Storage component for DeliveryBook.
*
* @param e the exception to be thrown by the Storage component for DeliveryBook
* @param expectedMessage the message expected inside exception thrown by the Logic component
*/
private void assertCommandFailureForExceptionFromDeliveryBookStorage(IOException e, String expectedMessage) {
Path prefPath = temporaryFolder.resolve("ExceptionUserPrefs.json");


JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(prefPath);

// Inject LogicManager with an DeliveryBookStorage that throws the IOException e when saving
JsonDeliveryBookStorage deliveryBookStorage =
new JsonDeliveryBookStorage(temporaryFolder.resolve("deliveryBook.json")) {
@Override
public void saveBook(ReadOnlyBook<Delivery> deliveryBook, Path filePath) throws IOException {
throw e;
}
};

JsonUserPrefsStorage userPrefsStorage =
new JsonUserPrefsStorage(temporaryFolder.resolve("ExceptionUserPrefs.json"));
StorageManager storage = new StorageManager(addressBookStorage, deliveryBookStorage, userPrefsStorage);

logic = new LogicManager(model, storage);

// Triggers the saveAddressBook method by executing an add command
String addCommand = CustomerAddCommand.COMMAND_WORD + NAME_DESC_AMY + PHONE_DESC_AMY
+ EMAIL_DESC_AMY + ADDRESS_DESC_AMY;

Customer expectedCustomer = new CustomerBuilder(AMY)
.withCustomerId(Customer.getCustomerCount()).build();


ModelManager expectedModel = new ModelManager();
// sets the expected model to be in logged in state
expectedModel.setLoginSuccess();
expectedModel.addCustomer(expectedCustomer);
assertCommandFailure(addCommand, CommandException.class, expectedMessage, expectedModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class CommandTestUtil {

public static final String VALID_NAME_CHIPS = "Chips";
public static final String INVALID_NOTE = "";

public static final String INVALID_NOTE_SPACES = " ";
public static final String INVALID_NOTE_DESC = " " + PREFIX_NOTE + INVALID_NOTE;
public static final String VALID_NOTE = "VALID NOTE";
public static final String VALID_NOTE_DESC = " " + PREFIX_NOTE + VALID_NOTE;
Expand All @@ -72,7 +74,8 @@ public class CommandTestUtil {
public static final String VALID_STATUS_DESC = " " + PREFIX_STATUS + VALID_STATUS_CREATED;
public static final String INVALID_ID_NEGATIVE = "-1";
public static final String INVALID_ID_NAN = "NaN";

public static final String INVALID_ID_ZERO = "0";
public static final String INVALID_ID_MAX_VALUE = "2147483648";
public static final String VALID_DELIVERY_DATE_1 = "2025-12-12";

public static final String VALID_DELIVERY_DATE_2 = "2025-11-11";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.Messages.MESSAGE_USER_NOT_AUTHENTICATED;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showCustomerAtIndex;
Expand All @@ -15,6 +16,8 @@

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.CommandTestUtil;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
Expand Down Expand Up @@ -70,6 +73,17 @@ public void execute_validIndexFilteredList_success() {
assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel, true);
}

@Test
public void execute_notLoggedIn_throwsCommandException() {
Model emptyModel =
new ModelManager(new AddressBook(), getTypicalDeliveryBook(), new UserPrefs(), false);
Index index = Index.fromOneBased(1);
CommandTestUtil.assertCommandFailure(
new CustomerDeleteCommand(index),
emptyModel,
MESSAGE_USER_NOT_AUTHENTICATED);
}


@Test
public void equals() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public void parseCommand_editDelivery() throws Exception {
assertEquals(new DeliveryEditCommand(INDEX_FIRST_PERSON, descriptor), command);
}

@Test
public void parseCommand_deliveryFind() throws Exception {
List<String> keywords = Arrays.asList("foo", "bar", "baz");
DeliveryFindCommand command = (DeliveryFindCommand) parser.parseCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import static seedu.address.logic.commands.CommandTestUtil.EMAIL_DESC_BOB;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ADDRESS_DESC;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_EMAIL_DESC;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ID_MAX_VALUE;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ID_NAN;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ID_NEGATIVE;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ID_ZERO;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_NAME_DESC;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_PHONE_DESC;
import static seedu.address.logic.commands.CommandTestUtil.NAME_DESC_AMY;
Expand Down Expand Up @@ -60,17 +64,23 @@ public void parse_missingParts_failure() {

@Test
public void parse_invalidPreamble_failure() {
// negative index
assertParseFailure(parser, "-5" + NAME_DESC_AMY, MESSAGE_INVALID_FORMAT);
// negative id
assertParseFailure(parser, INVALID_ID_NEGATIVE + NAME_DESC_AMY, MESSAGE_INVALID_FORMAT);

// zero index
assertParseFailure(parser, "0" + NAME_DESC_AMY, MESSAGE_INVALID_FORMAT);
// zero id
assertParseFailure(parser, INVALID_ID_ZERO + NAME_DESC_AMY, MESSAGE_INVALID_FORMAT);

// NaN id
assertParseFailure(parser, INVALID_ID_NAN + NAME_DESC_AMY, MESSAGE_INVALID_FORMAT);

// max value id
assertParseFailure(parser, INVALID_ID_MAX_VALUE + NAME_DESC_AMY, MESSAGE_INVALID_FORMAT);

// invalid arguments being parsed as preamble
assertParseFailure(parser, "1 some random string", MESSAGE_INVALID_FORMAT);

// invalid prefix being parsed as preamble
assertParseFailure(parser, "1 i/ string", MESSAGE_INVALID_FORMAT);
assertParseFailure(parser, "1 --i string", MESSAGE_INVALID_FORMAT);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,22 @@ public void parse_duplicatePrefix_throwsParseException() {
assertParseFailure(parser, "0001 0002",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, CustomerDeleteCommand.MESSAGE_USAGE));
}

@Test
public void parse_integerMaxValue_throwsParseException() {
assertParseFailure(parser, "2147483648",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, CustomerDeleteCommand.MESSAGE_USAGE));
}

@Test
public void parse_zero_throwsParseException() {
assertParseFailure(parser, "0",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, CustomerDeleteCommand.MESSAGE_USAGE));
}

@Test
public void parse_negativeValue_throwsParseException() {
assertParseFailure(parser, "-1",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, CustomerDeleteCommand.MESSAGE_USAGE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.logic.commands.CommandTestUtil.VALID_VIEW_CUSTOMER_ID_1;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.logic.parser.ParserUtil.MESSAGE_INVALID_INDEX;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -32,6 +33,18 @@ public void parse_validArgsExtraSpaceBeforeAfter_returnsCustomerViewCommand() {
new CustomerViewCommand(1));
}

@Test
public void parse_invalidArgsIntegerMaxValue_throwsParseException() {
assertParseFailure(parser, "2147483648",
MESSAGE_INVALID_INDEX);
}

@Test
public void parse_invalidArgsZero_throwsParseException() {
assertParseFailure(parser, "0",
MESSAGE_INVALID_INDEX);
}

@Test
public void parse_invalidArgsNegative_throwsParseException() {
assertParseFailure(parser, "-1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package seedu.address.logic.parser.delivery;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ID_MAX_VALUE;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ID_NAN;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ID_NEGATIVE;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ID_ZERO;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_NOTE;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_NOTE_SPACES;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NOTE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
Expand Down Expand Up @@ -36,19 +41,43 @@ public void parse_invalidNote_failure() {
assertParseFailure(parser, "1 " + PREFIX_NOTE + " " + INVALID_NOTE, Note.MESSAGE_CONSTRAINTS);
}

@Test
public void parse_invalidNoteOnlySpace_failure() {
assertParseFailure(parser, "1 " + PREFIX_NOTE + " " + INVALID_NOTE_SPACES, Note.MESSAGE_CONSTRAINTS);
}

@Test
public void parse_missingNotePrefix_failure() {
assertParseFailure(parser, "1 " + VALID_NOTE, MESSAGE_INVALID_FORMAT);
}

@Test
public void parse_invalidIdNegative_failure() {
assertParseFailure(parser, "-1 " + PREFIX_NOTE + " " + VALID_NOTE, MESSAGE_INVALID_FORMAT);
assertParseFailure(parser,
INVALID_ID_NEGATIVE + " " + PREFIX_NOTE + " " + VALID_NOTE, MESSAGE_INVALID_FORMAT);
}

@Test
public void parse_invalidIdNan_failure() {
assertParseFailure(parser, "NaN " + PREFIX_NOTE + " " + VALID_NOTE, MESSAGE_INVALID_FORMAT);
assertParseFailure(parser,
INVALID_ID_NAN + " " + PREFIX_NOTE + " " + VALID_NOTE, MESSAGE_INVALID_FORMAT);
}

@Test
public void parse_invalidIdZero_failure() {
assertParseFailure(parser,
INVALID_ID_ZERO + " " + PREFIX_NOTE + " " + VALID_NOTE, MESSAGE_INVALID_FORMAT);
}

@Test
public void parse_invalidIdIntegerMaxValue_failure() {
assertParseFailure(parser,
INVALID_ID_MAX_VALUE + " " + PREFIX_NOTE + " " + VALID_NOTE, MESSAGE_INVALID_FORMAT);
}

@Test
public void parse_invalidIdDuplicated_failure() {
assertParseFailure(parser, "1 1 " + PREFIX_NOTE + " " + VALID_NOTE, MESSAGE_INVALID_FORMAT);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package seedu.address.logic.parser.delivery;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ID_MAX_VALUE;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ID_NAN;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ID_NEGATIVE;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_ID_ZERO;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
Expand All @@ -20,9 +24,27 @@ public void parse_validArgs_returnsDeleteCommand() {

@Test
public void parse_invalidArgs_throwsParseException() {
assertParseFailure(parser, "a",
assertParseFailure(parser, INVALID_ID_NAN,
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeliveryDeleteCommand.MESSAGE_USAGE));
}

@Test
public void parse_invalidArgsZero_throwsParseException() {
assertParseFailure(parser, INVALID_ID_ZERO,
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeliveryDeleteCommand.MESSAGE_USAGE));
}

@Test
public void parse_invalidArgsNegative_throwsParseException() {
assertParseFailure(parser, INVALID_ID_NEGATIVE,
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeliveryDeleteCommand.MESSAGE_USAGE));
}

@Test
public void parse_invalidArgIntegerMaxValue_throwsParseException() {
assertParseFailure(parser, INVALID_ID_MAX_VALUE,
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeliveryDeleteCommand.MESSAGE_USAGE));
}
}


Loading
Loading