From cd9e234e29267aebd4196110eb51ea37d9be39a7 Mon Sep 17 00:00:00 2001 From: hacklego Date: Sun, 12 Mar 2017 00:44:13 +0100 Subject: [PATCH] Add PetResource tests --- .../esei/daa/rest/PetResourceUnitTest.java | 239 ++++++++++++++++ .../uvigo/esei/daa/rest/PetsResourceTest.java | 258 ++++++++++++++++++ 2 files changed, 497 insertions(+) create mode 100644 src/test/java/es/uvigo/esei/daa/rest/PetResourceUnitTest.java create mode 100644 src/test/java/es/uvigo/esei/daa/rest/PetsResourceTest.java 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 0000000..9583360 --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/rest/PetResourceUnitTest.java @@ -0,0 +1,239 @@ +package es.uvigo.esei.daa.rest; + + +import static es.uvigo.esei.daa.dataset.PetsDataset.*; +import static es.uvigo.esei.daa.matchers.HasHttpStatus.hasBadRequestStatus; +import static es.uvigo.esei.daa.matchers.HasHttpStatus.hasInternalServerErrorStatus; +import static es.uvigo.esei.daa.matchers.HasHttpStatus.hasOkStatus; +import static es.uvigo.esei.daa.matchers.IsEqualToPet.*; +import static java.util.Arrays.asList; +import static org.easymock.EasyMock.anyInt; +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.util.List; + +import javax.ws.rs.core.Response; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import es.uvigo.esei.daa.dao.DAOException; +import es.uvigo.esei.daa.dao.PetsDAO; +import es.uvigo.esei.daa.entities.Pet; + +public class PetResourceUnitTest { + private PetsDAO daoMock; + private PetsResource resource; + + @Before + public void setUp() throws Exception { + daoMock = createMock(PetsDAO.class); + resource = new PetsResource(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(), containsPetsInAnyOrder(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.setId(newId()); + pet.setName(newName()); + pet.setKind(newKind()); + pet.setBreed(newBreed()); + pet.setOwner(newOwner()); + + daoMock.modify(pet); + + replay(daoMock); + + final Response response = resource.modify( + pet.getId(), pet.getName(), pet.getKind(), pet.getBreed(), pet.getOwner()); + + 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(), newKind(), newBreed(), newOwner()); + + 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(), newKind(), newBreed(), newOwner()); + + 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(), newKind(), newBreed(), newOwner()); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + public void testAdd() throws Exception { + expect(daoMock.add(newPet())) + .andReturn(newPet()); + replay(daoMock); + + + final Response response = resource.add(newName(), newKind(), newBreed(), newOwner()); + + assertThat(response, hasOkStatus()); + assertThat((Pet) response.getEntity(), is(equalsToPet(newPet()))); + } + + @Test + public void testAddDAOException() throws Exception { + expect(daoMock.add(newPet())) + .andThrow(new DAOException()); + replay(daoMock); + + final Response response = resource.add(newName(), newKind(), newBreed(), newOwner()); + + assertThat(response, hasInternalServerErrorStatus()); + } + + @Test + public void testAddIllegalArgumentException() throws Exception { + expect(daoMock.add(newPet())) + .andThrow(new IllegalArgumentException()); + replay(daoMock); + + final Response response = resource.add(newName(), newKind(), newBreed(), newOwner()); + + assertThat(response, hasBadRequestStatus()); + } +} diff --git a/src/test/java/es/uvigo/esei/daa/rest/PetsResourceTest.java b/src/test/java/es/uvigo/esei/daa/rest/PetsResourceTest.java new file mode 100644 index 0000000..3b1904d --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/rest/PetsResourceTest.java @@ -0,0 +1,258 @@ +package es.uvigo.esei.daa.rest; + +import static es.uvigo.esei.daa.dataset.PetsDataset.*; +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.*; +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; + +import java.io.IOException; +import java.util.List; + +import javax.sql.DataSource; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Form; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +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 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; + +@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 PetsResourceTest 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("pets").request().get(); + assertThat(response, hasOkStatus()); + + final List pets = response.readEntity(new GenericType>(){}); + + assertThat(pets, containsPetsInAnyOrder(pets())); + } + + @Test + public void testGet() throws IOException { + final Response response = target("pets/" + 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("pets/" + nonExistentId()).request().get(); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + @ExpectedDatabase("/datasets/dataset-add.xml") + public void testAdd() throws IOException { + final Form form = new Form(); + form.param("name", newName()); + form.param("kind", newKind()); + form.param("breed", newBreed()); + form.param("owner", Integer.toString(newOwner())); + + final Response response = target("pets") + .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("kind", newKind()); + form.param("breed", newBreed()); + form.param("owner", Integer.toString(newOwner())); + + final Response response = target("pets") + .request(MediaType.APPLICATION_JSON_TYPE) + .post(entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + public void testAddMissingKind() throws IOException { + final Form form = new Form(); + form.param("name", newName()); + form.param("breed", newBreed()); + form.param("owner", Integer.toString(newOwner())); + + final Response response = target("pets") + .request(MediaType.APPLICATION_JSON_TYPE) + .post(entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + public void testAddMissingBreed() throws IOException { + final Form form = new Form(); + form.param("name", newName()); + form.param("kind", newKind()); + form.param("owner", Integer.toString(newOwner())); + + final Response response = target("pets") + .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("name", newName()); + form.param("kind", newKind()); + form.param("breed", newBreed()); + form.param("owner", Integer.toString(newOwner())); + + final Response response = target("pets/" + 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.setName(newName()); + pet.setKind(newKind()); + pet.setBreed(newBreed()); + pet.setOwner(newOwner()); + + + assertThat(modifiedPet, is(equalsToPet(pet))); + } + + @Test + public void testModifyName() throws IOException { + final Form form = new Form(); + form.param("name", newName()); + + final Response response = target("pets/" + existentId()) + .request(MediaType.APPLICATION_JSON_TYPE) + .put(entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + public void testModifyKind() throws IOException { + final Form form = new Form(); + form.param("kind", newKind()); + + final Response response = target("pets/" + existentId()) + .request(MediaType.APPLICATION_JSON_TYPE) + .put(entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); + + assertThat(response, hasBadRequestStatus()); + } + + @Test + public void testModifyBreed() throws IOException { + final Form form = new Form(); + form.param("breed", newBreed()); + + final Response response = target("pets/" + 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("name", newName()); + form.param("kind", newKind()); + form.param("breed", newBreed()); + form.param("owner", Integer.toString(newOwner())); + + final Response response = target("pets/" + 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("pets/" + 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("pets/" + nonExistentId()).request().delete(); + + assertThat(response, hasBadRequestStatus()); + } +} -- 2.18.1