Skip to content

Commit

Permalink
Merge pull request #89 from gohyongjing/branch-pair-test
Browse files Browse the repository at this point in the history
Add more tests for pair
  • Loading branch information
wz2k authored Mar 15, 2023
2 parents cc28f9d + 71e90f4 commit 3ee2100
Show file tree
Hide file tree
Showing 28 changed files with 341 additions and 142 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@ 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";

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";

}
49 changes: 16 additions & 33 deletions src/main/java/seedu/address/logic/commands/AddPairCommand.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
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;
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;

/**
Expand All @@ -28,15 +30,8 @@ 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_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_ADD_PAIR_SUCCESS = "New pair consisting of elderly with NRIC %1$s"
+ " and volunteer with NRIC %2$s added";

private final Nric elderlyNric;
private final Nric volunteerNric;
Expand All @@ -53,28 +48,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);
}

try {
volunteer = model.getVolunteer(volunteerNric);
} catch (PersonNotFoundException e) {
throw new CommandException(MESSAGE_VOLUNTEER_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));
}

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
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/seedu/address/logic/commands/DeletePairCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

Expand Down
16 changes: 14 additions & 2 deletions src/main/java/seedu/address/model/FriendlyLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -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}
* to {@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.
Expand Down
29 changes: 18 additions & 11 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,33 +124,40 @@ public interface Model {
void setVolunteer(Volunteer target, Volunteer editedVolunteer);

/**
* Returns true if a pair with the same identity as {@code pair} exists in the address book.
* Adds the given pair.
* {@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 FriendlyLink.
*/
void addPair(Nric elderlyNric, Nric volunteerNric);

/**
* 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);

/**
* 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.
* 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);

Expand Down
18 changes: 12 additions & 6 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ public boolean hasPair(Pair pair) {
return friendlyLink.hasPair(pair);
}

@Override
public void addPair(Nric elderlyNric, Nric volunteerNric) {
friendlyLink.addPair(elderlyNric, volunteerNric);
// TODO: implement updateFilteredPersonList(PREDICATE_SHOW_ALL_PAIRS);
}

@Override
public void addPair(Pair pair) {
friendlyLink.addPair(pair);
// TODO: implement updateFilteredPersonList(PREDICATE_SHOW_ALL_PAIRS);
}

@Override
public void deletePair(Pair target) {
friendlyLink.removePair(target);
Expand All @@ -172,12 +184,6 @@ public void deletePair(Nric elderlyNric, Nric volunteerNric) {
friendlyLink.removePair(elderlyNric, volunteerNric);
}

@Override
public void addPair(Pair pair) {
friendlyLink.addPair(pair);
// TODO: implement updateFilteredPersonList(PREDICATE_SHOW_ALL_PAIRS);
}

@Override
public void setPair(Pair target, Pair editedPair) {
requireAllNonNull(target, editedPair);
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/seedu/address/storage/pair/JsonAdaptedPair.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;

Expand All @@ -16,7 +21,7 @@
*/
class JsonAdaptedPair implements JsonSerializable<Pair> {

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;
Expand Down Expand Up @@ -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));
}

}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,7 +21,6 @@
*/
@JsonRootName(value = "pairs")
public class JsonSerializablePair implements JsonSerializable<FriendlyLink> {
public static final String MESSAGE_DUPLICATE_PAIR = "Persons list contains duplicate pair(s).";

private final List<JsonAdaptedPair> pairs = new ArrayList<>();

Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/data/JsonSerializablePairTest/duplicatePairs.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"pairs": [ {
"elderlyNric": "S6192384K",
"volunteerNric": "S9931763G"
"elderlyNric": "S9673908G",
"volunteerNric": "S7238791J"
}, {
"elderlyNric": "S6192384K",
"volunteerNric": "S9931763G"
"elderlyNric": "S9673908G",
"volunteerNric": "S7238791J"
} ]
}
2 changes: 1 addition & 1 deletion src/test/data/JsonSerializablePairTest/invalidPair.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"persons": [ {
"pairs": [ {
"elderlyNric": "Invalid elderly nric",
"volunteerNric": "S9931763G"
} ]
Expand Down
6 changes: 3 additions & 3 deletions src/test/data/JsonSerializablePairTest/typicalPairs.json
Original file line number Diff line number Diff line change
@@ -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"
} ]
}
Loading

0 comments on commit 3ee2100

Please sign in to comment.