diff --git a/src/main/java/es/uvigo/esei/daa/entities/Pet.java b/src/main/java/es/uvigo/esei/daa/entities/Pet.java index 5120014db8e5b6f2ebbb04a4c4bb758e9e0d6038..841e7fb92091f418e650c1d72f5f8f1ffb6a5aed 100644 --- a/src/main/java/es/uvigo/esei/daa/entities/Pet.java +++ b/src/main/java/es/uvigo/esei/daa/entities/Pet.java @@ -94,6 +94,25 @@ public class Pet this.especie = requireNonNull(especie, "Specie can't be null"); } - //Faltan los override, preguntarle a Martín + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + id; + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Pet)) + return false; + Pet other = (Pet) obj; + if (id != other.id) + return false; + return true; + } } diff --git a/src/test/java/es/uvigo/esei/daa/dao/PetDAOTest.java b/src/test/java/es/uvigo/esei/daa/dao/PetDAOTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d04be8cb2f726e26724357f2da8cf38dd75372a7 --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/dao/PetDAOTest.java @@ -0,0 +1,126 @@ +package es.uvigo.esei.daa.dao; + +import com.github.springtestdbunit.DbUnitTestExecutionListener; +import com.github.springtestdbunit.annotation.DatabaseSetup; +import com.github.springtestdbunit.annotation.ExpectedDatabase; +import es.uvigo.esei.daa.entities.Pet; +import es.uvigo.esei.daa.listeners.ApplicationContextBinding; +import es.uvigo.esei.daa.listeners.ApplicationContextJndiBindingTestExecutionListener; +import es.uvigo.esei.daa.listeners.DbManagement; +import es.uvigo.esei.daa.listeners.DbManagementTestExecutionListener; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import javax.sql.DataSource; + +import static es.uvigo.esei.daa.dataset.PetDataset.*; +import static es.uvigo.esei.daa.matchers.IsEqualToPet.containsPetInAnyOrder; +import static es.uvigo.esei.daa.matchers.IsEqualToPet.equalsToPet; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:contexts/mem-context.xml") +@TestExecutionListeners({ + DbUnitTestExecutionListener.class, + DbManagementTestExecutionListener.class, + ApplicationContextJndiBindingTestExecutionListener.class +}) +@ApplicationContextBinding( + jndiUrl = "java:/comp/env/jdbc/daaexample", + type = DataSource.class +) +@DbManagement( + create = "classpath:db/hsqldb.sql", + drop = "classpath:db/hsqldb-drop.sql" +) +@DatabaseSetup("/datasets/dataset.xml") +@ExpectedDatabase("/datasets/dataset.xml") +public class PetDAOTest { + private PetDAO dao; + + @Before + public void setUp() throws Exception { + this.dao = new PetDAO(); + } + + @Test + public void testList() throws DAOException { + assertThat(this.dao.list(), containsPetInAnyOrder(pets())); + } + + @Test + public void testGet() throws DAOException { + final Pet pet = this.dao.get(existentId()); + + assertThat(pet, is(equalsToPet(existentPet()))); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetNonExistentId() throws DAOException { + this.dao.get(nonExistentId()); + } + + @Test + @ExpectedDatabase("/datasets/dataset-delete.xml") + public void testDelete() throws DAOException { + this.dao.delete(existentId()); + + assertThat(this.dao.list(), containsPetInAnyOrder(petWithout(existentId()))); + } + + @Test(expected = IllegalArgumentException.class) + public void testDeleteNonExistentId() throws DAOException { + this.dao.delete(nonExistentId()); + } + + @Test + @ExpectedDatabase("/datasets/dataset-modify.xml") + public void testModify() throws DAOException { + final Pet pet = existentPet(); + pet.setNombre(newName()); + pet.setEspecie(newEspecie()); + + this.dao.modify(pet); + + final Pet persistentPet = this.dao.get(pet.getId()); + + assertThat(persistentPet, is(equalsToPet(pet))); + } + + @Test(expected = IllegalArgumentException.class) + public void testModifyNonExistentId() throws DAOException { + this.dao.modify(nonExistentPet()); + } + + @Test(expected = IllegalArgumentException.class) + public void testModifyNullPerson() throws DAOException { + this.dao.modify(null); + } + + @Test + @ExpectedDatabase("/datasets/dataset-add.xml") + public void testAdd() throws DAOException { + final Pet pet = this.dao.add(1, newName(), newEspecie()); + + assertThat(pet, is(equalsToPet(newPet()))); + + final Pet persistentPet = this.dao.get(pet.getId()); + + assertThat(persistentPet, is(equalsToPet(newPet()))); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddNullName() throws DAOException { + this.dao.add(1,null, newEspecie()); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddNullSurname() throws DAOException { + this.dao.add(1, newName(), null); + } +} diff --git a/src/test/java/es/uvigo/esei/daa/dao/PetDAOUnitTest.java b/src/test/java/es/uvigo/esei/daa/dao/PetDAOUnitTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f9a6be0aecb1486a6f52cf64bf6d79b2bfc35142 --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/dao/PetDAOUnitTest.java @@ -0,0 +1,250 @@ +package es.uvigo.esei.daa.dao; + +import com.mysql.jdbc.Statement; +import es.uvigo.esei.daa.entities.Pet; +import es.uvigo.esei.daa.util.DatabaseQueryUnitTest; +import org.junit.Test; + +import java.sql.SQLException; + +import static es.uvigo.esei.daa.dataset.PetDataset.*; +import static es.uvigo.esei.daa.matchers.IsEqualToPet.containsPetInAnyOrder; +import static es.uvigo.esei.daa.matchers.IsEqualToPet.equalsToPet; +import static org.easymock.EasyMock.*; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class PetDAOUnitTest extends DatabaseQueryUnitTest { + @Test + public void testList() throws Exception { + final Pet[] pet = pets(); + + for (Pet pets : pet) { + expectPetRow(pets); + } + expect(result.next()).andReturn(false); + result.close(); + + replayAll(); + final PetDAO petDAO = new PetDAO(); + + assertThat(petDAO.list(), containsPetInAnyOrder(pet)); + } + + @Test(expected = DAOException.class) + public void testListUnexpectedException() throws Exception { + expect(result.next()).andThrow(new SQLException()); + result.close(); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + petDAO.list(); + } + + @Test + public void testGet() throws Exception { + final Pet existentPet = existentPet(); + + expectPetRow(existentPet); + result.close(); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + + assertThat(petDAO.get(existentId()), is(equalTo(existentPet))); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetMissing() throws Exception { + expect(result.next()).andReturn(false); + result.close(); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + petDAO.get(existentId()); + } + + @Test(expected = DAOException.class) + public void testGetUnexpectedException() throws Exception { + expect(result.next()).andThrow(new SQLException()); + result.close(); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + petDAO.get(existentId()); + } + + @Test + public void testAdd() throws Exception { + final Pet pet = newPet(); + reset(connection); + expect(connection.prepareStatement(anyString(), eq(Statement.RETURN_GENERATED_KEYS))) + .andReturn(statement); + expect(statement.executeUpdate()).andReturn(1); + expect(statement.getGeneratedKeys()).andReturn(result); + + // Key retrieval + expect(result.next()).andReturn(true); + expect(result.getInt(1)).andReturn(pet.getId()); + connection.close(); + result.close(); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + final Pet newPet = petDAO.add(1, pet.getNombre(), pet.getEspecie()); + + assertThat(newPet, is(equalsToPet(pet))); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddNullName() throws Exception { + replayAll(); + + final PetDAO petDAO = new PetDAO(); + + resetAll(); // No expectations + + petDAO.add(1,null, newEspecie()); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddNullSurname() throws Exception { + replayAll(); + + final PetDAO petDAO = new PetDAO(); + + resetAll(); // No expectations + + petDAO.add(1,newName(), null); + } + + @Test(expected = DAOException.class) + public void testAddZeroUpdatedRows() throws Exception { + reset(connection); + expect(connection.prepareStatement(anyString(), eq(1))) + .andReturn(statement); + expect(statement.executeUpdate()).andReturn(0); + connection.close(); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + petDAO.add(1, newName(), newEspecie()); + } + + @Test(expected = DAOException.class) + public void testAddNoGeneratedKey() throws Exception { + reset(connection); + expect(connection.prepareStatement(anyString(), eq(1))) + .andReturn(statement); + expect(statement.executeUpdate()).andReturn(1); + expect(statement.getGeneratedKeys()).andReturn(result); + expect(result.next()).andReturn(false); + result.close(); + connection.close(); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + petDAO.add(1, newName(), newEspecie()); + } + + @Test(expected = DAOException.class) + public void testAddUnexpectedException() throws Exception { + reset(connection); + expect(connection.prepareStatement(anyString(), eq(1))) + .andReturn(statement); + expect(statement.executeUpdate()).andThrow(new SQLException()); + connection.close(); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + petDAO.add(1,newName(), newEspecie()); + } + + @Test + public void testDelete() throws Exception { + expect(statement.executeUpdate()).andReturn(1); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + petDAO.delete(existentId()); + } + + @Test(expected = IllegalArgumentException.class) + public void testDeleteInvalidId() throws Exception { + expect(statement.executeUpdate()).andReturn(0); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + petDAO.delete(existentId()); + } + + @Test(expected = DAOException.class) + public void testDeleteUnexpectedException() throws Exception { + expect(statement.executeUpdate()).andThrow(new SQLException()); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + petDAO.delete(existentId()); + } + + @Test + public void testModify() throws Exception { + expect(statement.executeUpdate()).andReturn(1); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + petDAO.modify(existentPet()); + } + + @Test(expected = IllegalArgumentException.class) + public void testModifyNullPet() throws Exception { + replayAll(); + + final PetDAO petDAO = new PetDAO(); + + resetAll(); // No expectations + + petDAO.modify(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testModifyZeroUpdatedRows() throws Exception { + expect(statement.executeUpdate()).andReturn(0); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + petDAO.modify(existentPet()); + } + + @Test(expected = DAOException.class) + public void testModifyUnexpectedException() throws Exception { + expect(statement.executeUpdate()).andThrow(new SQLException()); + + replayAll(); + + final PetDAO petDAO = new PetDAO(); + petDAO.modify(existentPet()); + } + + private void expectPetRow(Pet pet) throws SQLException { + expect(result.next()).andReturn(true); + expect(result.getInt("id")).andReturn(pet.getId()); + expect(result.getInt("personId")).andReturn(pet.getId()); + expect(result.getString("nombre")).andReturn(pet.getNombre()); + expect(result.getString("especie")).andReturn(pet.getEspecie()); + } +} diff --git a/src/test/java/es/uvigo/esei/daa/dataset/PetDataset.java b/src/test/java/es/uvigo/esei/daa/dataset/PetDataset.java new file mode 100644 index 0000000000000000000000000000000000000000..857e50612ee8f2572a1110c0241357099b97705b --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/dataset/PetDataset.java @@ -0,0 +1,74 @@ +package es.uvigo.esei.daa.dataset; + +import es.uvigo.esei.daa.entities.Pet; + +import java.util.Arrays; +import java.util.function.Predicate; + +import static java.util.Arrays.binarySearch; +import static java.util.Arrays.stream; + +public final class PetDataset { + private PetDataset() {} + + public static Pet[] pets() { + return new Pet[] { + new Pet(1, 1, "Dog", "Dog"), + new Pet(2, 1, "Perro", "Dog"), + new Pet(3, 1, "Per","Dog"), + new Pet(4, 1, "Soso","Dog"), + new Pet(5, 1, "Lino","Dog"), + new Pet(6, 1, "Lar","Dog"), + new Pet(7, 1, "Raul","Dog"), + new Pet(8, 1, "Man","Dog"), + new Pet(9, 1, "Justa","Dog"), + new Pet(10, 1, "Lita","Dog") + }; + } + + public static Pet[] petWithout(int ... ids) { + Arrays.sort(ids); + + final Predicate hasValidId = Pet -> + binarySearch(ids, Pet.getId()) < 0; + + return stream(pets()) + .filter(hasValidId) + .toArray(Pet[]::new); + } + + public static Pet Pet(int id) { + return stream(pets()) + .filter(Pet -> Pet.getId() == id) + .findAny() + .orElseThrow(IllegalArgumentException::new); + } + + public static int existentId() { + return 5; + } + + public static int nonExistentId() { + return 1234; + } + + public static Pet existentPet() { + return Pet(existentId()); + } + + public static Pet nonExistentPet() { + return new Pet(nonExistentId(), 1,"Dogu", "Smith"); + } + + public static String newName() { + return "Jo"; + } + + public static String newEspecie() { + return "Do"; + } + + public static Pet newPet() { + return new Pet(pets().length + 1,1, newName(), newEspecie()); + } +} diff --git a/src/test/java/es/uvigo/esei/daa/entities/PetUnitTest.java b/src/test/java/es/uvigo/esei/daa/entities/PetUnitTest.java new file mode 100644 index 0000000000000000000000000000000000000000..04f29ef2d74299b1017cc1e805a9deb586470106 --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/entities/PetUnitTest.java @@ -0,0 +1,97 @@ +package es.uvigo.esei.daa.entities; + +import nl.jqno.equalsverifier.EqualsVerifier; +import nl.jqno.equalsverifier.Warning; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +public class PetUnitTest { + @Test + public void testPetIntStringString() { + final int id = 1; + final int personId = 1; + final String name = "Jo"; + final String surname = "Do"; + + final Pet pet = new Pet(id, personId, name, surname); + + assertThat(pet.getId(), is(equalTo(id))); + assertThat(pet.getPersonId(), is(equalTo(personId))); + assertThat(pet.getNombre(), is(equalTo(name))); + assertThat(pet.getEspecie(), is(equalTo(surname))); + } + + @Test(expected = NullPointerException.class) + public void testPetIntStringStringNullName() { + new Pet(1, 1, null, "Do"); + } + + @Test(expected = NullPointerException.class) + public void testPetIntStringStringNullSurname() { + new Pet(1,1, "Jo", null); + } + + @Test + public void testSetName() { + final int id = 1; + final int personId = 1; + final String surname = "Do"; + + final Pet pet = new Pet(id, personId, "Jo", surname); + pet.setNombre("Ricolino"); + + assertThat(pet.getId(), is(equalTo(id))); + assertThat(pet.getPersonId(), is(equalTo(personId))); + assertThat(pet.getNombre(), is(equalTo("Ricolino"))); + assertThat(pet.getEspecie(), is(equalTo(surname))); + } + + @Test(expected = NullPointerException.class) + public void testSetNullName() { + final Pet pet = new Pet(1, 1, "Jo", "Do"); + + pet.setNombre(null); + } + + @Test + public void testSetEspecie() { + final int id = 1; + final int personId = 1; + final String name = "Do"; + + final Pet pet = new Pet(id, personId, name, "Do"); + pet.setEspecie("Dogo"); + + assertThat(pet.getId(), is(equalTo(id))); + assertThat(pet.getNombre(), is(equalTo(name))); + assertThat(pet.getEspecie(), is(equalTo("Dogo"))); + } + + @Test(expected = NullPointerException.class) + public void testSetNullEspecie() { + final Pet pet = new Pet(1, 1, "Jo", "Do"); + + pet.setEspecie(null); + } + + @Test + public void testEqualsObject() { + final Pet petA = new Pet(1,1, "Nombre A", "Especie A"); + final Pet petB = new Pet(1,1, "Nombre B", "Especie B"); + + assertTrue(petA.equals(petB)); + } + + @Test + public void testEqualsHashcode() { + EqualsVerifier.forClass(Pet.class) + .withIgnoredFields("nombre", "especie") + .suppress(Warning.STRICT_INHERITANCE) + .suppress(Warning.NONFINAL_FIELDS) + .verify(); + } +} diff --git a/src/test/java/es/uvigo/esei/daa/matchers/IsEqualToPet.java b/src/test/java/es/uvigo/esei/daa/matchers/IsEqualToPet.java new file mode 100644 index 0000000000000000000000000000000000000000..52601c9933a4aac7166d4462e37e79a8472bf148 --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/matchers/IsEqualToPet.java @@ -0,0 +1,56 @@ +package es.uvigo.esei.daa.matchers; + +import es.uvigo.esei.daa.entities.Pet; +import org.hamcrest.Factory; +import org.hamcrest.Matcher; + +public class IsEqualToPet extends IsEqualToEntity { + public IsEqualToPet(Pet entity) { + super(entity); + } + + @Override + protected boolean matchesSafely(Pet actual) { + this.clearDescribeTo(); + + if (actual == null) { + this.addTemplatedDescription("actual", expected.toString()); + return false; + } else { + return checkAttribute("id", Pet::getId, actual) + && checkAttribute("personId", Pet::getPersonId, actual) + && checkAttribute("nombre", Pet::getNombre, actual) + && checkAttribute("especie", Pet::getEspecie, actual); + } + } + + /** + * Factory method that creates a new {@link IsEqualToEntity} matcher with + * the provided {@link Pet} as the expected value. + * + * @param Pet the expected Pet. + * @return a new {@link IsEqualToEntity} matcher with the provided + * {@link Pet} as the expected value. + */ + @Factory + public static IsEqualToPet equalsToPet(Pet Pet) { + return new IsEqualToPet(Pet); + } + + /** + * Factory method that returns a new {@link Matcher} that includes several + * {@link IsEqualToPet} matchers, each one using an {@link Pet} of the + * provided ones as the expected value. + * + * @param Pets the Pets to be used as the expected values. + * @return a new {@link Matcher} that includes several + * {@link IsEqualToPet} matchers, each one using an {@link Pet} of the + * provided ones as the expected value. + * @see IsEqualToEntity#containsEntityInAnyOrder(java.util.function.Function, Object...) + */ + @Factory + public static Matcher> containsPetInAnyOrder(Pet ... Pets) { + return containsEntityInAnyOrder(IsEqualToPet::equalsToPet, Pets); + } + +} diff --git a/src/test/java/es/uvigo/esei/daa/rest/PetResourceTest.java b/src/test/java/es/uvigo/esei/daa/rest/PetResourceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..387a968baea4425ffc3af3bfec5da0448cffa4a1 --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/rest/PetResourceTest.java @@ -0,0 +1,213 @@ +package es.uvigo.esei.daa.rest; + +import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; +import com.github.springtestdbunit.DbUnitTestExecutionListener; +import com.github.springtestdbunit.annotation.DatabaseSetup; +import com.github.springtestdbunit.annotation.ExpectedDatabase; +import es.uvigo.esei.daa.DAAExampleApplication; +import es.uvigo.esei.daa.entities.Pet; +import es.uvigo.esei.daa.listeners.ApplicationContextBinding; +import es.uvigo.esei.daa.listeners.ApplicationContextJndiBindingTestExecutionListener; +import es.uvigo.esei.daa.listeners.DbManagement; +import es.uvigo.esei.daa.listeners.DbManagementTestExecutionListener; +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import javax.sql.DataSource; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.*; +import java.io.IOException; +import java.util.List; + +import static es.uvigo.esei.daa.dataset.PetDataset.*; +import static es.uvigo.esei.daa.matchers.HasHttpStatus.hasBadRequestStatus; +import static es.uvigo.esei.daa.matchers.HasHttpStatus.hasOkStatus; +import static es.uvigo.esei.daa.matchers.IsEqualToPet.containsPetInAnyOrder; +import static es.uvigo.esei.daa.matchers.IsEqualToPet.equalsToPet; +import static javax.ws.rs.client.Entity.entity; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:contexts/mem-context.xml") +@TestExecutionListeners({ + DbUnitTestExecutionListener.class, + DbManagementTestExecutionListener.class, + ApplicationContextJndiBindingTestExecutionListener.class +}) +@ApplicationContextBinding( + jndiUrl = "java:/comp/env/jdbc/daaexample", + type = DataSource.class +) +@DbManagement( + create = "classpath:db/hsqldb.sql", + drop = "classpath:db/hsqldb-drop.sql" +) +@DatabaseSetup("/datasets/dataset.xml") +@ExpectedDatabase("/datasets/dataset.xml") +public class PetResourceTest extends JerseyTest { + @Override + protected Application configure() { + return new DAAExampleApplication(); + } + + @Override + protected void configureClient(ClientConfig config) { + super.configureClient(config); + + // Enables JSON transformation in client + config.register(JacksonJsonProvider.class); + config.property("com.sun.jersey.api.json.POJOMappingFeature", Boolean.TRUE); + } + + @Test + public void testList() throws IOException { + final Response response = target("pet").request().get(); + assertThat(response, hasOkStatus()); + + final List pet = response.readEntity(new GenericType>(){}); + + assertThat(pet, containsPetInAnyOrder(pets())); + } + + @Test + public void testGet() throws IOException { + final Response response = target("pet/" + existentId()).request().get(); + assertThat(response, hasOkStatus()); + + final Pet pet = response.readEntity(Pet.class); + + assertThat(pet, is(equalsToPet(existentPet()))); + } + + @Test + public void testGetInvalidId() throws IOException { + final Response response = target("pet/" + nonExistentId()).request().get(); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + @ExpectedDatabase("/datasets/dataset-add.xml") + public void testAdd() throws IOException { + final Form form = new Form(); + form.param("nombre", newName()); + form.param("especie", newEspecie()); + + final Response response = target("pet") + .request(MediaType.APPLICATION_JSON_TYPE) + .post(entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); + assertThat(response, hasOkStatus()); + + final Pet pet = response.readEntity(Pet.class); + + assertThat(pet, is(equalsToPet(newPet()))); + } + + @Test + public void testAddMissingName() throws IOException { + final Form form = new Form(); + form.param("especie", newEspecie()); + + final Response response = target("pet") + .request(MediaType.APPLICATION_JSON_TYPE) + .post(entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + public void testAddMissingSpecie() throws IOException { + final Form form = new Form(); + form.param("nombre", newName()); + + final Response response = target("pet") + .request(MediaType.APPLICATION_JSON_TYPE) + .post(entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + @ExpectedDatabase("/datasets/dataset-modify.xml") + public void testModify() throws IOException { + final Form form = new Form(); + form.param("nombre", newName()); + form.param("especie", newEspecie()); + + final Response response = target("pet/" + existentId()) + .request(MediaType.APPLICATION_JSON_TYPE) + .put(entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); + assertThat(response, hasOkStatus()); + + final Pet modifiedPet = response.readEntity(Pet.class); + + final Pet pet = existentPet(); + pet.setNombre(newName()); + pet.setEspecie(newEspecie()); + + assertThat(modifiedPet, is(equalsToPet(pet))); + } + + @Test + public void testModifyName() throws IOException { + final Form form = new Form(); + form.param("nombre", newName()); + + final Response response = target("pet/" + existentId()) + .request(MediaType.APPLICATION_JSON_TYPE) + .put(entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + public void testModifyEspecie() throws IOException { + final Form form = new Form(); + form.param("especie", newEspecie()); + + final Response response = target("pet/" + existentId()) + .request(MediaType.APPLICATION_JSON_TYPE) + .put(entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + public void testModifyInvalidId() throws IOException { + final Form form = new Form(); + form.param("nombre", newName()); + form.param("especie", newEspecie()); + + final Response response = target("pete/" + nonExistentId()) + .request(MediaType.APPLICATION_JSON_TYPE) + .put(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + @ExpectedDatabase("/datasets/dataset-delete.xml") + public void testDelete() throws IOException { + final Response response = target("pet/" + existentId()).request().delete(); + + assertThat(response, hasOkStatus()); + + final Integer deletedId = response.readEntity(Integer.class); + + assertThat(deletedId, is(equalTo(existentId()))); + } + + @Test + public void testDeleteInvalidId() throws IOException { + final Response response = target("pet/" + nonExistentId()).request().delete(); + + assertThat(response, hasBadRequestStatus()); + } +} diff --git a/src/test/java/es/uvigo/esei/daa/rest/PetResourceUnitTest.java b/src/test/java/es/uvigo/esei/daa/rest/PetResourceUnitTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d7d00c7baae6bc58e70ac6281dc64a1944377e3b --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/rest/PetResourceUnitTest.java @@ -0,0 +1,226 @@ +package es.uvigo.esei.daa.rest; + +import es.uvigo.esei.daa.dao.DAOException; +import es.uvigo.esei.daa.dao.PetDAO; +import es.uvigo.esei.daa.entities.Pet; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import javax.ws.rs.core.Response; +import java.util.List; + +import static es.uvigo.esei.daa.dataset.PetDataset.*; +import static es.uvigo.esei.daa.matchers.HasHttpStatus.*; +import static es.uvigo.esei.daa.matchers.IsEqualToPet.containsPetInAnyOrder; +import static es.uvigo.esei.daa.matchers.IsEqualToPet.equalsToPet; +import static java.util.Arrays.asList; +import static org.easymock.EasyMock.*; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +public class PetResourceUnitTest { + private PetDAO daoMock; + private PetResource resource; + + @Before + public void setUp() throws Exception { + daoMock = createMock(PetDAO.class); + resource = new PetResource(daoMock); + } + + @After + public void tearDown() throws Exception { + try { + verify(daoMock); + } finally { + daoMock = null; + resource = null; + } + } + + @Test + @SuppressWarnings("unchecked") + public void testList() throws Exception { + final List pet = asList(pets()); + + expect(daoMock.list()).andReturn(pet); + + replay(daoMock); + + final Response response = resource.list(); + + assertThat(response, hasOkStatus()); + assertThat((List) response.getEntity(), containsPetInAnyOrder(pets())); + } + + @Test + public void testListDAOException() throws Exception { + expect(daoMock.list()).andThrow(new DAOException()); + + replay(daoMock); + + final Response response = resource.list(); + + assertThat(response, hasInternalServerErrorStatus()); + } + + @Test + public void testGet() throws Exception { + final Pet pet = existentPet(); + + expect(daoMock.get(pet.getId())).andReturn(pet); + + replay(daoMock); + + final Response response = resource.get(pet.getId()); + + assertThat(response, hasOkStatus()); + assertThat((Pet) response.getEntity(), is(equalsToPet(pet))); + } + + @Test + public void testGetDAOException() throws Exception { + expect(daoMock.get(anyInt())).andThrow(new DAOException()); + + replay(daoMock); + + final Response response = resource.get(existentId()); + + assertThat(response, hasInternalServerErrorStatus()); + } + + @Test + public void testGetIllegalArgumentException() throws Exception { + expect(daoMock.get(anyInt())).andThrow(new IllegalArgumentException()); + + replay(daoMock); + + final Response response = resource.get(existentId()); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + public void testDelete() throws Exception { + daoMock.delete(anyInt()); + + replay(daoMock); + + final Response response = resource.delete(1); + + assertThat(response, hasOkStatus()); + } + + @Test + public void testDeleteDAOException() throws Exception { + daoMock.delete(anyInt()); + expectLastCall().andThrow(new DAOException()); + + replay(daoMock); + + final Response response = resource.delete(1); + + assertThat(response, hasInternalServerErrorStatus()); + } + + @Test + public void testDeleteIllegalArgumentException() throws Exception { + daoMock.delete(anyInt()); + expectLastCall().andThrow(new IllegalArgumentException()); + replay(daoMock); + + final Response response = resource.delete(1); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + public void testModify() throws Exception { + final Pet pet = existentPet(); + pet.setNombre(newName()); + pet.setEspecie(newEspecie()); + + daoMock.modify(pet); + + replay(daoMock); + + final Response response = resource.modify( + pet.getId(), pet.getNombre(), pet.getEspecie()); + + assertThat(response, hasOkStatus()); + assertEquals(pet, response.getEntity()); + } + + @Test + public void testModifyDAOException() throws Exception { + daoMock.modify(anyObject()); + expectLastCall().andThrow(new DAOException()); + + replay(daoMock); + + final Response response = resource.modify(existentId(), newName(), newEspecie()); + + assertThat(response, hasInternalServerErrorStatus()); + } + + @Test + public void testModifyIllegalArgumentException() throws Exception { + daoMock.modify(anyObject()); + expectLastCall().andThrow(new IllegalArgumentException()); + + replay(daoMock); + + final Response response = resource.modify(existentId(), newName(), newEspecie()); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + public void testModifyNullPointerException() throws Exception { + daoMock.modify(anyObject()); + expectLastCall().andThrow(new NullPointerException()); + + replay(daoMock); + + final Response response = resource.modify(existentId(), newName(), newEspecie()); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + public void testAdd() throws Exception { + expect(daoMock.add(1,newName(), newEspecie())) + .andReturn(newPet()); + replay(daoMock); + + + final Response response = resource.add(1, newName(), newEspecie()); + + assertThat(response, hasOkStatus()); + assertThat((Pet) response.getEntity(), is(equalsToPet(newPet()))); + } + + @Test + public void testAddDAOException() throws Exception { + expect(daoMock.add(1, anyString(), anyString())) + .andThrow(new DAOException()); + replay(daoMock); + + final Response response = resource.add(1, newName(), newEspecie()); + + assertThat(response, hasInternalServerErrorStatus()); + } + + @Test + public void testAddIllegalArgumentException() throws Exception { + expect(daoMock.add(1,anyString(), anyString())) + .andThrow(new IllegalArgumentException()); + replay(daoMock); + + final Response response = resource.add(1, newName(), newEspecie()); + + assertThat(response, hasBadRequestStatus()); + } +} diff --git a/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java b/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java index a21ca3663b624f6cf2ea326a347de55ae8f96ca9..e784fb2dcaf705c0b209ffa925a14aa6bf427428 100644 --- a/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java +++ b/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java @@ -7,11 +7,15 @@ import org.junit.runners.Suite.SuiteClasses; import es.uvigo.esei.daa.dao.PeopleDAOUnitTest; import es.uvigo.esei.daa.entities.PersonUnitTest; import es.uvigo.esei.daa.rest.PeopleResourceUnitTest; +import es.uvigo.esei.daa.rest.PetResourceUnitTest; +import es.uvigo.esei.daa.dao.PetDAOUnitTest; @SuiteClasses({ PersonUnitTest.class, PeopleDAOUnitTest.class, - PeopleResourceUnitTest.class + PeopleResourceUnitTest.class, + PetDAOUnitTest.class, + PetResourceUnitTest.class }) @RunWith(Suite.class) public class UnitTestSuite { diff --git a/src/test/resources/datasets/dataset-add.xml b/src/test/resources/datasets/dataset-add.xml index 832a4a12b2244988bb86eb12ea33aa531677d9da..b9c64d0a5cc127ed51193180263908cea1e44ae7 100644 --- a/src/test/resources/datasets/dataset-add.xml +++ b/src/test/resources/datasets/dataset-add.xml @@ -16,4 +16,15 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/datasets/dataset-delete.xml b/src/test/resources/datasets/dataset-delete.xml index 7480a415572f1a39d6616d56359f522445b1febd..c77e9553aa2c32b3aaf952307d3f48d71b381767 100644 --- a/src/test/resources/datasets/dataset-delete.xml +++ b/src/test/resources/datasets/dataset-delete.xml @@ -14,4 +14,15 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/datasets/dataset-modify.xml b/src/test/resources/datasets/dataset-modify.xml index f840fb5a8cb65c2ef9fa22d155a1ee4cfcb7433e..8385cea56944d95dbb1c6c42ffe73d9150a6d08f 100644 --- a/src/test/resources/datasets/dataset-modify.xml +++ b/src/test/resources/datasets/dataset-modify.xml @@ -15,4 +15,15 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/datasets/dataset.dtd b/src/test/resources/datasets/dataset.dtd index c09a15e688a241515d3452ecec23bbb1d0ee966e..662ce370c3e13d80638d3f8c5c0f5a8545d2461b 100644 --- a/src/test/resources/datasets/dataset.dtd +++ b/src/test/resources/datasets/dataset.dtd @@ -1,12 +1,19 @@ - + + + - + + + + + + + + + + + \ No newline at end of file