diff --git a/src/main/java/seedu/address/model/AddressBook.java b/src/main/java/seedu/address/model/AddressBook.java index 340d447d2a16..bd8686160c31 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -278,7 +278,7 @@ public boolean removePerson(Person key) throws PersonNotFoundException { /** * Adds a pet patient to the address book. * Also checks the new pet patient's tags and updates {@link #petPatientTags} with any new tags found, - * and updates the Tag objects in the person to point to those in {@link #petPatientTags}. + * and updates the Tag objects in the pet patient to point to those in {@link #petPatientTags}. * * @throws DuplicatePetPatientException if an equivalent person already exists. */ diff --git a/src/main/java/seedu/address/storage/XmlAdaptedPetPatient.java b/src/main/java/seedu/address/storage/XmlAdaptedPetPatient.java index f84205863aa5..53c2f99825d0 100644 --- a/src/main/java/seedu/address/storage/XmlAdaptedPetPatient.java +++ b/src/main/java/seedu/address/storage/XmlAdaptedPetPatient.java @@ -8,6 +8,7 @@ import javax.xml.bind.annotation.XmlElement; import seedu.address.commons.exceptions.IllegalValueException; +import seedu.address.model.person.Nric; import seedu.address.model.petpatient.PetPatient; import seedu.address.model.petpatient.PetPatientName; import seedu.address.model.tag.Tag; @@ -16,7 +17,13 @@ * JAXB-friendly version of the PetPatient. */ public class XmlAdaptedPetPatient { - public static final String MISSING_FIELD_MESSAGE_FORMAT = "Pet patient's %s field is missing!"; + public static final String MISSING_NAME_FIELD_MESSAGE_FORMAT = "Pet patient's name field is missing!"; + public static final String MISSING_SPECIES_FIELD_MESSAGE_FORMAT = "Pet patient's species field is missing!"; + public static final String MISSING_BREED_FIELD_MESSAGE_FORMAT = "Pet patient's breed field is missing!"; + public static final String MISSING_COLOUR_FIELD_MESSAGE_FORMAT = "Pet patient's colour field is missing!"; + public static final String MISSING_BLOODTYPE_FIELD_MESSAGE_FORMAT = "Pet patient's blood type field is missing!"; + public static final String MISSING_OWNER_FIELD_MESSAGE_FORMAT = "Pet patient's owner field is missing!"; + @XmlElement(required = true) private String name; @@ -28,9 +35,15 @@ public class XmlAdaptedPetPatient { private String colour; @XmlElement(required = true) private String bloodType; + @XmlElement(required = true) + private String ownerNric; + @XmlElement + private String dateOfBirth; @XmlElement private List tagged = new ArrayList<>(); + @XmlElement + private String medicalHistory; /** * Constructs an XmlAdaptedPetPatient. @@ -42,12 +55,13 @@ public XmlAdaptedPetPatient() {} * Constructs an {@code XmlAdaptedPetPatient} with the given pet patient details. */ public XmlAdaptedPetPatient(String name, String species, String breed, String colour, - String bloodType, List tagged) { + String bloodType, String ownerNric, List tagged) { this.name = name; this.species = species; this.breed = breed; this.colour = colour; this.bloodType = bloodType; + this.ownerNric = ownerNric; if (tagged != null) { this.tagged = new ArrayList<>(tagged); } @@ -64,6 +78,7 @@ public XmlAdaptedPetPatient(PetPatient source) { breed = source.getBreed(); colour = source.getColour(); bloodType = source.getBloodType(); + ownerNric = source.getOwner().toString(); tagged = new ArrayList<>(); for (Tag tag : source.getTags()) { tagged.add(new XmlAdaptedTag(tag)); @@ -83,7 +98,7 @@ public PetPatient toModelType() throws IllegalValueException { if (this.name == null) { throw new IllegalValueException( - String.format(MISSING_FIELD_MESSAGE_FORMAT, PetPatientName.class.getSimpleName())); + String.format(MISSING_NAME_FIELD_MESSAGE_FORMAT, PetPatientName.class.getSimpleName())); } if (!PetPatientName.isValidName(this.name)) { throw new IllegalValueException(PetPatientName.MESSAGE_PET_NAME_CONSTRAINTS); @@ -91,23 +106,32 @@ public PetPatient toModelType() throws IllegalValueException { final PetPatientName name = new PetPatientName(this.name); if (this.species == null) { - throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT)); + throw new IllegalValueException(String.format(MISSING_SPECIES_FIELD_MESSAGE_FORMAT)); } if (this.breed == null) { - throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT)); + throw new IllegalValueException(String.format(MISSING_BREED_FIELD_MESSAGE_FORMAT)); } if (this.colour == null) { - throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT)); + throw new IllegalValueException(String.format(MISSING_COLOUR_FIELD_MESSAGE_FORMAT)); } if (this.bloodType == null) { - throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT)); + throw new IllegalValueException(String.format(MISSING_BLOODTYPE_FIELD_MESSAGE_FORMAT)); + } + + if (this.ownerNric == null) { + throw new IllegalValueException( + String.format(MISSING_OWNER_FIELD_MESSAGE_FORMAT, PetPatientName.class.getSimpleName())); + } + if (!Nric.isValidNric(this.ownerNric)) { + throw new IllegalValueException(Nric.MESSAGE_NRIC_CONSTRAINTS); } + final Nric ownerNric = new Nric(this.ownerNric); final Set tags = new HashSet<>(petPatientTags); - return new PetPatient(name, species, breed, colour, bloodType, tags); + return new PetPatient(name, species, breed, colour, bloodType, ownerNric, tags); } @Override @@ -126,6 +150,7 @@ public boolean equals(Object other) { && Objects.equals(breed, otherPetPatient.breed) && Objects.equals(colour, otherPetPatient.colour) && Objects.equals(bloodType, otherPetPatient.bloodType) + && Objects.equals(ownerNric, otherPetPatient.ownerNric) && tagged.equals(otherPetPatient.tagged); } } diff --git a/src/test/java/seedu/address/storage/XmlAdaptedPetPatientTest.java b/src/test/java/seedu/address/storage/XmlAdaptedPetPatientTest.java new file mode 100644 index 000000000000..899fc41f1c97 --- /dev/null +++ b/src/test/java/seedu/address/storage/XmlAdaptedPetPatientTest.java @@ -0,0 +1,181 @@ +package seedu.address.storage; + +import static org.junit.Assert.assertEquals; +import static seedu.address.storage.XmlAdaptedPetPatient.MISSING_BLOODTYPE_FIELD_MESSAGE_FORMAT; +import static seedu.address.storage.XmlAdaptedPetPatient.MISSING_BREED_FIELD_MESSAGE_FORMAT; +import static seedu.address.storage.XmlAdaptedPetPatient.MISSING_COLOUR_FIELD_MESSAGE_FORMAT; +import static seedu.address.storage.XmlAdaptedPetPatient.MISSING_NAME_FIELD_MESSAGE_FORMAT; +import static seedu.address.storage.XmlAdaptedPetPatient.MISSING_OWNER_FIELD_MESSAGE_FORMAT; +import static seedu.address.storage.XmlAdaptedPetPatient.MISSING_SPECIES_FIELD_MESSAGE_FORMAT; +import static seedu.address.testutil.TypicalPetPatients.JEWEL; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.Test; + +import seedu.address.commons.exceptions.IllegalValueException; +import seedu.address.model.petpatient.PetPatientName; +import seedu.address.testutil.Assert; + +public class XmlAdaptedPetPatientTest { + private static final String INVALID_NAME = "H@zel"; + private static final String EMPTY_FIELD = ""; + private static final String INVALID_TAG = "#friend"; + + private static final String VALID_NAME = JEWEL.getName().toString(); + private static final String VALID_SPECIES = JEWEL.getSpecies(); + private static final String VALID_BREED = JEWEL.getBreed(); + private static final String VALID_COLOUR = JEWEL.getColour(); + private static final String VALID_BLOODTYPE = JEWEL.getBloodType(); + private static final String VALID_OWNER = JEWEL.getOwner().toString(); + private static final List VALID_TAGS = JEWEL.getTags().stream() + .map(XmlAdaptedTag::new) + .collect(Collectors.toList()); + + @Test + public void toModelType_validPetPatientDetails_returnsPetPatient() throws Exception { + XmlAdaptedPetPatient petPatient = new XmlAdaptedPetPatient(JEWEL); + assertEquals(JEWEL, petPatient.toModelType()); + } + + @Test + public void toModelType_invalidName_throwsIllegalValueException() { + XmlAdaptedPetPatient petPatient = new XmlAdaptedPetPatient( + INVALID_NAME, + VALID_SPECIES, + VALID_BREED, + VALID_COLOUR, + VALID_BLOODTYPE, + VALID_OWNER, + VALID_TAGS + ); + String expectedMessage = PetPatientName.MESSAGE_PET_NAME_CONSTRAINTS; + Assert.assertThrows(IllegalValueException.class, expectedMessage, petPatient::toModelType); + } + + @Test + public void toModelType_nullName_throwsIllegalValueException() { + XmlAdaptedPetPatient petPatient = new XmlAdaptedPetPatient( + null, + VALID_SPECIES, + VALID_BREED, + VALID_COLOUR, + VALID_BLOODTYPE, + VALID_OWNER, + VALID_TAGS + ); + String expectedMessage = String.format( + MISSING_NAME_FIELD_MESSAGE_FORMAT, + PetPatientName.class.getSimpleName() + ); + Assert.assertThrows(IllegalValueException.class, expectedMessage, petPatient::toModelType); + } + + @Test + public void toModelType_nullSpecies_throwsIllegalValueException() { + XmlAdaptedPetPatient petPatient = new XmlAdaptedPetPatient( + VALID_NAME, + null, + VALID_BREED, + VALID_COLOUR, + VALID_BLOODTYPE, + VALID_OWNER, + VALID_TAGS + ); + String expectedMessage = String.format( + MISSING_SPECIES_FIELD_MESSAGE_FORMAT, + PetPatientName.class.getSimpleName() + ); + Assert.assertThrows(IllegalValueException.class, expectedMessage, petPatient::toModelType); + } + + @Test + public void toModelType_nullBreed_throwsIllegalValueException() { + XmlAdaptedPetPatient petPatient = new XmlAdaptedPetPatient( + VALID_NAME, + VALID_SPECIES, + null, + VALID_COLOUR, + VALID_BLOODTYPE, + VALID_OWNER, + VALID_TAGS + ); + String expectedMessage = String.format( + MISSING_BREED_FIELD_MESSAGE_FORMAT, + PetPatientName.class.getSimpleName() + ); + Assert.assertThrows(IllegalValueException.class, expectedMessage, petPatient::toModelType); + } + + @Test + public void toModelType_nullColour_throwsIllegalValueException() { + XmlAdaptedPetPatient petPatient = new XmlAdaptedPetPatient( + VALID_NAME, + VALID_SPECIES, + VALID_BREED, + null, + VALID_BLOODTYPE, + VALID_OWNER, + VALID_TAGS + ); + String expectedMessage = String.format( + MISSING_COLOUR_FIELD_MESSAGE_FORMAT, + PetPatientName.class.getSimpleName() + ); + Assert.assertThrows(IllegalValueException.class, expectedMessage, petPatient::toModelType); + } + + @Test + public void toModelType_nullBloodType_throwsIllegalValueException() { + XmlAdaptedPetPatient petPatient = new XmlAdaptedPetPatient( + VALID_NAME, + VALID_SPECIES, + VALID_BREED, + VALID_COLOUR, + null, + VALID_OWNER, + VALID_TAGS + ); + String expectedMessage = String.format( + MISSING_BLOODTYPE_FIELD_MESSAGE_FORMAT, + PetPatientName.class.getSimpleName() + ); + Assert.assertThrows(IllegalValueException.class, expectedMessage, petPatient::toModelType); + } + + @Test + public void toModelType_nullOwner_throwsIllegalValueException() { + XmlAdaptedPetPatient petPatient = new XmlAdaptedPetPatient( + VALID_NAME, + VALID_SPECIES, + VALID_BREED, + VALID_COLOUR, + VALID_BLOODTYPE, + null, + VALID_TAGS + ); + String expectedMessage = String.format( + MISSING_OWNER_FIELD_MESSAGE_FORMAT, + PetPatientName.class.getSimpleName() + ); + Assert.assertThrows(IllegalValueException.class, expectedMessage, petPatient::toModelType); + } + + @Test + public void toModelType_invalidTags_throwsIllegalValueException() { + List invalidTags = new ArrayList<>(VALID_TAGS); + invalidTags.add(new XmlAdaptedTag(INVALID_TAG)); + XmlAdaptedPetPatient petPatient = new XmlAdaptedPetPatient( + VALID_NAME, + VALID_SPECIES, + VALID_BREED, + VALID_COLOUR, + VALID_BLOODTYPE, + VALID_OWNER, + invalidTags + ); + Assert.assertThrows(IllegalValueException.class, petPatient::toModelType); + } +} diff --git a/src/test/java/seedu/address/testutil/PetPatientBuilder.java b/src/test/java/seedu/address/testutil/PetPatientBuilder.java new file mode 100644 index 000000000000..18568b70e7c3 --- /dev/null +++ b/src/test/java/seedu/address/testutil/PetPatientBuilder.java @@ -0,0 +1,118 @@ +package seedu.address.testutil; + +import java.util.HashSet; +import java.util.Set; + +import seedu.address.model.person.Nric; +import seedu.address.model.petpatient.PetPatient; +import seedu.address.model.petpatient.PetPatientName; +import seedu.address.model.tag.Tag; + +import seedu.address.model.util.SampleDataUtil; + +/** + * A utility class to help with building PetPatient objects. + */ +public class PetPatientBuilder { + public static final String DEFAULT_NAME = "Joseph"; + public static final String DEFAULT_SPECIES = "Cat"; + public static final String DEFAULT_BREED = "Persian Ragdoll"; + public static final String DEFAULT_COLOUR = "Brown"; + public static final String DEFAULT_BLOODTYPE = "AB"; + public static final String DEFAULT_OWNER = "G1234567B"; + public static final String DEFAULT_TAGS = "Injured"; + + private PetPatientName name; + private String species; + private String breed; + private String colour; + private String bloodType; + private Nric ownerNric; + private Set tags; + + public PetPatientBuilder() { + name = new PetPatientName(DEFAULT_NAME); + species = DEFAULT_SPECIES; + breed = DEFAULT_BREED; + colour = DEFAULT_COLOUR; + bloodType = DEFAULT_BLOODTYPE; + ownerNric = new Nric(DEFAULT_OWNER); + tags = SampleDataUtil.getTagSet(DEFAULT_TAGS); + } + + /** + * Initializes the PetPatientBuilder with the data of {@code petPatientToCopy}. + */ + public PetPatientBuilder(PetPatient petPatientToCopy) { + name = petPatientToCopy.getName(); + species = petPatientToCopy.getSpecies(); + breed = petPatientToCopy.getBreed(); + colour = petPatientToCopy.getColour(); + bloodType = petPatientToCopy.getBloodType(); + ownerNric = petPatientToCopy.getOwner(); + tags = new HashSet<>(petPatientToCopy.getTags()); + } + + /** + * Sets the {@code PetPatientName} of the {@code PetPatient} that we are building. + */ + public PetPatientBuilder withName(String name) { + this.name = new PetPatientName(name); + return this; + } + + /** + * Parses the {@code tags} into a {@code Set} and set it to the {@code PetPatient} that we are building. + */ + public PetPatientBuilder withTags(String ... tags) { + this.tags = SampleDataUtil.getTagSet(tags); + return this; + } + + /** + * Sets the species of the {@code PetPatient} that we are building. + */ + public PetPatientBuilder withSpecies(String species) { + this.species = species; + return this; + } + + /** + * Sets the breed of the {@code PetPatient} that we are building. + */ + public PetPatientBuilder withBreed(String breed) { + this.breed = breed; + return this; + } + + /** + * Sets the colour of the {@code PetPatient} that we are building. + */ + public PetPatientBuilder withColour(String colour) { + this.colour = colour; + return this; + } + + /** + * Sets the blood type of the {@code PetPatient} that we are building. + */ + public PetPatientBuilder withBloodType(String bloodType) { + this.bloodType = bloodType; + return this; + } + + /** + * Sets the {@code Nric} of the {@code PetPatient} that we are building. + * @param nric + * @return + */ + public PetPatientBuilder withOwnerNric(String nric) { + this.ownerNric = new Nric(nric); + return this; + } + + public PetPatient build() { + return new PetPatient(name, species, breed, colour, bloodType, ownerNric, tags); + } + +} diff --git a/src/test/java/seedu/address/testutil/TypicalPetPatients.java b/src/test/java/seedu/address/testutil/TypicalPetPatients.java index 3d4713efadf2..983a62b2b388 100644 --- a/src/test/java/seedu/address/testutil/TypicalPetPatients.java +++ b/src/test/java/seedu/address/testutil/TypicalPetPatients.java @@ -1,13 +1,29 @@ package seedu.address.testutil; import seedu.address.model.petpatient.PetPatient; -import seedu.address.model.petpatient.PetPatientName; /** * A utility class containing a list of {@code PetPatient} objects to be used in tests. */ public class TypicalPetPatients { - public static final PetPatient JOKER = new PetPatient(new PetPatientName("joker"), "cat", - "domestic shorthair", "brown and white", "O", TypicalPersons.BOB.getNric(), null); + public static final PetPatient JOKER = new PetPatientBuilder() + .withName("Joker") + .withSpecies("Cat") + .withBreed("Domestic Shorthair") + .withColour("Brown and White") + .withBloodType("O") + .withOwnerNric(TypicalPersons.BOB.getNric().toString()) + .withTags("Injured").build(); + + public static final PetPatient JEWEL = new PetPatientBuilder() + .withName("Jewel") + .withSpecies("Cat") + .withBreed("Persian Ragdoll") + .withColour("Calico") + .withBloodType("AB") + .withOwnerNric(TypicalPersons.ALICE.getNric().toString()) + .withTags(new String[]{}).build(); + + private TypicalPetPatients() {} }