Commit 2427b7c9 authored by Administrator's avatar Administrator

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.
parent e28f2a5e
...@@ -6,10 +6,11 @@ import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.EXISTENT_LOGIN; ...@@ -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.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_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.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.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.newOwnerWithoutPets; 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.domain.entities.OwnersDataset.owners;
import static es.uvigo.esei.xcs.http.util.HasHttpStatus.hasHttpStatus; import static es.uvigo.esei.xcs.http.util.HasHttpStatus.hasHttpStatus;
import static javax.ws.rs.client.Entity.json; import static javax.ws.rs.client.Entity.json;
...@@ -47,7 +48,6 @@ import org.junit.Test; ...@@ -47,7 +48,6 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import es.uvigo.esei.xcs.domain.entities.Owner; 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.rest.GenericTypes.ListOwnerType;
import es.uvigo.esei.xcs.service.OwnerService; import es.uvigo.esei.xcs.service.OwnerService;
...@@ -87,7 +87,7 @@ public class OwnerResourceRestTest { ...@@ -87,7 +87,7 @@ public class OwnerResourceRestTest {
assertThat(response, hasHttpStatus(OK)); assertThat(response, hasHttpStatus(OK));
final Owner owner = response.readEntity(Owner.class); final Owner owner = response.readEntity(Owner.class);
final Owner expected = owner("pepe"); final Owner expected = existentOwner();
assertThat(owner, is(equalsToOwner(expected))); assertThat(owner, is(equalsToOwner(expected)));
} }
...@@ -217,8 +217,8 @@ public class OwnerResourceRestTest { ...@@ -217,8 +217,8 @@ public class OwnerResourceRestTest {
public void testUpdatePassword( public void testUpdatePassword(
@ArquillianResteasyResource(BASE_PATH) ResteasyWebTarget webTarget @ArquillianResteasyResource(BASE_PATH) ResteasyWebTarget webTarget
) throws Exception { ) throws Exception {
final Owner owner = OwnersDataset.anyOwner(); final Owner owner = existentOwner();
owner.changePassword("newpassword"); owner.changePassword(newPasswordForExistentOwner());
final Response response = webTarget.request().put(json(owner)); final Response response = webTarget.request().put(json(owner));
......
...@@ -181,7 +181,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -181,7 +181,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
@Test @Test
public void testDelete() { public void testDelete() {
final String login = OwnersDataset.anyLogin(); final String login = anyLogin();
facade.remove(login); facade.remove(login);
......
...@@ -36,7 +36,7 @@ public class OwnerServiceIllegalAccessIntegrationTest { ...@@ -36,7 +36,7 @@ public class OwnerServiceIllegalAccessIntegrationTest {
private OwnerService facade; private OwnerService facade;
@EJB(beanName = "owner-caller") @EJB(beanName = "owner-caller")
private RoleCaller owner; private RoleCaller asOwner;
@Deployment @Deployment
public static Archive<?> createDeployment() { public static Archive<?> createDeployment() {
...@@ -88,36 +88,36 @@ public class OwnerServiceIllegalAccessIntegrationTest { ...@@ -88,36 +88,36 @@ public class OwnerServiceIllegalAccessIntegrationTest {
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
public void testGetRoleOwner() { public void testGetRoleOwner() {
this.owner.run(this::testGetNoRole); asOwner.run(this::testGetNoRole);
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
public void testListRoleOwner() { public void testListRoleOwner() {
this.owner.run(this::testListNoRole); asOwner.run(this::testListNoRole);
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
public void testFindByPetNameRoleOwner() { public void testFindByPetNameRoleOwner() {
this.owner.run(this::testFindByPetNameNoRole); asOwner.run(this::testFindByPetNameNoRole);
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
public void testCreateRoleOwner() { public void testCreateRoleOwner() {
this.owner.run(this::testCreateNoRole); asOwner.run(this::testCreateNoRole);
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
public void testUpdateRoleOwner() { public void testUpdateRoleOwner() {
this.owner.run(this::testUpdateNoRole); asOwner.run(this::testUpdateNoRole);
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
public void testRemoveRoleOwner() { public void testRemoveRoleOwner() {
this.owner.run(this::testRemoveNoRole); asOwner.run(this::testRemoveNoRole);
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
public void testGetPetsRoleOwner() { public void testGetPetsRoleOwner() {
this.owner.run(this::testGetPetsNoRole); asOwner.run(this::testGetPetsNoRole);
} }
} }
...@@ -3,18 +3,24 @@ package es.uvigo.esei.xcs.service; ...@@ -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.containsOwnersInAnyOrder;
import static es.uvigo.esei.xcs.domain.entities.IsEqualsToOwner.equalsToOwner; 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.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.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.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.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.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.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;
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.is;
import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.nullValue;
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;
...@@ -49,7 +55,7 @@ public class OwnerServiceIntegrationTest { ...@@ -49,7 +55,7 @@ public class OwnerServiceIntegrationTest {
private OwnerService facade; private OwnerService facade;
@EJB(beanName = "admin-caller") @EJB(beanName = "admin-caller")
private RoleCaller admin; private RoleCaller asAdmin;
@Deployment @Deployment
public static Archive<?> createDeployment() { public static Archive<?> createDeployment() {
...@@ -67,12 +73,11 @@ public class OwnerServiceIntegrationTest { ...@@ -67,12 +73,11 @@ public class OwnerServiceIntegrationTest {
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testGetOwner() { public void testGetOwner() {
final String login = "pepe"; final String login = existentLogin();
final Owner pepe = owner(login);
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 @Test
...@@ -80,7 +85,7 @@ public class OwnerServiceIntegrationTest { ...@@ -80,7 +85,7 @@ public class OwnerServiceIntegrationTest {
public void testGetOwnerNonExistent() { public void testGetOwnerNonExistent() {
final String login = nonExistentLogin(); final String login = nonExistentLogin();
final Owner actual = admin.call(() -> facade.get(login)); final Owner actual = asAdmin.call(() -> facade.get(login));
assertThat(actual, is(nullValue())); assertThat(actual, is(nullValue()));
} }
...@@ -88,13 +93,13 @@ public class OwnerServiceIntegrationTest { ...@@ -88,13 +93,13 @@ public class OwnerServiceIntegrationTest {
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testGetOwnerNull() { public void testGetOwnerNull() {
admin.call(() -> facade.get(null)); asAdmin.call(() -> facade.get(null));
} }
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testList() { public void testList() {
final List<Owner> actual = admin.call(() -> facade.list()); final List<Owner> actual = asAdmin.call(() -> facade.list());
assertThat(actual, is(containsOwnersInAnyOrder(owners()))); assertThat(actual, is(containsOwnersInAnyOrder(owners())));
} }
...@@ -102,23 +107,23 @@ public class OwnerServiceIntegrationTest { ...@@ -102,23 +107,23 @@ public class OwnerServiceIntegrationTest {
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testFindByPetName() { public void testFindByPetName() {
final String pet = "Juandog"; final String petName = petNameWithSingleOwner();
final Owner owner = ownersOf(petName)[0];
final List<Owner> owners = admin.call(() -> facade.findByPetName(pet)); final List<Owner> owners = asAdmin.call(() -> facade.findByPetName(petName));
final Owner owner = owners.get(0); assertThat(owners, hasSize(1));
final Owner juan = owner("juan"); assertThat(owners.get(0), is(equalsToOwner(owner)));
assertThat(owner, is(equalsToOwner(juan)));
} }
@Test @Test
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testFindByPetNameMultipleOwners() { public void testFindByPetNameMultipleOwners() {
final String pet = "Max"; final String petName = petNameWithMultipleOwners();
final List<Owner> owners = admin.call(() -> facade.findByPetName(pet)); final List<Owner> owners = asAdmin.call(() -> facade.findByPetName(petName));
final Owner[] expectedOwners = owners("juan", "ana"); final Owner[] expectedOwners = ownersOf(petName);
assertThat(owners, containsOwnersInAnyOrder(expectedOwners)); assertThat(owners, containsOwnersInAnyOrder(expectedOwners));
} }
...@@ -128,7 +133,7 @@ public class OwnerServiceIntegrationTest { ...@@ -128,7 +133,7 @@ public class OwnerServiceIntegrationTest {
public void testFindByPetNameNoPet() { public void testFindByPetNameNoPet() {
final String pet = nonExistentPetName(); final String pet = nonExistentPetName();
final List<Owner> owners = admin.call(() -> facade.findByPetName(pet)); final List<Owner> owners = asAdmin.call(() -> facade.findByPetName(pet));
assertThat(owners, is(empty())); assertThat(owners, is(empty()));
} }
...@@ -136,7 +141,7 @@ public class OwnerServiceIntegrationTest { ...@@ -136,7 +141,7 @@ public class OwnerServiceIntegrationTest {
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testFindByPetNameNull() { public void testFindByPetNameNull() {
admin.run(() -> facade.findByPetName(null)); asAdmin.run(() -> facade.findByPetName(null));
} }
@Test @Test
...@@ -144,7 +149,7 @@ public class OwnerServiceIntegrationTest { ...@@ -144,7 +149,7 @@ public class OwnerServiceIntegrationTest {
public void testCreateWithoutPets() { public void testCreateWithoutPets() {
final Owner newOwner = newOwnerWithoutPets(); 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))); assertThat(actual, is(equalsToOwner(newOwner)));
} }
...@@ -152,7 +157,7 @@ public class OwnerServiceIntegrationTest { ...@@ -152,7 +157,7 @@ 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 = admin.call(() -> facade.create(newOwnerWithFreshPets())); final Owner actual = asAdmin.call(() -> facade.create(newOwnerWithFreshPets()));
assertThat(actual, is(equalsToOwner(newOwnerWithPersistentPets()))); assertThat(actual, is(equalsToOwner(newOwnerWithPersistentPets())));
} }
...@@ -160,22 +165,22 @@ public class OwnerServiceIntegrationTest { ...@@ -160,22 +165,22 @@ public class OwnerServiceIntegrationTest {
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testCreateExistentLogin() { public void testCreateExistentLogin() {
admin.run(() -> facade.create(anyOwner())); asAdmin.run(() -> facade.create(existentOwner()));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testCreateNull() { public void testCreateNull() {
admin.call(() -> facade.create(null)); asAdmin.call(() -> facade.create(null));
} }
@Test @Test
@ShouldMatchDataSet("owners-update-password.xml") @ShouldMatchDataSet("owners-update-password.xml")
public void testUpdatePassword() { public void testUpdatePassword() {
final Owner owner = anyOwner(); final Owner owner = existentOwner();
owner.changePassword("newpassword"); owner.changePassword(newPasswordForExistentOwner());
admin.run(() -> facade.update(owner)); asAdmin.run(() -> facade.update(owner));
} }
@Test @Test
...@@ -183,7 +188,7 @@ public class OwnerServiceIntegrationTest { ...@@ -183,7 +188,7 @@ public class OwnerServiceIntegrationTest {
public void testUpdateNewOwnerWithoutPets() { public void testUpdateNewOwnerWithoutPets() {
final Owner newOwner = newOwnerWithoutPets(); 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))); assertThat(actual, is(equalsToOwner(newOwner)));
} }
...@@ -191,7 +196,7 @@ public class OwnerServiceIntegrationTest { ...@@ -191,7 +196,7 @@ public class OwnerServiceIntegrationTest {
@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 = admin.call(() -> facade.update(newOwnerWithFreshPets())); final Owner actual = asAdmin.call(() -> facade.update(newOwnerWithFreshPets()));
assertThat(actual, is(equalsToOwner(newOwnerWithPersistentPets()))); assertThat(actual, is(equalsToOwner(newOwnerWithPersistentPets())));
} }
...@@ -199,25 +204,25 @@ public class OwnerServiceIntegrationTest { ...@@ -199,25 +204,25 @@ public class OwnerServiceIntegrationTest {
@Test @Test
@ShouldMatchDataSet("owners-remove-without-pets.xml") @ShouldMatchDataSet("owners-remove-without-pets.xml")
public void testRemoveWithoutPets() { public void testRemoveWithoutPets() {
admin.run(() -> facade.remove(ownerWithoutPets().getLogin())); asAdmin.run(() -> facade.remove(ownerWithoutPets().getLogin()));
} }
@Test @Test
@ShouldMatchDataSet("owners-remove-with-pets.xml") @ShouldMatchDataSet("owners-remove-with-pets.xml")
public void testRemoveWithPets() { public void testRemoveWithPets() {
admin.run(() -> facade.remove(ownerWithPets().getLogin())); asAdmin.run(() -> facade.remove(ownerWithPets().getLogin()));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testRemoveNonExistentOwner() { public void testRemoveNonExistentOwner() {
admin.run(() -> facade.remove(nonExistentLogin())); asAdmin.run(() -> facade.remove(nonExistentLogin()));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testRemoveNull() { public void testRemoveNull() {
admin.run(() -> facade.remove(null)); asAdmin.run(() -> facade.remove(null));
} }
@Test @Test
...@@ -226,7 +231,7 @@ public class OwnerServiceIntegrationTest { ...@@ -226,7 +231,7 @@ public class OwnerServiceIntegrationTest {
final Owner owner = ownerWithPets(); final Owner owner = ownerWithPets();
final Pet[] ownedPets = owner.getPets().toArray(new Pet[0]); final Pet[] ownedPets = owner.getPets().toArray(new Pet[0]);
final List<Pet> pets = admin.call(() -> facade.getPets(owner.getLogin())); final List<Pet> pets = asAdmin.call(() -> facade.getPets(owner.getLogin()));
assertThat(pets, containsPetsInAnyOrder(ownedPets)); assertThat(pets, containsPetsInAnyOrder(ownedPets));
} }
...@@ -236,7 +241,7 @@ public class OwnerServiceIntegrationTest { ...@@ -236,7 +241,7 @@ public class OwnerServiceIntegrationTest {
public void testGetPetsNoPets() { public void testGetPetsNoPets() {
final Owner owner = ownerWithoutPets(); final Owner owner = ownerWithoutPets();
final List<Pet> pets = admin.call(() -> facade.getPets(owner.getLogin())); final List<Pet> pets = asAdmin.call(() -> facade.getPets(owner.getLogin()));
assertThat(pets, is(empty())); assertThat(pets, is(empty()));
} }
...@@ -244,12 +249,12 @@ public class OwnerServiceIntegrationTest { ...@@ -244,12 +249,12 @@ public class OwnerServiceIntegrationTest {
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testGetPetsNonExistentOwner() { public void testGetPetsNonExistentOwner() {
admin.call(() -> facade.getPets(nonExistentLogin())); asAdmin.call(() -> facade.getPets(nonExistentLogin()));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testGetPetsNull() { public void testGetPetsNull() {
admin.call(() -> facade.getPets(null)); asAdmin.call(() -> facade.getPets(null));
} }
} }
...@@ -41,10 +41,10 @@ public class PetServiceIllegalAccessIntegrationTest { ...@@ -41,10 +41,10 @@ public class PetServiceIllegalAccessIntegrationTest {
private TestPrincipal principal; private TestPrincipal principal;
@EJB(beanName = "admin-caller") @EJB(beanName = "admin-caller")
private RoleCaller admin; private RoleCaller asAdmin;
@EJB(beanName = "owner-caller") @EJB(beanName = "owner-caller")
private RoleCaller owner; private RoleCaller asOwner;
@Deployment @Deployment
public static Archive<?> createDeployment() { public static Archive<?> createDeployment() {
...@@ -86,27 +86,27 @@ public class PetServiceIllegalAccessIntegrationTest { ...@@ -86,27 +86,27 @@ public class PetServiceIllegalAccessIntegrationTest {
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
public void testGetRoleAdmin() { public void testGetRoleAdmin() {
this.admin.run(this::testGetNoRole); asAdmin.run(this::testGetNoRole);
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
public void testListRoleAdmin() { public void testListRoleAdmin() {
this.admin.run(this::testListNoRole); asAdmin.run(this::testListNoRole);
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
public void testCreateRoleAdmin() { public void testCreateRoleAdmin() {
this.admin.run(this::testCreateNoRole); asAdmin.run(this::testCreateNoRole);
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
public void testUpdateRoleAdmin() { public void testUpdateRoleAdmin() {
this.admin.run(this::testUpdateNoRole); asAdmin.run(this::testUpdateNoRole);
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
public void testRemoveRoleAdmin() { public void testRemoveRoleAdmin() {
this.admin.run(this::testRemoveNoRole); asAdmin.run(this::testRemoveNoRole);
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
...@@ -118,7 +118,7 @@ public class PetServiceIllegalAccessIntegrationTest { ...@@ -118,7 +118,7 @@ public class PetServiceIllegalAccessIntegrationTest {
principal.setName(owner2.getLogin()); principal.setName(owner2.getLogin());
this.owner.run(() -> this.facade.get(pet1.getId())); asOwner.run(() -> facade.get(pet1.getId()));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
...@@ -130,7 +130,7 @@ public class PetServiceIllegalAccessIntegrationTest { ...@@ -130,7 +130,7 @@ public class PetServiceIllegalAccessIntegrationTest {
principal.setName(owner2.getLogin()); principal.setName(owner2.getLogin());
this.owner.run(() -> this.facade.create(pet)); asOwner.run(() -> facade.create(pet));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
...@@ -143,7 +143,7 @@ public class PetServiceIllegalAccessIntegrationTest { ...@@ -143,7 +143,7 @@ public class PetServiceIllegalAccessIntegrationTest {
principal.setName(owner2.getLogin()); principal.setName(owner2.getLogin());
this.owner.run(() -> this.facade.update(pet1)); asOwner.run(() -> facade.update(pet1));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
...@@ -155,6 +155,6 @@ public class PetServiceIllegalAccessIntegrationTest { ...@@ -155,6 +155,6 @@ public class PetServiceIllegalAccessIntegrationTest {
principal.setName(owner2.getLogin()); principal.setName(owner2.getLogin());
this.owner.run(() -> this.facade.remove(pet1.getId())); asOwner.run(() -> facade.remove(pet1.getId()));
} }
} }
...@@ -48,7 +48,7 @@ public class PetServiceIntegrationTest { ...@@ -48,7 +48,7 @@ public class PetServiceIntegrationTest {
private PetService facade; private PetService facade;
@EJB(beanName = "owner-caller") @EJB(beanName = "owner-caller")
private RoleCaller owner; private RoleCaller asOwner;
@Inject @Inject
private TestPrincipal principal; private TestPrincipal principal;
...@@ -71,9 +71,9 @@ public class PetServiceIntegrationTest { ...@@ -71,9 +71,9 @@ public class PetServiceIntegrationTest {
public void testGet() throws LoginException { public void testGet() throws LoginException {
final int id = existentPetId(); final int id = existentPetId();
final Pet pet = pet(id); 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)); assertThat(actual, equalsToPet(pet));
} }
...@@ -83,9 +83,9 @@ public class PetServiceIntegrationTest { ...@@ -83,9 +83,9 @@ public class PetServiceIntegrationTest {
public void testGetBadId() throws LoginException { public void testGetBadId() throws LoginException {
final int id = nonExistentPetId(); 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())); assertThat(actual, is(nullValue()));
} }
...@@ -97,9 +97,9 @@ public class PetServiceIntegrationTest { ...@@ -97,9 +97,9 @@ public class PetServiceIntegrationTest {
final Owner ownerWithPets = ownerWithPets(); final Owner ownerWithPets = ownerWithPets();
final int petId = ownerWithPets.getPets().iterator().next().getId(); 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 @Test
...@@ -107,9 +107,9 @@ public class PetServiceIntegrationTest { ...@@ -107,9 +107,9 @@ public class PetServiceIntegrationTest {
public void testList() throws LoginException { public void testList() throws LoginException {
final Owner owner = ownerWithPets(); final Owner owner = ownerWithPets();
final Pet[] ownedPets = owner.getPets().toArray(new Pet[0]); final Pet[] ownedPets = owner.getPets().toArray(new Pet[0]);
this.principal.setName(owner.getLogin()); principal.setName(owner.getLogin());
final List<Pet> pets = this.owner.call(() -> facade.list()); final List<Pet> pets = asOwner.call(() -> facade.list());
assertThat(pets, containsPetsInAnyOrder(ownedPets)); assertThat(pets, containsPetsInAnyOrder(ownedPets));
} }
...@@ -119,9 +119,9 @@ public class PetServiceIntegrationTest { ...@@ -119,9 +119,9 @@ public class PetServiceIntegrationTest {
public void testListNoPets() throws LoginException { public void testListNoPets() throws LoginException {
final Owner owner = ownerWithoutPets(); final Owner owner = ownerWithoutPets();
this.principal.setName(owner.getLogin()); principal.setName(owner.getLogin());
final List<Pet> pets = this.owner.call(() -> facade.list()); final List<Pet> pets = asOwner.call(() -> facade.list());
assertThat(pets, is(empty())); assertThat(pets, is(empty()));
} }
...@@ -130,19 +130,19 @@ public class PetServiceIntegrationTest { ...@@ -130,19 +130,19 @@ public class PetServiceIntegrationTest {
@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 owner = ownerWithoutPets();
this.principal.setName(owner.getLogin()); principal.setName(owner.getLogin());
final Pet pet = newPet(); final Pet pet = newPet();
this.owner.call(() -> facade.create(pet)); asOwner.call(() -> facade.create(pet));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet({ "owners.xml" }) @ShouldMatchDataSet({ "owners.xml" })
public void testCreateNull() { 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) @Test(expected = EJBTransactionRolledbackException.class)
...@@ -151,11 +151,11 @@ public class PetServiceIntegrationTest { ...@@ -151,11 +151,11 @@ public class PetServiceIntegrationTest {
final Owner owner = ownerWithoutPets(); final Owner owner = ownerWithoutPets();
final Owner otherOwner = ownerWithPets(); final Owner otherOwner = ownerWithPets();
this.principal.setName(owner.getLogin()); principal.setName(owner.getLogin());
final Pet pet = newPetWithOwner(otherOwner); final Pet pet = newPetWithOwner(otherOwner);
this.owner.run(() -> facade.create(pet)); asOwner.run(() -> facade.create(pet));
} }
@Test @Test
...@@ -164,30 +164,30 @@ public class PetServiceIntegrationTest { ...@@ -164,30 +164,30 @@ public class PetServiceIntegrationTest {
final int id = existentPetId(); final int id = existentPetId();
final Pet pet = pet(id); final Pet pet = pet(id);
this.principal.setName(pet.getOwner().getLogin()); principal.setName(pet.getOwner().getLogin());
pet.setName("UpdateName"); pet.setName("UpdateName");
pet.setAnimal(AnimalType.BIRD); pet.setAnimal(AnimalType.BIRD);
pet.setBirth(new Date(946771261000L)); pet.setBirth(new Date(946771261000L));
this.owner.run(() -> facade.update(pet)); asOwner.run(() -> facade.update(pet));
} }
@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 owner = ownerWithoutPets();
this.principal.setName(owner.getLogin()); principal.setName(owner.getLogin());
final Pet pet = newPetWithOwner(owner); final Pet pet = newPetWithOwner(owner);
this.owner.call(() -> facade.update(pet)); asOwner.call(() -> facade.update(pet));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
@ShouldMatchDataSet("owners.xml") @ShouldMatchDataSet("owners.xml")
public void testUpdateNull() throws LoginException { public void testUpdateNull() throws LoginException {
this.owner.run(() -> facade.update(null)); asOwner.run(() -> facade.update(null));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
...@@ -196,11 +196,11 @@ public class PetServiceIntegrationTest { ...@@ -196,11 +196,11 @@ public class PetServiceIntegrationTest {
final Owner owner = ownerWithoutPets(); final Owner owner = ownerWithoutPets();
final Owner otherOwner = ownerWithPets(); final Owner otherOwner = ownerWithPets();
this.principal.setName(owner.getLogin()); principal.setName(owner.getLogin());
final Pet pet = otherOwner.getPets().iterator().next(); final Pet pet = otherOwner.getPets().iterator().next();
this.owner.run(() -> facade.update(pet)); asOwner.run(() -> facade.update(pet));
} }
@Test(expected = EJBTransactionRolledbackException.class) @Test(expected = EJBTransactionRolledbackException.class)
...@@ -209,11 +209,11 @@ public class PetServiceIntegrationTest { ...@@ -209,11 +209,11 @@ public class PetServiceIntegrationTest {
final int id = existentPetId(); final int id = existentPetId();
final Pet pet = pet(id); final Pet pet = pet(id);
this.principal.setName(pet.getOwner().getLogin()); principal.setName(pet.getOwner().getLogin());
pet.setOwner(null); pet.setOwner(null);
this.owner.run(() -> facade.update(pet)); asOwner.run(() -> facade.update(pet));
} }
@Test @Test
...@@ -221,9 +221,9 @@ public class PetServiceIntegrationTest { ...@@ -221,9 +221,9 @@ public class PetServiceIntegrationTest {
public void testRemove() throws LoginException { public void testRemove() throws LoginException {
final int id = existentPetId(); final int id = existentPetId();
final Pet pet = pet(id); 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) @Test(expected = EJBTransactionRolledbackException.class)
...@@ -231,9 +231,9 @@ public class PetServiceIntegrationTest { ...@@ -231,9 +231,9 @@ public class PetServiceIntegrationTest {
public void testRemoveBadId() throws LoginException { public void testRemoveBadId() throws LoginException {
final int id = nonExistentPetId(); 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) @Test(expected = EJBTransactionRolledbackException.class)
...@@ -243,8 +243,8 @@ public class PetServiceIntegrationTest { ...@@ -243,8 +243,8 @@ public class PetServiceIntegrationTest {
final Owner ownerWithPets = ownerWithPets(); final Owner ownerWithPets = ownerWithPets();
final int petId = ownerWithPets.getPets().iterator().next().getId(); 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));
} }
} }
...@@ -3,8 +3,10 @@ package es.uvigo.esei.xcs.domain.entities; ...@@ -3,8 +3,10 @@ package es.uvigo.esei.xcs.domain.entities;
import static java.util.Arrays.stream; import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toSet; import static java.util.stream.Collectors.toSet;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Set; import java.util.Set;
public class OwnersDataset { public class OwnersDataset {
...@@ -13,20 +15,13 @@ public class OwnersDataset { ...@@ -13,20 +15,13 @@ public class OwnersDataset {
public static final String OWNER_WITH_PETS_LOGIN = "juan"; public static final String OWNER_WITH_PETS_LOGIN = "juan";
public static final String OWNER_WITHOUT_PETS_LOGIN = "lorena"; public static final String OWNER_WITHOUT_PETS_LOGIN = "lorena";
public static Owner owner(String login) { public static Owner ownerWithLogin(String login) {
return stream(owners()) return stream(owners())
.filter(owner -> owner.getLogin().equals(login)) .filter(owner -> owner.getLogin().equals(login))
.findFirst() .findFirst()
.orElseThrow(IllegalArgumentException::new); .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) { public static Owner[] owners(String ... logins) {
final Set<String> loginSet = stream(logins).collect(toSet()); final Set<String> loginSet = stream(logins).collect(toSet());
...@@ -52,6 +47,36 @@ public class OwnersDataset { ...@@ -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<Owner> 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() { public static Pet[] pets() {
return stream(owners()) return stream(owners())
.map(Owner::getPets) .map(Owner::getPets)
...@@ -60,11 +85,19 @@ public class OwnersDataset { ...@@ -60,11 +85,19 @@ public class OwnersDataset {
} }
public static Owner newOwnerWithoutPets() { 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() { public static Owner newOwnerWithFreshPets() {
return new Owner("jacinto", "jacintopass", return new Owner(newOwnerLogin(), newOwnerPassword(),
new Pet("Jacintocat", AnimalType.CAT, new Date(946684861000L)), new Pet("Jacintocat", AnimalType.CAT, new Date(946684861000L)),
new Pet("Jacintodo", AnimalType.DOG, new Date(946684861000L)), new Pet("Jacintodo", AnimalType.DOG, new Date(946684861000L)),
new Pet("Jacintobird", AnimalType.BIRD, new Date(946684861000L)) new Pet("Jacintobird", AnimalType.BIRD, new Date(946684861000L))
...@@ -72,23 +105,47 @@ public class OwnersDataset { ...@@ -72,23 +105,47 @@ public class OwnersDataset {
} }
public static Owner newOwnerWithPersistentPets() { 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(7, "Jacintocat", AnimalType.CAT, new Date(946684861000L)),
new Pet(8, "Jacintodo", AnimalType.DOG, new Date(946684861000L)), new Pet(8, "Jacintodo", AnimalType.DOG, new Date(946684861000L)),
new Pet(9, "Jacintobird", AnimalType.BIRD, 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() { 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() { public static Owner ownerWithPets() {
return owner(OWNER_WITH_PETS_LOGIN); return ownerWithLogin(OWNER_WITH_PETS_LOGIN);
} }
public static Owner ownerWithoutPets() { public static Owner ownerWithoutPets() {
return owner(OWNER_WITHOUT_PETS_LOGIN); return ownerWithLogin(OWNER_WITHOUT_PETS_LOGIN);
} }
public static Pet anyPet() { public static Pet anyPet() {
...@@ -103,18 +160,6 @@ public class OwnersDataset { ...@@ -103,18 +160,6 @@ public class OwnersDataset {
return new Pet("Lorenacat", AnimalType.CAT, new Date(946684861000L), owner); 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() { public static String existentPetName() {
return "Pepecat"; return "Pepecat";
} }
...@@ -130,9 +175,4 @@ public class OwnersDataset { ...@@ -130,9 +175,4 @@ public class OwnersDataset {
public static int nonExistentPetId() { public static int nonExistentPetId() {
return 1000000; return 1000000;
} }
public static Owner nonExistentOwner() {
final String login = nonExistentLogin();
return new Owner(login, login + "pass");
}
} }
...@@ -2,6 +2,9 @@ package es.uvigo.esei.xcs.service.util.security; ...@@ -2,6 +2,9 @@ package es.uvigo.esei.xcs.service.util.security;
import java.util.function.Supplier; import java.util.function.Supplier;
import javax.ejb.Local;
@Local
public interface RoleCaller { public interface RoleCaller {
public <V> V call(Supplier<V> supplier); public <V> V call(Supplier<V> supplier);
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
<dataset> <dataset>
<User login="jose" password="A3F6F4B40B24E2FD61F08923ED452F34" role="ADMIN"/> <User login="jose" password="A3F6F4B40B24E2FD61F08923ED452F34" role="ADMIN"/>
<User login="pepe" password="B43B4D046860B2BD945BCA2597BF9F07" role="OWNER"/> <User login="pepe" password="5E9D11A14AD1C8DD77E98EF9B53FD1BA" role="OWNER"/>
<User login="juan" password="5E9D11A14AD1C8DD77E98EF9B53FD1BA" role="OWNER"/> <User login="juan" password="B4FBB95580592697DC71488A1F19277E" role="OWNER"/>
<User login="ana" password="22BEEAE33E9B2657F9610621502CD7A4" role="OWNER"/> <User login="ana" password="22BEEAE33E9B2657F9610621502CD7A4" role="OWNER"/>
<User login="lorena" password="05009E420932C21E5A68F5EF1AADD530" role="OWNER"/> <User login="lorena" password="05009E420932C21E5A68F5EF1AADD530" role="OWNER"/>
......
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