offline building of site deploy to improve performance.
[utils] / security / usermgt / src / test / java / org / wamblee / security / authentication / InMemoryUserSetTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.security.authentication;
17
18 import junit.framework.TestCase;
19
20 import org.wamblee.security.authentication.Group;
21 import org.wamblee.security.authentication.GroupSet;
22 import org.wamblee.security.authentication.InMemoryGroupSet;
23 import org.wamblee.security.authentication.InMemoryUserSet;
24 import org.wamblee.security.authentication.RegexpNameValidator;
25 import org.wamblee.security.authentication.User;
26 import org.wamblee.security.authentication.UserMgtException;
27 import org.wamblee.security.authentication.UserSet;
28 import org.wamblee.security.authentication.UserMgtException.Reason;
29
30
31 import java.sql.SQLException;
32
33 import java.util.Set;
34
35 /**
36  * Tests the inmemory user set. Intended to be subclassed for other
37  * implementations of user set.
38  */
39 public class InMemoryUserSetTest extends TestCase {
40     protected static final String PASSWORD = "abc123";
41
42     private UserSet users;
43
44     private GroupSet groups;
45
46     private Group group;
47
48     /**
49      * This method must be overriden in subclasses.
50      * 
51      * @return New user set object.
52      */
53     protected UserSet createUserSet() {
54         return new InMemoryUserSet(new RegexpNameValidator(
55             RegexpNameValidator.PASSWORD_PATTERN, Reason.INVALID_PASSWORD,
56             "Password must contain at least 6 characters"),
57             new Md5HexMessageDigester());
58     }
59
60     /**
61      * This method must be overriden in subclasses.
62      * 
63      * @return New group set object.
64      */
65     protected GroupSet createGroupSet() {
66         return new InMemoryGroupSet();
67     }
68
69     /*
70      * (non-Javadoc)
71      * 
72      * @see org.wamblee.test.SpringTestCase#setUp()
73      */
74     @Override
75     protected void setUp() throws Exception {
76         super.setUp();
77         users = createUserSet();
78         groups = createGroupSet();
79         group = new Group("group0");
80         groups.add(group);
81         checkUserCount(0);
82     }
83
84     protected UserSet getUsers() {
85         return users;
86     }
87
88     protected GroupSet getGroups() {
89         return groups;
90     }
91
92     protected Group createGroup(String aName) {
93         return new Group(aName);
94     }
95
96     protected User createUser(String aName, String aPassword, Group aGroup)
97         throws UserMgtException {
98         return UsermgtTestUtils.createUser(aName, aPassword, aGroup);
99     }
100
101     protected void addUserToGroup(User aUser, Group aGroup)
102         throws UserMgtException {
103         aUser.addGroup(aGroup);
104     }
105
106     protected void removeUserFromGroup(User aUser, Group aGroup)
107         throws UserMgtException {
108         aUser.removeGroup(aGroup);
109     }
110
111     /**
112      * Additional check to be implemented by a subclass.
113      * 
114      * @param aUser
115      *            User to check for existence.
116      * 
117      */
118     protected void checkUserExists(String aUser) throws Exception {
119         // Empty
120     }
121
122     /**
123      * Additional check to be implemented by a subclass.
124      * 
125      * @param aUser
126      *            User to check for non-existence.
127      * 
128      */
129     protected void checkUserNotExists(String aUser) throws Exception {
130         // Empty
131     }
132
133     /**
134      * Additional check to be implemented by a subclass.
135      * 
136      * @param aSize
137      *            Expected number of users.
138      * 
139      */
140     protected void checkUserCount(int aSize) throws Exception {
141         assertEquals(aSize, users.size());
142     }
143
144     /**
145      * Additional check to be implemented by a subclass.
146      * 
147      * @param aUser
148      *            User to check for existence.
149      * 
150      */
151     protected void checkGroupExists(String aUser) throws Exception {
152         // Empty
153     }
154
155     /**
156      * Additional check to be implemented by a subclass.
157      * 
158      * @param aUser
159      *            User to check for non-existence.
160      * 
161      */
162     protected void checkGroupNotExists(String aUser) throws Exception {
163         // Empty
164     }
165
166     /**
167      * Additional check to be implemented by a subclass.
168      * 
169      * @param aSize
170      *            Expected number of users.
171      * 
172      */
173     protected void checkGroupCount(int aSize) throws Exception {
174         // Empty
175     }
176
177     /**
178      * Adds a user and verifies that the user is added using find(), list(), and
179      * contains().
180      * 
181      */
182     public void testAdd() throws Exception {
183         User user = createUser("user1", PASSWORD, group);
184         assertTrue(users.add(user));
185         checkUserExists(user.getName());
186         checkUserCount(1);
187
188         User user2 = users.find("user1");
189         assertNotNull(user2);
190         assertEquals(user.getName(), user2.getName());
191
192         Set<User> set = users.list();
193         assertEquals(1, set.size());
194         assertTrue(set.contains(user));
195     }
196
197     /**
198      * Tries to find a non-existing user. Verifies that null is returned.
199      * 
200      */
201     public void testFindUnknownUser() throws Exception {
202         User user1 = createUser("user1", PASSWORD, group);
203         User user2 = createUser("user2", PASSWORD, group);
204         users.add(user1);
205         users.add(user2);
206         checkUserExists(user1.getName());
207         checkUserExists(user2.getName());
208
209         assertNull(users.find("user3"));
210         checkUserNotExists("user3");
211     }
212
213     /**
214      * Adds duplicate user. Verifies that the existing user is left untouched.
215      * 
216      */
217     public void testAddDuplicateUser() throws Exception {
218         User user1 = createUser("user1", PASSWORD, group);
219         users.add(user1);
220
221         assertEquals(1, users.list().size());
222         assertTrue(users.contains(user1));
223         user1 = createUser("user1", PASSWORD, group);
224         assertFalse(users.add(user1));
225         assertEquals(1, users.list().size());
226
227         checkUserExists(user1.getName());
228         checkUserCount(1);
229     }
230
231     /**
232      * Removes a user. Verifies that the user is removed and the return value is
233      * true.
234      * 
235      */
236     public void testRemoveUser() throws Exception {
237         User user1 = createUser("user1", PASSWORD, group);
238         users.add(user1);
239         assertTrue(users.contains(user1));
240         checkUserCount(1);
241
242         assertTrue(users.remove(user1));
243         assertFalse(users.contains(user1));
244         assertNull(users.find(user1.getName()));
245         assertEquals(0, users.list().size());
246         checkUserCount(0);
247     }
248
249     /**
250      * Removes a non-existing user. Verifies that no users are removed an that
251      * the return value is true.
252      * 
253      */
254     public void testRemoveNonExistingUser() throws Exception,
255         UserMgtException {
256         User user1 = createUser("user1", PASSWORD, group);
257         users.add(user1);
258         checkUserCount(1);
259
260         User nonExistingUser = createUser("user2", PASSWORD, group);
261         assertFalse(users.remove(nonExistingUser));
262         assertTrue(users.contains(user1));
263         assertEquals(1, users.list().size());
264         checkUserCount(1);
265     }
266
267     /**
268      * Adds a number of users to the set and verifies that list() returns them
269      * all.
270      * 
271      */
272     public void testList() throws Exception {
273         User user1 = createUser("user1", PASSWORD, group);
274         User user2 = createUser("user2", PASSWORD, group);
275         User user3 = createUser("user3", PASSWORD, group);
276         assertTrue(users.add(user1));
277         assertTrue(users.add(user2));
278         assertTrue(users.add(user3));
279
280         checkUserExists(user1.getName());
281         checkUserExists(user2.getName());
282         checkUserExists(user3.getName());
283
284         Set<User> set = users.list();
285         assertTrue(set.contains(user1));
286         assertTrue(set.contains(user2));
287         assertTrue(set.contains(user3));
288
289         checkUserCount(3);
290     }
291
292     /**
293      * Adds several users to different groups and verifies that the correct
294      * users are returned when looking for users in different groups.
295      * 
296      * @throws SQLException
297      */
298     public void testListByGroup() throws Exception {
299         Group group1 = new Group("group1");
300         Group group2 = new Group("group2");
301         Group group3 = new Group("group3");
302         groups.add(group1);
303         groups.add(group2);
304         groups.add(group3);
305
306         // user1 user2 user3
307         // group1 y
308         // group2 y y
309         // group3 y y y
310         User user1 = createUser("user1", PASSWORD, group1);
311         user1.addGroup(group2);
312         user1.addGroup(group3);
313
314         User user2 = createUser("user2", PASSWORD, group2);
315         user2.addGroup(group3);
316
317         User user3 = createUser("user3", PASSWORD, group3);
318         users.add(user1);
319         users.add(user2);
320         users.add(user3);
321
322         checkUserExists(user1.getName());
323         checkUserExists(user2.getName());
324         checkUserExists(user3.getName());
325
326         checkGroupExists(group1.getName());
327         checkGroupExists(group2.getName());
328         checkGroupExists(group3.getName());
329
330         checkUserCount(3);
331         checkGroupCount(3 + 1); // also count the group that was created in the
332                                 // setUp().
333
334         Set<User> list = users.list(group1);
335         assertTrue(list.contains(user1));
336         assertEquals(1, list.size());
337
338         list = users.list(group2);
339         assertTrue(list.contains(user1));
340         assertTrue(list.contains(user2));
341         assertEquals(2, list.size());
342
343         list = users.list(group3);
344         assertTrue(list.contains(user1));
345         assertTrue(list.contains(user2));
346         assertTrue(list.contains(user3));
347         assertEquals(3, list.size());
348     }
349 }