Add test for entitie pet

parent f004b2f1
......@@ -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';
......@@ -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;
}
......
......@@ -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())
......
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();
}
}
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