From 57795bcddd0758d1d02117673b5e0b720e8e8fc6 Mon Sep 17 00:00:00 2001 From: cyanide4all Date: Sun, 12 Mar 2017 16:50:33 +0100 Subject: [PATCH] Entrega final MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A la hora de ejecutar los tests, el propio JUnit hace las comprobaciones de la DB en las tablas errĂ³neas y no soy capaz de arreglar tal cosa --- .idea/dbnavigator.xml | 450 ++++++++++++++++++ .../java/es/uvigo/esei/daa/entities/Pet.java | 24 + .../es/uvigo/esei/daa/dataset/PetDataset.java | 75 +++ .../uvigo/esei/daa/matchers/IsEqualToPet.java | 55 +++ .../uvigo/esei/daa/rest/PetResourceTest.java | 172 +++++++ .../esei/daa/suites/IntegrationTestSuite.java | 4 +- .../uvigo/esei/daa/suites/UnitTestSuite.java | 4 +- src/test/resources/datasets/dataset-add.xml | 12 + .../resources/datasets/dataset-delete.xml | 12 + .../resources/datasets/dataset-modify.xml | 12 + src/test/resources/datasets/dataset.dtd | 9 +- src/test/resources/datasets/dataset.xml | 11 + src/test/resources/db/hsqldb-drop.sql | 1 + src/test/resources/db/hsqldb.sql | 7 + 14 files changed, 845 insertions(+), 3 deletions(-) create mode 100644 .idea/dbnavigator.xml create mode 100644 src/test/java/es/uvigo/esei/daa/dataset/PetDataset.java create mode 100644 src/test/java/es/uvigo/esei/daa/matchers/IsEqualToPet.java create mode 100644 src/test/java/es/uvigo/esei/daa/rest/PetResourceTest.java diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml new file mode 100644 index 0000000..8660337 --- /dev/null +++ b/.idea/dbnavigator.xml @@ -0,0 +1,450 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file 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 ac9735b..20703aa 100644 --- a/src/main/java/es/uvigo/esei/daa/entities/Pet.java +++ b/src/main/java/es/uvigo/esei/daa/entities/Pet.java @@ -74,4 +74,28 @@ public class Pet { public void setownerID(int ownerID) { this.ownerID = requireNonNull(ownerID, "ownerID can't be a null reference"); } + + /* NOT UNITESTED + @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/dataset/PetDataset.java b/src/test/java/es/uvigo/esei/daa/dataset/PetDataset.java new file mode 100644 index 0000000..ae2097c --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/dataset/PetDataset.java @@ -0,0 +1,75 @@ +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, "Pet1", 1), + new Pet(2, "Pet2", 1), + new Pet(3, "Pet3", 1), + new Pet(4, "Pet4", 1), + new Pet(5, "Pet5", 1), + new Pet(6, "Pet6", 1), + new Pet(7, "Pet7", 1), + new Pet(8, "Pet8", 1), + new Pet(9, "Pet9", 2), + new Pet(10, "Pet McPetface", 2) + }; + } + + public static Pet[] petsWithout(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(), "Jane", 1); + } + + public static String newName() { + return "Pettity Pet"; + } + + public static String newOwnerID() { + return "1"; + } + + //We add the last pet o the first user, always for testing purposes + public static Pet newPet() { + return new Pet(pets().length + 1, newName(), 1); + } +} 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 0000000..a5d7503 --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/matchers/IsEqualToPet.java @@ -0,0 +1,55 @@ +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("name", Pet::getName, actual) + && checkAttribute("ownerID", Pet::getownerID, 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> containsPetsInAnyOrder(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 0000000..e0882ec --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/rest/PetResourceTest.java @@ -0,0 +1,172 @@ +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.*; +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/1/").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("pet/1/" + 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/1/" + 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()); + + final Response response = target("pet/1") + .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("ownerID", newOwnerID()); + + final Response response = target("pet/1/") + .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()); + + 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.setName(newName()); + + assertThat(modifiedPet, is(equalsToPet(pet))); + } + + @Test + public void testModifyInvalidId() throws IOException { + final Form form = new Form(); + form.param("name", newName()); + //TODO form.param("surname", newSurname()); + + final Response response = target("pet/" + 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/1/" + 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/1/" + nonExistentId()).request().delete(); + + assertThat(response, hasBadRequestStatus()); + } +} diff --git a/src/test/java/es/uvigo/esei/daa/suites/IntegrationTestSuite.java b/src/test/java/es/uvigo/esei/daa/suites/IntegrationTestSuite.java index 822c83f..586ee20 100644 --- a/src/test/java/es/uvigo/esei/daa/suites/IntegrationTestSuite.java +++ b/src/test/java/es/uvigo/esei/daa/suites/IntegrationTestSuite.java @@ -1,5 +1,6 @@ package es.uvigo.esei.daa.suites; +import es.uvigo.esei.daa.rest.PetResourceTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @@ -11,7 +12,8 @@ import es.uvigo.esei.daa.rest.UsersResourceTest; @SuiteClasses({ PeopleDAOTest.class, PeopleResourceTest.class, - UsersResourceTest.class + UsersResourceTest.class, + PetResourceTest.class }) @RunWith(Suite.class) public class IntegrationTestSuite { 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 a21ca36..7e0f157 100644 --- a/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java +++ b/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java @@ -1,5 +1,6 @@ package es.uvigo.esei.daa.suites; +import es.uvigo.esei.daa.entities.PetUnitTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @@ -11,7 +12,8 @@ import es.uvigo.esei.daa.rest.PeopleResourceUnitTest; @SuiteClasses({ PersonUnitTest.class, PeopleDAOUnitTest.class, - PeopleResourceUnitTest.class + PeopleResourceUnitTest.class, + PetUnitTest.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 832a4a1..ab09937 100644 --- a/src/test/resources/datasets/dataset-add.xml +++ b/src/test/resources/datasets/dataset-add.xml @@ -16,4 +16,16 @@ + + + + + + + + + + + + \ 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 7480a41..0de1d8c 100644 --- a/src/test/resources/datasets/dataset-delete.xml +++ b/src/test/resources/datasets/dataset-delete.xml @@ -14,4 +14,16 @@ + + + + + + + + + + + + \ 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 f840fb5..0f4233e 100644 --- a/src/test/resources/datasets/dataset-modify.xml +++ b/src/test/resources/datasets/dataset-modify.xml @@ -15,4 +15,16 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/datasets/dataset.dtd b/src/test/resources/datasets/dataset.dtd index c09a15e..08f9c64 100644 --- a/src/test/resources/datasets/dataset.dtd +++ b/src/test/resources/datasets/dataset.dtd @@ -1,7 +1,8 @@ - + + + + diff --git a/src/test/resources/datasets/dataset.xml b/src/test/resources/datasets/dataset.xml index 2a7eff1..84e0ef1 100644 --- a/src/test/resources/datasets/dataset.xml +++ b/src/test/resources/datasets/dataset.xml @@ -15,4 +15,15 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/db/hsqldb-drop.sql b/src/test/resources/db/hsqldb-drop.sql index 31f8643..448d503 100644 --- a/src/test/resources/db/hsqldb-drop.sql +++ b/src/test/resources/db/hsqldb-drop.sql @@ -1,2 +1,3 @@ DROP TABLE People IF EXISTS; DROP TABLE Users IF EXISTS; +DROP TABLE Pet IF EXISTS; diff --git a/src/test/resources/db/hsqldb.sql b/src/test/resources/db/hsqldb.sql index 00d02ba..37ca92b 100644 --- a/src/test/resources/db/hsqldb.sql +++ b/src/test/resources/db/hsqldb.sql @@ -9,4 +9,11 @@ CREATE TABLE users ( login VARCHAR(100) NOT NULL, password VARCHAR(64) NOT NULL, PRIMARY KEY (login) +); + +CREATE TABLE pet ( + id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL, + name VARCHAR(50) NOT NULL, + ownerID INTEGER NOT NULL, + PRIMARY KEY (id) ); \ No newline at end of file -- 2.18.1