Commit 2e5babc8 authored by Santi's avatar Santi

Test pets

parent f0ea308b
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>DAA Example</title> <title>DAA Example</title>
<link rel="stylesheet" <link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head> </head>
<body> <body>
<header> <header>
<div class="navbar navbar-dark bg-dark box-shadow"> <div class="navbar navbar-dark bg-dark box-shadow">
<div class="container d-flex justify-content-between"> <div class="container d-flex justify-content-between">
<a href="#" class="navbar-brand d-flex align-items-center"> <a href="#" class="navbar-brand d-flex align-items-center">
<strong>DAA Example</strong> <strong>DAA Example</strong>
</a> </a>
<button id="logout" class="btn btn-dark">Cerrar sesión</button> <button id="logout" class="btn btn-dark">Cerrar sesión</button>
</div> </div>
</div> </div>
</header> </header>
<div class="container"> <div class="container">
<div id="people-container"> <div id="people-container">
<h1 class="display-5 mt-3 mb-3">Personas</h1> <h1 class="display-5 mt-3 mb-3">Personas</h1>
</div> </div>
</div> <div id="pet-container">
<h1 class="display-5 mt-3 mb-3">Mascotas</h1>
<script type="text/javascript" </div>
src="http://code.jquery.com/jquery-2.2.4.min.js"></script> </div>
<script type="text/javascript" src="js/dao/people.js"></script>
<script type="text/javascript" src="js/view/people.js"></script>
<script type="text/javascript" src="js/login.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
$('#logout').click(function(event) {
event.preventDefault();
doLogout();
});
var view = new PeopleView(new PeopleDAO(), <script type="text/javascript"
'people-container', 'people-container' src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
); <script type="text/javascript" src="js/dao/people.js"></script>
<script type="text/javascript" src="js/view/people.js"></script>
<script type="text/javascript" src="js/dao/pets.js"></script>
<script type="text/javascript" src="js/view/pets.js"></script>
<script type="text/javascript" src="js/login.js"></script>
<script type="text/javascript">
$(document).ready(
function () {
$('#logout').click(function (event) {
event.preventDefault();
doLogout();
});
document.getElementById("pet-container").style.display = "none";
document.getElementById('people-container').style.display = 'block';
view.init(); var view = new PeopleView(new PeopleDAO(),
}); 'people-container', 'people-container'
</script> );
view.init();
});
</script>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -69,14 +69,15 @@ public class PetsResourceTest extends JerseyTest { ...@@ -69,14 +69,15 @@ public class PetsResourceTest extends JerseyTest {
config.property("com.sun.jersey.api.json.POJOMappingFeature", Boolean.TRUE); config.property("com.sun.jersey.api.json.POJOMappingFeature", Boolean.TRUE);
} }
//@Test @Test
public void testList() throws IOException { public void testList() throws IOException {
final Response response = target("pets").request() final Response response = target("pets").request()
.header("Authorization", "Basic " + userToken(adminLogin())) .header("Authorization", "Basic " + userToken(adminLogin()))
.get(); .get();
assertThat(response, hasOkStatus()); assertThat(response, hasOkStatus());
final List<Pet> pets = response.readEntity(new GenericType<List<Pet>>(){}); final List<Pet> pets = response.readEntity(new GenericType<List<Pet>>() {
});
assertThat(pets, containsPetsInAnyOrder(pets())); assertThat(pets, containsPetsInAnyOrder(pets()));
} }
......
package es.uvigo.esei.daa.rest; package es.uvigo.esei.daa.rest;
import es.uvigo.esei.daa.dao.DAOException;
import es.uvigo.esei.daa.dao.PetsDAO; import es.uvigo.esei.daa.dao.PetsDAO;
import es.uvigo.esei.daa.entities.Pet; import es.uvigo.esei.daa.entities.Pet;
import org.hamcrest.CoreMatchers;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List;
import static es.uvigo.esei.daa.dataset.PetsDataset.existentPet; import static es.uvigo.esei.daa.dataset.PeopleDataset.newName;
import static es.uvigo.esei.daa.matchers.HasHttpStatus.hasOkStatus; import static es.uvigo.esei.daa.dataset.PetsDataset.*;
import static es.uvigo.esei.daa.matchers.HasHttpStatus.*;
import static es.uvigo.esei.daa.matchers.IsEqualToPet.containsPetsInAnyOrder;
import static es.uvigo.esei.daa.matchers.IsEqualToPet.equalsToPet; import static es.uvigo.esei.daa.matchers.IsEqualToPet.equalsToPet;
import static java.util.Arrays.asList;
import static org.easymock.EasyMock.*; import static org.easymock.EasyMock.*;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
public class PetsResourceUnitTest { public class PetsResourceUnitTest {
...@@ -51,5 +58,142 @@ public class PetsResourceUnitTest { ...@@ -51,5 +58,142 @@ public class PetsResourceUnitTest {
assertThat((Pet) response.getEntity(), is(equalsToPet(pet))); assertThat((Pet) response.getEntity(), is(equalsToPet(pet)));
} }
@Test
@SuppressWarnings("unchecked")
public void testList() throws Exception {
final List<Pet> people = asList(pets());
expect(daoMock.list()).andReturn(people);
replay(daoMock);
final Response response = resource.list();
assertThat(response, hasOkStatus());
assertThat((List<Pet>) 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 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.setName(newName());
pet.setOwner(newOwner());
daoMock.modify(pet);
replay(daoMock);
final Response response = resource.modify(
pet.getId(), pet.getName(), pet.getOwner());
assertThat(response, hasOkStatus());
assertEquals(pet, response.getEntity());
}
@Test
public void testAdd() throws Exception {
expect(daoMock.add(newName(), newOwner()))
.andReturn(newPet());
replay(daoMock);
final Response response = resource.add(newName(), newOwner());
assertThat(response, hasOkStatus());
assertThat((Pet) response.getEntity(), CoreMatchers.is(equalsToPet(newPet())));
}
@Test
public void testAddDAOException() throws Exception {
expect(daoMock.add(anyString(), anyInt()))
.andThrow(new DAOException());
replay(daoMock);
final Response response = resource.add(newName(), newOwner());
assertThat(response, hasInternalServerErrorStatus());
}
@Test
public void testAddIllegalArgumentException() throws Exception {
expect(daoMock.add(anyString(), anyInt()))
.andThrow(new IllegalArgumentException());
replay(daoMock);
final Response response = resource.add(newName(), newOwner());
assertThat(response, hasBadRequestStatus());
}
} }
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