Add createdDate to UserGroup & fix createGroup method (#763).

Change-Id: Ifd1d9d85552fc5016e688487a0a02d4c82bff5cb
diff --git a/src/main/java/de/ids_mannheim/korap/dao/UserGroupDao.java b/src/main/java/de/ids_mannheim/korap/dao/UserGroupDao.java
index 8a91e9c..e74e777 100644
--- a/src/main/java/de/ids_mannheim/korap/dao/UserGroupDao.java
+++ b/src/main/java/de/ids_mannheim/korap/dao/UserGroupDao.java
@@ -1,5 +1,6 @@
 package de.ids_mannheim.korap.dao;
 
+import java.time.ZonedDateTime;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -21,6 +22,7 @@
 
 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.constant.QueryAccessStatus;
 import de.ids_mannheim.korap.entity.Role;
@@ -52,9 +54,6 @@
     @PersistenceContext
     private EntityManager entityManager;
 
-    @Autowired
-    private RoleDao roleDao;
-
     public int createGroup (String name, String description, String createdBy,
             UserGroupStatus status) throws KustvaktException {
         ParameterChecker.checkStringValue(name, "name");
@@ -66,14 +65,16 @@
         group.setDescription(description);
         group.setStatus(status);
         group.setCreatedBy(createdBy);
+        group.setCreatedDate(ZonedDateTime.now());
         entityManager.persist(group);
-
-        Set<Role> roles = new HashSet<Role>();
-        roles.add(roleDao
-                .retrieveRoleById(PredefinedRole.USER_GROUP_ADMIN.getId()));
-        roles.add(roleDao
-                .retrieveRoleById(PredefinedRole.VC_ACCESS_ADMIN.getId()));
-
+        entityManager.flush();
+        
+        Set<Role> roles = createUserGroupAdminRoles(group);
+        for (Role role : roles) {
+            entityManager.persist(role);
+        }
+        entityManager.flush();
+        
         UserGroupMember owner = new UserGroupMember();
         owner.setUserId(createdBy);
         owner.setCreatedBy(createdBy);
@@ -81,9 +82,27 @@
         owner.setGroup(group);
         owner.setRoles(roles);
         entityManager.persist(owner);
-
+        entityManager.flush();
+        
         return group.getId();
     }
+    
+    private Set<Role> createUserGroupAdminRoles (UserGroup group) {
+        Set<Role> roles = new HashSet<Role>();
+        roles.add(new Role(PredefinedRole.USER_GROUP_ADMIN_DELETE,
+                PrivilegeType.DELETE, group));
+        roles.add(new Role(PredefinedRole.USER_GROUP_ADMIN_READ,
+                PrivilegeType.READ, group));
+        roles.add(new Role(PredefinedRole.USER_GROUP_ADMIN_WRITE,
+                PrivilegeType.WRITE, group));
+        roles.add(new Role(PredefinedRole.QUERY_ADMIN_DELETE,
+                PrivilegeType.DELETE, group));
+        roles.add(new Role(PredefinedRole.QUERY_ADMIN_READ,
+                PrivilegeType.READ, group));
+        roles.add(new Role(PredefinedRole.QUERY_ADMIN_WRITE,
+                PrivilegeType.WRITE, group));
+        return roles;
+    }
 
     public void deleteGroup (int groupId, String deletedBy,
             boolean isSoftDelete) throws KustvaktException {
diff --git a/src/main/java/de/ids_mannheim/korap/entity/UserGroup.java b/src/main/java/de/ids_mannheim/korap/entity/UserGroup.java
index 9a7db7d..4508165 100644
--- a/src/main/java/de/ids_mannheim/korap/entity/UserGroup.java
+++ b/src/main/java/de/ids_mannheim/korap/entity/UserGroup.java
@@ -1,5 +1,6 @@
 package de.ids_mannheim.korap.entity;
 
+import java.time.ZonedDateTime;
 import java.util.List;
 
 import jakarta.persistence.CascadeType;
@@ -46,11 +47,14 @@
     private String createdBy;
     @Column(name = "deleted_by")
     private String deletedBy;
+    @Column(name = "created_date")
+    private ZonedDateTime createdDate;
 
     @Enumerated(EnumType.STRING)
     private UserGroupStatus status;
 
-    @OneToMany(mappedBy = "group", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
+    @OneToMany(mappedBy = "group", fetch = FetchType.LAZY, 
+            cascade = CascadeType.REMOVE)
     private List<UserGroupMember> members;
 
     @OneToMany(mappedBy = "userGroup", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
diff --git a/src/test/java/de/ids_mannheim/korap/dao/DaoTestBase.java b/src/test/java/de/ids_mannheim/korap/dao/DaoTestBase.java
new file mode 100644
index 0000000..408f1a4
--- /dev/null
+++ b/src/test/java/de/ids_mannheim/korap/dao/DaoTestBase.java
@@ -0,0 +1,60 @@
+package de.ids_mannheim.korap.dao;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+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;
+import de.ids_mannheim.korap.constant.UserGroupStatus;
+import de.ids_mannheim.korap.entity.UserGroup;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.exceptions.StatusCodes;
+import de.ids_mannheim.korap.service.UserGroupService;
+
+public class DaoTestBase {
+    
+    @Autowired
+    protected UserGroupDao userGroupDao;
+    @Autowired
+    protected UserGroupService userGroupService;
+
+    protected UserGroup createUserGroup (String groupName, String createdBy)
+            throws KustvaktException {
+        int groupId = userGroupDao.createGroup(groupName, null, createdBy,
+                UserGroupStatus.ACTIVE);
+        // retrieve group
+        UserGroup group = userGroupDao.retrieveGroupById(groupId, true);
+        assertEquals(groupName, group.getName());
+        assertEquals(createdBy, group.getCreatedBy());
+        assertEquals(UserGroupStatus.ACTIVE, group.getStatus());
+        assertNotNull(group.getCreatedDate());
+        return group;
+    }
+    
+    protected UserGroup createDoryGroup () throws KustvaktException {
+        UserGroup group = createUserGroup("dory-group", "dory");
+        userGroupService.addGroupMember("nemo", group, "dory",
+                GroupMemberStatus.ACTIVE);
+        userGroupService.addGroupMember("marlin", group, "dory",
+                GroupMemberStatus.PENDING);
+        userGroupService.addGroupMember("pearl", group, "dory",
+                GroupMemberStatus.DELETED);
+
+        return group;
+    }
+    
+    protected void deleteUserGroup (int groupId, String username)
+            throws KustvaktException {
+        userGroupDao.deleteGroup(groupId, username, false);
+        KustvaktException exception = assertThrows(KustvaktException.class,
+                () -> {
+                    userGroupDao.retrieveGroupById(groupId);
+                });
+        assertEquals(StatusCodes.NO_RESOURCE_FOUND,
+                exception.getStatusCode().intValue());
+
+    }
+}
diff --git a/src/test/java/de/ids_mannheim/korap/dao/UserGroupDaoTest.java b/src/test/java/de/ids_mannheim/korap/dao/UserGroupDaoTest.java
index c698321..15662f9 100644
--- a/src/test/java/de/ids_mannheim/korap/dao/UserGroupDaoTest.java
+++ b/src/test/java/de/ids_mannheim/korap/dao/UserGroupDaoTest.java
@@ -1,11 +1,7 @@
 package de.ids_mannheim.korap.dao;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
 
-import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 
@@ -15,115 +11,91 @@
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
 
-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.QueryAccessStatus;
 import de.ids_mannheim.korap.constant.QueryType;
 import de.ids_mannheim.korap.constant.ResourceType;
-import de.ids_mannheim.korap.constant.UserGroupStatus;
 import de.ids_mannheim.korap.entity.QueryDO;
 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;
-import de.ids_mannheim.korap.exceptions.StatusCodes;
 import de.ids_mannheim.korap.user.User.CorpusAccess;
 
 @ExtendWith(SpringExtension.class)
 @ContextConfiguration("classpath:test-config.xml")
-public class UserGroupDaoTest {
-
-    @Autowired
-    private UserGroupDao userGroupDao;
-
+public class UserGroupDaoTest extends DaoTestBase{
+    
     @Autowired
     private QueryDao virtualCorpusDao;
 
     @Autowired
     private RoleDao roleDao;
 
-    @Autowired
-    private FullConfiguration config;
-
-    @Test
-    public void createDeleteNewUserGroup () throws KustvaktException {
-        String groupName = "test group";
-        String createdBy = "test class";
-        // create group
-        int groupId = userGroupDao.createGroup(groupName, null, createdBy,
-                UserGroupStatus.ACTIVE);
-        // retrieve group
-        UserGroup group = userGroupDao.retrieveGroupById(groupId, true);
-        assertEquals(groupName, group.getName());
-        assertEquals(createdBy, group.getCreatedBy());
-        assertEquals(UserGroupStatus.ACTIVE, group.getStatus());
-        assertNull(group.getDeletedBy());
-        // group member
+    private void testGroupMemberAndRoles (UserGroup group, String createdBy) {
+     // group member
         List<UserGroupMember> members = group.getMembers();
         assertEquals(1, members.size());
         UserGroupMember m = members.get(0);
-        assertEquals(GroupMemberStatus.ACTIVE, m.getStatus());
-        assertEquals(createdBy, m.getCreatedBy());
         assertEquals(createdBy, m.getUserId());
+        
         // member roles
         Set<Role> roles = roleDao.retrieveRoleByGroupMemberId(m.getId());
-        assertEquals(2, roles.size());
-        ArrayList<Role> roleList = new ArrayList<>(2);
-        roleList.addAll(roles);
-        Collections.sort(roleList);
-        assertEquals(PredefinedRole.USER_GROUP_ADMIN.getId(),
-                roleList.get(0).getId());
-        assertEquals(PredefinedRole.VC_ACCESS_ADMIN.getId(),
-                roleList.get(1).getId());
-        // retrieve VC by group
-        List<QueryDO> vc = virtualCorpusDao.retrieveQueryByGroup(groupId);
-        assertEquals(0, vc.size());
-        // soft delete group
-        userGroupDao.deleteGroup(groupId, createdBy,
-                config.isSoftDeleteGroup());
-        group = userGroupDao.retrieveGroupById(groupId);
-        assertEquals(UserGroupStatus.DELETED, group.getStatus());
-        // hard delete
-        userGroupDao.deleteGroup(groupId, createdBy, false);
-        KustvaktException exception = assertThrows(KustvaktException.class,
-                () -> {
-                    userGroupDao.retrieveGroupById(groupId);
-                });
-        assertEquals(StatusCodes.NO_RESOURCE_FOUND,
-                exception.getStatusCode().intValue());
+        assertEquals(6, roles.size());
+    }
+
+    @Test
+    public void createDeleteNewUserGroup () throws KustvaktException {
+        String groupName = "test-group";
+        String createdBy = "test-user";
+        UserGroup group = createUserGroup(groupName, createdBy);
+        
+        testGroupMemberAndRoles(group, createdBy);
+        
+        int groupId = group.getId();
+//        // retrieve VC by group
+//        List<QueryDO> vc = virtualCorpusDao.retrieveQueryByGroup(groupId);
+//        assertEquals(0, vc.size());
+        
+        deleteUserGroup(groupId, createdBy);
     }
 
     @Test
     public void retrieveGroupWithMembers () throws KustvaktException {
+        UserGroup group = createDoryGroup();
         // dory group
-        List<UserGroupMember> members = userGroupDao.retrieveGroupById(2, true)
-                .getMembers();
+        List<UserGroupMember> members = userGroupDao
+                .retrieveGroupById(group.getId(), true).getMembers();
         assertEquals(4, members.size());
+        
         UserGroupMember m = members.get(1);
         Set<Role> roles = m.getRoles();
-        assertEquals(2, roles.size());
-        List<Role> sortedRoles = new ArrayList<>(roles);
-        Collections.sort(sortedRoles);
-        assertEquals(PredefinedRole.USER_GROUP_MEMBER.name(),
-                sortedRoles.get(0).getName());
-        assertEquals(PredefinedRole.VC_ACCESS_MEMBER.name(),
-                sortedRoles.get(1).getName());
+        assertEquals(0, roles.size());
+//        assertEquals(2, roles.size());
+        
+//        List<Role> sortedRoles = new ArrayList<>(roles);
+//        Collections.sort(sortedRoles);
+//        assertEquals(PredefinedRole.USER_GROUP_MEMBER.name(),
+//                sortedRoles.get(0).getName());
+//        assertEquals(PredefinedRole.VC_ACCESS_MEMBER.name(),
+//                sortedRoles.get(1).getName());
+        
+        retrieveGroupByUserId();
+        deleteUserGroup(group.getId(), "dory");
     }
 
-    @Test
-    public void retrieveGroupByUserId () throws KustvaktException {
+    private void retrieveGroupByUserId () throws KustvaktException {
         List<UserGroup> group = userGroupDao.retrieveGroupByUserId("dory");
-        assertEquals(2, group.size());
+        assertEquals(1, group.size());
         group = userGroupDao.retrieveGroupByUserId("pearl");
         assertEquals(0, group.size());
     }
 
     @Test
     public void addVCToGroup () throws KustvaktException {
+        UserGroup group = createDoryGroup();
         // dory group
-        int groupId = 2;
-        UserGroup group = userGroupDao.retrieveGroupById(groupId);
+        int groupId = group.getId();
+       
         String createdBy = "dory";
         String name = "dory new vc";
         int id = virtualCorpusDao.createQuery(name, ResourceType.PROJECT,
@@ -133,13 +105,14 @@
         userGroupDao.addQueryToGroup(virtualCorpus, createdBy,
                 QueryAccessStatus.ACTIVE, group);
         List<QueryDO> vc = virtualCorpusDao.retrieveQueryByGroup(groupId);
-        assertEquals(2, vc.size());
-        assertEquals(name, vc.get(1).getName());
+        assertEquals(1, vc.size());
+        assertEquals(name, vc.get(0).getName());
         // delete vc from group
         userGroupDao.deleteQueryFromGroup(virtualCorpus.getId(), groupId);
         vc = virtualCorpusDao.retrieveQueryByGroup(groupId);
-        assertEquals(1, vc.size());
+        assertEquals(0, vc.size());
         // delete vc
         virtualCorpusDao.deleteQuery(virtualCorpus);
+        deleteUserGroup(group.getId(), "dory");
     }
 }