Commit 8a27be78 authored by miferreiro's avatar miferreiro

Adds tests to check the idOwner of the pet entity

Now, it is checked whether the negative idOwner correctly throws the exception IllegalArgumentException.
parent 60457198
......@@ -92,8 +92,16 @@ public class PetsDAO extends DAO {
*/
public Pet add(String name, String specie, int idOwner)
throws DAOException, IllegalArgumentException {
if (name == null || specie == null) {
throw new IllegalArgumentException("name and specie can't be null " + specie);
if (name == null) {
throw new IllegalArgumentException("name can't be null " + name);
}
if ( specie == null) {
throw new IllegalArgumentException("specie can't be null " + specie);
}
if(idOwner < 0) {
throw new IllegalArgumentException("idOwner can't be negative " + specie);
}
try (Connection conn = this.getConnection()) {
......@@ -197,7 +205,7 @@ public class PetsDAO extends DAO {
public List<Pet> getPets(int idOwner)
throws DAOException, IllegalArgumentException {
if ( idOwner <= 0) {
if ( idOwner < 0) {
throw new IllegalArgumentException("idOwner can't be negative");
}
......
......@@ -2,6 +2,7 @@ package es.uvigo.esei.daa.entities;
import static java.util.Objects.requireNonNull;
/**
* An entity that represents a pet.
*
......@@ -89,7 +90,10 @@ public class Pet {
* @throws NullPointerException if the {@code idOwner} is {@code null}.
*/
public void setIdOwner(int idOwner) {
this.idOwner = requireNonNull(idOwner, "idOwner can't be null");
if(idOwner < 1) {
throw new IllegalArgumentException(idOwner + " can't be less than 0");
}
this.idOwner = idOwner;
}
@Override
......
......@@ -9,6 +9,7 @@ import static es.uvigo.esei.daa.dataset.PetsDataset.newSpecie;
import static es.uvigo.esei.daa.dataset.PetsDataset.newIdOwner;
import static es.uvigo.esei.daa.dataset.PetsDataset.nonExistentId;
import static es.uvigo.esei.daa.dataset.PetsDataset.nonExistentIdOwner;
import static es.uvigo.esei.daa.dataset.PetsDataset.negativeIdOwner;
import static es.uvigo.esei.daa.dataset.PetsDataset.nonExistentPet;
import static es.uvigo.esei.daa.dataset.PetsDataset.pets;
import static es.uvigo.esei.daa.dataset.PetsDataset.petsOwner;
......@@ -72,6 +73,11 @@ public class PetsDAOTest {
assertThat(this.dao.getPets(existentIdOwner()), containsPetsInAnyOrder(petsOwner(existentIdOwner())));
}
@Test(expected = IllegalArgumentException.class)
public void testGetPetsNegativeIdOwner() throws DAOException {
this.dao.getPets(negativeIdOwner());
}
@Test
public void testGet() throws DAOException {
final Pet pet = this.dao.get(existentId());
......@@ -83,7 +89,7 @@ public class PetsDAOTest {
public void testGetNonExistentId() throws DAOException {
this.dao.get(nonExistentId());
}
@Test
@ExpectedDatabase("/datasets/dataset-delete-pets.xml")
public void testDelete() throws DAOException {
......@@ -140,7 +146,12 @@ public class PetsDAOTest {
}
@Test(expected = IllegalArgumentException.class)
public void testAddNullSurname() throws DAOException {
public void testAddNullSpecie() throws DAOException {
this.dao.add(newName(), null, newIdOwner());
}
@Test(expected = IllegalArgumentException.class)
public void testAddNegativeIdOwner() throws DAOException {
this.dao.add(newName(), newSpecie(),negativeIdOwner());
}
}
......@@ -72,6 +72,10 @@ public class PetsDataset {
return 1234;
}
public static int negativeIdOwner() {
return -1;
}
public static Pet existentPet() {
return pet(existentId());
}
......
......@@ -36,10 +36,10 @@ public class PetUnitTest {
new Pet(1, "Rex", null, 1);
}
// @Test(expected = Error.class)
// public void testPetIntStringStringIntNullidOwner() {
// new Pet(1, "Rex", "Dog", null);
// }
@Test(expected = IllegalArgumentException.class)
public void testPetIntStringStringIntNegativeIdOwner() {
new Pet(1, "Rex", "Dog", -1);
}
@Test
public void testSetName() {
......@@ -100,12 +100,12 @@ public class PetUnitTest {
assertThat(pet.getIdOwner(), is(equalTo(1)));
}
// @Test(expected = Error.class)
// public void testSetNullIdOwner() {
// final Pet pet = new Pet(1, "Rex", "Dog", null);
//
// pet.setSpecie(null);
// }
@Test(expected = IllegalArgumentException.class)
public void testSetNegativeIdOwner() {
final Pet pet = new Pet(1, "Rex", "Dog", 1);
pet.setIdOwner(-1);
}
@Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment