1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.security.user.dao;
16
17 import org.apache.log4j.Logger;
18 import org.junit.AfterClass;
19 import org.junit.Before;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.kuali.mobility.security.group.api.Group;
24 import org.kuali.mobility.security.group.api.GroupDao;
25 import org.kuali.mobility.security.user.api.User;
26 import org.kuali.mobility.security.user.api.UserDao;
27 import org.kuali.mobility.security.user.entity.UserImpl;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.beans.factory.annotation.Qualifier;
30 import org.springframework.test.context.ContextConfiguration;
31 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32
33 import javax.persistence.EntityManager;
34 import javax.persistence.PersistenceContext;
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import static org.junit.Assert.assertFalse;
39 import static org.junit.Assert.assertTrue;
40
41
42
43
44 @RunWith(SpringJUnit4ClassRunner.class)
45 @ContextConfiguration(value = "classpath:TestSpringBeans.xml")
46 public class UserDaoImplTest {
47 private static final Logger LOG = Logger.getLogger(UserDaoImplTest.class);
48
49 private static final String[] LOGIN_NAME = {null, "mojojojo", "fuzzy", "bubbles"};
50
51 @PersistenceContext
52 private EntityManager entityManager;
53
54 @Autowired
55 @Qualifier("kmeUserDao")
56 private UserDao userDao;
57
58 @Autowired
59 @Qualifier("kmeGroupDao")
60 private GroupDao groupDao;
61
62 @BeforeClass
63 public static void setUpClass() throws Exception {
64 }
65
66 @AfterClass
67 public static void tearDownClass() throws Exception {
68 }
69
70 @Before
71 public void preTest() {
72 }
73
74 @Test
75 public void loadUserById() {
76 User user = getUserDao().loadUserById(new Long(2));
77 assertTrue("User was not loaded.", user != null);
78 LOG.debug("User ID [" + user.getId() + "]");
79 LOG.debug("User LOGIN_NM [" + user.getLoginName() + "]");
80 LOG.debug("User DISPLAY_NM [" + user.getDisplayName() + "]");
81 LOG.debug("User FIRST_NM [" + user.getFirstName() + "]");
82 LOG.debug("User LAST_NM [" + user.getLastName() + "]");
83 LOG.debug("User EMAIL_ADDR_X [" + user.getEmail() + "]");
84 assertTrue("Login name did not match expected.", LOGIN_NAME[2].equals(user.getLoginName()));
85 assertTrue("User had no attributes, expected 2", null != user.getAttributes() && !user.getAttributes().isEmpty());
86
87 List<Group> groups = user.getGroups();
88 for (Group g : groups) {
89 LOG.debug("Group found: [ID:" + g.getId() + "][Name:" + g.getName() + "].");
90 }
91 assertTrue("Groups were null for user and should not have been.", groups != null && !groups.isEmpty());
92 }
93
94 @Test
95 public void loadUserByLoginName() {
96 User user = getUserDao().loadUserByLoginName("fuzzy");
97 assertFalse("Failed to find any users for specified login name.", user == null);
98 }
99
100 @Test
101 public void testSaveUser() {
102 String fName = "Bubbles";
103 String lName = "Utonium";
104 String displayName = "Bubbles Utonium";
105 String email = "bubbles@powerpuff.com";
106 String url = "http://www.powerpuff.com";
107 String campus = "TOWNSVILLE";
108
109 User user = new UserImpl();
110 user.setLoginName(LOGIN_NAME[3]);
111 user.setFirstName(fName);
112 user.setLastName(lName);
113 user.setDisplayName(displayName);
114 user.setEmail(email);
115 user.setRequestURL(url);
116 user.setViewCampus(campus);
117
118 user.addAttribute("ATTRIBUTE_A", "valueA");
119
120 List<Group> groups = new ArrayList<Group>();
121 user.setGroups(groups);
122
123 Long id = getUserDao().saveUser(user);
124
125 LOG.debug("User saved with id " + id);
126 assertTrue("User failed to save, ID = null", null != id);
127 assertTrue("User display name didn't match.", displayName.equals(user.getDisplayName()));
128 assertTrue("User first name didn't match.", fName.equals(user.getFirstName()));
129 assertTrue("User last name didn't match.", lName.equals(user.getLastName()));
130 assertTrue("User login name didn't match.", LOGIN_NAME[3].equals(user.getLoginName()));
131 assertTrue("User request url didn't match.", url.equals(user.getRequestURL()));
132 assertTrue("User email didn't match.", email.equals(user.getEmail()));
133 assertFalse("User is a member of the test group and shouldn't be.", user.isMember("Rowdy Rough Boys"));
134 assertFalse("User is a public user and shouldn't be.", user.isPublicUser());
135 assertTrue("Attribute A was not found and should have been.", user.getAttribute("ATTRIBUTE_A").size() == 1);
136 assertTrue("Attribute B was found and should not have been.", user.getAttribute("ATTRIBUTE_B").size() == 0);
137 assertTrue("Found more attribute names than expected.", user.getAttributeNames().size() == 1);
138 }
139
140 @Test
141 public void testUpdateUser() {
142 User user = getUserDao().loadUserById(new Long(1));
143 assertTrue("User was not loaded.", user != null);
144 assertTrue("Login name did not match expected.", LOGIN_NAME[1].equals(user.getLoginName()));
145
146 Group group = getGroupDao().getGroup(new Long(2));
147 user.addGroup(group);
148
149 user.addAttribute("ATTRIBUTE_Z", "valueZ");
150
151 Long id = getUserDao().saveUser(user);
152
153 assertTrue("Id changed on update and shouldn't have.", user.getId().compareTo(id) == 0);
154 }
155
156 @Test
157 public void testSaveNullUser() {
158 Long id = getUserDao().saveUser(null);
159 assertTrue("Saved user and shouldn't have.", id == null);
160 }
161
162 @Test
163 public void testRemoveUserAttribute() {
164 Long oldId = new Long(2);
165 User user = getUserDao().loadUserById(oldId);
166 assertTrue("User was not loaded.", user != null);
167 LOG.debug("User has [" + user.getAttributes().size() + "] attributes.");
168 boolean didRemove = user.removeAttribute(null);
169 assertFalse("removeAttribute returned true for null argument.", didRemove);
170 assertTrue("User should have 2 attributes and does not.", user.getAttributes().size() == 2);
171 user.removeAttribute("attributeZ");
172 assertTrue("Removed an attribute that didn't exist.", user.getAttributes().size() == 2);
173 didRemove = user.removeAttribute("ATTRIBUTE_D");
174 assertTrue("removeAttribute returned false for attribute d.", didRemove);
175 assertTrue("Failed to remove attribute d.", user.getAttributes().size() == 1);
176
177 Long newId = getUserDao().saveUser(user);
178 assertTrue("Failed to properly save user after removing attributes.", oldId.compareTo(newId) == 0);
179
180 User user2 = getUserDao().loadUserById(oldId);
181 assertTrue("User loaded from database has wrong number of attributes. Expected 1, found "+user2.getAttributes().size(), user2.getAttributes().size() == 1);
182 }
183
184 public EntityManager getEntityManager() {
185 return entityManager;
186 }
187
188 public void setEntityManager(EntityManager entityManager) {
189 this.entityManager = entityManager;
190 }
191
192 public UserDao getUserDao() {
193 return userDao;
194 }
195
196 public void setUserDao(UserDao userDao) {
197 this.userDao = userDao;
198 }
199
200 public GroupDao getGroupDao() {
201 return groupDao;
202 }
203
204 public void setGroupDao(GroupDao groupDao) {
205 this.groupDao = groupDao;
206 }
207 }