Skip to content

Commit

Permalink
Fix bug when invalid data is in addressbook.json that causes program …
Browse files Browse the repository at this point in the history
…to crash
  • Loading branch information
guanquann committed Mar 31, 2024
1 parent da71b9a commit 59b1c0d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/storage/JsonAdaptedOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ public Order toModelType() throws IllegalValueException {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, "Remark"));
}

if (!Date.isValidDate(arrivalDate)) {
throw new IllegalValueException(Date.MESSAGE_CONSTRAINTS);
}
final Date modelDate = new Date(arrivalDate);

if (!Remark.isValidRemark(remark)) {
throw new IllegalValueException(Remark.MESSAGE_CONSTRAINTS);
}
final Remark modelRemark = new Remark(remark);

return new Order(modelDate, modelRemark);
Expand Down
18 changes: 16 additions & 2 deletions src/test/java/seedu/address/storage/JsonAdaptedOrderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import seedu.address.model.order.Remark;

public class JsonAdaptedOrderTest {
private static final Order ORDER = new Order(new Date("2020-01-01"), new Remark("100 chicken wings"));

private static final String VALID_DATE = "2020-01-01";
private static final String VALID_REMARK = "100 chicken wings";
private static final String INVALID_DATE = "2020-13-01";
private static final String INVALID_REMARK = "****";

private static final Order ORDER = new Order(new Date(VALID_DATE), new Remark(VALID_REMARK));

@Test
public void toModelType_validOrderDetails_returnsOrder() throws IllegalValueException {
Expand All @@ -29,9 +31,21 @@ public void toModelType_nullDate_throwsIllegalValueException() {
assertThrows(IllegalValueException.class, jsonAdaptedOrder::toModelType);
}

@Test
public void toModelType_invalidDate_throwsIllegalValueException() {
JsonAdaptedOrder jsonAdaptedOrder = new JsonAdaptedOrder(INVALID_DATE, VALID_REMARK);
assertThrows(IllegalValueException.class, jsonAdaptedOrder::toModelType);
}

@Test
public void toModelType_nullRemark_throwsIllegalValueException() {
JsonAdaptedOrder jsonAdaptedOrder = new JsonAdaptedOrder(VALID_DATE, null);
assertThrows(IllegalValueException.class, jsonAdaptedOrder::toModelType);
}

@Test
public void toModelType_invalidRemark_throwsIllegalValueException() {
JsonAdaptedOrder jsonAdaptedOrder = new JsonAdaptedOrder(VALID_DATE, INVALID_REMARK);
assertThrows(IllegalValueException.class, jsonAdaptedOrder::toModelType);
}
}

0 comments on commit 59b1c0d

Please sign in to comment.