From 1fb6f62cfb4a0d68b284a5458b40c578b9c8271b Mon Sep 17 00:00:00 2001 From: Miguel Reboiro-Jato Date: Tue, 15 Nov 2016 14:17:35 +0100 Subject: [PATCH] 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. --- .../esei/xcs/rest/OwnerResourceRestTest.java | 29 +++--- .../esei/xcs/rest/OwnerResourceUnitTest.java | 17 ++-- .../service/OwnerServiceIntegrationTest.java | 69 ++++++------- .../service/PetServiceIntegrationTest.java | 97 ++++++++----------- .../xcs/domain/entities/IsEqualToEntity.java | 2 +- .../xcs/domain/entities/OwnersDataset.java | 15 ++- .../esei/xcs/http/util/HasHttpStatus.java | 25 +++++ 7 files changed, 133 insertions(+), 121 deletions(-) diff --git a/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceRestTest.java b/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceRestTest.java index 9a44eda..587a679 100644 --- a/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceRestTest.java +++ b/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceRestTest.java @@ -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.newPasswordForExistentOwner; 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.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.junit.Assert.assertThat; @@ -58,7 +57,7 @@ public class OwnerResourceRestTest { @Deployment public static Archive createDeployment() { - final WebArchive archive = ShrinkWrap.create(WebArchive.class, "test.war") + return ShrinkWrap.create(WebArchive.class, "test.war") .addClass(OwnerResource.class) .addClasses(CORSFilter.class, IllegalArgumentExceptionMapper.class, SecurityExceptionMapper.class) .addPackage(OwnerService.class.getPackage()) @@ -67,8 +66,6 @@ public class OwnerResourceRestTest { .addAsWebInfResource("jboss-web.xml") .addAsWebInfResource("web.xml") .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); - - return archive; } @Test @InSequence(1) @@ -84,7 +81,7 @@ public class OwnerResourceRestTest { ) throws Exception { final Response response = webTarget.request().get(); - assertThat(response, hasHttpStatus(OK)); + assertThat(response, hasOkStatus()); final Owner owner = response.readEntity(Owner.class); final Owner expected = existentOwner(); @@ -112,7 +109,7 @@ public class OwnerResourceRestTest { ) throws Exception { final Response response = webTarget.request().get(); - assertThat(response, hasHttpStatus(BAD_REQUEST)); + assertThat(response, hasBadRequestStatus()); } @Test @InSequence(6) @@ -135,7 +132,7 @@ public class OwnerResourceRestTest { ) throws Exception { final Response response = webTarget.request().get(); - assertThat(response, hasHttpStatus(OK)); + assertThat(response, hasOkStatus()); final List list = ListOwnerType.readEntity(response); assertThat(list, containsOwnersInAnyOrder(owners())); @@ -196,7 +193,7 @@ public class OwnerResourceRestTest { private void testCreateOwner(WebTarget webTarget, Owner newOwner, Owner persistentOwner) { final Response response = webTarget.request().post(json(newOwner)); - assertThat(response, hasHttpStatus(CREATED)); + assertThat(response, hasCreatedStatus()); final String location = response.getHeaderString("Location"); @@ -222,7 +219,7 @@ public class OwnerResourceRestTest { final Response response = webTarget.request().put(json(owner)); - assertThat(response, hasHttpStatus(OK)); + assertThat(response, hasOkStatus()); } @Test @InSequence(32) @@ -245,7 +242,7 @@ public class OwnerResourceRestTest { ) throws Exception { final Response response = webTarget.request().delete(); - assertThat(response, hasHttpStatus(OK)); + assertThat(response, hasOkStatus()); } @Test @InSequence(42) @@ -268,7 +265,7 @@ public class OwnerResourceRestTest { ) throws Exception { final Response response = webTarget.request().delete(); - assertThat(response, hasHttpStatus(OK)); + assertThat(response, hasOkStatus()); } @Test @InSequence(45) @@ -291,7 +288,7 @@ public class OwnerResourceRestTest { ) throws Exception { final Response response = webTarget.request().delete(); - assertThat(response, hasHttpStatus(METHOD_NOT_ALLOWED)); + assertThat(response, hasMethodNotAllowedStatus()); } @Test @InSequence(48) diff --git a/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceUnitTest.java b/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceUnitTest.java index 7902692..abfce26 100644 --- a/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceUnitTest.java +++ b/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceUnitTest.java @@ -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.newOwnerWithPersistentPets; 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.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.expectLastCall; import static org.hamcrest.CoreMatchers.equalTo; @@ -71,7 +70,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { final Response response = resource.get(owner.getLogin()); - assertThat(response, hasHttpStatus(OK)); + assertThat(response, hasOkStatus()); assertThat(response.getEntity(), is(instanceOf(Owner.class))); assertThat((Owner) response.getEntity(), is(equalToOwner(owner))); } @@ -107,7 +106,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { final Response response = resource.list(); - assertThat(response, hasHttpStatus(OK)); + assertThat(response, hasOkStatus()); assertThat(response.getEntity(), is(instanceOf(List.class))); assertThat((List) response.getEntity(), containsOwnersInAnyOrder(owners)); } @@ -122,7 +121,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { final Response response = resource.list(); - assertThat(response, hasHttpStatus(OK)); + assertThat(response, hasOkStatus()); assertThat(response.getEntity(), is(instanceOf(List.class))); assertThat((List) response.getEntity(), is(empty())); } @@ -148,7 +147,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { final Response response = resource.create(newOwner); - assertThat(response, hasHttpStatus(CREATED)); + assertThat(response, hasCreatedStatus()); assertThat(response.getHeaderString("Location"), is(equalTo(mockUri.toString()))); } @@ -183,7 +182,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { final Response response = resource.update(owner); - assertThat(response, hasHttpStatus(OK)); + assertThat(response, hasOkStatus()); } @Test(expected = IllegalArgumentException.class) @@ -203,7 +202,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { final Response response = resource.delete(login); - assertThat(response, hasHttpStatus(OK)); + assertThat(response, hasOkStatus()); } @Test(expected = IllegalArgumentException.class) diff --git a/service/src/test/java/es/uvigo/esei/xcs/service/OwnerServiceIntegrationTest.java b/service/src/test/java/es/uvigo/esei/xcs/service/OwnerServiceIntegrationTest.java index f197e79..0a7e3bd 100644 --- a/service/src/test/java/es/uvigo/esei/xcs/service/OwnerServiceIntegrationTest.java +++ b/service/src/test/java/es/uvigo/esei/xcs/service/OwnerServiceIntegrationTest.java @@ -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.equalToOwner; 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.newOwnerWithFreshPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithPersistentPets; @@ -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.nonExistentLogin; 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.ownerWithoutPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owners; @@ -24,6 +22,7 @@ import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.junit.Assert.assertThat; +import java.util.Collection; import java.util.List; import javax.ejb.EJB; @@ -59,35 +58,31 @@ public class OwnerServiceIntegrationTest { @Deployment 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) .addPackage(RoleCaller.class.getPackage()) .addPackage(Owner.class.getPackage()) .addAsResource("test-persistence.xml", "META-INF/persistence.xml") .addAsWebInfResource("jboss-web.xml") .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); - - return archive; } @Test @ShouldMatchDataSet("owners.xml") 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 @ShouldMatchDataSet("owners.xml") public void testGetOwnerNonExistent() { - final String login = nonExistentLogin(); - - final Owner actual = asAdmin.call(() -> facade.get(login)); + final Owner actualOwner = asAdmin.call(() -> facade.get(nonExistentLogin())); - assertThat(actual, is(nullValue())); + assertThat(actualOwner, is(nullValue())); } @Test(expected = EJBTransactionRolledbackException.class) @@ -99,9 +94,9 @@ public class OwnerServiceIntegrationTest { @Test @ShouldMatchDataSet("owners.xml") public void testList() { - final List actual = asAdmin.call(() -> facade.list()); + final List actualOwners = asAdmin.call(() -> facade.list()); - assertThat(actual, is(containsOwnersInAnyOrder(owners()))); + assertThat(actualOwners, containsOwnersInAnyOrder(owners())); } @Test @@ -110,10 +105,10 @@ public class OwnerServiceIntegrationTest { final String petName = petNameWithSingleOwner(); final Owner owner = ownersOf(petName)[0]; - final List owners = asAdmin.call(() -> facade.findByPetName(petName)); + final List actualOwners = asAdmin.call(() -> facade.findByPetName(petName)); - assertThat(owners, hasSize(1)); - assertThat(owners.get(0), is(equalToOwner(owner))); + assertThat(actualOwners, hasSize(1)); + assertThat(actualOwners.get(0), is(equalToOwner(owner))); } @Test @@ -121,21 +116,21 @@ public class OwnerServiceIntegrationTest { public void testFindByPetNameMultipleOwners() { final String petName = petNameWithMultipleOwners(); - final List owners = asAdmin.call(() -> facade.findByPetName(petName)); + final List actualOwners = asAdmin.call(() -> facade.findByPetName(petName)); final Owner[] expectedOwners = ownersOf(petName); - assertThat(owners, containsOwnersInAnyOrder(expectedOwners)); + assertThat(actualOwners, containsOwnersInAnyOrder(expectedOwners)); } @Test @ShouldMatchDataSet("owners.xml") public void testFindByPetNameNoPet() { - final String pet = nonExistentPetName(); + final String nonExistentPet = nonExistentPetName(); - final List owners = asAdmin.call(() -> facade.findByPetName(pet)); + final List actualOwners = asAdmin.call(() -> facade.findByPetName(nonExistentPet)); - assertThat(owners, is(empty())); + assertThat(actualOwners, is(empty())); } @Test(expected = EJBTransactionRolledbackException.class) @@ -157,9 +152,9 @@ public class OwnerServiceIntegrationTest { @Test @ShouldMatchDataSet({"owners.xml", "owners-create-with-pets.xml"}) 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) @@ -183,10 +178,10 @@ public class OwnerServiceIntegrationTest { @Test @ShouldMatchDataSet("owners-update-password.xml") public void testUpdatePassword() { - final Owner owner = existentOwner(); - owner.changePassword(newPasswordForExistentOwner()); + final Owner existentOwner = existentOwner(); + existentOwner.changePassword(newPasswordForExistentOwner()); - asAdmin.run(() -> facade.update(owner)); + asAdmin.run(() -> facade.update(existentOwner)); } @Test @@ -194,17 +189,17 @@ public class OwnerServiceIntegrationTest { public void testUpdateNewOwnerWithoutPets() { 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 @ShouldMatchDataSet({"owners.xml", "owners-create-with-pets.xml"}) 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 @@ -235,21 +230,19 @@ public class OwnerServiceIntegrationTest { @ShouldMatchDataSet("owners.xml") public void testGetPets() { final Owner owner = ownerWithPets(); - final Pet[] ownedPets = owner.getPets().toArray(new Pet[0]); + final Collection ownedPets = owner.getPets(); - final List pets = asAdmin.call(() -> facade.getPets(owner.getLogin())); + final List actualPets = asAdmin.call(() -> facade.getPets(owner.getLogin())); - assertThat(pets, containsPetsInAnyOrder(ownedPets)); + assertThat(actualPets, containsPetsInAnyOrder(ownedPets)); } @Test @ShouldMatchDataSet("owners.xml") public void testGetPetsNoPets() { - final Owner owner = ownerWithoutPets(); - - final List pets = asAdmin.call(() -> facade.getPets(owner.getLogin())); + final List actualPets = asAdmin.call(() -> facade.getPets(ownerWithoutPets().getLogin())); - assertThat(pets, is(empty())); + assertThat(actualPets, is(empty())); } @Test(expected = EJBTransactionRolledbackException.class) diff --git a/service/src/test/java/es/uvigo/esei/xcs/service/PetServiceIntegrationTest.java b/service/src/test/java/es/uvigo/esei/xcs/service/PetServiceIntegrationTest.java index 79bc89d..fb55283 100644 --- a/service/src/test/java/es/uvigo/esei/xcs/service/PetServiceIntegrationTest.java +++ b/service/src/test/java/es/uvigo/esei/xcs/service/PetServiceIntegrationTest.java @@ -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.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.newPet; 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.ownerWithPets; 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.nullValue; import static org.hamcrest.collection.IsEmptyCollection.empty; @@ -55,37 +57,33 @@ public class PetServiceIntegrationTest { @Deployment 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) .addPackage(RoleCaller.class.getPackage()) .addPackage(Pet.class.getPackage()) .addAsResource("test-persistence.xml", "META-INF/persistence.xml") .addAsWebInfResource("jboss-web.xml") .addAsWebInfResource("beans.xml", "beans.xml"); - - return archive; } @Test @ShouldMatchDataSet("owners.xml") public void testGet() throws LoginException { - final int id = existentPetId(); - final Pet pet = pet(id); - principal.setName(pet.getOwner().getLogin()); + final Pet existentPet = existentPet(); + + 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 @ShouldMatchDataSet("owners.xml") public void testGetBadId() throws LoginException { - final int id = nonExistentPetId(); - principal.setName(ownerWithoutPets().getLogin()); - final Pet actual = asOwner.call(() -> facade.get(id)); + final Pet actual = asOwner.call(() -> facade.get(nonExistentPetId())); assertThat(actual, is(nullValue())); } @@ -94,8 +92,7 @@ public class PetServiceIntegrationTest { @ShouldMatchDataSet("owners.xml") public void testGetOthersPetId() throws LoginException { final Owner ownerWithoutPets = ownerWithoutPets(); - final Owner ownerWithPets = ownerWithPets(); - final int petId = ownerWithPets.getPets().iterator().next().getId(); + final int petId = anyPetOf(ownerWithPets()).getId(); principal.setName(ownerWithoutPets.getLogin()); @@ -105,21 +102,19 @@ public class PetServiceIntegrationTest { @Test @ShouldMatchDataSet("owners.xml") public void testList() throws LoginException { - final Owner owner = ownerWithPets(); - final Pet[] ownedPets = owner.getPets().toArray(new Pet[0]); - principal.setName(owner.getLogin()); + final Owner ownerWithPets = ownerWithPets(); - final List pets = asOwner.call(() -> facade.list()); + principal.setName(ownerWithPets.getLogin()); - assertThat(pets, containsPetsInAnyOrder(ownedPets)); + final List actualPets = asOwner.call(() -> facade.list()); + + assertThat(actualPets, containsPetsInAnyOrder(ownerWithPets.getPets())); } @Test @ShouldMatchDataSet("owners.xml") public void testListNoPets() throws LoginException { - final Owner owner = ownerWithoutPets(); - - principal.setName(owner.getLogin()); + principal.setName(ownerWithoutPets().getLogin()); final List pets = asOwner.call(() -> facade.list()); @@ -129,10 +124,11 @@ public class PetServiceIntegrationTest { @Test @ShouldMatchDataSet({ "owners.xml", "owners-create-pet.xml" }) public void testCreate() { - final Owner owner = ownerWithoutPets(); - principal.setName(owner.getLogin()); + final Owner ownerWithoutPets = ownerWithoutPets(); - final Pet pet = newPetWithOwner(owner); + principal.setName(ownerWithoutPets.getLogin()); + + final Pet pet = newPetWithOwner(ownerWithoutPets); asOwner.call(() -> facade.create(pet)); } @@ -158,12 +154,9 @@ public class PetServiceIntegrationTest { @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet({ "owners.xml" }) public void testCreateWrongOwner() { - final Owner owner = ownerWithoutPets(); - final Owner otherOwner = ownerWithPets(); - - principal.setName(owner.getLogin()); + principal.setName(ownerWithoutPets().getLogin()); - final Pet pet = newPetWithOwner(otherOwner); + final Pet pet = newPetWithOwner(ownerWithPets()); asOwner.run(() -> facade.create(pet)); } @@ -171,25 +164,25 @@ public class PetServiceIntegrationTest { @Test @ShouldMatchDataSet("owners-update-pet.xml") public void testUpdate() throws LoginException { - final int id = existentPetId(); - final Pet pet = pet(id); + final Pet existentPet = existentPet(); - principal.setName(pet.getOwner().getLogin()); + principal.setName(existentPet.getOwner().getLogin()); - pet.setName("UpdateName"); - pet.setAnimal(AnimalType.BIRD); - pet.setBirth(new Date(946771261000L)); + existentPet.setName("UpdateName"); + existentPet.setAnimal(AnimalType.BIRD); + existentPet.setBirth(new Date(946771261000L)); - asOwner.run(() -> facade.update(pet)); + asOwner.run(() -> facade.update(existentPet)); } @Test @ShouldMatchDataSet({ "owners.xml", "owners-create-pet.xml" }) public void testUpdateNewPetWithOwner() { - final Owner owner = ownerWithoutPets(); - principal.setName(owner.getLogin()); + final Owner ownerWithoutPets = ownerWithoutPets(); + + principal.setName(ownerWithoutPets.getLogin()); - final Pet pet = newPetWithOwner(owner); + final Pet pet = newPetWithOwner(ownerWithoutPets); asOwner.call(() -> facade.update(pet)); } @@ -203,12 +196,9 @@ public class PetServiceIntegrationTest { @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet({ "owners.xml" }) public void testUpdateWrongOwner() { - final Owner owner = ownerWithoutPets(); - final Owner otherOwner = ownerWithPets(); - - principal.setName(owner.getLogin()); + principal.setName(ownerWithoutPets().getLogin()); - final Pet pet = otherOwner.getPets().iterator().next(); + final Pet pet = anyPetOf(ownerWithPets()); asOwner.run(() -> facade.update(pet)); } @@ -217,7 +207,7 @@ public class PetServiceIntegrationTest { @ShouldMatchDataSet({ "owners.xml", "owners-create-pet.xml" }) public void testUpdatePetNoOwner() { final int id = existentPetId(); - final Pet pet = pet(id); + final Pet pet = petWithId(id); principal.setName(pet.getOwner().getLogin()); pet.setOwner(null); @@ -229,29 +219,26 @@ public class PetServiceIntegrationTest { @Test @ShouldMatchDataSet("owners-remove-pet.xml") public void testRemove() throws LoginException { - final int id = existentPetId(); - final Pet pet = pet(id); - principal.setName(pet.getOwner().getLogin()); + final Pet existentPet = existentPet(); - asOwner.run(() -> facade.remove(id)); + principal.setName(existentPet.getOwner().getLogin()); + + asOwner.run(() -> facade.remove(existentPet.getId())); } @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet("owners.xml") public void testRemoveBadId() throws LoginException { - final int id = nonExistentPetId(); - principal.setName(ownerWithoutPets().getLogin()); - asOwner.run(() -> facade.remove(id)); + asOwner.run(() -> facade.remove(nonExistentPetId())); } @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet("owners.xml") public void testRemoveOthersPetId() throws LoginException { final Owner ownerWithoutPets = ownerWithoutPets(); - final Owner ownerWithPets = ownerWithPets(); - final int petId = ownerWithPets.getPets().iterator().next().getId(); + final int petId = anyPetOf(ownerWithPets()).getId(); principal.setName(ownerWithoutPets.getLogin()); diff --git a/tests/src/main/java/es/uvigo/esei/xcs/domain/entities/IsEqualToEntity.java b/tests/src/main/java/es/uvigo/esei/xcs/domain/entities/IsEqualToEntity.java index 704730f..c3de4aa 100644 --- a/tests/src/main/java/es/uvigo/esei/xcs/domain/entities/IsEqualToEntity.java +++ b/tests/src/main/java/es/uvigo/esei/xcs/domain/entities/IsEqualToEntity.java @@ -37,7 +37,7 @@ public abstract class IsEqualToEntity extends TypeSafeMatcher { /** * Constructs a new instance of {@link IsEqualToEntity}. * - * @param entity the expected tentity. + * @param entity the expected entity. */ public IsEqualToEntity(final T entity) { this.expected = requireNonNull(entity); diff --git a/tests/src/main/java/es/uvigo/esei/xcs/domain/entities/OwnersDataset.java b/tests/src/main/java/es/uvigo/esei/xcs/domain/entities/OwnersDataset.java index 5c1d027..337b250 100644 --- a/tests/src/main/java/es/uvigo/esei/xcs/domain/entities/OwnersDataset.java +++ b/tests/src/main/java/es/uvigo/esei/xcs/domain/entities/OwnersDataset.java @@ -99,7 +99,7 @@ public class OwnersDataset { return owners.toArray(new Owner[owners.size()]); } - public static Pet pet(int id) { + public static Pet petWithId(int id) { return stream(pets()) .filter(pet -> pet.getId() == id) .findFirst() @@ -177,8 +177,15 @@ public class OwnersDataset { 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() { - return pet(existentPetId()); + return petWithId(existentPetId()); } public static Pet newPet() { @@ -201,6 +208,10 @@ public class OwnersDataset { return 2; } + public static Pet existentPet() { + return petWithId(existentPetId()); + } + public static int nonExistentPetId() { return 1000000; } diff --git a/tests/src/main/java/es/uvigo/esei/xcs/http/util/HasHttpStatus.java b/tests/src/main/java/es/uvigo/esei/xcs/http/util/HasHttpStatus.java index dcd75e9..fe4af53 100644 --- a/tests/src/main/java/es/uvigo/esei/xcs/http/util/HasHttpStatus.java +++ b/tests/src/main/java/es/uvigo/esei/xcs/http/util/HasHttpStatus.java @@ -7,6 +7,11 @@ import org.hamcrest.Description; import org.hamcrest.Factory; 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 { private StatusType status; @@ -37,4 +42,24 @@ public class HasHttpStatus extends TypeSafeMatcher { public static HasHttpStatus hasHttpStatus(StatusType 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); + } } -- 2.18.1