test

parent 5e0b2e2d
...@@ -94,6 +94,25 @@ public class Pet ...@@ -94,6 +94,25 @@ public class Pet
this.especie = requireNonNull(especie, "Specie can't be null"); 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;
}
} }
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);
}
}
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());
}
}
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<Pet> 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());
}
}
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();
}
}
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<Pet> {
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<Iterable<? extends Pet>> containsPetInAnyOrder(Pet ... Pets) {
return containsEntityInAnyOrder(IsEqualToPet::equalsToPet, Pets);
}
}
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> pet = response.readEntity(new GenericType<List<Pet>>(){});
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());
}
}
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> pet = asList(pets());
expect(daoMock.list()).andReturn(pet);
replay(daoMock);
final Response response = resource.list();
assertThat(response, hasOkStatus());
assertThat((List<Pet>) 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());
}
}
...@@ -7,11 +7,15 @@ import org.junit.runners.Suite.SuiteClasses; ...@@ -7,11 +7,15 @@ import org.junit.runners.Suite.SuiteClasses;
import es.uvigo.esei.daa.dao.PeopleDAOUnitTest; import es.uvigo.esei.daa.dao.PeopleDAOUnitTest;
import es.uvigo.esei.daa.entities.PersonUnitTest; import es.uvigo.esei.daa.entities.PersonUnitTest;
import es.uvigo.esei.daa.rest.PeopleResourceUnitTest; import es.uvigo.esei.daa.rest.PeopleResourceUnitTest;
import es.uvigo.esei.daa.rest.PetResourceUnitTest;
import es.uvigo.esei.daa.dao.PetDAOUnitTest;
@SuiteClasses({ @SuiteClasses({
PersonUnitTest.class, PersonUnitTest.class,
PeopleDAOUnitTest.class, PeopleDAOUnitTest.class,
PeopleResourceUnitTest.class PeopleResourceUnitTest.class,
PetDAOUnitTest.class,
PetResourceUnitTest.class
}) })
@RunWith(Suite.class) @RunWith(Suite.class)
public class UnitTestSuite { public class UnitTestSuite {
......
...@@ -16,4 +16,15 @@ ...@@ -16,4 +16,15 @@
<users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" /> <users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" />
<users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" /> <users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" />
<pet id="1" personId="1" nombre="Dog" especie="Dog"/>
<pet id="2" personId="1" nombre="Perro" especie="Dog"/>
<pet id="3" personId="1" nombre="Per" especie="Dog"/>
<pet id="4" personId="1" nombre="Soso" especie="Dog"/>
<pet id="5" personId="1" nombre="Lino" especie="Dog"/>
<pet id="6" personId="1" nombre="Lar" especie="Dog"/>
<pet id="7" personId="1" nombre="Raul" especie="Dog"/>
<pet id="8" personId="1" nombre="Man" especie="Dog"/>
<pet id="9" personId="1" nombre="Justa" especie="Dog"/>
<pet id="10" personId="1" nombre="Lista" especie="Dog"/>
</dataset> </dataset>
\ No newline at end of file
...@@ -14,4 +14,15 @@ ...@@ -14,4 +14,15 @@
<users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" /> <users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" />
<users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" /> <users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" />
<pet id="1" personId="1" nombre="Dog" especie="Dog"/>
<pet id="2" personId="1" nombre="Perro" especie="Dog"/>
<pet id="3" personId="1" nombre="Per" especie="Dog"/>
<pet id="4" personId="1" nombre="Soso" especie="Dog"/>
<pet id="5" personId="1" nombre="Lino" especie="Dog"/>
<pet id="6" personId="1" nombre="Lar" especie="Dog"/>
<pet id="7" personId="1" nombre="Raul" especie="Dog"/>
<pet id="8" personId="1" nombre="Man" especie="Dog"/>
<pet id="9" personId="1" nombre="Justa" especie="Dog"/>
<pet id="10" personId="1" nombre="Lista" especie="Dog"/>
</dataset> </dataset>
\ No newline at end of file
...@@ -15,4 +15,15 @@ ...@@ -15,4 +15,15 @@
<users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" /> <users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" />
<users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" /> <users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" />
<pet id="1" personId="1" nombre="Dog" especie="Dog"/>
<pet id="2" personId="1" nombre="Perro" especie="Dog"/>
<pet id="3" personId="1" nombre="Per" especie="Dog"/>
<pet id="4" personId="1" nombre="Soso" especie="Dog"/>
<pet id="5" personId="1" nombre="Lino" especie="Dog"/>
<pet id="6" personId="1" nombre="Lar" especie="Dog"/>
<pet id="7" personId="1" nombre="Raul" especie="Dog"/>
<pet id="8" personId="1" nombre="Man" especie="Dog"/>
<pet id="9" personId="1" nombre="Justa" especie="Dog"/>
<pet id="10" personId="1" nombre="Lista" especie="Dog"/>
</dataset> </dataset>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT dataset (people*, users*)> <!ELEMENT dataset (people*, users*, pet*)>
<!ELEMENT people EMPTY> <!ELEMENT people EMPTY>
<!ELEMENT users EMPTY> <!ELEMENT users EMPTY>
<!ELEMENT pet EMPTY>
<!ATTLIST people <!ATTLIST people
id CDATA #IMPLIED id CDATA #IMPLIED
name CDATA #IMPLIED name CDATA #IMPLIED
surname CDATA #IMPLIED surname CDATA #IMPLIED
> >
<!ATTLIST pet
id CDATA #IMPLIED
personId CDATA #IMPLIED
nombre CDATA #IMPLIED
especie CDATA #IMPLIED
>
<!ATTLIST users <!ATTLIST users
login CDATA #IMPLIED login CDATA #IMPLIED
password CDATA #IMPLIED password CDATA #IMPLIED
......
...@@ -11,8 +11,18 @@ ...@@ -11,8 +11,18 @@
<people id="7" name="Perico" surname="Palotes" /> <people id="7" name="Perico" surname="Palotes" />
<people id="8" name="Patricia" surname="Pérez" /> <people id="8" name="Patricia" surname="Pérez" />
<people id="9" name="Julia" surname="Justa" /> <people id="9" name="Julia" surname="Justa" />
<people id="10" name="Juan" surname="Jiménez" />
<users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" /> <users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" />
<users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" /> <users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" />
<pet id="1" personId="1" nombre="Dog" especie="Dog"/>
<pet id="2" personId="1" nombre="Perro" especie="Dog"/>
<pet id="3" personId="1" nombre="Per" especie="Dog"/>
<pet id="4" personId="1" nombre="Soso" especie="Dog"/>
<pet id="5" personId="1" nombre="Lino" especie="Dog"/>
<pet id="6" personId="1" nombre="Lar" especie="Dog"/>
<pet id="7" personId="1" nombre="Raul" especie="Dog"/>
<pet id="8" personId="1" nombre="Man" especie="Dog"/>
<pet id="9" personId="1" nombre="Justa" especie="Dog"/>
<pet id="10" personId="1" nombre="Lista" especie="Dog"/>
</dataset> </dataset>
\ No newline at end of file
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