From aadbd925a6608484fc8e46be6d4707764a12f431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Barciela=20Mart=C3=ADn?= Date: Thu, 15 Mar 2018 13:59:25 +0100 Subject: [PATCH] Add test for entitie pet --- db/mysql.sql | 8 ++ .../java/es/uvigo/esei/daa/entities/Pet.java | 4 - .../es/uvigo/esei/daa/rest/PetResource.java | 10 +-- .../uvigo/esei/daa/entities/PetUnitTest.java | 86 +++++++++++++++++++ 4 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 src/test/java/es/uvigo/esei/daa/entities/PetUnitTest.java diff --git a/db/mysql.sql b/db/mysql.sql index 459a52c..caf3c32 100644 --- a/db/mysql.sql +++ b/db/mysql.sql @@ -13,4 +13,12 @@ CREATE TABLE `daaexample`.`users` ( PRIMARY KEY (`login`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE `daaexample`.`pets` ( + `idPet` int NOT NULL AUTO_INCREMENT, + `name` varchar(50) NOT NULL, + `idOwner` int NOT NULL, + PRIMARY KEY (`idPet`), + FOREING KEY (`idOwner`) REFERENCES people(id), +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + GRANT ALL ON `daaexample`.* TO 'daa'@'localhost' IDENTIFIED BY 'daa'; 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 054cdad..682e5ba 100644 --- a/src/main/java/es/uvigo/esei/daa/entities/Pet.java +++ b/src/main/java/es/uvigo/esei/daa/entities/Pet.java @@ -28,10 +28,6 @@ public class Pet { return idOwner; } - public void setId(int id) { - this.id = id; - } - public void setName(String name) { this.name = name; } diff --git a/src/main/java/es/uvigo/esei/daa/rest/PetResource.java b/src/main/java/es/uvigo/esei/daa/rest/PetResource.java index 00ce05b..1d61fbf 100644 --- a/src/main/java/es/uvigo/esei/daa/rest/PetResource.java +++ b/src/main/java/es/uvigo/esei/daa/rest/PetResource.java @@ -10,7 +10,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import es.uvigo.esei.daa.dao.DAOException; -import es.uvigo.esei.daa.entities.Person; +import es.uvigo.esei.daa.entities.Pet; @Path("/pets") @Produces(MediaType.APPLICATION_JSON) @@ -20,17 +20,17 @@ public class PetResource { @Path("/{idPet}") public Response get(@PathParam("idPet") int idPet) { try { - final Person person = this.dao.get(id); + final Pet pet = this.dao.get(id); - return Response.ok(person).build(); + return Response.ok(pet).build(); } catch (IllegalArgumentException iae) { - LOG.log(Level.FINE, "Invalid person id in get method", iae); + LOG.log(Level.FINE, "Invalid pet id in get method", iae); return Response.status(Response.Status.BAD_REQUEST) .entity(iae.getMessage()) .build(); } catch (DAOException e) { - LOG.log(Level.SEVERE, "Error getting a person", e); + LOG.log(Level.SEVERE, "Error getting a pet", e); return Response.serverError() .entity(e.getMessage()) 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 0000000..6621921 --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/entities/PetUnitTest.java @@ -0,0 +1,86 @@ +package es.uvigo.esei.daa.entities; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; +import nl.jqno.equalsverifier.Warning; + +public class PetUnitTest { + + @Test + public void testIntStringString() { + + final int id = 1; + final String name = "cocodrilo"; + final int idOwner = 4; + + final Pet pet = new Pet(id,name,idOwner); + + assertThat(pet.getId(), equalTo(id)); + assertThat(pet.getName(), equalTo(name)); + assertThat(pet.getIdOwner(), equalTo(idOwner)); + + + } + + @Test(expected = NullPointerException.class) + public void testPetIntStringIntNullName() { + new Pet(1, null, 3); + } + + @Test + public void testSetName() { + final int id = 1; + final int idOwner = 5; + + final Pet pet = new Pet(id, "gato", idOwner); + pet.setName("manoplas"); + + assertThat(pet.getId(), is(equalTo(id))); + assertThat(pet.getName(), is(equalTo("manoplas"))); + assertThat(pet.getIdOwner(), is(equalTo(idOwner))); + } + + @Test(expected = NullPointerException.class) + public void testSetNullName() { + final Pet person = new Pet(1, "Coco", 3); + + person.setName(null); + } + + @Test + public void testnewOwner() { + final int id = 1; + final String name = "manoplas"; + + final Pet pet = new Pet(id, name, 2); + pet.setOwner(4); + + assertThat(pet.getId(), is(equalTo(id))); + assertThat(pet.getName(), is(equalTo(name))); + assertThat(pet.getIdOwner(), is(equalTo(4))); + } + + @Test + public void testEqualsObject() { + final Pet petA = new Pet(1, "Name A", 2); + final Pet petB = new Pet(1, "Name B", 3); + + assertTrue(petA.equals(petB)); + } + + @Test + public void testEqualsHashcode() { + EqualsVerifier.forClass(Pet.class) + .withIgnoredFields("name", "idOwner") + .suppress(Warning.STRICT_INHERITANCE) + .suppress(Warning.NONFINAL_FIELDS) + .verify(); + } + +} -- 2.18.1