Commit 57795bcd authored by cyanide4all's avatar cyanide4all

Entrega final

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
parent 967b883c
This diff is collapsed.
...@@ -74,4 +74,28 @@ public class Pet { ...@@ -74,4 +74,28 @@ public class Pet {
public void setownerID(int ownerID) { public void setownerID(int ownerID) {
this.ownerID = requireNonNull(ownerID, "ownerID can't be a null reference"); 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;
}
*/
} }
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<Pet> 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);
}
}
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<Pet> {
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<Iterable<? extends Pet>> containsPetsInAnyOrder(Pet ... pets) {
return containsEntityInAnyOrder(IsEqualToPet::equalsToPet, pets);
}
}
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<Pet> pets = response.readEntity(new GenericType<List<Pet>>(){});
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());
}
}
package es.uvigo.esei.daa.suites; package es.uvigo.esei.daa.suites;
import es.uvigo.esei.daa.rest.PetResourceTest;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Suite; import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses; import org.junit.runners.Suite.SuiteClasses;
...@@ -11,7 +12,8 @@ import es.uvigo.esei.daa.rest.UsersResourceTest; ...@@ -11,7 +12,8 @@ import es.uvigo.esei.daa.rest.UsersResourceTest;
@SuiteClasses({ @SuiteClasses({
PeopleDAOTest.class, PeopleDAOTest.class,
PeopleResourceTest.class, PeopleResourceTest.class,
UsersResourceTest.class UsersResourceTest.class,
PetResourceTest.class
}) })
@RunWith(Suite.class) @RunWith(Suite.class)
public class IntegrationTestSuite { public class IntegrationTestSuite {
......
package es.uvigo.esei.daa.suites; package es.uvigo.esei.daa.suites;
import es.uvigo.esei.daa.entities.PetUnitTest;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Suite; import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses; import org.junit.runners.Suite.SuiteClasses;
...@@ -11,7 +12,8 @@ import es.uvigo.esei.daa.rest.PeopleResourceUnitTest; ...@@ -11,7 +12,8 @@ import es.uvigo.esei.daa.rest.PeopleResourceUnitTest;
@SuiteClasses({ @SuiteClasses({
PersonUnitTest.class, PersonUnitTest.class,
PeopleDAOUnitTest.class, PeopleDAOUnitTest.class,
PeopleResourceUnitTest.class PeopleResourceUnitTest.class,
PetUnitTest.class
}) })
@RunWith(Suite.class) @RunWith(Suite.class)
public class UnitTestSuite { public class UnitTestSuite {
......
...@@ -16,4 +16,16 @@ ...@@ -16,4 +16,16 @@
<users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" /> <users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" />
<users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" /> <users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" />
<pet id="1" name="Pet1" ownerID="1"/>
<pet id="2" name="Pet2" ownerID="1"/>
<pet id="3" name="Pet3" ownerID="1"/>
<pet id="4" name="Pet4" ownerID="1"/>
<pet id="5" name="Pet5" ownerID="1"/>
<pet id="6" name="Pet6" ownerID="1"/>
<pet id="7" name="Pet7" ownerID="1"/>
<pet id="8" name="Pet8" ownerID="1"/>
<pet id="9" name="Pet9" ownerID="2"/>
<pet id="10" name="Pet McPetface" ownerID="2"/>
<pet id="11" name="Pettity Pet" ownerID="1"/>
</dataset> </dataset>
\ No newline at end of file
...@@ -14,4 +14,16 @@ ...@@ -14,4 +14,16 @@
<users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" /> <users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" />
<users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" /> <users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" />
<pet id="1" name="Pet1" ownerID="1"/>
<pet id="2" name="Pet2" ownerID="1"/>
<pet id="3" name="Pet3" ownerID="1"/>
<pet id="4" name="Pet4" ownerID="1"/>
<pet id="5" name="Pet5" ownerID="1"/>
<pet id="6" name="Pet6" ownerID="1"/>
<pet id="7" name="Pet7" ownerID="1"/>
<pet id="8" name="Pet8" ownerID="1"/>
<pet id="9" name="Pet9" ownerID="2"/>
<pet id="10" name="Pet McPetface" ownerID="2"/>
</dataset> </dataset>
\ No newline at end of file
...@@ -15,4 +15,16 @@ ...@@ -15,4 +15,16 @@
<users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" /> <users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" />
<users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" /> <users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" />
<pet id="1" name="Pet1" ownerID="1"/>
<pet id="2" name="Pet2" ownerID="1"/>
<pet id="3" name="Pet3" ownerID="1"/>
<pet id="4" name="Pet4" ownerID="1"/>
<pet id="5" name="Pet5" ownerID="1"/>
<pet id="6" name="Pet6" ownerID="1"/>
<pet id="7" name="Pet7" ownerID="1"/>
<pet id="8" name="Pet8" ownerID="1"/>
<pet id="9" name="Pet9" ownerID="2"/>
<pet id="10" name="Pet McPetface" ownerID="2"/>
</dataset> </dataset>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT dataset (people*, users*)> <!ELEMENT dataset (people*, users*, pet*)>
<!ELEMENT people EMPTY> <!ELEMENT people EMPTY>
<!ELEMENT users EMPTY> <!ELEMENT users EMPTY>
<!ELEMENT pet EMPTY>
<!ATTLIST people <!ATTLIST people
id CDATA #IMPLIED id CDATA #IMPLIED
name CDATA #IMPLIED name CDATA #IMPLIED
...@@ -11,3 +12,9 @@ ...@@ -11,3 +12,9 @@
login CDATA #IMPLIED login CDATA #IMPLIED
password CDATA #IMPLIED password CDATA #IMPLIED
> >
<!ATTLIST pet
id CDATA #IMPLIED
name CDATA #IMPLIED
ownerID CDATA #IMPLIED
>
...@@ -15,4 +15,15 @@ ...@@ -15,4 +15,15 @@
<users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" /> <users login="admin" password="43f413b773f7d0cfad0e8e6529ec1249ce71e8697919eab30d82d800a3986b70" />
<users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" /> <users login="normal" password="688f21dd2d65970f174e2c9d35159250a8a23e27585452683db8c5d10b586336" />
<pet id="1" name="Pet1" ownerID="1"/>
<pet id="2" name="Pet2" ownerID="1"/>
<pet id="3" name="Pet3" ownerID="1"/>
<pet id="4" name="Pet4" ownerID="1"/>
<pet id="5" name="Pet5" ownerID="1"/>
<pet id="6" name="Pet6" ownerID="1"/>
<pet id="7" name="Pet7" ownerID="1"/>
<pet id="8" name="Pet8" ownerID="1"/>
<pet id="9" name="Pet9" ownerID="2"/>
<pet id="10" name="Pet McPetface" ownerID="2"/>
</dataset> </dataset>
\ No newline at end of file
DROP TABLE People IF EXISTS; DROP TABLE People IF EXISTS;
DROP TABLE Users IF EXISTS; DROP TABLE Users IF EXISTS;
DROP TABLE Pet IF EXISTS;
...@@ -10,3 +10,10 @@ CREATE TABLE users ( ...@@ -10,3 +10,10 @@ CREATE TABLE users (
password VARCHAR(64) NOT NULL, password VARCHAR(64) NOT NULL,
PRIMARY KEY (login) 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
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