ItEr39S19CUAltaGrupoUsuarios: Implemented a many-to-many relation between User and Profile.

The relation is not navigable in the direction Profile -> User.
A new unit test has been added.
This commit is contained in:
Jacobo Aragunde Pérez 2009-12-18 10:50:34 +01:00 committed by Javier Moran Rua
parent bccd7a2047
commit ada8a56932
3 changed files with 50 additions and 0 deletions

View file

@ -34,6 +34,7 @@ import org.navalplanner.business.users.daos.IUserDAO;
* Entity for modeling a user.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
* @author Jacobo Aragunde Perez <jaragunde@igalia.com>
*/
public class User extends BaseEntity {
@ -46,6 +47,8 @@ public class User extends BaseEntity {
@NotEmpty(message="user roles not specified")
private Set<UserRole> roles = new HashSet<UserRole>();
private Set<Profile> profiles = new HashSet<Profile>();
private String email;
/**
@ -98,6 +101,18 @@ public class User extends BaseEntity {
return email;
}
public Set<Profile> getProfiles() {
return profiles;
}
public void addProfile(Profile profile) {
profiles.add(profile);
}
public void removeProfile(Profile profile) {
profiles.remove(profile);
}
@AssertTrue(message="login name is already being used by another user")
public boolean checkConstraintUniqueLoginName() {

View file

@ -32,6 +32,11 @@
</type>
</element>
</set>
<set name="profiles" table="USER_PROFILES">
<key column="USER_ID"/>
<many-to-many column="PROFILE_ID"
class="org.navalplanner.business.users.entities.Profile"/>
</set>
</class>
<class name="Profile" table="NAVAL_PROFILE">

View file

@ -36,7 +36,9 @@ import org.navalplanner.business.common.IAdHocTransactionService;
import org.navalplanner.business.common.IOnTransaction;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.users.daos.IProfileDAO;
import org.navalplanner.business.users.daos.IUserDAO;
import org.navalplanner.business.users.entities.Profile;
import org.navalplanner.business.users.entities.User;
import org.navalplanner.business.users.entities.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
@ -49,6 +51,7 @@ import org.springframework.transaction.annotation.Transactional;
* Tests for <code>IUserDAO</code>.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
* @author Jacobo Aragunde Perez <jaragunde@igalia.com>
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE,
@ -62,6 +65,9 @@ public class UserDAOTest {
@Autowired
private IUserDAO userDAO;
@Autowired
IProfileDAO profileDAO;
@Test
public void testBasicSave() throws InstanceNotFoundException {
@ -196,7 +202,26 @@ public class UserDAOTest {
transactionService.runOnTransaction(createUsers);
transactionService.runOnTransaction(updateUser1);
}
@Test
public void testListProfiles() throws InstanceNotFoundException{
User user = createUser(getUniqueName());
userDAO.save(user);
Profile profile = createProfile(getUniqueName());
profileDAO.save(profile);
int previous = user.getProfiles().size();
user.addProfile(profile);
userDAO.save(user);
assertEquals(previous + 1, userDAO.find(user.getId()).getProfiles().size());
previous = user.getProfiles().size();
user.removeProfile(profile);
userDAO.save(user);
assertEquals(previous - 1, userDAO.find(user.getId()).getProfiles().size());
}
private String getUniqueName() {
@ -221,4 +246,9 @@ public class UserDAOTest {
UserRole.values()[1];
}
private Profile createProfile(String profileName) {
Set<UserRole> roles = new HashSet<UserRole>();
roles.add(UserRole.ROLE_BASIC_USER);
return Profile.create(profileName, roles);
}
}