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;
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));
......
......@@ -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);
......
......@@ -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);
}
}
......@@ -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<Owner> actual = admin.call(() -> facade.list());
final List<Owner> 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<Owner> owners = admin.call(() -> facade.findByPetName(pet));
final List<Owner> 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<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));
}
......@@ -128,7 +133,7 @@ public class OwnerServiceIntegrationTest {
public void testFindByPetNameNoPet() {
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()));
}
......@@ -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<Pet> pets = admin.call(() -> facade.getPets(owner.getLogin()));
final List<Pet> 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<Pet> pets = admin.call(() -> facade.getPets(owner.getLogin()));
final List<Pet> 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));
}
}
......@@ -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()));
}
}
......@@ -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<Pet> pets = this.owner.call(() -> facade.list());
final List<Pet> 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<Pet> pets = this.owner.call(() -> facade.list());
final List<Pet> 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));
}
}
......@@ -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<String> 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<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() {
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");
}
}
......@@ -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> V call(Supplier<V> supplier);
......
......@@ -2,8 +2,8 @@
<dataset>
<User login="jose" password="A3F6F4B40B24E2FD61F08923ED452F34" role="ADMIN"/>
<User login="pepe" password="B43B4D046860B2BD945BCA2597BF9F07" role="OWNER"/>
<User login="juan" password="5E9D11A14AD1C8DD77E98EF9B53FD1BA" role="OWNER"/>
<User login="pepe" password="5E9D11A14AD1C8DD77E98EF9B53FD1BA" role="OWNER"/>
<User login="juan" password="B4FBB95580592697DC71488A1F19277E" role="OWNER"/>
<User login="ana" password="22BEEAE33E9B2657F9610621502CD7A4" 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