From 2427b7c9433110cd7216a07945e76ff7ea46a43b Mon Sep 17 00:00:00 2001 From: Miguel Reboiro-Jato Date: Mon, 2 Nov 2015 19:47:26 +0100 Subject: [PATCH] Refactorizes some test classes Some test classes have been refactorized for a more consistent use of the OwnersDataset class and for a simpler test implementation. --- .../esei/xcs/rest/OwnerResourceRestTest.java | 10 +- .../esei/xcs/rest/OwnerResourceUnitTest.java | 2 +- ...erServiceIllegalAccessIntegrationTest.java | 16 +-- .../service/OwnerServiceIntegrationTest.java | 79 +++++++------- ...etServiceIllegalAccessIntegrationTest.java | 22 ++-- .../service/PetServiceIntegrationTest.java | 64 +++++------ .../xcs/domain/entities/OwnersDataset.java | 102 ++++++++++++------ .../xcs/service/util/security/RoleCaller.java | 3 + .../datasets/owners-update-password.xml | 4 +- 9 files changed, 175 insertions(+), 127 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 48c478c..9ca0d3a 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 @@ -6,10 +6,11 @@ import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.EXISTENT_LOGIN; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.NON_EXISTENT_LOGIN; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.OWNER_WITHOUT_PETS_LOGIN; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.OWNER_WITH_PETS_LOGIN; +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.newOwnerWithoutPets; -import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owner; +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 javax.ws.rs.client.Entity.json; @@ -47,7 +48,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import es.uvigo.esei.xcs.domain.entities.Owner; -import es.uvigo.esei.xcs.domain.entities.OwnersDataset; import es.uvigo.esei.xcs.rest.GenericTypes.ListOwnerType; import es.uvigo.esei.xcs.service.OwnerService; @@ -87,7 +87,7 @@ public class OwnerResourceRestTest { assertThat(response, hasHttpStatus(OK)); final Owner owner = response.readEntity(Owner.class); - final Owner expected = owner("pepe"); + final Owner expected = existentOwner(); assertThat(owner, is(equalsToOwner(expected))); } @@ -217,8 +217,8 @@ public class OwnerResourceRestTest { public void testUpdatePassword( @ArquillianResteasyResource(BASE_PATH) ResteasyWebTarget webTarget ) throws Exception { - final Owner owner = OwnersDataset.anyOwner(); - owner.changePassword("newpassword"); + final Owner owner = existentOwner(); + owner.changePassword(newPasswordForExistentOwner()); final Response response = webTarget.request().put(json(owner)); 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 1e61feb..d8f00f5 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 @@ -181,7 +181,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { @Test public void testDelete() { - final String login = OwnersDataset.anyLogin(); + final String login = anyLogin(); facade.remove(login); diff --git a/service/src/test/java/es/uvigo/esei/xcs/service/OwnerServiceIllegalAccessIntegrationTest.java b/service/src/test/java/es/uvigo/esei/xcs/service/OwnerServiceIllegalAccessIntegrationTest.java index a92caf1..a7e8c9e 100644 --- a/service/src/test/java/es/uvigo/esei/xcs/service/OwnerServiceIllegalAccessIntegrationTest.java +++ b/service/src/test/java/es/uvigo/esei/xcs/service/OwnerServiceIllegalAccessIntegrationTest.java @@ -36,7 +36,7 @@ public class OwnerServiceIllegalAccessIntegrationTest { private OwnerService facade; @EJB(beanName = "owner-caller") - private RoleCaller owner; + private RoleCaller asOwner; @Deployment public static Archive createDeployment() { @@ -88,36 +88,36 @@ public class OwnerServiceIllegalAccessIntegrationTest { @Test(expected = EJBTransactionRolledbackException.class) public void testGetRoleOwner() { - this.owner.run(this::testGetNoRole); + asOwner.run(this::testGetNoRole); } @Test(expected = EJBTransactionRolledbackException.class) public void testListRoleOwner() { - this.owner.run(this::testListNoRole); + asOwner.run(this::testListNoRole); } @Test(expected = EJBTransactionRolledbackException.class) public void testFindByPetNameRoleOwner() { - this.owner.run(this::testFindByPetNameNoRole); + asOwner.run(this::testFindByPetNameNoRole); } @Test(expected = EJBTransactionRolledbackException.class) public void testCreateRoleOwner() { - this.owner.run(this::testCreateNoRole); + asOwner.run(this::testCreateNoRole); } @Test(expected = EJBTransactionRolledbackException.class) public void testUpdateRoleOwner() { - this.owner.run(this::testUpdateNoRole); + asOwner.run(this::testUpdateNoRole); } @Test(expected = EJBTransactionRolledbackException.class) public void testRemoveRoleOwner() { - this.owner.run(this::testRemoveNoRole); + asOwner.run(this::testRemoveNoRole); } @Test(expected = EJBTransactionRolledbackException.class) public void testGetPetsRoleOwner() { - this.owner.run(this::testGetPetsNoRole); + asOwner.run(this::testGetPetsNoRole); } } 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 064b8b1..767f8db 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,18 +3,24 @@ package es.uvigo.esei.xcs.service; import static es.uvigo.esei.xcs.domain.entities.IsEqualsToOwner.containsOwnersInAnyOrder; import static es.uvigo.esei.xcs.domain.entities.IsEqualsToOwner.equalsToOwner; import static es.uvigo.esei.xcs.domain.entities.IsEqualsToPet.containsPetsInAnyOrder; -import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.anyOwner; +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; 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.nonExistentLogin; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.nonExistentPetName; -import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owner; +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; +import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.ownersOf; +import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.petNameWithMultipleOwners; +import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.petNameWithSingleOwner; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.junit.Assert.assertThat; @@ -49,7 +55,7 @@ public class OwnerServiceIntegrationTest { private OwnerService facade; @EJB(beanName = "admin-caller") - private RoleCaller admin; + private RoleCaller asAdmin; @Deployment public static Archive createDeployment() { @@ -67,12 +73,11 @@ public class OwnerServiceIntegrationTest { @Test @ShouldMatchDataSet("owners.xml") public void testGetOwner() { - final String login = "pepe"; - final Owner pepe = owner(login); + final String login = existentLogin(); - final Owner actual = admin.call(() -> facade.get(login)); + final Owner actual = asAdmin.call(() -> facade.get(login)); - assertThat(actual, is(equalsToOwner(pepe))); + assertThat(actual, is(equalsToOwner(ownerWithLogin(login)))); } @Test @@ -80,7 +85,7 @@ public class OwnerServiceIntegrationTest { public void testGetOwnerNonExistent() { final String login = nonExistentLogin(); - final Owner actual = admin.call(() -> facade.get(login)); + final Owner actual = asAdmin.call(() -> facade.get(login)); assertThat(actual, is(nullValue())); } @@ -88,13 +93,13 @@ public class OwnerServiceIntegrationTest { @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet("owners.xml") public void testGetOwnerNull() { - admin.call(() -> facade.get(null)); + asAdmin.call(() -> facade.get(null)); } @Test @ShouldMatchDataSet("owners.xml") public void testList() { - final List actual = admin.call(() -> facade.list()); + final List actual = asAdmin.call(() -> facade.list()); assertThat(actual, is(containsOwnersInAnyOrder(owners()))); } @@ -102,23 +107,23 @@ public class OwnerServiceIntegrationTest { @Test @ShouldMatchDataSet("owners.xml") public void testFindByPetName() { - final String pet = "Juandog"; + final String petName = petNameWithSingleOwner(); + final Owner owner = ownersOf(petName)[0]; - final List owners = admin.call(() -> facade.findByPetName(pet)); + final List owners = asAdmin.call(() -> facade.findByPetName(petName)); - final Owner owner = owners.get(0); - final Owner juan = owner("juan"); - assertThat(owner, is(equalsToOwner(juan))); + assertThat(owners, hasSize(1)); + assertThat(owners.get(0), is(equalsToOwner(owner))); } @Test @ShouldMatchDataSet("owners.xml") public void testFindByPetNameMultipleOwners() { - final String pet = "Max"; + final String petName = petNameWithMultipleOwners(); - final List owners = admin.call(() -> facade.findByPetName(pet)); + final List owners = asAdmin.call(() -> facade.findByPetName(petName)); - final Owner[] expectedOwners = owners("juan", "ana"); + final Owner[] expectedOwners = ownersOf(petName); assertThat(owners, containsOwnersInAnyOrder(expectedOwners)); } @@ -128,7 +133,7 @@ public class OwnerServiceIntegrationTest { public void testFindByPetNameNoPet() { final String pet = nonExistentPetName(); - final List owners = admin.call(() -> facade.findByPetName(pet)); + final List owners = asAdmin.call(() -> facade.findByPetName(pet)); assertThat(owners, is(empty())); } @@ -136,7 +141,7 @@ public class OwnerServiceIntegrationTest { @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet("owners.xml") public void testFindByPetNameNull() { - admin.run(() -> facade.findByPetName(null)); + asAdmin.run(() -> facade.findByPetName(null)); } @Test @@ -144,7 +149,7 @@ public class OwnerServiceIntegrationTest { public void testCreateWithoutPets() { final Owner newOwner = newOwnerWithoutPets(); - final Owner actual = admin.call(() -> facade.create(newOwner)); + final Owner actual = asAdmin.call(() -> facade.create(newOwner)); assertThat(actual, is(equalsToOwner(newOwner))); } @@ -152,7 +157,7 @@ public class OwnerServiceIntegrationTest { @Test @ShouldMatchDataSet({"owners.xml", "owners-create-with-pets.xml"}) public void testCreateWithPets() { - final Owner actual = admin.call(() -> facade.create(newOwnerWithFreshPets())); + final Owner actual = asAdmin.call(() -> facade.create(newOwnerWithFreshPets())); assertThat(actual, is(equalsToOwner(newOwnerWithPersistentPets()))); } @@ -160,22 +165,22 @@ public class OwnerServiceIntegrationTest { @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet("owners.xml") public void testCreateExistentLogin() { - admin.run(() -> facade.create(anyOwner())); + asAdmin.run(() -> facade.create(existentOwner())); } @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet("owners.xml") public void testCreateNull() { - admin.call(() -> facade.create(null)); + asAdmin.call(() -> facade.create(null)); } @Test @ShouldMatchDataSet("owners-update-password.xml") public void testUpdatePassword() { - final Owner owner = anyOwner(); - owner.changePassword("newpassword"); + final Owner owner = existentOwner(); + owner.changePassword(newPasswordForExistentOwner()); - admin.run(() -> facade.update(owner)); + asAdmin.run(() -> facade.update(owner)); } @Test @@ -183,7 +188,7 @@ public class OwnerServiceIntegrationTest { public void testUpdateNewOwnerWithoutPets() { final Owner newOwner = newOwnerWithoutPets(); - final Owner actual = admin.call(() -> facade.update(newOwner)); + final Owner actual = asAdmin.call(() -> facade.update(newOwner)); assertThat(actual, is(equalsToOwner(newOwner))); } @@ -191,7 +196,7 @@ public class OwnerServiceIntegrationTest { @Test @ShouldMatchDataSet({"owners.xml", "owners-create-with-pets.xml"}) public void testUpdateNewOwnerWithPets() { - final Owner actual = admin.call(() -> facade.update(newOwnerWithFreshPets())); + final Owner actual = asAdmin.call(() -> facade.update(newOwnerWithFreshPets())); assertThat(actual, is(equalsToOwner(newOwnerWithPersistentPets()))); } @@ -199,25 +204,25 @@ public class OwnerServiceIntegrationTest { @Test @ShouldMatchDataSet("owners-remove-without-pets.xml") public void testRemoveWithoutPets() { - admin.run(() -> facade.remove(ownerWithoutPets().getLogin())); + asAdmin.run(() -> facade.remove(ownerWithoutPets().getLogin())); } @Test @ShouldMatchDataSet("owners-remove-with-pets.xml") public void testRemoveWithPets() { - admin.run(() -> facade.remove(ownerWithPets().getLogin())); + asAdmin.run(() -> facade.remove(ownerWithPets().getLogin())); } @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet("owners.xml") public void testRemoveNonExistentOwner() { - admin.run(() -> facade.remove(nonExistentLogin())); + asAdmin.run(() -> facade.remove(nonExistentLogin())); } @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet("owners.xml") public void testRemoveNull() { - admin.run(() -> facade.remove(null)); + asAdmin.run(() -> facade.remove(null)); } @Test @@ -226,7 +231,7 @@ public class OwnerServiceIntegrationTest { final Owner owner = ownerWithPets(); final Pet[] ownedPets = owner.getPets().toArray(new Pet[0]); - final List pets = admin.call(() -> facade.getPets(owner.getLogin())); + final List pets = asAdmin.call(() -> facade.getPets(owner.getLogin())); assertThat(pets, containsPetsInAnyOrder(ownedPets)); } @@ -236,7 +241,7 @@ public class OwnerServiceIntegrationTest { public void testGetPetsNoPets() { final Owner owner = ownerWithoutPets(); - final List pets = admin.call(() -> facade.getPets(owner.getLogin())); + final List pets = asAdmin.call(() -> facade.getPets(owner.getLogin())); assertThat(pets, is(empty())); } @@ -244,12 +249,12 @@ public class OwnerServiceIntegrationTest { @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet("owners.xml") public void testGetPetsNonExistentOwner() { - admin.call(() -> facade.getPets(nonExistentLogin())); + asAdmin.call(() -> facade.getPets(nonExistentLogin())); } @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet("owners.xml") public void testGetPetsNull() { - admin.call(() -> facade.getPets(null)); + asAdmin.call(() -> facade.getPets(null)); } } diff --git a/service/src/test/java/es/uvigo/esei/xcs/service/PetServiceIllegalAccessIntegrationTest.java b/service/src/test/java/es/uvigo/esei/xcs/service/PetServiceIllegalAccessIntegrationTest.java index f619259..67b7ee4 100644 --- a/service/src/test/java/es/uvigo/esei/xcs/service/PetServiceIllegalAccessIntegrationTest.java +++ b/service/src/test/java/es/uvigo/esei/xcs/service/PetServiceIllegalAccessIntegrationTest.java @@ -41,10 +41,10 @@ public class PetServiceIllegalAccessIntegrationTest { private TestPrincipal principal; @EJB(beanName = "admin-caller") - private RoleCaller admin; + private RoleCaller asAdmin; @EJB(beanName = "owner-caller") - private RoleCaller owner; + private RoleCaller asOwner; @Deployment public static Archive createDeployment() { @@ -86,27 +86,27 @@ public class PetServiceIllegalAccessIntegrationTest { @Test(expected = EJBTransactionRolledbackException.class) public void testGetRoleAdmin() { - this.admin.run(this::testGetNoRole); + asAdmin.run(this::testGetNoRole); } @Test(expected = EJBTransactionRolledbackException.class) public void testListRoleAdmin() { - this.admin.run(this::testListNoRole); + asAdmin.run(this::testListNoRole); } @Test(expected = EJBTransactionRolledbackException.class) public void testCreateRoleAdmin() { - this.admin.run(this::testCreateNoRole); + asAdmin.run(this::testCreateNoRole); } @Test(expected = EJBTransactionRolledbackException.class) public void testUpdateRoleAdmin() { - this.admin.run(this::testUpdateNoRole); + asAdmin.run(this::testUpdateNoRole); } @Test(expected = EJBTransactionRolledbackException.class) public void testRemoveRoleAdmin() { - this.admin.run(this::testRemoveNoRole); + asAdmin.run(this::testRemoveNoRole); } @Test(expected = EJBTransactionRolledbackException.class) @@ -118,7 +118,7 @@ public class PetServiceIllegalAccessIntegrationTest { principal.setName(owner2.getLogin()); - this.owner.run(() -> this.facade.get(pet1.getId())); + asOwner.run(() -> facade.get(pet1.getId())); } @Test(expected = EJBTransactionRolledbackException.class) @@ -130,7 +130,7 @@ public class PetServiceIllegalAccessIntegrationTest { principal.setName(owner2.getLogin()); - this.owner.run(() -> this.facade.create(pet)); + asOwner.run(() -> facade.create(pet)); } @Test(expected = EJBTransactionRolledbackException.class) @@ -143,7 +143,7 @@ public class PetServiceIllegalAccessIntegrationTest { principal.setName(owner2.getLogin()); - this.owner.run(() -> this.facade.update(pet1)); + asOwner.run(() -> facade.update(pet1)); } @Test(expected = EJBTransactionRolledbackException.class) @@ -155,6 +155,6 @@ public class PetServiceIllegalAccessIntegrationTest { principal.setName(owner2.getLogin()); - this.owner.run(() -> this.facade.remove(pet1.getId())); + asOwner.run(() -> facade.remove(pet1.getId())); } } 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 0a944a3..4a9a235 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 @@ -48,7 +48,7 @@ public class PetServiceIntegrationTest { private PetService facade; @EJB(beanName = "owner-caller") - private RoleCaller owner; + private RoleCaller asOwner; @Inject private TestPrincipal principal; @@ -71,9 +71,9 @@ public class PetServiceIntegrationTest { public void testGet() throws LoginException { final int id = existentPetId(); final Pet pet = pet(id); - this.principal.setName(pet.getOwner().getLogin()); + principal.setName(pet.getOwner().getLogin()); - final Pet actual = this.owner.call(() -> facade.get(id)); + final Pet actual = asOwner.call(() -> facade.get(id)); assertThat(actual, equalsToPet(pet)); } @@ -83,9 +83,9 @@ public class PetServiceIntegrationTest { public void testGetBadId() throws LoginException { final int id = nonExistentPetId(); - this.principal.setName(ownerWithoutPets().getLogin()); + principal.setName(ownerWithoutPets().getLogin()); - final Pet actual = this.owner.call(() -> facade.get(id)); + final Pet actual = asOwner.call(() -> facade.get(id)); assertThat(actual, is(nullValue())); } @@ -97,9 +97,9 @@ public class PetServiceIntegrationTest { final Owner ownerWithPets = ownerWithPets(); final int petId = ownerWithPets.getPets().iterator().next().getId(); - this.principal.setName(ownerWithoutPets.getLogin()); + principal.setName(ownerWithoutPets.getLogin()); - this.owner.run(() -> facade.get(petId)); + asOwner.run(() -> facade.get(petId)); } @Test @@ -107,9 +107,9 @@ public class PetServiceIntegrationTest { public void testList() throws LoginException { final Owner owner = ownerWithPets(); final Pet[] ownedPets = owner.getPets().toArray(new Pet[0]); - this.principal.setName(owner.getLogin()); + principal.setName(owner.getLogin()); - final List pets = this.owner.call(() -> facade.list()); + final List pets = asOwner.call(() -> facade.list()); assertThat(pets, containsPetsInAnyOrder(ownedPets)); } @@ -119,9 +119,9 @@ public class PetServiceIntegrationTest { public void testListNoPets() throws LoginException { final Owner owner = ownerWithoutPets(); - this.principal.setName(owner.getLogin()); + principal.setName(owner.getLogin()); - final List pets = this.owner.call(() -> facade.list()); + final List pets = asOwner.call(() -> facade.list()); assertThat(pets, is(empty())); } @@ -130,19 +130,19 @@ public class PetServiceIntegrationTest { @ShouldMatchDataSet({ "owners.xml", "owners-create-pet.xml" }) public void testCreate() { final Owner owner = ownerWithoutPets(); - this.principal.setName(owner.getLogin()); + principal.setName(owner.getLogin()); final Pet pet = newPet(); - this.owner.call(() -> facade.create(pet)); + asOwner.call(() -> facade.create(pet)); } @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet({ "owners.xml" }) public void testCreateNull() { - this.principal.setName(ownerWithoutPets().getLogin()); + principal.setName(ownerWithoutPets().getLogin()); - this.owner.run(() -> facade.create(null)); + asOwner.run(() -> facade.create(null)); } @Test(expected = EJBTransactionRolledbackException.class) @@ -151,11 +151,11 @@ public class PetServiceIntegrationTest { final Owner owner = ownerWithoutPets(); final Owner otherOwner = ownerWithPets(); - this.principal.setName(owner.getLogin()); + principal.setName(owner.getLogin()); final Pet pet = newPetWithOwner(otherOwner); - this.owner.run(() -> facade.create(pet)); + asOwner.run(() -> facade.create(pet)); } @Test @@ -164,30 +164,30 @@ public class PetServiceIntegrationTest { final int id = existentPetId(); final Pet pet = pet(id); - this.principal.setName(pet.getOwner().getLogin()); + principal.setName(pet.getOwner().getLogin()); pet.setName("UpdateName"); pet.setAnimal(AnimalType.BIRD); pet.setBirth(new Date(946771261000L)); - this.owner.run(() -> facade.update(pet)); + asOwner.run(() -> facade.update(pet)); } @Test @ShouldMatchDataSet({ "owners.xml", "owners-create-pet.xml" }) public void testUpdateNewPetWithOwner() { final Owner owner = ownerWithoutPets(); - this.principal.setName(owner.getLogin()); + principal.setName(owner.getLogin()); final Pet pet = newPetWithOwner(owner); - this.owner.call(() -> facade.update(pet)); + asOwner.call(() -> facade.update(pet)); } @Test(expected = EJBTransactionRolledbackException.class) @ShouldMatchDataSet("owners.xml") public void testUpdateNull() throws LoginException { - this.owner.run(() -> facade.update(null)); + asOwner.run(() -> facade.update(null)); } @Test(expected = EJBTransactionRolledbackException.class) @@ -196,11 +196,11 @@ public class PetServiceIntegrationTest { final Owner owner = ownerWithoutPets(); final Owner otherOwner = ownerWithPets(); - this.principal.setName(owner.getLogin()); + principal.setName(owner.getLogin()); final Pet pet = otherOwner.getPets().iterator().next(); - this.owner.run(() -> facade.update(pet)); + asOwner.run(() -> facade.update(pet)); } @Test(expected = EJBTransactionRolledbackException.class) @@ -209,11 +209,11 @@ public class PetServiceIntegrationTest { final int id = existentPetId(); final Pet pet = pet(id); - this.principal.setName(pet.getOwner().getLogin()); + principal.setName(pet.getOwner().getLogin()); pet.setOwner(null); - this.owner.run(() -> facade.update(pet)); + asOwner.run(() -> facade.update(pet)); } @Test @@ -221,9 +221,9 @@ public class PetServiceIntegrationTest { public void testRemove() throws LoginException { final int id = existentPetId(); final Pet pet = pet(id); - this.principal.setName(pet.getOwner().getLogin()); + principal.setName(pet.getOwner().getLogin()); - this.owner.run(() -> facade.remove(id)); + asOwner.run(() -> facade.remove(id)); } @Test(expected = EJBTransactionRolledbackException.class) @@ -231,9 +231,9 @@ public class PetServiceIntegrationTest { public void testRemoveBadId() throws LoginException { final int id = nonExistentPetId(); - this.principal.setName(ownerWithoutPets().getLogin()); + principal.setName(ownerWithoutPets().getLogin()); - this.owner.run(() -> facade.remove(id)); + asOwner.run(() -> facade.remove(id)); } @Test(expected = EJBTransactionRolledbackException.class) @@ -243,8 +243,8 @@ public class PetServiceIntegrationTest { final Owner ownerWithPets = ownerWithPets(); final int petId = ownerWithPets.getPets().iterator().next().getId(); - this.principal.setName(ownerWithoutPets.getLogin()); + principal.setName(ownerWithoutPets.getLogin()); - this.owner.run(() -> facade.remove(petId)); + asOwner.run(() -> facade.remove(petId)); } } 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 2b4bf11..8677f35 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 @@ -3,8 +3,10 @@ package es.uvigo.esei.xcs.domain.entities; import static java.util.Arrays.stream; import static java.util.stream.Collectors.toSet; +import java.util.ArrayList; import java.util.Collection; import java.util.Date; +import java.util.List; import java.util.Set; public class OwnersDataset { @@ -13,20 +15,13 @@ public class OwnersDataset { public static final String OWNER_WITH_PETS_LOGIN = "juan"; public static final String OWNER_WITHOUT_PETS_LOGIN = "lorena"; - public static Owner owner(String login) { + public static Owner ownerWithLogin(String login) { return stream(owners()) .filter(owner -> owner.getLogin().equals(login)) .findFirst() .orElseThrow(IllegalArgumentException::new); } - public static Pet pet(int id) { - return stream(pets()) - .filter(pet -> pet.getId() == id) - .findFirst() - .orElseThrow(IllegalArgumentException::new); - } - public static Owner[] owners(String ... logins) { final Set loginSet = stream(logins).collect(toSet()); @@ -52,6 +47,36 @@ public class OwnersDataset { }; } + public static String petNameWithMultipleOwners() { + return "Max"; + } + + public static String petNameWithSingleOwner() { + return "Juandog"; + } + + public static Owner[] ownersOf(String petName) { + final List owners = new ArrayList<>(); + + for (Owner owner : owners()) { + for (Pet pet : owner.getPets()) { + if (pet.getName().equals(petName)) { + owners.add(owner); + break; + } + } + } + + return owners.toArray(new Owner[owners.size()]); + } + + public static Pet pet(int id) { + return stream(pets()) + .filter(pet -> pet.getId() == id) + .findFirst() + .orElseThrow(IllegalArgumentException::new); + } + public static Pet[] pets() { return stream(owners()) .map(Owner::getPets) @@ -60,11 +85,19 @@ public class OwnersDataset { } public static Owner newOwnerWithoutPets() { - return new Owner("jacinto", "jacintopass"); + return new Owner(newOwnerLogin(), newOwnerPassword()); + } + + public static String newOwnerLogin() { + return "jacinto"; + } + + public static String newOwnerPassword() { + return "jacintopass"; } public static Owner newOwnerWithFreshPets() { - return new Owner("jacinto", "jacintopass", + return new Owner(newOwnerLogin(), newOwnerPassword(), new Pet("Jacintocat", AnimalType.CAT, new Date(946684861000L)), new Pet("Jacintodo", AnimalType.DOG, new Date(946684861000L)), new Pet("Jacintobird", AnimalType.BIRD, new Date(946684861000L)) @@ -72,23 +105,47 @@ public class OwnersDataset { } public static Owner newOwnerWithPersistentPets() { - return new Owner("jacinto", "jacintopass", + return new Owner(newOwnerLogin(), newOwnerPassword(), new Pet(7, "Jacintocat", AnimalType.CAT, new Date(946684861000L)), new Pet(8, "Jacintodo", AnimalType.DOG, new Date(946684861000L)), new Pet(9, "Jacintobird", AnimalType.BIRD, new Date(946684861000L)) ); } + public static String anyLogin() { + return existentLogin(); + } + + public static String existentLogin() { + return EXISTENT_LOGIN; + } + + public static String nonExistentLogin() { + return NON_EXISTENT_LOGIN; + } + public static Owner anyOwner() { - return ownerWithPets(); + return ownerWithLogin(anyLogin()); + } + + public static Owner existentOwner() { + return ownerWithLogin(existentLogin()); + } + + public static String newPasswordForExistentOwner() { + return "newpassword"; + } + + public static Owner nonExistentOwner() { + return new Owner(nonExistentLogin(), nonExistentLogin() + "pass"); } public static Owner ownerWithPets() { - return owner(OWNER_WITH_PETS_LOGIN); + return ownerWithLogin(OWNER_WITH_PETS_LOGIN); } public static Owner ownerWithoutPets() { - return owner(OWNER_WITHOUT_PETS_LOGIN); + return ownerWithLogin(OWNER_WITHOUT_PETS_LOGIN); } public static Pet anyPet() { @@ -103,18 +160,6 @@ public class OwnersDataset { return new Pet("Lorenacat", AnimalType.CAT, new Date(946684861000L), owner); } - public static String anyLogin() { - return EXISTENT_LOGIN; - } - - public static String existentLogin() { - return EXISTENT_LOGIN; - } - - public static String nonExistentLogin() { - return NON_EXISTENT_LOGIN; - } - public static String existentPetName() { return "Pepecat"; } @@ -130,9 +175,4 @@ public class OwnersDataset { public static int nonExistentPetId() { return 1000000; } - - public static Owner nonExistentOwner() { - final String login = nonExistentLogin(); - return new Owner(login, login + "pass"); - } } diff --git a/tests/src/main/java/es/uvigo/esei/xcs/service/util/security/RoleCaller.java b/tests/src/main/java/es/uvigo/esei/xcs/service/util/security/RoleCaller.java index 92cd46f..9babf0e 100644 --- a/tests/src/main/java/es/uvigo/esei/xcs/service/util/security/RoleCaller.java +++ b/tests/src/main/java/es/uvigo/esei/xcs/service/util/security/RoleCaller.java @@ -2,6 +2,9 @@ package es.uvigo.esei.xcs.service.util.security; import java.util.function.Supplier; +import javax.ejb.Local; + +@Local public interface RoleCaller { public V call(Supplier supplier); diff --git a/tests/src/main/resources/datasets/owners-update-password.xml b/tests/src/main/resources/datasets/owners-update-password.xml index 4c0f8e1..e3ba3dd 100644 --- a/tests/src/main/resources/datasets/owners-update-password.xml +++ b/tests/src/main/resources/datasets/owners-update-password.xml @@ -2,8 +2,8 @@ - - + + -- 2.18.1