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