View Javadoc
1   package org.itracker.persistence.dao;
2   
3   import org.itracker.AbstractDependencyInjectionTest;
4   import org.itracker.model.PermissionType;
5   import org.itracker.model.User;
6   import org.itracker.model.util.UserUtilities;
7   import org.junit.Test;
8   import org.springframework.dao.DataIntegrityViolationException;
9   
10  import java.util.Date;
11  import java.util.List;
12  import java.util.Map;
13  import java.util.Set;
14  import static org.itracker.Assert.*;
15  
16  public class UserDAOImplIT extends AbstractDependencyInjectionTest {
17  
18      private UserDAO userDAO;
19  
20      @Test
21      public void testCreateUser() {
22  
23          User user = new User("admin_test2", "admin_test2", "admin firstname2", "admin lastname2", "", true);
24          user.setCreateDate(new Date());
25          user.setLastModifiedDate(new Date());
26          userDAO.saveOrUpdate(user);
27  
28          User foundUser = userDAO.findByLogin("admin_test2");
29          assertNotNull(foundUser);
30          assertEquals("admin_test2", foundUser.getPassword());
31          assertEquals("admin firstname2", foundUser.getFirstName());
32          assertEquals("admin lastname2", foundUser.getLastName());
33          assertTrue(foundUser.isSuperUser());
34          // delete for repeated testing in same persistence
35          userDAO.delete(user);
36      }
37  
38      @Test
39      public void testCreateUserWithNotNullPK() {
40  
41          try {
42              User user = new User("admin_test3", "admin_test3", "admin firstname3", "admin lastname3", "", true);
43              user.setId(-1);
44              user.setCreateDate(new Date());
45              userDAO.saveOrUpdate(user);
46          } catch (Exception e) {
47              // Expected behavior
48              assertTrue(e instanceof DataIntegrityViolationException);
49          }
50  
51      }
52  
53      @Test
54      public void testFindUsersForProjectByAllPermissionTypeList() {
55  
56          Integer[] permissionTypes = new Integer[]{
57                  UserUtilities.PERMISSION_PRODUCT_ADMIN,
58                  UserUtilities.PERMISSION_CREATE,
59                  UserUtilities.PERMISSION_EDIT
60          };
61  
62          List<User> users = userDAO.findUsersForProjectByAllPermissionTypeList(2, permissionTypes);
63  
64          assertEquals(2, users.size());
65  
66          assertContainsUser(userDAO.findByPrimaryKey(2), users);
67          assertContainsUser(userDAO.findByPrimaryKey(3), users);
68  
69      }
70  
71      @Test
72      public void testFindAll() {
73          List<User> users = userDAO.findAll();
74          assertNotNull(users);
75          assertEquals(5, users.size());
76      }
77  
78      @Test
79      public void testFindActive() {
80          List<User> users = userDAO.findActive();
81          assertNotNull(users);
82          assertEquals(4, users.size());
83      }
84  
85      @Test
86      public void testFindByStatus() {
87          List<User> users = userDAO.findByStatus(1);
88  
89          assertNotNull(users);
90          // TODO: fails, please fix
91          assertEquals(4, users.size());
92      }
93  
94      @Test
95      public void testFindSuperUsers() {
96          List<User> users = userDAO.findSuperUsers();
97  
98          assertNotNull(users);
99          assertEquals(1, users.size());
100     }
101 
102     @Test
103     public void testFindByRegistrationType() {
104         List<User> users = userDAO.findByRegistrationType(1);
105 
106         assertNotNull(users);
107         assertEquals(5, users.size());
108     }
109 
110     @Test
111     public void testGetUsersMapOfProjectsAndPermissionTypes() {
112         Map<Integer, Set<PermissionType>> map =
113                 userDAO.getUsersMapOfProjectsAndPermissionTypes(userDAO.findByPrimaryKey(2));
114 
115         assertNotNull(map);
116         // all permissions connected with user 2 have only project 2 in relation
117         assertEquals("projects", 1, map.size());
118     }
119 
120     private void assertContainsUser(User user, List<User> users) {
121 
122         if (!users.contains(user)) {
123             fail("User not found in the list.");
124         }
125 
126     }
127 
128     @Override
129     public void onSetUp() throws Exception {
130         super.onSetUp();
131 
132         userDAO = (UserDAO) applicationContext.getBean("userDAO");
133     }
134 
135     protected String[] getDataSetFiles() {
136         return new String[]{
137                 "dataset/userpreferencesbean_dataset.xml",
138                 "dataset/userbean_dataset.xml",
139                 "dataset/projectbean_dataset.xml",
140                 "dataset/permissionbean_dataset.xml"
141         };
142     }
143 
144 
145 }