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 { ...@@ -92,8 +92,16 @@ public class PetsDAO extends DAO {
*/ */
public Pet add(String name, String specie, int idOwner) public Pet add(String name, String specie, int idOwner)
throws DAOException, IllegalArgumentException { throws DAOException, IllegalArgumentException {
if (name == null || specie == null) { if (name == null) {
throw new IllegalArgumentException("name and specie can't be null " + specie); 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()) { try (Connection conn = this.getConnection()) {
...@@ -197,7 +205,7 @@ public class PetsDAO extends DAO { ...@@ -197,7 +205,7 @@ public class PetsDAO extends DAO {
public List<Pet> getPets(int idOwner) public List<Pet> getPets(int idOwner)
throws DAOException, IllegalArgumentException { throws DAOException, IllegalArgumentException {
if ( idOwner <= 0) { if ( idOwner < 0) {
throw new IllegalArgumentException("idOwner can't be negative"); throw new IllegalArgumentException("idOwner can't be negative");
} }
......
...@@ -2,6 +2,7 @@ package es.uvigo.esei.daa.entities; ...@@ -2,6 +2,7 @@ package es.uvigo.esei.daa.entities;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
/** /**
* An entity that represents a pet. * An entity that represents a pet.
* *
...@@ -89,7 +90,10 @@ public class Pet { ...@@ -89,7 +90,10 @@ public class Pet {
* @throws NullPointerException if the {@code idOwner} is {@code null}. * @throws NullPointerException if the {@code idOwner} is {@code null}.
*/ */
public void setIdOwner(int idOwner) { 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 @Override
......
...@@ -9,6 +9,7 @@ import static es.uvigo.esei.daa.dataset.PetsDataset.newSpecie; ...@@ -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.newIdOwner;
import static es.uvigo.esei.daa.dataset.PetsDataset.nonExistentId; 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.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.nonExistentPet;
import static es.uvigo.esei.daa.dataset.PetsDataset.pets; import static es.uvigo.esei.daa.dataset.PetsDataset.pets;
import static es.uvigo.esei.daa.dataset.PetsDataset.petsOwner; import static es.uvigo.esei.daa.dataset.PetsDataset.petsOwner;
...@@ -72,6 +73,11 @@ public class PetsDAOTest { ...@@ -72,6 +73,11 @@ public class PetsDAOTest {
assertThat(this.dao.getPets(existentIdOwner()), containsPetsInAnyOrder(petsOwner(existentIdOwner()))); assertThat(this.dao.getPets(existentIdOwner()), containsPetsInAnyOrder(petsOwner(existentIdOwner())));
} }
@Test(expected = IllegalArgumentException.class)
public void testGetPetsNegativeIdOwner() throws DAOException {
this.dao.getPets(negativeIdOwner());
}
@Test @Test
public void testGet() throws DAOException { public void testGet() throws DAOException {
final Pet pet = this.dao.get(existentId()); final Pet pet = this.dao.get(existentId());
...@@ -83,7 +89,7 @@ public class PetsDAOTest { ...@@ -83,7 +89,7 @@ public class PetsDAOTest {
public void testGetNonExistentId() throws DAOException { public void testGetNonExistentId() throws DAOException {
this.dao.get(nonExistentId()); this.dao.get(nonExistentId());
} }
@Test @Test
@ExpectedDatabase("/datasets/dataset-delete-pets.xml") @ExpectedDatabase("/datasets/dataset-delete-pets.xml")
public void testDelete() throws DAOException { public void testDelete() throws DAOException {
...@@ -140,7 +146,12 @@ public class PetsDAOTest { ...@@ -140,7 +146,12 @@ public class PetsDAOTest {
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testAddNullSurname() throws DAOException { public void testAddNullSpecie() throws DAOException {
this.dao.add(newName(), null, newIdOwner()); 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 { ...@@ -72,6 +72,10 @@ public class PetsDataset {
return 1234; return 1234;
} }
public static int negativeIdOwner() {
return -1;
}
public static Pet existentPet() { public static Pet existentPet() {
return pet(existentId()); return pet(existentId());
} }
......
...@@ -36,10 +36,10 @@ public class PetUnitTest { ...@@ -36,10 +36,10 @@ public class PetUnitTest {
new Pet(1, "Rex", null, 1); new Pet(1, "Rex", null, 1);
} }
// @Test(expected = Error.class) @Test(expected = IllegalArgumentException.class)
// public void testPetIntStringStringIntNullidOwner() { public void testPetIntStringStringIntNegativeIdOwner() {
// new Pet(1, "Rex", "Dog", null); new Pet(1, "Rex", "Dog", -1);
// } }
@Test @Test
public void testSetName() { public void testSetName() {
...@@ -100,12 +100,12 @@ public class PetUnitTest { ...@@ -100,12 +100,12 @@ public class PetUnitTest {
assertThat(pet.getIdOwner(), is(equalTo(1))); assertThat(pet.getIdOwner(), is(equalTo(1)));
} }
// @Test(expected = Error.class) @Test(expected = IllegalArgumentException.class)
// public void testSetNullIdOwner() { public void testSetNegativeIdOwner() {
// final Pet pet = new Pet(1, "Rex", "Dog", null); final Pet pet = new Pet(1, "Rex", "Dog", 1);
//
// pet.setSpecie(null); pet.setIdOwner(-1);
// } }
@Test @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