From 9aae2956adc404cbfee7527345ca21fb16df715a Mon Sep 17 00:00:00 2001 From: gohyongjing Date: Tue, 7 Mar 2023 22:52:02 +0800 Subject: [PATCH 1/5] Add tests for JsonPairStorage --- src/main/java/seedu/address/MainApp.java | 2 +- .../seedu/address/commons/core/Messages.java | 7 +++ .../logic/commands/AddPairCommand.java | 49 +++++++------------ .../logic/commands/DeletePairCommand.java | 13 ++--- .../seedu/address/model/FriendlyLink.java | 16 +++++- src/main/java/seedu/address/model/Model.java | 19 ++++--- .../seedu/address/model/ModelManager.java | 18 ++++--- .../address/storage/pair/JsonAdaptedPair.java | 22 +++++++-- .../logic/commands/AddPairCommandTest.java | 27 +++++++++- .../logic/commands/CommandTestUtil.java | 12 ++--- .../logic/commands/DeletePairTest.java | 6 ++- .../commands/EditElderlyCommandTest.java | 6 +-- .../commands/EditElderlyDescriptorTest.java | 4 +- .../address/logic/commands/ModelStub.java | 5 ++ .../parser/AddElderlyCommandParserTest.java | 6 +-- .../parser/EditElderlyCommandParserTest.java | 8 +-- .../seedu/address/model/pair/PairTest.java | 10 ++-- .../address/model/person/ElderlyTest.java | 8 +-- .../address/storage/JsonPairStorageTest.java | 44 +++++++++++++++-- .../address/testutil/FriendlyLinkBuilder.java | 41 ++++++++++++++++ .../java/seedu/address/testutil/TestUtil.java | 16 ++---- .../address/testutil/TypicalElderly.java | 8 +-- .../seedu/address/testutil/TypicalPairs.java | 16 +++--- 23 files changed, 246 insertions(+), 117 deletions(-) diff --git a/src/main/java/seedu/address/MainApp.java b/src/main/java/seedu/address/MainApp.java index 8f3435a35ca..d7aacc2ca76 100644 --- a/src/main/java/seedu/address/MainApp.java +++ b/src/main/java/seedu/address/MainApp.java @@ -85,9 +85,9 @@ private void initializeAppManagers( private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) { FriendlyLink applicationCache = new FriendlyLink(); try { - storage.readPair(applicationCache); storage.readElderly(applicationCache); storage.readVolunteer(applicationCache); + storage.readPair(applicationCache); } catch (DataConversionException e) { logger.warning("Data file not in the correct format. Will be starting with an empty FriendlyLink"); } catch (IOException e) { diff --git a/src/main/java/seedu/address/commons/core/Messages.java b/src/main/java/seedu/address/commons/core/Messages.java index 9fcaa73a7a5..5566443746c 100644 --- a/src/main/java/seedu/address/commons/core/Messages.java +++ b/src/main/java/seedu/address/commons/core/Messages.java @@ -15,5 +15,12 @@ public class Messages { public static final String MESSAGE_DUPLICATE_ELDERLY = "This elderly already exists in the database"; public static final String MESSAGE_DUPLICATE_VOLUNTEER = "This volunteer already exists in the database"; public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; + public static final String MESSAGE_ELDERLY_NOT_FOUND = + "The elderly with NRIC %1$s does not exist in FriendlyLink"; + public static final String MESSAGE_VOLUNTEER_NOT_FOUND = + "The volunteer with NRIC %1$s does not exist in FriendlyLink"; + public static final String MESSAGE_PAIR_NOT_FOUND = + "The pair consisting of elderly with NRIC %1$s and volunteer with NRIC %2$s" + + " does not exist in FriendlyLink"; } diff --git a/src/main/java/seedu/address/logic/commands/AddPairCommand.java b/src/main/java/seedu/address/logic/commands/AddPairCommand.java index 4a89e50c851..7acdaaa7a43 100644 --- a/src/main/java/seedu/address/logic/commands/AddPairCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddPairCommand.java @@ -1,16 +1,17 @@ package seedu.address.logic.commands; import static java.util.Objects.requireNonNull; +import static seedu.address.commons.core.Messages.MESSAGE_ELDERLY_NOT_FOUND; +import static seedu.address.commons.core.Messages.MESSAGE_VOLUNTEER_NOT_FOUND; import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC_ELDERLY; import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC_VOLUNTEER; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; -import seedu.address.model.pair.Pair; -import seedu.address.model.person.Elderly; -import seedu.address.model.person.Volunteer; -import seedu.address.model.person.exceptions.PersonNotFoundException; +import seedu.address.model.pair.exceptions.DuplicatePairException; +import seedu.address.model.person.exceptions.ElderlyNotFoundException; +import seedu.address.model.person.exceptions.VolunteerNotFoundException; import seedu.address.model.person.information.Nric; /** @@ -28,15 +29,11 @@ public class AddPairCommand extends Command { + PREFIX_NRIC_ELDERLY + "s02133334I " + PREFIX_NRIC_VOLUNTEER + "T2245343a "; - public static final String MESSAGE_SUCCESS = "New pair added: %1$s"; + public static final String MESSAGE_ADD_PAIR_SUCCESS = "New pair consisting of elderly with NRIC %1$s" + + " and volunteer with NRIC %2$s added"; - public static final String MESSAGE_DUPLICATE_PAIR = "This pair already exists in FriendlyLink"; - - public static final String MESSAGE_ELDERLY_NOT_FOUND = - "The elderly with the specified NRIC does not exist in FriendlyLink"; - - public static final String MESSAGE_VOLUNTEER_NOT_FOUND = - "The volunteer with the specified NRIC does not exist in FriendlyLink"; + public static final String MESSAGE_DUPLICATE_PAIR = "This pair consisting of elderly with NRIC %1$s" + + " and volunteer with NRIC %2$s already exists in FriendlyLink"; private final Nric elderlyNric; private final Nric volunteerNric; @@ -53,28 +50,16 @@ public AddPairCommand(Nric elderlyNric, Nric volunteerNric) { @Override public CommandResult execute(Model model) throws CommandException { requireNonNull(model); - Elderly elderly; - Volunteer volunteer; - try { - elderly = model.getElderly(elderlyNric); - } catch (PersonNotFoundException e) { - throw new CommandException(MESSAGE_ELDERLY_NOT_FOUND); + model.addPair(elderlyNric, volunteerNric); + return new CommandResult(String.format(MESSAGE_ADD_PAIR_SUCCESS, elderlyNric, volunteerNric)); + } catch (ElderlyNotFoundException e) { + throw new CommandException(String.format(MESSAGE_ELDERLY_NOT_FOUND, elderlyNric)); + } catch (VolunteerNotFoundException e) { + throw new CommandException(String.format(MESSAGE_VOLUNTEER_NOT_FOUND, volunteerNric)); + } catch (DuplicatePairException e) { + throw new CommandException(String.format(MESSAGE_DUPLICATE_PAIR, elderlyNric, volunteerNric)); } - - try { - volunteer = model.getVolunteer(volunteerNric); - } catch (PersonNotFoundException e) { - throw new CommandException(MESSAGE_VOLUNTEER_NOT_FOUND); - } - - Pair toAdd = new Pair(elderly, volunteer); - if (model.hasPair(toAdd)) { - throw new CommandException(MESSAGE_DUPLICATE_PAIR); - } - - model.addPair(toAdd); - return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd)); } @Override diff --git a/src/main/java/seedu/address/logic/commands/DeletePairCommand.java b/src/main/java/seedu/address/logic/commands/DeletePairCommand.java index 1648e273093..7f159a3495f 100644 --- a/src/main/java/seedu/address/logic/commands/DeletePairCommand.java +++ b/src/main/java/seedu/address/logic/commands/DeletePairCommand.java @@ -1,6 +1,9 @@ package seedu.address.logic.commands; import static java.util.Objects.requireNonNull; +import static seedu.address.commons.core.Messages.MESSAGE_ELDERLY_NOT_FOUND; +import static seedu.address.commons.core.Messages.MESSAGE_PAIR_NOT_FOUND; +import static seedu.address.commons.core.Messages.MESSAGE_VOLUNTEER_NOT_FOUND; import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC_ELDERLY; import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC_VOLUNTEER; @@ -32,16 +35,6 @@ public class DeletePairCommand extends Command { public static final String MESSAGE_DELETE_PAIR_SUCCESS = "Deleted Pair consisting of" + " elderly with NRIC %1$s and volunteer with NRIC %2$s"; - public static final String MESSAGE_ELDERLY_NOT_FOUND = - "The elderly with NRIC %1$s does not exist in FriendlyLink"; - - public static final String MESSAGE_VOLUNTEER_NOT_FOUND = - "The volunteer with NRIC %1$s does not exist in FriendlyLink"; - - public static final String MESSAGE_PAIR_NOT_FOUND = - "The pair consisting of elderly with NRIC %1$s and volunteer with NRIC %2$s" - + " does not exist in FriendlyLink"; - private final Nric elderlyNric; private final Nric volunteerNric; diff --git a/src/main/java/seedu/address/model/FriendlyLink.java b/src/main/java/seedu/address/model/FriendlyLink.java index 5f8f2096739..d741091df2e 100644 --- a/src/main/java/seedu/address/model/FriendlyLink.java +++ b/src/main/java/seedu/address/model/FriendlyLink.java @@ -180,7 +180,7 @@ public void setVolunteer(Volunteer target, Volunteer editedPerson) { /** * Removes {@code key} from {@code FriendlyLink}. - * {@code key} must exist in the elderly's list. + * {@code key} must exist in the elderly list. */ public void removeElderly(Elderly key) { elderly.remove(key); @@ -208,7 +208,7 @@ public boolean hasPair(Pair pair) { } /** - * Adds a {@pair} to {@code FriendlyLink}. + * Adds a {code @pair} to {@code FriendlyLink}. * The pair must not already exist in {@code FriendlyLink}. * * @param pair Pair to add into FriendlyLink. @@ -217,6 +217,18 @@ public void addPair(Pair pair) { pairs.add(pair); } + /** + * Adds a pair consisting of elderly with {@code elderlyNric} and volunteer with {@code volunteerNric} + * from {@code FriendlyLink}. + * The pair must not already exist in {@code FriendlyLink}. + * + * @param elderlyNric Nric of elderly. + * @param volunteerNric Nric of volunteer. + */ + public void addPair(Nric elderlyNric, Nric volunteerNric) { + pairs.add(new Pair(getElderly(elderlyNric), getVolunteer(volunteerNric))); + } + /** * Replaces the given pair {@code target} in the list with {@code editedPair}. * {@code target} must exist in FriendlyLink. diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 0e99e655cc5..2406bfedee8 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -123,6 +123,19 @@ public interface Model { */ void setVolunteer(Volunteer target, Volunteer editedVolunteer); + /** + * Adds the given pair. + * {@code pair} must not already exist in the address book. + */ + void addPair(Pair pair); + + /** + * Adds the given pair. + * The pair consisting of elderly with {@code elderlyNric} and volunteer with {@code volunteerNric} + * must not already exist in the address book. + */ + void addPair(Nric elderlyNric, Nric volunteerNric); + /** * Returns true if a pair with the same identity as {@code pair} exists in the address book. */ @@ -141,12 +154,6 @@ public interface Model { */ void deletePair(Nric elderlyNric, Nric volunteerNric); - /** - * Adds the given pair. - * {@code pair} must not already exist in the address book. - */ - void addPair(Pair pair); - /** * Replaces the given pair {@code target} with {@code editedPair}. * {@code target} must exist in the address book. diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 25e28f6470e..caf4c3ef4c8 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -163,13 +163,9 @@ public boolean hasPair(Pair pair) { } @Override - public void deletePair(Pair target) { - friendlyLink.removePair(target); - } - - @Override - public void deletePair(Nric elderlyNric, Nric volunteerNric) { + public void addPair(Nric elderlyNric, Nric volunteerNric) { friendlyLink.removePair(elderlyNric, volunteerNric); + // TODO: implement updateFilteredPersonList(PREDICATE_SHOW_ALL_PAIRS); } @Override @@ -178,6 +174,16 @@ public void addPair(Pair pair) { // TODO: implement updateFilteredPersonList(PREDICATE_SHOW_ALL_PAIRS); } + @Override + public void deletePair(Pair target) { + friendlyLink.removePair(target); + } + + @Override + public void deletePair(Nric elderlyNric, Nric volunteerNric) { + friendlyLink.removePair(elderlyNric, volunteerNric); + } + @Override public void setPair(Pair target, Pair editedPair) { requireAllNonNull(target, editedPair); diff --git a/src/main/java/seedu/address/storage/pair/JsonAdaptedPair.java b/src/main/java/seedu/address/storage/pair/JsonAdaptedPair.java index ee9e46670c0..cca16f442e4 100644 --- a/src/main/java/seedu/address/storage/pair/JsonAdaptedPair.java +++ b/src/main/java/seedu/address/storage/pair/JsonAdaptedPair.java @@ -1,5 +1,8 @@ package seedu.address.storage.pair; +import static seedu.address.commons.core.Messages.MESSAGE_ELDERLY_NOT_FOUND; +import static seedu.address.commons.core.Messages.MESSAGE_VOLUNTEER_NOT_FOUND; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; @@ -8,6 +11,8 @@ import seedu.address.model.pair.Pair; import seedu.address.model.person.Elderly; import seedu.address.model.person.Volunteer; +import seedu.address.model.person.exceptions.ElderlyNotFoundException; +import seedu.address.model.person.exceptions.VolunteerNotFoundException; import seedu.address.model.person.information.Nric; import seedu.address.storage.JsonSerializable; @@ -16,7 +21,7 @@ */ class JsonAdaptedPair implements JsonSerializable { - public static final String MISSING_ELDERLY_FIELD_MESSAGE_FORMAT = "Elderly's %s field is missing!"; + public static final String MISSING_ELDERLY_FIELD_MESSAGE_FORMAT = "Elderly member's %s field is missing!"; public static final String MISSING_VOLUNTEER_FIELD_MESSAGE_FORMAT = "Volunteer's %s field is missing!"; private final String elderlyNric; @@ -57,12 +62,19 @@ public Pair toModelType(FriendlyLink friendlyLink) throws IllegalValueException if (!(Nric.isValidNric(elderlyNric) && Nric.isValidNric(volunteerNric))) { throw new IllegalValueException(Nric.MESSAGE_CONSTRAINTS); } - final Nric modelElderlyNric = new Nric(elderlyNric); - Elderly elderly = friendlyLink.getElderly(modelElderlyNric); + final Nric modelElderlyNric = new Nric(elderlyNric); final Nric modelVolunteerNric = new Nric(volunteerNric); - Volunteer volunteer = friendlyLink.getVolunteer(modelVolunteerNric); - return new Pair(elderly, volunteer); + try { + Elderly elderly = friendlyLink.getElderly(modelElderlyNric); + Volunteer volunteer = friendlyLink.getVolunteer(modelVolunteerNric); + return new Pair(elderly, volunteer); + } catch (ElderlyNotFoundException e) { + throw new IllegalValueException(String.format(MESSAGE_ELDERLY_NOT_FOUND, modelElderlyNric)); + } catch (VolunteerNotFoundException e) { + throw new IllegalValueException(String.format(MESSAGE_VOLUNTEER_NOT_FOUND, modelVolunteerNric)); + } + } } diff --git a/src/test/java/seedu/address/logic/commands/AddPairCommandTest.java b/src/test/java/seedu/address/logic/commands/AddPairCommandTest.java index 61a1a8fce5f..704fb618da4 100644 --- a/src/test/java/seedu/address/logic/commands/AddPairCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddPairCommandTest.java @@ -14,6 +14,7 @@ import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.FriendlyLink; import seedu.address.model.pair.Pair; +import seedu.address.model.pair.exceptions.DuplicatePairException; import seedu.address.model.person.Elderly; import seedu.address.model.person.Volunteer; import seedu.address.model.person.information.Nric; @@ -42,7 +43,9 @@ public void execute_pairAcceptedByModel_addSuccessful() throws Exception { Volunteer volunteer = validPair.getVolunteer(); ModelStubAcceptingPairAdded modelStub = new ModelStubAcceptingPairAdded(elderly, volunteer); CommandResult commandResult = new AddPairCommand(elderly.getNric(), volunteer.getNric()).execute(modelStub); - assertEquals(String.format(AddPairCommand.MESSAGE_SUCCESS, validPair), commandResult.getFeedbackToUser()); + String expectedMessage = String.format(AddPairCommand.MESSAGE_ADD_PAIR_SUCCESS, + elderly.getNric(), volunteer.getNric()); + assertEquals(expectedMessage, commandResult.getFeedbackToUser()); assertEquals(validPair, modelStub.pair); } @@ -54,8 +57,9 @@ public void execute_duplicatePair_throwsCommandException() { Nric volunteerNric = validPair.getVolunteer().getNric(); AddPairCommand addPairCommand = new AddPairCommand(elderlyNric, volunteerNric); + String expectedMessage = String.format(AddPairCommand.MESSAGE_DUPLICATE_PAIR, elderlyNric, volunteerNric); assertThrows(CommandException.class, - AddPairCommand.MESSAGE_DUPLICATE_PAIR, () -> addPairCommand.execute(modelStub)); + expectedMessage, () -> addPairCommand.execute(modelStub)); } @Test @@ -106,6 +110,15 @@ public boolean hasPair(Pair pair) { requireNonNull(pair); return this.pair.isSamePair(pair); } + + @Override + public void addPair(Nric elderlyNric, Nric volunteerNric) { + if (pair.getElderly().getNric().equals(elderlyNric) + && pair.getVolunteer().getNric().equals(volunteerNric)) { + throw new DuplicatePairException(); + } + } + } /** @@ -134,6 +147,16 @@ public void addPair(Pair pair) { this.pair = pair; } + @Override + public void addPair(Nric elderlyNric, Nric volunteerNric) { + if (pair != null + && pair.getElderly().getNric().equals(elderlyNric) + && pair.getVolunteer().getNric().equals(volunteerNric)) { + throw new DuplicatePairException(); + } + this.pair = new Pair(getElderly(elderlyNric), getVolunteer(volunteerNric)); + } + @Override public Elderly getElderly(Nric nric) { return elderly; diff --git a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java index 24df2113760..f385d57f382 100644 --- a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java +++ b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java @@ -52,8 +52,8 @@ public class CommandTestUtil { public static final String VALID_AGE_AMY = "68"; public static final String VALID_AGE_BOB = "85"; - public static final String VALID_RISKLEVEL_AMY = "LOW"; - public static final String VALID_RISKLEVEL_BOB = "MEDIUM"; + public static final String VALID_RISK_LEVEL_AMY = "LOW"; + public static final String VALID_RISK_LEVEL_BOB = "MEDIUM"; public static final String VALID_TAG_SINGLE = "single"; public static final String VALID_TAG_STRONG = "strong"; @@ -72,8 +72,8 @@ public class CommandTestUtil { public static final String AGE_DESC_AMY = " " + PREFIX_AGE + VALID_AGE_AMY; public static final String AGE_DESC_BOB = " " + PREFIX_AGE + VALID_AGE_BOB; - public static final String RISK_DESC_AMY = " " + PREFIX_RISK + VALID_RISKLEVEL_AMY; - public static final String RISK_DESC_BOB = " " + PREFIX_RISK + VALID_RISKLEVEL_BOB; + public static final String RISK_DESC_AMY = " " + PREFIX_RISK + VALID_RISK_LEVEL_AMY; + public static final String RISK_DESC_BOB = " " + PREFIX_RISK + VALID_RISK_LEVEL_BOB; public static final String TAG_DESC_SINGLE = " " + PREFIX_TAG + VALID_TAG_SINGLE; public static final String TAG_DESC_STRONG = " " + PREFIX_TAG + VALID_TAG_STRONG; @@ -106,12 +106,12 @@ public class CommandTestUtil { static { DESC_ELDERLY_AMY = new EditElderlyDescriptorBuilder().withName(VALID_NAME_AMY) .withPhone(VALID_PHONE_AMY).withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY) - .withNric(VALID_NRIC_AMY).withAge(VALID_AGE_AMY).withRiskLevel(VALID_RISKLEVEL_AMY) + .withNric(VALID_NRIC_AMY).withAge(VALID_AGE_AMY).withRiskLevel(VALID_RISK_LEVEL_AMY) .withTags(VALID_TAG_STRONG).build(); DESC_ELDERLY_BOB = new EditElderlyDescriptorBuilder().withName(VALID_NAME_BOB) .withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_BOB).withAddress(VALID_ADDRESS_BOB) - .withNric(VALID_NRIC_BOB).withAge(VALID_AGE_BOB).withRiskLevel(VALID_RISKLEVEL_BOB) + .withNric(VALID_NRIC_BOB).withAge(VALID_AGE_BOB).withRiskLevel(VALID_RISK_LEVEL_BOB) .withTags(VALID_TAG_SINGLE, VALID_TAG_STRONG).build(); DESC_VOLUNTEER_AMY = new EditVolunteerDescriptorBuilder().withName(VALID_NAME_AMY) diff --git a/src/test/java/seedu/address/logic/commands/DeletePairTest.java b/src/test/java/seedu/address/logic/commands/DeletePairTest.java index 383822c5144..0c11012b37a 100644 --- a/src/test/java/seedu/address/logic/commands/DeletePairTest.java +++ b/src/test/java/seedu/address/logic/commands/DeletePairTest.java @@ -5,6 +5,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.commons.core.Messages.MESSAGE_ELDERLY_NOT_FOUND; +import static seedu.address.commons.core.Messages.MESSAGE_VOLUNTEER_NOT_FOUND; import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalElderly.ALICE; @@ -54,14 +56,14 @@ public void execute_invalidPerson_failure() throws Exception { //Invalid Elderly final DeletePairCommand deletePairCommand1 = new DeletePairCommand(AMY.getNric(), BOB.getNric()); String expectedMessage = - String.format(DeletePairCommand.MESSAGE_ELDERLY_NOT_FOUND, AMY.getNric()); + String.format(MESSAGE_ELDERLY_NOT_FOUND, AMY.getNric()); assertThrows(CommandException.class, expectedMessage, () -> deletePairCommand1.execute(model)); assertTrue(model.hasPair(pair)); //Invalid Volunteer expectedMessage = - String.format(DeletePairCommand.MESSAGE_VOLUNTEER_NOT_FOUND, BENSON.getNric()); + String.format(MESSAGE_VOLUNTEER_NOT_FOUND, BENSON.getNric()); final DeletePairCommand deletePairCommand2 = new DeletePairCommand(ALICE.getNric(), BENSON.getNric()); assertThrows(CommandException.class, expectedMessage, () -> deletePairCommand2.execute(model)); diff --git a/src/test/java/seedu/address/logic/commands/EditElderlyCommandTest.java b/src/test/java/seedu/address/logic/commands/EditElderlyCommandTest.java index 2fe8c99c07a..ae0dcb5d007 100644 --- a/src/test/java/seedu/address/logic/commands/EditElderlyCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/EditElderlyCommandTest.java @@ -7,7 +7,7 @@ import static seedu.address.logic.commands.CommandTestUtil.DESC_ELDERLY_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_RISKLEVEL_BOB; +import static seedu.address.logic.commands.CommandTestUtil.VALID_RISK_LEVEL_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_SINGLE; import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; @@ -58,11 +58,11 @@ public void execute_someFieldsSpecifiedUnfilteredElderlyList_success() { ElderlyBuilder elderlyInList = new ElderlyBuilder(lastElderly); Elderly editedElderly = elderlyInList.withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB) - .withRiskLevel(VALID_RISKLEVEL_BOB).withTags(VALID_TAG_SINGLE).build(); + .withRiskLevel(VALID_RISK_LEVEL_BOB).withTags(VALID_TAG_SINGLE).build(); EditElderlyDescriptor descriptor = new EditElderlyDescriptorBuilder().withName(VALID_NAME_BOB) .withPhone(VALID_PHONE_BOB) - .withRiskLevel(VALID_RISKLEVEL_BOB) + .withRiskLevel(VALID_RISK_LEVEL_BOB) .withTags(VALID_TAG_SINGLE).build(); EditElderlyCommand editElderlyCommand = new EditElderlyCommand(indexLastElderly, descriptor); diff --git a/src/test/java/seedu/address/logic/commands/EditElderlyDescriptorTest.java b/src/test/java/seedu/address/logic/commands/EditElderlyDescriptorTest.java index 513b8c8449f..3da867666f5 100644 --- a/src/test/java/seedu/address/logic/commands/EditElderlyDescriptorTest.java +++ b/src/test/java/seedu/address/logic/commands/EditElderlyDescriptorTest.java @@ -10,7 +10,7 @@ import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_NRIC_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_RISKLEVEL_BOB; +import static seedu.address.logic.commands.CommandTestUtil.VALID_RISK_LEVEL_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_SINGLE; import org.junit.jupiter.api.Test; @@ -69,7 +69,7 @@ public void equals() { // different risk level -> returns false editedAmy = new EditElderlyDescriptorBuilder(DESC_ELDERLY_AMY) - .withRiskLevel(VALID_RISKLEVEL_BOB).build(); + .withRiskLevel(VALID_RISK_LEVEL_BOB).build(); assertFalse(DESC_ELDERLY_AMY.equals(editedAmy)); // different tags -> returns false diff --git a/src/test/java/seedu/address/logic/commands/ModelStub.java b/src/test/java/seedu/address/logic/commands/ModelStub.java index 24c78fe7edb..5631426e1d4 100644 --- a/src/test/java/seedu/address/logic/commands/ModelStub.java +++ b/src/test/java/seedu/address/logic/commands/ModelStub.java @@ -62,6 +62,11 @@ public void addPair(Pair pair) { throw new AssertionError("This method should not be called."); } + @Override + public void addPair(Nric elderlyNric, Nric volunteerNric) { + throw new AssertionError("This method should not be called."); + } + @Override public boolean hasPair(Pair pair) { throw new AssertionError("This method should not be called."); diff --git a/src/test/java/seedu/address/logic/parser/AddElderlyCommandParserTest.java b/src/test/java/seedu/address/logic/parser/AddElderlyCommandParserTest.java index 71c1c226266..5cf06c02cdf 100644 --- a/src/test/java/seedu/address/logic/parser/AddElderlyCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddElderlyCommandParserTest.java @@ -33,7 +33,7 @@ import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_NRIC_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_RISKLEVEL_BOB; +import static seedu.address.logic.commands.CommandTestUtil.VALID_RISK_LEVEL_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_SINGLE; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_STRONG; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; @@ -149,11 +149,11 @@ public void parse_compulsoryFieldMissing_failure() { // missing risk level prefix assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB - + NRIC_ELDERLY_DESC_BOB + AGE_DESC_BOB + VALID_RISKLEVEL_BOB, expectedMessage); + + NRIC_ELDERLY_DESC_BOB + AGE_DESC_BOB + VALID_RISK_LEVEL_BOB, expectedMessage); // all prefixes missing assertParseFailure(parser, VALID_NAME_BOB + VALID_PHONE_BOB + VALID_EMAIL_BOB + VALID_ADDRESS_BOB - + VALID_NRIC_BOB + VALID_AGE_BOB + VALID_RISKLEVEL_BOB, expectedMessage); + + VALID_NRIC_BOB + VALID_AGE_BOB + VALID_RISK_LEVEL_BOB, expectedMessage); } @Test diff --git a/src/test/java/seedu/address/logic/parser/EditElderlyCommandParserTest.java b/src/test/java/seedu/address/logic/parser/EditElderlyCommandParserTest.java index 2fa7e1e230f..52c29a58e86 100644 --- a/src/test/java/seedu/address/logic/parser/EditElderlyCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/EditElderlyCommandParserTest.java @@ -34,7 +34,7 @@ import static seedu.address.logic.commands.CommandTestUtil.VALID_NRIC_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_AMY; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_RISKLEVEL_AMY; +import static seedu.address.logic.commands.CommandTestUtil.VALID_RISK_LEVEL_AMY; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_SINGLE; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_STRONG; import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; @@ -123,7 +123,7 @@ public void parse_invalidValue_failure() { // multiple invalid values, but only the first invalid value is captured assertParseFailure(parser, "1" + INVALID_NAME_DESC + INVALID_EMAIL_DESC + VALID_ADDRESS_AMY + VALID_PHONE_AMY + VALID_NRIC_AMY + VALID_AGE_AMY - + VALID_RISKLEVEL_AMY, Name.MESSAGE_CONSTRAINTS); + + VALID_RISK_LEVEL_AMY, Name.MESSAGE_CONSTRAINTS); } @Test @@ -136,7 +136,7 @@ public void parse_allFieldsSpecified_success() { EditElderlyDescriptor descriptor = new EditElderlyDescriptorBuilder() .withName(VALID_NAME_AMY).withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_AMY) .withAddress(VALID_ADDRESS_AMY).withNric(VALID_NRIC_AMY).withAge(VALID_AGE_AMY) - .withRiskLevel(VALID_RISKLEVEL_AMY).withTags(VALID_TAG_SINGLE, VALID_TAG_STRONG).build(); + .withRiskLevel(VALID_RISK_LEVEL_AMY).withTags(VALID_TAG_SINGLE, VALID_TAG_STRONG).build(); EditElderlyCommand expectedCommand = new EditElderlyCommand(targetIndex, descriptor); @@ -196,7 +196,7 @@ public void parse_oneFieldSpecified_success() { // risk level userInput = targetIndex.getOneBased() + RISK_DESC_AMY; - descriptor = new EditElderlyDescriptorBuilder().withRiskLevel(VALID_RISKLEVEL_AMY).build(); + descriptor = new EditElderlyDescriptorBuilder().withRiskLevel(VALID_RISK_LEVEL_AMY).build(); expectedCommand = new EditElderlyCommand(targetIndex, descriptor); assertParseSuccess(parser, userInput, expectedCommand); diff --git a/src/test/java/seedu/address/model/pair/PairTest.java b/src/test/java/seedu/address/model/pair/PairTest.java index be6eb7b5971..099a3c2fb70 100644 --- a/src/test/java/seedu/address/model/pair/PairTest.java +++ b/src/test/java/seedu/address/model/pair/PairTest.java @@ -9,7 +9,7 @@ import static seedu.address.logic.commands.CommandTestUtil.VALID_NRIC_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_SINGLE; -import static seedu.address.testutil.TypicalElderly.AMY; +import static seedu.address.testutil.TypicalElderly.ALICE; import static seedu.address.testutil.TypicalElderly.CHARLIE; import static seedu.address.testutil.TypicalPairs.PAIR1; import static seedu.address.testutil.TypicalPairs.PAIR2; @@ -32,15 +32,15 @@ public void isSamePair() { assertFalse(PAIR1.isSamePair(null)); // same nric, all other attributes different -> returns true - Elderly editedAmy = new ElderlyBuilder(AMY).withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_BOB) + Elderly editedAlice = new ElderlyBuilder(ALICE).withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_BOB) .withAddress(VALID_ADDRESS_BOB).withTags(VALID_TAG_SINGLE).build(); - Pair editedPair1 = new PairBuilder(PAIR1).withElderly(editedAmy).build(); + Pair editedPair1 = new PairBuilder(PAIR1).withElderly(editedAlice).build(); assertTrue(PAIR1.isSamePair(editedPair1)); // TODO: check that a different name returns true if nric is the same. // different nric, all other attributes same -> returns false - editedAmy = new ElderlyBuilder(AMY).withNric(VALID_NRIC_BOB).build(); - editedPair1 = new PairBuilder(PAIR1).withElderly(editedAmy).build(); + editedAlice = new ElderlyBuilder(ALICE).withNric(VALID_NRIC_BOB).build(); + editedPair1 = new PairBuilder(PAIR1).withElderly(editedAlice).build(); assertFalse(PAIR1.isSamePair(editedPair1)); } diff --git a/src/test/java/seedu/address/model/person/ElderlyTest.java b/src/test/java/seedu/address/model/person/ElderlyTest.java index 606cd447a1b..3828665ec63 100644 --- a/src/test/java/seedu/address/model/person/ElderlyTest.java +++ b/src/test/java/seedu/address/model/person/ElderlyTest.java @@ -11,7 +11,7 @@ import static seedu.address.logic.commands.CommandTestUtil.VALID_NRIC_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_AMY; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_RISKLEVEL_BOB; +import static seedu.address.logic.commands.CommandTestUtil.VALID_RISK_LEVEL_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_SINGLE; import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalElderly.ALICE; @@ -39,14 +39,14 @@ public void isSamePerson() { // same name, same nric, all other attributes different -> returns true Elderly editedAlice = new ElderlyBuilder(ALICE).withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_BOB) - .withAddress(VALID_ADDRESS_BOB).withAge(VALID_AGE_BOB).withRiskLevel(VALID_RISKLEVEL_BOB) + .withAddress(VALID_ADDRESS_BOB).withAge(VALID_AGE_BOB).withRiskLevel(VALID_RISK_LEVEL_BOB) .withTags(VALID_TAG_SINGLE).build(); assertTrue(ALICE.isSamePerson(editedAlice)); // same name, different nric, all other attributes different -> returns false editedAlice = new ElderlyBuilder(ALICE).withPhone(VALID_PHONE_AMY).withEmail(VALID_EMAIL_BOB) .withAddress(VALID_ADDRESS_BOB).withTags(VALID_TAG_SINGLE).withNric(VALID_NRIC_BOB) - .withRiskLevel(VALID_RISKLEVEL_BOB).withAge(VALID_AGE_BOB).build(); + .withRiskLevel(VALID_RISK_LEVEL_BOB).withAge(VALID_AGE_BOB).build(); assertFalse(ALICE.isSamePerson(editedAlice)); // different name, all other attributes same -> returns false @@ -106,7 +106,7 @@ public void equals() { assertNotEquals(ALICE, editedAlice); // different risklevel -> returns false - editedAlice = new ElderlyBuilder(ALICE).withRiskLevel(VALID_RISKLEVEL_BOB).build(); + editedAlice = new ElderlyBuilder(ALICE).withRiskLevel(VALID_RISK_LEVEL_BOB).build(); assertNotEquals(ALICE, editedAlice); // different tags -> returns false diff --git a/src/test/java/seedu/address/storage/JsonPairStorageTest.java b/src/test/java/seedu/address/storage/JsonPairStorageTest.java index 11da15674e2..5074ff95906 100644 --- a/src/test/java/seedu/address/storage/JsonPairStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonPairStorageTest.java @@ -1,7 +1,9 @@ package seedu.address.storage; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TestUtil.getTypicalFriendlyLink; import java.io.IOException; import java.nio.file.Path; @@ -14,6 +16,10 @@ import seedu.address.model.FriendlyLink; import seedu.address.model.ReadOnlyPair; import seedu.address.storage.pair.JsonPairStorage; +import seedu.address.testutil.FriendlyLinkBuilder; +import seedu.address.testutil.TypicalElderly; +import seedu.address.testutil.TypicalPairs; +import seedu.address.testutil.TypicalVolunteers; public class JsonPairStorageTest { private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "JsonPairStorageTest"); @@ -52,10 +58,35 @@ public void readPair_invalidPair_throwDataConversionException() { assertThrows(DataConversionException.class, () -> readPair("invalidPair.json")); } - // TODO: readPair_invalidAndValidPair_throwDataConversionException() test case, - // can follow the one in elderlyStorageTest + @Test + public void readPair_invalidAndValidPair_throwDataConversionException() { + assertThrows(DataConversionException.class, () -> readPair("invalidAndValidPair.json")); + } - // TODO: readAndSavePair_allInOrder_success() test case, can follow the one in elderlyStorageTest + @Test + public void readAndSavePair_allInOrder_success() throws Exception { + Path filePath = testFolder.resolve("TempPair.json"); + FriendlyLink original = getTypicalFriendlyLink(); + JsonPairStorage jsonPairStorage = new JsonPairStorage(filePath); + + // Save in new file and read back + jsonPairStorage.savePair(original, filePath); + ReadOnlyPair readBack = jsonPairStorage.readPair(filePath, getNoPairsTypicalFriendlyLink()).get(); + assertEquals(original.getPairList(), readBack.getPairList()); + + // Modify data, overwrite exiting file, and read back + original.removePair(TypicalPairs.PAIR1); + original.addPair(TypicalPairs.PAIR1); + jsonPairStorage.savePair(original, filePath); + readBack = jsonPairStorage.readPair(filePath, getNoPairsTypicalFriendlyLink()).get(); + assertEquals(original.getPairList(), readBack.getPairList()); + + // Save and read without specifying file path + original.addPair(TypicalPairs.PAIR4); + jsonPairStorage.savePair(original); // file path not specified + readBack = jsonPairStorage.readPair(getNoPairsTypicalFriendlyLink()).get(); // file path not specified + assertEquals(original.getPairList(), readBack.getPairList()); + } @Test public void savePair_nullPair_throwsNullPointerException() { @@ -74,6 +105,13 @@ private void savePair(ReadOnlyPair pair, String filePath) { } } + private FriendlyLink getNoPairsTypicalFriendlyLink() { + return new FriendlyLinkBuilder() + .withElderly(TypicalElderly.getTypicalElderly()) + .withVolunteers(TypicalVolunteers.getTypicalVolunteers()) + .build(); + } + @Test public void savePair_nullFilePath_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> savePair(new FriendlyLink(), null)); diff --git a/src/test/java/seedu/address/testutil/FriendlyLinkBuilder.java b/src/test/java/seedu/address/testutil/FriendlyLinkBuilder.java index f18e9518988..8817281f847 100644 --- a/src/test/java/seedu/address/testutil/FriendlyLinkBuilder.java +++ b/src/test/java/seedu/address/testutil/FriendlyLinkBuilder.java @@ -1,6 +1,9 @@ package seedu.address.testutil; +import java.util.List; + import seedu.address.model.FriendlyLink; +import seedu.address.model.pair.Pair; import seedu.address.model.person.Elderly; import seedu.address.model.person.Volunteer; @@ -29,6 +32,16 @@ public FriendlyLinkBuilder withVolunteer(Volunteer volunteer) { return this; } + /** + * Adds all {@code volunteers} to the {@code FriendlyLink} that we are building. + */ + public FriendlyLinkBuilder withVolunteers(List volunteers) { + for (Volunteer volunteer : volunteers) { + friendlyLink.addVolunteer(volunteer); + } + return this; + } + /** * Adds a new {@code Elderly} to the {@code FriendlyLink} that we are building. */ @@ -37,6 +50,34 @@ public FriendlyLinkBuilder withElderly(Elderly elderly) { return this; } + /** + * Adds all {@code elderly} to the {@code FriendlyLink} that we are building. + */ + public FriendlyLinkBuilder withElderly(List elderly) { + for (Elderly elderlyMember : elderly) { + friendlyLink.addElderly(elderlyMember); + } + return this; + } + + /** + * Adds a new {@code Pair} to the {@code FriendlyLink} that we are building. + */ + public FriendlyLinkBuilder withPair(Pair pair) { + friendlyLink.addPair(pair); + return this; + } + + /** + * Adds all {@code pairs} to the {@code FriendlyLink} that we are building. + */ + public FriendlyLinkBuilder withPairs(List pairs) { + for (Pair pair : pairs) { + friendlyLink.addPair(pair); + } + return this; + } + public FriendlyLink build() { return friendlyLink; } diff --git a/src/test/java/seedu/address/testutil/TestUtil.java b/src/test/java/seedu/address/testutil/TestUtil.java index 89594161ac8..69b368855f9 100644 --- a/src/test/java/seedu/address/testutil/TestUtil.java +++ b/src/test/java/seedu/address/testutil/TestUtil.java @@ -102,16 +102,10 @@ public static Pair getPair(Model model, Index index) { * Returns a {@code FriendlyLink} with all the typical volunteers, elderly and pairs. */ public static FriendlyLink getTypicalFriendlyLink() { - FriendlyLink fl = new FriendlyLink(); - for (Volunteer volunteer : TypicalVolunteers.getTypicalVolunteers()) { - fl.addVolunteer(volunteer); - } - for (Elderly elderly : TypicalElderly.getTypicalElderly()) { - fl.addElderly(elderly); - } - for (Pair pair : TypicalPairs.getTypicalPairs()) { - fl.addPair(pair); - } - return fl; + return new FriendlyLinkBuilder() + .withElderly(TypicalElderly.getTypicalElderly()) + .withVolunteers(TypicalVolunteers.getTypicalVolunteers()) + .withPairs(TypicalPairs.getTypicalPairs()) + .build(); } } diff --git a/src/test/java/seedu/address/testutil/TypicalElderly.java b/src/test/java/seedu/address/testutil/TypicalElderly.java index 02c64312cf8..6108c60a8e2 100644 --- a/src/test/java/seedu/address/testutil/TypicalElderly.java +++ b/src/test/java/seedu/address/testutil/TypicalElderly.java @@ -13,8 +13,8 @@ import static seedu.address.logic.commands.CommandTestUtil.VALID_NRIC_CHARLIE; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_AMY; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_RISKLEVEL_AMY; -import static seedu.address.logic.commands.CommandTestUtil.VALID_RISKLEVEL_BOB; +import static seedu.address.logic.commands.CommandTestUtil.VALID_RISK_LEVEL_AMY; +import static seedu.address.logic.commands.CommandTestUtil.VALID_RISK_LEVEL_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_SINGLE; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_STRONG; @@ -63,10 +63,10 @@ public class TypicalElderly { // Manually added - Person's details found in {@code CommandTestUtil} public static final Elderly AMY = new ElderlyBuilder().withName(VALID_NAME_AMY).withPhone(VALID_PHONE_AMY) .withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY).withTags(VALID_TAG_SINGLE) - .withNric(VALID_NRIC_AMY).withAge(VALID_AGE_AMY).withRiskLevel(VALID_RISKLEVEL_AMY).build(); + .withNric(VALID_NRIC_AMY).withAge(VALID_AGE_AMY).withRiskLevel(VALID_RISK_LEVEL_AMY).build(); public static final Elderly BOB = new ElderlyBuilder().withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB) .withEmail(VALID_EMAIL_BOB).withAddress(VALID_ADDRESS_BOB).withTags(VALID_TAG_SINGLE, VALID_TAG_STRONG) - .withNric(VALID_NRIC_BOB).withAge(VALID_AGE_BOB).withRiskLevel(VALID_RISKLEVEL_BOB).build(); + .withNric(VALID_NRIC_BOB).withAge(VALID_AGE_BOB).withRiskLevel(VALID_RISK_LEVEL_BOB).build(); // TODO: Replace magic strings public static final Elderly CHARLIE = new ElderlyBuilder().withName("Charlie Kurz") diff --git a/src/test/java/seedu/address/testutil/TypicalPairs.java b/src/test/java/seedu/address/testutil/TypicalPairs.java index e4ddf86e570..48a910cf76b 100644 --- a/src/test/java/seedu/address/testutil/TypicalPairs.java +++ b/src/test/java/seedu/address/testutil/TypicalPairs.java @@ -1,10 +1,12 @@ package seedu.address.testutil; -import static seedu.address.testutil.TypicalElderly.AMY; -import static seedu.address.testutil.TypicalElderly.BOB; -import static seedu.address.testutil.TypicalElderly.CHARLIE; +import static seedu.address.testutil.TypicalElderly.ALICE; +import static seedu.address.testutil.TypicalElderly.BENSON; +import static seedu.address.testutil.TypicalElderly.CARL; +import static seedu.address.testutil.TypicalElderly.FIONA; import static seedu.address.testutil.TypicalVolunteers.DANIEL; import static seedu.address.testutil.TypicalVolunteers.ELLE; +import static seedu.address.testutil.TypicalVolunteers.GEORGE; import java.util.ArrayList; import java.util.Arrays; @@ -17,12 +19,14 @@ */ public class TypicalPairs { - public static final Pair PAIR1 = new PairBuilder().withElderly(AMY) + public static final Pair PAIR1 = new PairBuilder().withElderly(ALICE) .withVolunteer(ELLE).build(); - public static final Pair PAIR2 = new PairBuilder().withElderly(CHARLIE) + public static final Pair PAIR2 = new PairBuilder().withElderly(CARL) .withVolunteer(DANIEL).build(); - public static final Pair PAIR3 = new PairBuilder().withElderly(BOB) + public static final Pair PAIR3 = new PairBuilder().withElderly(BENSON) .withVolunteer(ELLE).build(); + public static final Pair PAIR4 = new PairBuilder().withElderly(FIONA) + .withVolunteer(GEORGE).build(); private TypicalPairs() {} // prevents instantiation From fc1c7d60100c6a04fabbfead0d145c1de0b44463 Mon Sep 17 00:00:00 2001 From: gohyongjing Date: Wed, 8 Mar 2023 23:38:56 +0800 Subject: [PATCH 2/5] Add tests for JsonPairStorage --- src/main/java/seedu/address/model/ModelManager.java | 2 +- .../seedu/address/storage/JsonPairStorageTest.java | 11 +---------- src/test/java/seedu/address/testutil/TestUtil.java | 11 +++++++++++ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index caf4c3ef4c8..ef68305151d 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -164,7 +164,7 @@ public boolean hasPair(Pair pair) { @Override public void addPair(Nric elderlyNric, Nric volunteerNric) { - friendlyLink.removePair(elderlyNric, volunteerNric); + friendlyLink.addPair(elderlyNric, volunteerNric); // TODO: implement updateFilteredPersonList(PREDICATE_SHOW_ALL_PAIRS); } diff --git a/src/test/java/seedu/address/storage/JsonPairStorageTest.java b/src/test/java/seedu/address/storage/JsonPairStorageTest.java index 5074ff95906..e3b967d1e69 100644 --- a/src/test/java/seedu/address/storage/JsonPairStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonPairStorageTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TestUtil.getNoPairsTypicalFriendlyLink; import static seedu.address.testutil.TestUtil.getTypicalFriendlyLink; import java.io.IOException; @@ -16,10 +17,7 @@ import seedu.address.model.FriendlyLink; import seedu.address.model.ReadOnlyPair; import seedu.address.storage.pair.JsonPairStorage; -import seedu.address.testutil.FriendlyLinkBuilder; -import seedu.address.testutil.TypicalElderly; import seedu.address.testutil.TypicalPairs; -import seedu.address.testutil.TypicalVolunteers; public class JsonPairStorageTest { private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "JsonPairStorageTest"); @@ -105,13 +103,6 @@ private void savePair(ReadOnlyPair pair, String filePath) { } } - private FriendlyLink getNoPairsTypicalFriendlyLink() { - return new FriendlyLinkBuilder() - .withElderly(TypicalElderly.getTypicalElderly()) - .withVolunteers(TypicalVolunteers.getTypicalVolunteers()) - .build(); - } - @Test public void savePair_nullFilePath_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> savePair(new FriendlyLink(), null)); diff --git a/src/test/java/seedu/address/testutil/TestUtil.java b/src/test/java/seedu/address/testutil/TestUtil.java index 69b368855f9..32fccc20410 100644 --- a/src/test/java/seedu/address/testutil/TestUtil.java +++ b/src/test/java/seedu/address/testutil/TestUtil.java @@ -108,4 +108,15 @@ public static FriendlyLink getTypicalFriendlyLink() { .withPairs(TypicalPairs.getTypicalPairs()) .build(); } + + /** + * Returns a {@code FriendlyLink} with all the typical volunteers and elderly. + */ + public static FriendlyLink getNoPairsTypicalFriendlyLink() { + return new FriendlyLinkBuilder() + .withElderly(TypicalElderly.getTypicalElderly()) + .withVolunteers(TypicalVolunteers.getTypicalVolunteers()) + .build(); + } + } From 3519fd02872dbb240205ea60b7de55e14569643a Mon Sep 17 00:00:00 2001 From: gohyongjing Date: Thu, 9 Mar 2023 00:09:38 +0800 Subject: [PATCH 3/5] Add tests for JsonSerialisablePair --- .../seedu/address/commons/core/Messages.java | 4 ++ .../logic/commands/AddPairCommand.java | 4 +- .../storage/pair/JsonSerializablePair.java | 6 ++- .../duplicatePairs.json | 8 ++-- .../JsonSerializablePairTest/invalidPair.json | 2 +- .../typicalPairs.json | 6 +-- .../logic/commands/AddPairCommandTest.java | 4 +- .../storage/JsonSerializablePairTest.java | 47 +++++++++++++++++-- 8 files changed, 61 insertions(+), 20 deletions(-) diff --git a/src/main/java/seedu/address/commons/core/Messages.java b/src/main/java/seedu/address/commons/core/Messages.java index 5566443746c..2f9ddb023a6 100644 --- a/src/main/java/seedu/address/commons/core/Messages.java +++ b/src/main/java/seedu/address/commons/core/Messages.java @@ -23,4 +23,8 @@ public class Messages { "The pair consisting of elderly with NRIC %1$s and volunteer with NRIC %2$s" + " does not exist in FriendlyLink"; + public static final String MESSAGE_DUPLICATE_PAIR = + "This pair consisting of elderly with NRIC %1$s" + + " and volunteer with NRIC %2$s already exists in FriendlyLink"; + } diff --git a/src/main/java/seedu/address/logic/commands/AddPairCommand.java b/src/main/java/seedu/address/logic/commands/AddPairCommand.java index 7acdaaa7a43..7c8ebd384db 100644 --- a/src/main/java/seedu/address/logic/commands/AddPairCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddPairCommand.java @@ -1,6 +1,7 @@ package seedu.address.logic.commands; import static java.util.Objects.requireNonNull; +import static seedu.address.commons.core.Messages.MESSAGE_DUPLICATE_PAIR; import static seedu.address.commons.core.Messages.MESSAGE_ELDERLY_NOT_FOUND; import static seedu.address.commons.core.Messages.MESSAGE_VOLUNTEER_NOT_FOUND; import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; @@ -32,9 +33,6 @@ public class AddPairCommand extends Command { public static final String MESSAGE_ADD_PAIR_SUCCESS = "New pair consisting of elderly with NRIC %1$s" + " and volunteer with NRIC %2$s added"; - public static final String MESSAGE_DUPLICATE_PAIR = "This pair consisting of elderly with NRIC %1$s" - + " and volunteer with NRIC %2$s already exists in FriendlyLink"; - private final Nric elderlyNric; private final Nric volunteerNric; diff --git a/src/main/java/seedu/address/storage/pair/JsonSerializablePair.java b/src/main/java/seedu/address/storage/pair/JsonSerializablePair.java index 99fbb7d2e21..2fe093ae5d2 100644 --- a/src/main/java/seedu/address/storage/pair/JsonSerializablePair.java +++ b/src/main/java/seedu/address/storage/pair/JsonSerializablePair.java @@ -1,5 +1,7 @@ package seedu.address.storage.pair; +import static seedu.address.commons.core.Messages.MESSAGE_DUPLICATE_PAIR; + import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -19,7 +21,6 @@ */ @JsonRootName(value = "pairs") public class JsonSerializablePair implements JsonSerializable { - public static final String MESSAGE_DUPLICATE_PAIR = "Persons list contains duplicate pair(s)."; private final List pairs = new ArrayList<>(); @@ -60,7 +61,8 @@ private void unserializeEntities(FriendlyLink friendlyLink) throws IllegalValueE for (JsonAdaptedPair jsonAdaptedPair : pairs) { Pair pair = jsonAdaptedPair.toModelType(friendlyLink); if (friendlyLink.hasPair(pair)) { - throw new IllegalValueException(MESSAGE_DUPLICATE_PAIR); + throw new IllegalValueException(String.format( + MESSAGE_DUPLICATE_PAIR, pair.getElderly().getNric(), pair.getVolunteer().getNric())); } friendlyLink.addPair(pair); } diff --git a/src/test/data/JsonSerializablePairTest/duplicatePairs.json b/src/test/data/JsonSerializablePairTest/duplicatePairs.json index b9369edfc6a..1af1578652a 100644 --- a/src/test/data/JsonSerializablePairTest/duplicatePairs.json +++ b/src/test/data/JsonSerializablePairTest/duplicatePairs.json @@ -1,9 +1,9 @@ { "pairs": [ { - "elderlyNric": "S6192384K", - "volunteerNric": "S9931763G" + "elderlyNric": "S9673908G", + "volunteerNric": "S7238791J" }, { - "elderlyNric": "S6192384K", - "volunteerNric": "S9931763G" + "elderlyNric": "S9673908G", + "volunteerNric": "S7238791J" } ] } diff --git a/src/test/data/JsonSerializablePairTest/invalidPair.json b/src/test/data/JsonSerializablePairTest/invalidPair.json index 59fe66fd249..6b22a9e5eed 100644 --- a/src/test/data/JsonSerializablePairTest/invalidPair.json +++ b/src/test/data/JsonSerializablePairTest/invalidPair.json @@ -1,5 +1,5 @@ { - "persons": [ { + "pairs": [ { "elderlyNric": "Invalid elderly nric", "volunteerNric": "S9931763G" } ] diff --git a/src/test/data/JsonSerializablePairTest/typicalPairs.json b/src/test/data/JsonSerializablePairTest/typicalPairs.json index 91527b881aa..039cbb78dab 100644 --- a/src/test/data/JsonSerializablePairTest/typicalPairs.json +++ b/src/test/data/JsonSerializablePairTest/typicalPairs.json @@ -1,13 +1,13 @@ { "_comment": "Pairs save file which contains the same Pair values as in TypicalPairs#getTypicalPairs()", "pairs" : [ { - "elderlyNric": "S1234567A", + "elderlyNric": "S9673908G", "volunteerNric": "S7238791J" }, { - "elderlyNric": "T3456789D", + "elderlyNric": "S3634466J", "volunteerNric": "S0203151E" }, { - "elderlyNric": "T2345678C", + "elderlyNric": "S6878241D", "volunteerNric": "S7238791J" } ] } diff --git a/src/test/java/seedu/address/logic/commands/AddPairCommandTest.java b/src/test/java/seedu/address/logic/commands/AddPairCommandTest.java index 704fb618da4..c066d1099bc 100644 --- a/src/test/java/seedu/address/logic/commands/AddPairCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddPairCommandTest.java @@ -3,6 +3,7 @@ import static java.util.Objects.requireNonNull; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static seedu.address.commons.core.Messages.MESSAGE_DUPLICATE_PAIR; import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; import static seedu.address.logic.commands.CommandTestUtil.VALID_NRIC_AMY; import static seedu.address.logic.commands.CommandTestUtil.VALID_NRIC_BOB; @@ -56,8 +57,7 @@ public void execute_duplicatePair_throwsCommandException() { Nric elderlyNric = validPair.getElderly().getNric(); Nric volunteerNric = validPair.getVolunteer().getNric(); AddPairCommand addPairCommand = new AddPairCommand(elderlyNric, volunteerNric); - - String expectedMessage = String.format(AddPairCommand.MESSAGE_DUPLICATE_PAIR, elderlyNric, volunteerNric); + String expectedMessage = String.format(MESSAGE_DUPLICATE_PAIR, elderlyNric, volunteerNric); assertThrows(CommandException.class, expectedMessage, () -> addPairCommand.execute(modelStub)); } diff --git a/src/test/java/seedu/address/storage/JsonSerializablePairTest.java b/src/test/java/seedu/address/storage/JsonSerializablePairTest.java index 04498f307c8..42eac63403d 100644 --- a/src/test/java/seedu/address/storage/JsonSerializablePairTest.java +++ b/src/test/java/seedu/address/storage/JsonSerializablePairTest.java @@ -1,9 +1,23 @@ package seedu.address.storage; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static seedu.address.commons.core.Messages.MESSAGE_DUPLICATE_PAIR; +import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TestUtil.getNoPairsTypicalFriendlyLink; +import static seedu.address.testutil.TypicalPairs.PAIR1; +import static seedu.address.testutil.TypicalPairs.getTypicalPairs; + import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; + +import org.junit.jupiter.api.Test; +import seedu.address.commons.exceptions.IllegalValueException; +import seedu.address.commons.util.JsonUtil; import seedu.address.model.FriendlyLink; +import seedu.address.model.pair.Pair; +import seedu.address.storage.pair.JsonSerializablePair; public class JsonSerializablePairTest { @@ -13,10 +27,33 @@ public class JsonSerializablePairTest { private static final Path DUPLICATE_PAIRS_FILE = TEST_DATA_FOLDER.resolve("duplicatePairs.json"); private static final FriendlyLink appTestCache = new FriendlyLink(); - // TODO: toModelType_typicalPairsFile_success(), follow JsonSerializableElderlyTest - - // TODO: toModelType_invalidPairFile_throwsIllegalValueException(), follow JsonSerializableElderlyTest - - // TODO: toModelType_duplicatePairs_throwsIllegalValueException(), follow JsonSerializableElderlyTest + @Test + public void toModelType_typicalPairsFile_success() throws Exception { + FriendlyLink appTestCache = getNoPairsTypicalFriendlyLink(); + JsonSerializablePair dataFromFile = JsonUtil.readJsonFile(TYPICAL_PAIRS_FILE, + JsonSerializablePair.class).get(); + FriendlyLink friendlyLinkFromFile = dataFromFile.toModelType(appTestCache); + List typicalPairs = getTypicalPairs(); + assertEquals(friendlyLinkFromFile.getPairList(), typicalPairs); + } + + @Test + public void toModelType_invalidPairFile_throwsIllegalValueException() throws Exception { + FriendlyLink appTestCache = getNoPairsTypicalFriendlyLink(); + JsonSerializablePair dataFromFile = JsonUtil.readJsonFile(INVALID_PAIR_FILE, + JsonSerializablePair.class).get(); + assertThrows(IllegalValueException.class, () -> dataFromFile.toModelType(appTestCache)); + } + + @Test + public void toModelType_duplicatePair_throwsIllegalValueException() throws Exception { + FriendlyLink appTestCache = getNoPairsTypicalFriendlyLink(); + JsonSerializablePair dataFromFile = JsonUtil.readJsonFile(DUPLICATE_PAIRS_FILE, + JsonSerializablePair.class).get(); + String expectedMessage = String.format( + MESSAGE_DUPLICATE_PAIR, PAIR1.getElderly().getNric(), PAIR1.getVolunteer().getNric()); + assertThrows(IllegalValueException.class, + expectedMessage, () -> dataFromFile.toModelType(appTestCache)); + } } From 6b0e4591187b7644ba03254188ad0340676b3982 Mon Sep 17 00:00:00 2001 From: gohyongjing Date: Thu, 9 Mar 2023 00:28:32 +0800 Subject: [PATCH 4/5] Add tests for AddPairCommand --- .../logic/commands/AddPairCommandTest.java | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/src/test/java/seedu/address/logic/commands/AddPairCommandTest.java b/src/test/java/seedu/address/logic/commands/AddPairCommandTest.java index c066d1099bc..05456ab3782 100644 --- a/src/test/java/seedu/address/logic/commands/AddPairCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddPairCommandTest.java @@ -4,11 +4,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static seedu.address.commons.core.Messages.MESSAGE_DUPLICATE_PAIR; +import static seedu.address.commons.core.Messages.MESSAGE_ELDERLY_NOT_FOUND; +import static seedu.address.commons.core.Messages.MESSAGE_VOLUNTEER_NOT_FOUND; import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; import static seedu.address.logic.commands.CommandTestUtil.VALID_NRIC_AMY; import static seedu.address.logic.commands.CommandTestUtil.VALID_NRIC_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_NRIC_CHARLIE; import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalPairs.PAIR1; +import static seedu.address.testutil.TypicalPairs.PAIR2; import org.junit.jupiter.api.Test; @@ -18,6 +22,8 @@ import seedu.address.model.pair.exceptions.DuplicatePairException; import seedu.address.model.person.Elderly; import seedu.address.model.person.Volunteer; +import seedu.address.model.person.exceptions.ElderlyNotFoundException; +import seedu.address.model.person.exceptions.VolunteerNotFoundException; import seedu.address.model.person.information.Nric; import seedu.address.testutil.PairBuilder; @@ -50,6 +56,27 @@ public void execute_pairAcceptedByModel_addSuccessful() throws Exception { assertEquals(validPair, modelStub.pair); } + @Test + public void execute_invalidPerson_throwsCommandException() { + Nric pair1ElderlyNric = PAIR1.getElderly().getNric(); + Nric pair1VolunteerNric = PAIR1.getVolunteer().getNric(); + Nric pair2ElderlyNric = PAIR2.getElderly().getNric(); + Nric pair2VolunteerNric = PAIR2.getVolunteer().getNric(); + ModelStub modelStub = new ModelStubAcceptingPairAdded(PAIR1.getElderly(), PAIR1.getVolunteer()); + + // Invalid elderly -> throws exception + AddPairCommand addPairCommand1 = new AddPairCommand(pair2ElderlyNric, pair1VolunteerNric); + String expectedMessage = String.format(MESSAGE_ELDERLY_NOT_FOUND, pair2ElderlyNric); + assertThrows(CommandException.class, + expectedMessage, () -> addPairCommand1.execute(modelStub)); + + // Invalid volunteer -> throws exception + AddPairCommand addPairCommand2 = new AddPairCommand(pair1ElderlyNric, pair2VolunteerNric); + expectedMessage = String.format(MESSAGE_VOLUNTEER_NOT_FOUND, pair2VolunteerNric); + assertThrows(CommandException.class, + expectedMessage, () -> addPairCommand2.execute(modelStub)); + } + @Test public void execute_duplicatePair_throwsCommandException() { Pair validPair = new PairBuilder().build(); @@ -66,6 +93,7 @@ public void execute_duplicatePair_throwsCommandException() { public void equals() { AddPairCommand addPair1Command = new AddPairCommand(validAmyNric, validBobNric); AddPairCommand addPair2Command = new AddPairCommand(validAmyNric, validCharlieNric); + AddPairCommand addPair3Command = new AddPairCommand(validBobNric, validAmyNric); // same object -> returns true assertEquals(addPair1Command, addPair1Command); @@ -82,6 +110,7 @@ public void equals() { // different person -> returns false assertNotEquals(addPair1Command, addPair2Command); + assertNotEquals(addPair1Command, addPair3Command); } /** @@ -141,15 +170,13 @@ public boolean hasPair(Pair pair) { return pair.isSamePair(this.pair); } - @Override - public void addPair(Pair pair) { - requireNonNull(pair); - this.pair = pair; - } - @Override public void addPair(Nric elderlyNric, Nric volunteerNric) { - if (pair != null + if (!elderlyNric.equals(elderly.getNric())) { + throw new ElderlyNotFoundException(); + } else if (!volunteerNric.equals(volunteer.getNric())) { + throw new VolunteerNotFoundException(); + } else if (pair != null && pair.getElderly().getNric().equals(elderlyNric) && pair.getVolunteer().getNric().equals(volunteerNric)) { throw new DuplicatePairException(); From 71e90f4f6ec2f74d637d12a4c6f48d26cb9ecb87 Mon Sep 17 00:00:00 2001 From: gohyongjing Date: Tue, 14 Mar 2023 11:44:00 +0800 Subject: [PATCH 5/5] Fix javadocs typos in Model and FriendlyLink --- .../java/seedu/address/model/FriendlyLink.java | 4 ++-- src/main/java/seedu/address/model/Model.java | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/seedu/address/model/FriendlyLink.java b/src/main/java/seedu/address/model/FriendlyLink.java index d741091df2e..c4312fed72f 100644 --- a/src/main/java/seedu/address/model/FriendlyLink.java +++ b/src/main/java/seedu/address/model/FriendlyLink.java @@ -208,7 +208,7 @@ public boolean hasPair(Pair pair) { } /** - * Adds a {code @pair} to {@code FriendlyLink}. + * Adds a {@code pair} to {@code FriendlyLink}. * The pair must not already exist in {@code FriendlyLink}. * * @param pair Pair to add into FriendlyLink. @@ -219,7 +219,7 @@ public void addPair(Pair pair) { /** * Adds a pair consisting of elderly with {@code elderlyNric} and volunteer with {@code volunteerNric} - * from {@code FriendlyLink}. + * to {@code FriendlyLink}. * The pair must not already exist in {@code FriendlyLink}. * * @param elderlyNric Nric of elderly. diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 2406bfedee8..5b91d0cb9cd 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -125,39 +125,39 @@ public interface Model { /** * Adds the given pair. - * {@code pair} must not already exist in the address book. + * {@code pair} must not already exist in FriendlyLink. */ void addPair(Pair pair); /** * Adds the given pair. * The pair consisting of elderly with {@code elderlyNric} and volunteer with {@code volunteerNric} - * must not already exist in the address book. + * must not already exist in FriendlyLink. */ void addPair(Nric elderlyNric, Nric volunteerNric); /** - * Returns true if a pair with the same identity as {@code pair} exists in the address book. + * Returns true if a pair with the same identity as {@code pair} exists in FriendlyLink. */ boolean hasPair(Pair pair); /** * Deletes the given pair. - * The pair must exist in the address book. + * The pair must exist in FriendlyLink. */ void deletePair(Pair target); /** * Deletes the given pair. * The pair consisting of elderly with {@code elderlyNric} and volunteer with {@code volunteerNric} - * must exist in the address book. + * must exist in FriendlyLink. */ void deletePair(Nric elderlyNric, Nric volunteerNric); /** * Replaces the given pair {@code target} with {@code editedPair}. - * {@code target} must exist in the address book. - * The pair identity of {@code editedPair} must not be the same as another existing pair in the address book. + * {@code target} must exist in FriendlyLink. + * The pair identity of {@code editedPair} must not be the same as another existing pair in FriendlyLink. */ void setPair(Pair target, Pair editedPair);