Commit 1fb6f62c authored by Administrator's avatar Administrator

Reviews and refactors the tests code

The tests code for rest and service modules have been reviewed to
simplify and make it more readable. This change includes some
improvements on the OwnersDataset and some matchers classes.
parent 0e34458e
...@@ -12,12 +12,11 @@ import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithPersis ...@@ -12,12 +12,11 @@ import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithPersis
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithoutPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithoutPets;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newPasswordForExistentOwner; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newPasswordForExistentOwner;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owners; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owners;
import static es.uvigo.esei.xcs.http.util.HasHttpStatus.hasHttpStatus; import static es.uvigo.esei.xcs.http.util.HasHttpStatus.hasBadRequestStatus;
import static es.uvigo.esei.xcs.http.util.HasHttpStatus.hasCreatedStatus;
import static es.uvigo.esei.xcs.http.util.HasHttpStatus.hasMethodNotAllowedStatus;
import static es.uvigo.esei.xcs.http.util.HasHttpStatus.hasOkStatus;
import static javax.ws.rs.client.Entity.json; import static javax.ws.rs.client.Entity.json;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.METHOD_NOT_ALLOWED;
import static javax.ws.rs.core.Response.Status.OK;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
...@@ -58,7 +57,7 @@ public class OwnerResourceRestTest { ...@@ -58,7 +57,7 @@ public class OwnerResourceRestTest {
@Deployment @Deployment
public static Archive<?> createDeployment() { public static Archive<?> createDeployment() {
final WebArchive archive = ShrinkWrap.create(WebArchive.class, "test.war") return ShrinkWrap.create(WebArchive.class, "test.war")
.addClass(OwnerResource.class) .addClass(OwnerResource.class)
.addClasses(CORSFilter.class, IllegalArgumentExceptionMapper.class, SecurityExceptionMapper.class) .addClasses(CORSFilter.class, IllegalArgumentExceptionMapper.class, SecurityExceptionMapper.class)
.addPackage(OwnerService.class.getPackage()) .addPackage(OwnerService.class.getPackage())
...@@ -67,8 +66,6 @@ public class OwnerResourceRestTest { ...@@ -67,8 +66,6 @@ public class OwnerResourceRestTest {
.addAsWebInfResource("jboss-web.xml") .addAsWebInfResource("jboss-web.xml")
.addAsWebInfResource("web.xml") .addAsWebInfResource("web.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
return archive;
} }
@Test @InSequence(1) @Test @InSequence(1)
...@@ -84,7 +81,7 @@ public class OwnerResourceRestTest { ...@@ -84,7 +81,7 @@ public class OwnerResourceRestTest {
) throws Exception { ) throws Exception {
final Response response = webTarget.request().get(); final Response response = webTarget.request().get();
assertThat(response, hasHttpStatus(OK)); assertThat(response, hasOkStatus());
final Owner owner = response.readEntity(Owner.class); final Owner owner = response.readEntity(Owner.class);
final Owner expected = existentOwner(); final Owner expected = existentOwner();
...@@ -112,7 +109,7 @@ public class OwnerResourceRestTest { ...@@ -112,7 +109,7 @@ public class OwnerResourceRestTest {
) throws Exception { ) throws Exception {
final Response response = webTarget.request().get(); final Response response = webTarget.request().get();
assertThat(response, hasHttpStatus(BAD_REQUEST)); assertThat(response, hasBadRequestStatus());
} }
@Test @InSequence(6) @Test @InSequence(6)
...@@ -135,7 +132,7 @@ public class OwnerResourceRestTest { ...@@ -135,7 +132,7 @@ public class OwnerResourceRestTest {
) throws Exception { ) throws Exception {
final Response response = webTarget.request().get(); final Response response = webTarget.request().get();
assertThat(response, hasHttpStatus(OK)); assertThat(response, hasOkStatus());
final List<Owner> list = ListOwnerType.readEntity(response); final List<Owner> list = ListOwnerType.readEntity(response);
assertThat(list, containsOwnersInAnyOrder(owners())); assertThat(list, containsOwnersInAnyOrder(owners()));
...@@ -196,7 +193,7 @@ public class OwnerResourceRestTest { ...@@ -196,7 +193,7 @@ public class OwnerResourceRestTest {
private void testCreateOwner(WebTarget webTarget, Owner newOwner, Owner persistentOwner) { private void testCreateOwner(WebTarget webTarget, Owner newOwner, Owner persistentOwner) {
final Response response = webTarget.request().post(json(newOwner)); final Response response = webTarget.request().post(json(newOwner));
assertThat(response, hasHttpStatus(CREATED)); assertThat(response, hasCreatedStatus());
final String location = response.getHeaderString("Location"); final String location = response.getHeaderString("Location");
...@@ -222,7 +219,7 @@ public class OwnerResourceRestTest { ...@@ -222,7 +219,7 @@ public class OwnerResourceRestTest {
final Response response = webTarget.request().put(json(owner)); final Response response = webTarget.request().put(json(owner));
assertThat(response, hasHttpStatus(OK)); assertThat(response, hasOkStatus());
} }
@Test @InSequence(32) @Test @InSequence(32)
...@@ -245,7 +242,7 @@ public class OwnerResourceRestTest { ...@@ -245,7 +242,7 @@ public class OwnerResourceRestTest {
) throws Exception { ) throws Exception {
final Response response = webTarget.request().delete(); final Response response = webTarget.request().delete();
assertThat(response, hasHttpStatus(OK)); assertThat(response, hasOkStatus());
} }
@Test @InSequence(42) @Test @InSequence(42)
...@@ -268,7 +265,7 @@ public class OwnerResourceRestTest { ...@@ -268,7 +265,7 @@ public class OwnerResourceRestTest {
) throws Exception { ) throws Exception {
final Response response = webTarget.request().delete(); final Response response = webTarget.request().delete();
assertThat(response, hasHttpStatus(OK)); assertThat(response, hasOkStatus());
} }
@Test @InSequence(45) @Test @InSequence(45)
...@@ -291,7 +288,7 @@ public class OwnerResourceRestTest { ...@@ -291,7 +288,7 @@ public class OwnerResourceRestTest {
) throws Exception { ) throws Exception {
final Response response = webTarget.request().delete(); final Response response = webTarget.request().delete();
assertThat(response, hasHttpStatus(METHOD_NOT_ALLOWED)); assertThat(response, hasMethodNotAllowedStatus());
} }
@Test @InSequence(48) @Test @InSequence(48)
......
...@@ -8,11 +8,10 @@ import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.existentOwner; ...@@ -8,11 +8,10 @@ import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.existentOwner;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithFreshPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithFreshPets;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithPersistentPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithPersistentPets;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owners; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owners;
import static es.uvigo.esei.xcs.http.util.HasHttpStatus.hasHttpStatus; import static es.uvigo.esei.xcs.http.util.HasHttpStatus.hasCreatedStatus;
import static es.uvigo.esei.xcs.http.util.HasHttpStatus.hasOkStatus;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.OK;
import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall; import static org.easymock.EasyMock.expectLastCall;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
...@@ -71,7 +70,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -71,7 +70,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
final Response response = resource.get(owner.getLogin()); final Response response = resource.get(owner.getLogin());
assertThat(response, hasHttpStatus(OK)); assertThat(response, hasOkStatus());
assertThat(response.getEntity(), is(instanceOf(Owner.class))); assertThat(response.getEntity(), is(instanceOf(Owner.class)));
assertThat((Owner) response.getEntity(), is(equalToOwner(owner))); assertThat((Owner) response.getEntity(), is(equalToOwner(owner)));
} }
...@@ -107,7 +106,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -107,7 +106,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
final Response response = resource.list(); final Response response = resource.list();
assertThat(response, hasHttpStatus(OK)); assertThat(response, hasOkStatus());
assertThat(response.getEntity(), is(instanceOf(List.class))); assertThat(response.getEntity(), is(instanceOf(List.class)));
assertThat((List<Owner>) response.getEntity(), containsOwnersInAnyOrder(owners)); assertThat((List<Owner>) response.getEntity(), containsOwnersInAnyOrder(owners));
} }
...@@ -122,7 +121,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -122,7 +121,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
final Response response = resource.list(); final Response response = resource.list();
assertThat(response, hasHttpStatus(OK)); assertThat(response, hasOkStatus());
assertThat(response.getEntity(), is(instanceOf(List.class))); assertThat(response.getEntity(), is(instanceOf(List.class)));
assertThat((List<Owner>) response.getEntity(), is(empty())); assertThat((List<Owner>) response.getEntity(), is(empty()));
} }
...@@ -148,7 +147,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -148,7 +147,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
final Response response = resource.create(newOwner); final Response response = resource.create(newOwner);
assertThat(response, hasHttpStatus(CREATED)); assertThat(response, hasCreatedStatus());
assertThat(response.getHeaderString("Location"), is(equalTo(mockUri.toString()))); assertThat(response.getHeaderString("Location"), is(equalTo(mockUri.toString())));
} }
...@@ -183,7 +182,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -183,7 +182,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
final Response response = resource.update(owner); final Response response = resource.update(owner);
assertThat(response, hasHttpStatus(OK)); assertThat(response, hasOkStatus());
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
...@@ -203,7 +202,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -203,7 +202,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
final Response response = resource.delete(login); final Response response = resource.delete(login);
assertThat(response, hasHttpStatus(OK)); assertThat(response, hasOkStatus());
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
......
...@@ -3,7 +3,6 @@ package es.uvigo.esei.xcs.service; ...@@ -3,7 +3,6 @@ package es.uvigo.esei.xcs.service;
import static es.uvigo.esei.xcs.domain.entities.IsEqualToOwner.containsOwnersInAnyOrder; import static es.uvigo.esei.xcs.domain.entities.IsEqualToOwner.containsOwnersInAnyOrder;
import static es.uvigo.esei.xcs.domain.entities.IsEqualToOwner.equalToOwner; import static es.uvigo.esei.xcs.domain.entities.IsEqualToOwner.equalToOwner;
import static es.uvigo.esei.xcs.domain.entities.IsEqualToPet.containsPetsInAnyOrder; import static es.uvigo.esei.xcs.domain.entities.IsEqualToPet.containsPetsInAnyOrder;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.existentLogin;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.existentOwner; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.existentOwner;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithFreshPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithFreshPets;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithPersistentPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithPersistentPets;
...@@ -11,7 +10,6 @@ import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithoutPet ...@@ -11,7 +10,6 @@ import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithoutPet
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newPasswordForExistentOwner; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newPasswordForExistentOwner;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.nonExistentLogin; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.nonExistentLogin;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.nonExistentPetName; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.nonExistentPetName;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.ownerWithLogin;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.ownerWithPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.ownerWithPets;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.ownerWithoutPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.ownerWithoutPets;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owners; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owners;
...@@ -24,6 +22,7 @@ import static org.hamcrest.collection.IsCollectionWithSize.hasSize; ...@@ -24,6 +22,7 @@ import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.util.Collection;
import java.util.List; import java.util.List;
import javax.ejb.EJB; import javax.ejb.EJB;
...@@ -59,35 +58,31 @@ public class OwnerServiceIntegrationTest { ...@@ -59,35 +58,31 @@ public class OwnerServiceIntegrationTest {
@Deployment @Deployment
public static Archive<?> createDeployment() { public static Archive<?> createDeployment() {
final WebArchive archive = ShrinkWrap.create(WebArchive.class, "test.war") return ShrinkWrap.create(WebArchive.class, "test.war")
.addClasses(OwnerService.class, OwnersDataset.class) .addClasses(OwnerService.class, OwnersDataset.class)
.addPackage(RoleCaller.class.getPackage()) .addPackage(RoleCaller.class.getPackage())
.addPackage(Owner.class.getPackage()) .addPackage(Owner.class.getPackage())
.addAsResource("test-persistence.xml", "META-INF/persistence.xml") .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource("jboss-web.xml") .addAsWebInfResource("jboss-web.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
return archive;
} }
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testGetOwner() { public void testGetOwner() {
final String login = existentLogin(); final Owner existentOwner = existentOwner();
final Owner actual = asAdmin.call(() -> facade.get(login)); final Owner actualOwner = asAdmin.call(() -> facade.get(existentOwner.getLogin()));
assertThat(actual, is(equalToOwner(ownerWithLogin(login)))); assertThat(actualOwner, is(equalToOwner(existentOwner)));
} }
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testGetOwnerNonExistent() { public void testGetOwnerNonExistent() {
final String login = nonExistentLogin(); final Owner actualOwner = asAdmin.call(() -> facade.get(nonExistentLogin()));
final Owner actual = asAdmin.call(() -> facade.get(login));
assertThat(actual, is(nullValue())); assertThat(actualOwner, is(nullValue()));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
...@@ -99,9 +94,9 @@ public class OwnerServiceIntegrationTest { ...@@ -99,9 +94,9 @@ public class OwnerServiceIntegrationTest {
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testList() { public void testList() {
final List<Owner> actual = asAdmin.call(() -> facade.list()); final List<Owner> actualOwners = asAdmin.call(() -> facade.list());
assertThat(actual, is(containsOwnersInAnyOrder(owners()))); assertThat(actualOwners, containsOwnersInAnyOrder(owners()));
} }
@Test @Test
...@@ -110,10 +105,10 @@ public class OwnerServiceIntegrationTest { ...@@ -110,10 +105,10 @@ public class OwnerServiceIntegrationTest {
final String petName = petNameWithSingleOwner(); final String petName = petNameWithSingleOwner();
final Owner owner = ownersOf(petName)[0]; final Owner owner = ownersOf(petName)[0];
final List<Owner> owners = asAdmin.call(() -> facade.findByPetName(petName)); final List<Owner> actualOwners = asAdmin.call(() -> facade.findByPetName(petName));
assertThat(owners, hasSize(1)); assertThat(actualOwners, hasSize(1));
assertThat(owners.get(0), is(equalToOwner(owner))); assertThat(actualOwners.get(0), is(equalToOwner(owner)));
} }
@Test @Test
...@@ -121,21 +116,21 @@ public class OwnerServiceIntegrationTest { ...@@ -121,21 +116,21 @@ public class OwnerServiceIntegrationTest {
public void testFindByPetNameMultipleOwners() { public void testFindByPetNameMultipleOwners() {
final String petName = petNameWithMultipleOwners(); final String petName = petNameWithMultipleOwners();
final List<Owner> owners = asAdmin.call(() -> facade.findByPetName(petName)); final List<Owner> actualOwners = asAdmin.call(() -> facade.findByPetName(petName));
final Owner[] expectedOwners = ownersOf(petName); final Owner[] expectedOwners = ownersOf(petName);
assertThat(owners, containsOwnersInAnyOrder(expectedOwners)); assertThat(actualOwners, containsOwnersInAnyOrder(expectedOwners));
} }
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testFindByPetNameNoPet() { public void testFindByPetNameNoPet() {
final String pet = nonExistentPetName(); final String nonExistentPet = nonExistentPetName();
final List<Owner> owners = asAdmin.call(() -> facade.findByPetName(pet)); final List<Owner> actualOwners = asAdmin.call(() -> facade.findByPetName(nonExistentPet));
assertThat(owners, is(empty())); assertThat(actualOwners, is(empty()));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
...@@ -157,9 +152,9 @@ public class OwnerServiceIntegrationTest { ...@@ -157,9 +152,9 @@ public class OwnerServiceIntegrationTest {
@Test @Test
@ShouldMatchDataSet({"owners.xml", "owners-create-with-pets.xml"}) @ShouldMatchDataSet({"owners.xml", "owners-create-with-pets.xml"})
public void testCreateWithPets() { public void testCreateWithPets() {
final Owner actual = asAdmin.call(() -> facade.create(newOwnerWithFreshPets())); final Owner actualOwner = asAdmin.call(() -> facade.create(newOwnerWithFreshPets()));
assertThat(actual, is(equalToOwner(newOwnerWithPersistentPets()))); assertThat(actualOwner, is(equalToOwner(newOwnerWithPersistentPets())));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
...@@ -183,10 +178,10 @@ public class OwnerServiceIntegrationTest { ...@@ -183,10 +178,10 @@ public class OwnerServiceIntegrationTest {
@Test @Test
@ShouldMatchDataSet("owners-update-password.xml") @ShouldMatchDataSet("owners-update-password.xml")
public void testUpdatePassword() { public void testUpdatePassword() {
final Owner owner = existentOwner(); final Owner existentOwner = existentOwner();
owner.changePassword(newPasswordForExistentOwner()); existentOwner.changePassword(newPasswordForExistentOwner());
asAdmin.run(() -> facade.update(owner)); asAdmin.run(() -> facade.update(existentOwner));
} }
@Test @Test
...@@ -194,17 +189,17 @@ public class OwnerServiceIntegrationTest { ...@@ -194,17 +189,17 @@ public class OwnerServiceIntegrationTest {
public void testUpdateNewOwnerWithoutPets() { public void testUpdateNewOwnerWithoutPets() {
final Owner newOwner = newOwnerWithoutPets(); final Owner newOwner = newOwnerWithoutPets();
final Owner actual = asAdmin.call(() -> facade.update(newOwner)); final Owner actualOwner = asAdmin.call(() -> facade.update(newOwner));
assertThat(actual, is(equalToOwner(newOwner))); assertThat(actualOwner, is(equalToOwner(newOwner)));
} }
@Test @Test
@ShouldMatchDataSet({"owners.xml", "owners-create-with-pets.xml"}) @ShouldMatchDataSet({"owners.xml", "owners-create-with-pets.xml"})
public void testUpdateNewOwnerWithPets() { public void testUpdateNewOwnerWithPets() {
final Owner actual = asAdmin.call(() -> facade.update(newOwnerWithFreshPets())); final Owner actualOwner = asAdmin.call(() -> facade.update(newOwnerWithFreshPets()));
assertThat(actual, is(equalToOwner(newOwnerWithPersistentPets()))); assertThat(actualOwner, is(equalToOwner(newOwnerWithPersistentPets())));
} }
@Test @Test
...@@ -235,21 +230,19 @@ public class OwnerServiceIntegrationTest { ...@@ -235,21 +230,19 @@ public class OwnerServiceIntegrationTest {
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testGetPets() { public void testGetPets() {
final Owner owner = ownerWithPets(); final Owner owner = ownerWithPets();
final Pet[] ownedPets = owner.getPets().toArray(new Pet[0]); final Collection<Pet> ownedPets = owner.getPets();
final List<Pet> pets = asAdmin.call(() -> facade.getPets(owner.getLogin())); final List<Pet> actualPets = asAdmin.call(() -> facade.getPets(owner.getLogin()));
assertThat(pets, containsPetsInAnyOrder(ownedPets)); assertThat(actualPets, containsPetsInAnyOrder(ownedPets));
} }
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testGetPetsNoPets() { public void testGetPetsNoPets() {
final Owner owner = ownerWithoutPets(); final List<Pet> actualPets = asAdmin.call(() -> facade.getPets(ownerWithoutPets().getLogin()));
final List<Pet> pets = asAdmin.call(() -> facade.getPets(owner.getLogin()));
assertThat(pets, is(empty())); assertThat(actualPets, is(empty()));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
......
...@@ -2,13 +2,15 @@ package es.uvigo.esei.xcs.service; ...@@ -2,13 +2,15 @@ package es.uvigo.esei.xcs.service;
import static es.uvigo.esei.xcs.domain.entities.IsEqualToPet.containsPetsInAnyOrder; import static es.uvigo.esei.xcs.domain.entities.IsEqualToPet.containsPetsInAnyOrder;
import static es.uvigo.esei.xcs.domain.entities.IsEqualToPet.equalToPet; import static es.uvigo.esei.xcs.domain.entities.IsEqualToPet.equalToPet;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.anyPetOf;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.existentPet;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.existentPetId; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.existentPetId;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newPet; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newPet;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newPetWithOwner; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newPetWithOwner;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.nonExistentPetId; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.nonExistentPetId;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.ownerWithPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.ownerWithPets;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.ownerWithoutPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.ownerWithoutPets;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.pet; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.petWithId;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.hamcrest.collection.IsEmptyCollection.empty;
...@@ -55,37 +57,33 @@ public class PetServiceIntegrationTest { ...@@ -55,37 +57,33 @@ public class PetServiceIntegrationTest {
@Deployment @Deployment
public static Archive<?> createDeployment() { public static Archive<?> createDeployment() {
final WebArchive archive = ShrinkWrap.create(WebArchive.class, "test.war") return ShrinkWrap.create(WebArchive.class, "test.war")
.addClasses(PetService.class, OwnersDataset.class) .addClasses(PetService.class, OwnersDataset.class)
.addPackage(RoleCaller.class.getPackage()) .addPackage(RoleCaller.class.getPackage())
.addPackage(Pet.class.getPackage()) .addPackage(Pet.class.getPackage())
.addAsResource("test-persistence.xml", "META-INF/persistence.xml") .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource("jboss-web.xml") .addAsWebInfResource("jboss-web.xml")
.addAsWebInfResource("beans.xml", "beans.xml"); .addAsWebInfResource("beans.xml", "beans.xml");
return archive;
} }
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testGet() throws LoginException { public void testGet() throws LoginException {
final int id = existentPetId(); final Pet existentPet = existentPet();
final Pet pet = pet(id);
principal.setName(pet.getOwner().getLogin()); principal.setName(existentPet.getOwner().getLogin());
final Pet actual = asOwner.call(() -> facade.get(id)); final Pet actualPet = asOwner.call(() -> facade.get(existentPet.getId()));
assertThat(actual, equalToPet(pet)); assertThat(actualPet, equalToPet(existentPet));
} }
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testGetBadId() throws LoginException { public void testGetBadId() throws LoginException {
final int id = nonExistentPetId();
principal.setName(ownerWithoutPets().getLogin()); principal.setName(ownerWithoutPets().getLogin());
final Pet actual = asOwner.call(() -> facade.get(id)); final Pet actual = asOwner.call(() -> facade.get(nonExistentPetId()));
assertThat(actual, is(nullValue())); assertThat(actual, is(nullValue()));
} }
...@@ -94,8 +92,7 @@ public class PetServiceIntegrationTest { ...@@ -94,8 +92,7 @@ public class PetServiceIntegrationTest {
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testGetOthersPetId() throws LoginException { public void testGetOthersPetId() throws LoginException {
final Owner ownerWithoutPets = ownerWithoutPets(); final Owner ownerWithoutPets = ownerWithoutPets();
final Owner ownerWithPets = ownerWithPets(); final int petId = anyPetOf(ownerWithPets()).getId();
final int petId = ownerWithPets.getPets().iterator().next().getId();
principal.setName(ownerWithoutPets.getLogin()); principal.setName(ownerWithoutPets.getLogin());
...@@ -105,21 +102,19 @@ public class PetServiceIntegrationTest { ...@@ -105,21 +102,19 @@ public class PetServiceIntegrationTest {
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testList() throws LoginException { public void testList() throws LoginException {
final Owner owner = ownerWithPets(); final Owner ownerWithPets = ownerWithPets();
final Pet[] ownedPets = owner.getPets().toArray(new Pet[0]);
principal.setName(owner.getLogin());
final List<Pet> pets = asOwner.call(() -> facade.list()); principal.setName(ownerWithPets.getLogin());
assertThat(pets, containsPetsInAnyOrder(ownedPets)); final List<Pet> actualPets = asOwner.call(() -> facade.list());
assertThat(actualPets, containsPetsInAnyOrder(ownerWithPets.getPets()));
} }
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testListNoPets() throws LoginException { public void testListNoPets() throws LoginException {
final Owner owner = ownerWithoutPets(); principal.setName(ownerWithoutPets().getLogin());
principal.setName(owner.getLogin());
final List<Pet> pets = asOwner.call(() -> facade.list()); final List<Pet> pets = asOwner.call(() -> facade.list());
...@@ -129,10 +124,11 @@ public class PetServiceIntegrationTest { ...@@ -129,10 +124,11 @@ public class PetServiceIntegrationTest {
@Test @Test
@ShouldMatchDataSet({ "owners.xml", "owners-create-pet.xml" }) @ShouldMatchDataSet({ "owners.xml", "owners-create-pet.xml" })
public void testCreate() { public void testCreate() {
final Owner owner = ownerWithoutPets(); final Owner ownerWithoutPets = ownerWithoutPets();
principal.setName(owner.getLogin());
final Pet pet = newPetWithOwner(owner); principal.setName(ownerWithoutPets.getLogin());
final Pet pet = newPetWithOwner(ownerWithoutPets);
asOwner.call(() -> facade.create(pet)); asOwner.call(() -> facade.create(pet));
} }
...@@ -158,12 +154,9 @@ public class PetServiceIntegrationTest { ...@@ -158,12 +154,9 @@ public class PetServiceIntegrationTest {
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet({ "owners.xml" }) @ShouldMatchDataSet({ "owners.xml" })
public void testCreateWrongOwner() { public void testCreateWrongOwner() {
final Owner owner = ownerWithoutPets(); principal.setName(ownerWithoutPets().getLogin());
final Owner otherOwner = ownerWithPets();
principal.setName(owner.getLogin());
final Pet pet = newPetWithOwner(otherOwner); final Pet pet = newPetWithOwner(ownerWithPets());
asOwner.run(() -> facade.create(pet)); asOwner.run(() -> facade.create(pet));
} }
...@@ -171,25 +164,25 @@ public class PetServiceIntegrationTest { ...@@ -171,25 +164,25 @@ public class PetServiceIntegrationTest {
@Test @Test
@ShouldMatchDataSet("owners-update-pet.xml") @ShouldMatchDataSet("owners-update-pet.xml")
public void testUpdate() throws LoginException { public void testUpdate() throws LoginException {
final int id = existentPetId(); final Pet existentPet = existentPet();
final Pet pet = pet(id);
principal.setName(pet.getOwner().getLogin()); principal.setName(existentPet.getOwner().getLogin());
pet.setName("UpdateName"); existentPet.setName("UpdateName");
pet.setAnimal(AnimalType.BIRD); existentPet.setAnimal(AnimalType.BIRD);
pet.setBirth(new Date(946771261000L)); existentPet.setBirth(new Date(946771261000L));
asOwner.run(() -> facade.update(pet)); asOwner.run(() -> facade.update(existentPet));
} }
@Test @Test
@ShouldMatchDataSet({ "owners.xml", "owners-create-pet.xml" }) @ShouldMatchDataSet({ "owners.xml", "owners-create-pet.xml" })
public void testUpdateNewPetWithOwner() { public void testUpdateNewPetWithOwner() {
final Owner owner = ownerWithoutPets(); final Owner ownerWithoutPets = ownerWithoutPets();
principal.setName(owner.getLogin());
principal.setName(ownerWithoutPets.getLogin());
final Pet pet = newPetWithOwner(owner); final Pet pet = newPetWithOwner(ownerWithoutPets);
asOwner.call(() -> facade.update(pet)); asOwner.call(() -> facade.update(pet));
} }
...@@ -203,12 +196,9 @@ public class PetServiceIntegrationTest { ...@@ -203,12 +196,9 @@ public class PetServiceIntegrationTest {
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet({ "owners.xml" }) @ShouldMatchDataSet({ "owners.xml" })
public void testUpdateWrongOwner() { public void testUpdateWrongOwner() {
final Owner owner = ownerWithoutPets(); principal.setName(ownerWithoutPets().getLogin());
final Owner otherOwner = ownerWithPets();
principal.setName(owner.getLogin());
final Pet pet = otherOwner.getPets().iterator().next(); final Pet pet = anyPetOf(ownerWithPets());
asOwner.run(() -> facade.update(pet)); asOwner.run(() -> facade.update(pet));
} }
...@@ -217,7 +207,7 @@ public class PetServiceIntegrationTest { ...@@ -217,7 +207,7 @@ public class PetServiceIntegrationTest {
@ShouldMatchDataSet({ "owners.xml", "owners-create-pet.xml" }) @ShouldMatchDataSet({ "owners.xml", "owners-create-pet.xml" })
public void testUpdatePetNoOwner() { public void testUpdatePetNoOwner() {
final int id = existentPetId(); final int id = existentPetId();
final Pet pet = pet(id); final Pet pet = petWithId(id);
principal.setName(pet.getOwner().getLogin()); principal.setName(pet.getOwner().getLogin());
pet.setOwner(null); pet.setOwner(null);
...@@ -229,29 +219,26 @@ public class PetServiceIntegrationTest { ...@@ -229,29 +219,26 @@ public class PetServiceIntegrationTest {
@Test @Test
@ShouldMatchDataSet("owners-remove-pet.xml") @ShouldMatchDataSet("owners-remove-pet.xml")
public void testRemove() throws LoginException { public void testRemove() throws LoginException {
final int id = existentPetId(); final Pet existentPet = existentPet();
final Pet pet = pet(id);
principal.setName(pet.getOwner().getLogin());
asOwner.run(() -> facade.remove(id)); principal.setName(existentPet.getOwner().getLogin());
asOwner.run(() -> facade.remove(existentPet.getId()));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testRemoveBadId() throws LoginException { public void testRemoveBadId() throws LoginException {
final int id = nonExistentPetId();
principal.setName(ownerWithoutPets().getLogin()); principal.setName(ownerWithoutPets().getLogin());
asOwner.run(() -> facade.remove(id)); asOwner.run(() -> facade.remove(nonExistentPetId()));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testRemoveOthersPetId() throws LoginException { public void testRemoveOthersPetId() throws LoginException {
final Owner ownerWithoutPets = ownerWithoutPets(); final Owner ownerWithoutPets = ownerWithoutPets();
final Owner ownerWithPets = ownerWithPets(); final int petId = anyPetOf(ownerWithPets()).getId();
final int petId = ownerWithPets.getPets().iterator().next().getId();
principal.setName(ownerWithoutPets.getLogin()); principal.setName(ownerWithoutPets.getLogin());
......
...@@ -37,7 +37,7 @@ public abstract class IsEqualToEntity<T> extends TypeSafeMatcher<T> { ...@@ -37,7 +37,7 @@ public abstract class IsEqualToEntity<T> extends TypeSafeMatcher<T> {
/** /**
* Constructs a new instance of {@link IsEqualToEntity}. * Constructs a new instance of {@link IsEqualToEntity}.
* *
* @param entity the expected tentity. * @param entity the expected entity.
*/ */
public IsEqualToEntity(final T entity) { public IsEqualToEntity(final T entity) {
this.expected = requireNonNull(entity); this.expected = requireNonNull(entity);
......
...@@ -99,7 +99,7 @@ public class OwnersDataset { ...@@ -99,7 +99,7 @@ public class OwnersDataset {
return owners.toArray(new Owner[owners.size()]); return owners.toArray(new Owner[owners.size()]);
} }
public static Pet pet(int id) { public static Pet petWithId(int id) {
return stream(pets()) return stream(pets())
.filter(pet -> pet.getId() == id) .filter(pet -> pet.getId() == id)
.findFirst() .findFirst()
...@@ -177,8 +177,15 @@ public class OwnersDataset { ...@@ -177,8 +177,15 @@ public class OwnersDataset {
return ownerWithLogin(OWNER_WITHOUT_PETS_LOGIN); return ownerWithLogin(OWNER_WITHOUT_PETS_LOGIN);
} }
public static Pet anyPetOf(Owner owner) {
if (owner.getPets().isEmpty())
throw new IllegalArgumentException("owner doesn't have pets");
return owner.getPets().iterator().next();
}
public static Pet anyPet() { public static Pet anyPet() {
return pet(existentPetId()); return petWithId(existentPetId());
} }
public static Pet newPet() { public static Pet newPet() {
...@@ -201,6 +208,10 @@ public class OwnersDataset { ...@@ -201,6 +208,10 @@ public class OwnersDataset {
return 2; return 2;
} }
public static Pet existentPet() {
return petWithId(existentPetId());
}
public static int nonExistentPetId() { public static int nonExistentPetId() {
return 1000000; return 1000000;
} }
......
...@@ -7,6 +7,11 @@ import org.hamcrest.Description; ...@@ -7,6 +7,11 @@ import org.hamcrest.Description;
import org.hamcrest.Factory; import org.hamcrest.Factory;
import org.hamcrest.TypeSafeMatcher; import org.hamcrest.TypeSafeMatcher;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.METHOD_NOT_ALLOWED;
import static javax.ws.rs.core.Response.Status.OK;
public class HasHttpStatus extends TypeSafeMatcher<Response> { public class HasHttpStatus extends TypeSafeMatcher<Response> {
private StatusType status; private StatusType status;
...@@ -37,4 +42,24 @@ public class HasHttpStatus extends TypeSafeMatcher<Response> { ...@@ -37,4 +42,24 @@ public class HasHttpStatus extends TypeSafeMatcher<Response> {
public static HasHttpStatus hasHttpStatus(StatusType status) { public static HasHttpStatus hasHttpStatus(StatusType status) {
return new HasHttpStatus(status); return new HasHttpStatus(status);
} }
@Factory
public static HasHttpStatus hasOkStatus() {
return new HasHttpStatus(OK);
}
@Factory
public static HasHttpStatus hasCreatedStatus() {
return new HasHttpStatus(CREATED);
}
@Factory
public static HasHttpStatus hasMethodNotAllowedStatus() {
return new HasHttpStatus(METHOD_NOT_ALLOWED);
}
@Factory
public static HasHttpStatus hasBadRequestStatus() {
return new HasHttpStatus(BAD_REQUEST);
}
} }
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