Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY1718S2#75 from CS2103JAN2018-F14-B2/u…
Browse files Browse the repository at this point in the history
…pdateStorageForAddCommand

Storage Enhancements for PetPatients
  • Loading branch information
Aquarinte authored Mar 27, 2018
2 parents 08615d9 + 7697f1a commit 2be17fd
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
41 changes: 33 additions & 8 deletions src/main/java/seedu/address/storage/XmlAdaptedPetPatient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<XmlAdaptedTag> tagged = new ArrayList<>();
@XmlElement
private String medicalHistory;

/**
* Constructs an XmlAdaptedPetPatient.
Expand All @@ -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<XmlAdaptedTag> tagged) {
String bloodType, String ownerNric, List<XmlAdaptedTag> 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);
}
Expand All @@ -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));
Expand All @@ -83,31 +98,40 @@ 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);
}
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<Tag> tags = new HashSet<>(petPatientTags);
return new PetPatient(name, species, breed, colour, bloodType, tags);
return new PetPatient(name, species, breed, colour, bloodType, ownerNric, tags);
}

@Override
Expand All @@ -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);
}
}
181 changes: 181 additions & 0 deletions src/test/java/seedu/address/storage/XmlAdaptedPetPatientTest.java
Original file line number Diff line number Diff line change
@@ -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<XmlAdaptedTag> 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<XmlAdaptedTag> 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);
}
}
Loading

0 comments on commit 2be17fd

Please sign in to comment.