Update retrieveMemberByRole using PredefinedRole instead of roleId #763

Change-Id: Ifda82372d79bfbd0199d9bfc29592d0fd57e5ee8
diff --git a/src/main/java/de/ids_mannheim/korap/dao/UserGroupMemberDao.java b/src/main/java/de/ids_mannheim/korap/dao/UserGroupMemberDao.java
index c399fd6..1bf7fb9 100644
--- a/src/main/java/de/ids_mannheim/korap/dao/UserGroupMemberDao.java
+++ b/src/main/java/de/ids_mannheim/korap/dao/UserGroupMemberDao.java
@@ -17,6 +17,7 @@
 import org.springframework.transaction.annotation.Transactional;
 
 import de.ids_mannheim.korap.constant.GroupMemberStatus;
+import de.ids_mannheim.korap.constant.PredefinedRole;
 import de.ids_mannheim.korap.entity.Role;
 import de.ids_mannheim.korap.entity.Role_;
 import de.ids_mannheim.korap.entity.UserGroupMember;
@@ -104,9 +105,8 @@
     }
 
     @SuppressWarnings("unchecked")
-    public List<UserGroupMember> retrieveMemberByRole (int groupId, int roleId)
-            throws KustvaktException {
-        ParameterChecker.checkIntegerValue(roleId, "roleId");
+    public List<UserGroupMember> retrieveMemberByRole (int groupId,
+            PredefinedRole role) throws KustvaktException {
         ParameterChecker.checkIntegerValue(groupId, "groupId");
 
         CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
@@ -121,7 +121,7 @@
                         groupId),
                 criteriaBuilder.equal(root.get(UserGroupMember_.status),
                         GroupMemberStatus.ACTIVE),
-                criteriaBuilder.equal(memberRole.get(Role_.id), roleId));
+                criteriaBuilder.equal(memberRole.get(Role_.NAME), role));
 
         query.select(root);
         query.where(predicate);
@@ -131,9 +131,9 @@
         }
         catch (NoResultException e) {
             throw new KustvaktException(
-                    StatusCodes.NO_RESULT_FOUND, "No member with role " + roleId
+                    StatusCodes.NO_RESULT_FOUND, "No member with role " + role.name()
                             + " is found in group " + groupId,
-                    String.valueOf(roleId));
+                    role.name());
         }
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/service/UserGroupService.java b/src/main/java/de/ids_mannheim/korap/service/UserGroupService.java
index 22fe070..bfa8b45 100644
--- a/src/main/java/de/ids_mannheim/korap/service/UserGroupService.java
+++ b/src/main/java/de/ids_mannheim/korap/service/UserGroupService.java
@@ -18,6 +18,7 @@
 import de.ids_mannheim.korap.config.FullConfiguration;
 import de.ids_mannheim.korap.constant.GroupMemberStatus;
 import de.ids_mannheim.korap.constant.PredefinedRole;
+import de.ids_mannheim.korap.constant.PrivilegeType;
 import de.ids_mannheim.korap.constant.UserGroupStatus;
 import de.ids_mannheim.korap.dao.AdminDao;
 import de.ids_mannheim.korap.dao.RoleDao;
@@ -115,7 +116,7 @@
     private List<UserGroupMember> retrieveMembers (int groupId, String username)
             throws KustvaktException {
         List<UserGroupMember> groupAdmins = groupMemberDao.retrieveMemberByRole(
-                groupId, PredefinedRole.USER_GROUP_ADMIN.getId());
+                groupId, PredefinedRole.USER_GROUP_ADMIN_DELETE);
 
         List<UserGroupMember> members = null;
         for (UserGroupMember admin : groupAdmins) {
@@ -165,17 +166,23 @@
     public List<UserGroupMember> retrieveQueryAccessAdmins (UserGroup userGroup)
             throws KustvaktException {
         List<UserGroupMember> groupAdmins = groupMemberDao.retrieveMemberByRole(
-                userGroup.getId(), PredefinedRole.VC_ACCESS_ADMIN.getId());
+                userGroup.getId(), PredefinedRole.QUERY_ADMIN_DELETE);
         return groupAdmins;
     }
 
-    private void setMemberRoles () {
+    private void setMemberRoles (UserGroup userGroup) {
         if (memberRoles == null) {
+            
+            Role r1 = new Role(PredefinedRole.USER_GROUP_MEMBER_DELETE,
+                    PrivilegeType.DELETE, userGroup);
+            Role r2 = new Role(PredefinedRole.QUERY_MEMBER_READ,
+                    PrivilegeType.DELETE, userGroup);
+            roleDao.addRole(r1);
+            roleDao.addRole(r2);
+            
             memberRoles = new HashSet<Role>(2);
-            memberRoles.add(roleDao.retrieveRoleById(
-                    PredefinedRole.USER_GROUP_MEMBER.getId()));
-            memberRoles.add(roleDao
-                    .retrieveRoleById(PredefinedRole.VC_ACCESS_MEMBER.getId()));
+            memberRoles.add(r1);
+            memberRoles.add(r2);
         }
     }
 
@@ -407,7 +414,7 @@
 
         List<UserGroupMember> userGroupAdmins = groupMemberDao
                 .retrieveMemberByRole(userGroup.getId(),
-                        PredefinedRole.USER_GROUP_ADMIN.getId());
+                        PredefinedRole.USER_GROUP_ADMIN_DELETE);
 
         for (UserGroupMember admin : userGroupAdmins) {
             if (username.equals(admin.getUserId())) {
@@ -432,7 +439,7 @@
             throws KustvaktException {
 
         ParameterChecker.checkStringValue(username, "userId");
-        ParameterChecker.checkStringValue(groupName, "groupId");
+        ParameterChecker.checkStringValue(groupName, "groupName");
 
         UserGroup userGroup = userGroupDao.retrieveGroupByName(groupName,
                 false);
@@ -471,7 +478,7 @@
 
             if (expiration.isAfter(now)) {
                 member.setStatus(GroupMemberStatus.ACTIVE);
-                setMemberRoles();
+                setMemberRoles(userGroup);
                 member.setRoles(memberRoles);
                 groupMemberDao.updateMember(member);
             }
@@ -564,7 +571,7 @@
     }
 
     public void editMemberRoles (String username, String groupName,
-            String memberUsername, List<Integer> roleIds)
+            String memberUsername, List<PredefinedRole> roleIds)
             throws KustvaktException {
 
         ParameterChecker.checkStringValue(username, "username");
@@ -591,7 +598,7 @@
 
             Set<Role> roles = new HashSet<>();
             for (int i = 0; i < roleIds.size(); i++) {
-                roles.add(roleDao.retrieveRoleById(roleIds.get(i)));
+                roles.add(roleDao.retrieveRoleByName(roleIds.get(i)));
             }
             member.setRoles(roles);
             groupMemberDao.updateMember(member);
@@ -604,7 +611,7 @@
     }
 
     public void addMemberRoles (String username, String groupName,
-            String memberUsername, List<Integer> roleIds)
+            String memberUsername, List<PredefinedRole> roleNames)
             throws KustvaktException {
 
         ParameterChecker.checkStringValue(username, "username");
@@ -630,8 +637,8 @@
             }
 
             Set<Role> roles = member.getRoles();
-            for (int i = 0; i < roleIds.size(); i++) {
-                roles.add(roleDao.retrieveRoleById(roleIds.get(i)));
+            for (PredefinedRole role : roleNames) {
+                roles.add(roleDao.retrieveRoleByName(role));
             }
             member.setRoles(roles);
             groupMemberDao.updateMember(member);
diff --git a/src/test/java/de/ids_mannheim/korap/dao/DaoTestBase.java b/src/test/java/de/ids_mannheim/korap/dao/DaoTestBase.java
index 408f1a4..31cf098 100644
--- a/src/test/java/de/ids_mannheim/korap/dao/DaoTestBase.java
+++ b/src/test/java/de/ids_mannheim/korap/dao/DaoTestBase.java
@@ -4,7 +4,6 @@
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
-import org.junit.jupiter.api.AfterAll;
 import org.springframework.beans.factory.annotation.Autowired;
 
 import de.ids_mannheim.korap.constant.GroupMemberStatus;
diff --git a/src/test/java/de/ids_mannheim/korap/dao/UserGroupMemberDaoTest.java b/src/test/java/de/ids_mannheim/korap/dao/UserGroupMemberDaoTest.java
index e3fdc87..4907c70 100644
--- a/src/test/java/de/ids_mannheim/korap/dao/UserGroupMemberDaoTest.java
+++ b/src/test/java/de/ids_mannheim/korap/dao/UserGroupMemberDaoTest.java
@@ -5,6 +5,8 @@
 import java.util.List;
 import java.util.Set;
 
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -12,41 +14,60 @@
 import org.springframework.test.context.junit.jupiter.SpringExtension;
 
 import de.ids_mannheim.korap.constant.PredefinedRole;
+import de.ids_mannheim.korap.constant.PrivilegeType;
 import de.ids_mannheim.korap.entity.Role;
+import de.ids_mannheim.korap.entity.UserGroup;
 import de.ids_mannheim.korap.entity.UserGroupMember;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 
 @ExtendWith(SpringExtension.class)
 @ContextConfiguration("classpath:test-config.xml")
-public class UserGroupMemberDaoTest {
+public class UserGroupMemberDaoTest extends DaoTestBase {
 
     @Autowired
     private UserGroupMemberDao dao;
 
     @Autowired
     private RoleDao roleDao;
+    
+    private static UserGroup group;
 
+    @BeforeEach
+    public void setUp() throws KustvaktException {
+        group = createDoryGroup();
+    }
+
+    @AfterEach
+    public void tearDown() throws KustvaktException {
+        deleteUserGroup(group.getId(), "dory");
+    }
+    
     @Test
     public void testRetrieveMemberByRole () throws KustvaktException {
         // dory group
-        List<UserGroupMember> vcaAdmins = dao.retrieveMemberByRole(2,
-                PredefinedRole.VC_ACCESS_ADMIN.getId());
+        List<UserGroupMember> vcaAdmins = dao.retrieveMemberByRole(group.getId(),
+                PredefinedRole.QUERY_ADMIN_DELETE);
         // System.out.println(vcaAdmins);
         assertEquals(1, vcaAdmins.size());
         assertEquals(vcaAdmins.get(0).getUserId(), "dory");
     }
 
+    // EM: now it is possible to add duplicate member role !
     @Test
     public void testAddSameMemberRole () throws KustvaktException {
-        UserGroupMember member = dao.retrieveMemberById("dory", 1);
+        int groupId = group.getId();
+        
+        Role newRole = new Role(PredefinedRole.USER_GROUP_ADMIN_DELETE,
+                PrivilegeType.DELETE, group);
+        roleDao.addRole(newRole);
+        
+        UserGroupMember member = dao.retrieveMemberById("dory", groupId);
         Set<Role> roles = member.getRoles();
-        Role adminRole = roleDao
-                .retrieveRoleById(PredefinedRole.USER_GROUP_ADMIN.getId());
-        roles.add(adminRole);
+        roles.add(newRole);
         member.setRoles(roles);
         dao.updateMember(member);
-        member = dao.retrieveMemberById("dory", 1);
+        member = dao.retrieveMemberById("dory", groupId);
         member.getRoles();
-        assertEquals(2, roles.size());
+        assertEquals(7, roles.size());
     }
 }