View Javadoc

1   /**
2    * 2007, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
3    * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
4    */
5   
6   package pt.digitalis.dif.utils.identity.tests;
7   
8   import java.util.ArrayList;
9   import java.util.HashMap;
10  import java.util.List;
11  import java.util.Map;
12  import java.util.Set;
13  
14  import junit.framework.TestCase;
15  import pt.digitalis.dif.controller.security.managers.IIdentityManager;
16  import pt.digitalis.dif.controller.security.managers.impl.IdentityManagerStaticImpl;
17  import pt.digitalis.dif.controller.security.objects.DIFGroupImpl;
18  import pt.digitalis.dif.controller.security.objects.DIFUserImpl;
19  import pt.digitalis.dif.controller.security.objects.IDIFGroup;
20  import pt.digitalis.dif.controller.security.objects.IDIFUser;
21  import pt.digitalis.dif.exception.InternalFrameworkException;
22  import pt.digitalis.dif.exception.security.IdentityManagerException;
23  import pt.digitalis.dif.ioc.DIFIoCRegistry;
24  import pt.digitalis.dif.utils.Pagination;
25  
26  /**
27   * Defines unit tests for the identity manager implementation.
28   * 
29   * @author Luis Pinto <a href="mailto:lpinto@digitalis.pt">lpinto@digitalis.pt</a><br/>
30   * @created 2012/10/30
31   */
32  public abstract class AbstractIdentityManagerImplTest extends TestCase {
33  
34      /** A bulk parameter name. */
35      final static protected String A_BULK_ATTRIBUTE_NAME = "aBulkAttributeName";
36  
37      /** A bulk parameter value. */
38      final static protected String A_BULK_ATTRIBUTE_VALUE = "aBulkAttributeValue";
39  
40      /** A changed attribute value. */
41      final static protected String A_CHANGED_ATTRIBUTE_VALUE = "aChangedAttributeValue";
42  
43      /** A changed bulk parameter value. */
44      final static protected String A_CHANGED_BULK_ATTRIBUTE_VALUE = "aChangedBulkAttributeValue";
45  
46      /** An attribute name. */
47      final static protected String AN_ATTRIBUTE_NAME = "anAttributeName";
48  
49      /** An attribute value. */
50      final static protected String AN_ATTRIBUTE_VALUE = "anAttributeValue";
51  
52      /** Another attribute name. */
53      final static protected String ANOTHER_ATTRIBUTE_NAME = "anotherAttributeName";
54  
55      /** Another parameter value. */
56      final static protected String ANOTHER_ATTRIBUTE_VALUE = "anotherAttributeValue";
57  
58      /** Another bulk parameter name. */
59      final static protected String ANOTHER_BULK_ATTRIBUTE_NAME = "anotherBulkAttributeName";
60  
61      /** Another bulk parameter value. */
62      final static protected String ANOTHER_BULK_ATTRIBUTE_VALUE = "anotherBulkAttributeValue";
63  
64      /** Another changed parameter value. */
65      final static protected String ANOTHER_CHANGED_ATTRIBUTE_VALUE = "anotherChangedAttributeValue";
66  
67      /** Another changed bulk parameter value. */
68      final static protected String ANOTHER_CHANGED_BULK_PARAMETER_VALUE = "anotherChangedBulkAttributeValue";
69  
70      /** The new user ID. */
71      final static private String ATTRIBUTE_KEY = "key";
72  
73      /**  */
74      final static private String ATTRIBUTE_VALUE = "some attribute value";
75  
76      /** A bad group ID. */
77      final static private String BAD_GROUP = "badGroup";
78  
79      /** A bad password. */
80      final static private String BAD_PASS = "badPass";
81  
82      /** A bad username. */
83      final static private String BAD_USER = "badUser";
84  
85      /** Changed group description. */
86      final static private String CHANGED_DESC = "changedGroupDesc";
87  
88      /** Changed group name. */
89      final static private String CHANGED_GROUP_NAME = "newGroup";
90  
91      /** The changed mail. */
92      final static private String CHANGED_MAIL = "changed@mail.pt";
93  
94      /** Changed group name. */
95      final static private String CHANGED_PARENT_GROUP_NAME = "childGroup";
96  
97      /** A changed password. */
98      final static private String CHANGED_PASS = "changedPass";
99  
100     /** Changed attributes value. */
101     final static private String CHANGED_USER = "changedUser";
102 
103     /** A child group ID. */
104     final static private String CHILD_GROUP_1_ID = "childGroup1ID";
105 
106     /** Another child group ID. */
107     final static private String CHILD_GROUP_2_ID = "childGroup2ID";
108 
109     /** Group description. */
110     final static private String DESC = "aGroupDesc";
111 
112     /** A parent group ID. */
113     final static private String GRAND_PARENT_GROUP_ID = "grandParentGroupID";
114 
115     /** The 'N/A' String literal. */
116     final static public String N_A = "N/A";
117 
118     /** New group name. */
119     final static private String NEW_GROUP_NAME = "newGroup";
120 
121     /** The new user ID. */
122     final static private String NEW_USER = "newUser";
123 
124     /** A parent group ID. */
125     final static private String PARENT_GROUP_ID = "parentGroupID";
126 
127     /** The user mail. */
128     final static private String USER_MAIL = "user@mail.pt";
129 
130     /** The user password. */
131     final static private String USER_PASS = "passwd";
132 
133     /** The test object. */
134     protected IIdentityManager identityManagerImpl = null;
135 
136     /** Main group name. */
137     protected String MAIN_GROUP_NAME = "mainGroup";
138 
139     /** Test group 1. */
140     private String TEST_GRP_1_ID = "testGroup1";
141 
142     /** Test group 2. */
143     private String TEST_GRP_2_ID = "testGroup2";
144 
145     /** Test group 3. */
146     private String TEST_GRP_3_ID = "testGroup3";
147 
148     /** Test user 1. */
149     private String TEST_USER_1_ID = "testUser1";
150 
151     /** Test user 2. */
152     private String TEST_USER_2_ID = "testUser2";
153 
154     /** Test user 3. */
155     private String TEST_USER_3_ID = "testUser3";
156 
157     /**
158      * Creates a group and adds it to the identity manager.
159      * 
160      * @throws IdentityManagerException
161      *             if the user can't be created
162      */
163     protected void createGroup() throws IdentityManagerException
164     {
165         createGroup(null);
166     }
167 
168     /**
169      * Creates a group and adds it to the identity manager.
170      * 
171      * @param groupName
172      * @throws IdentityManagerException
173      *             if the user can't be created
174      */
175     protected void createGroup(String groupName) throws IdentityManagerException
176     {
177         createGroup(groupName, MAIN_GROUP_NAME);
178     }
179 
180     /**
181      * Creates a group and adds it to the identity manager.
182      * 
183      * @param groupName
184      *            the group name
185      * @param parentGroup
186      *            the group parent group
187      * @throws IdentityManagerException
188      *             if the user can't be created
189      */
190     protected void createGroup(String groupName, String parentGroup) throws IdentityManagerException
191     {
192         String finalGroupName = groupName;
193         if (finalGroupName == null || "".equals(finalGroupName))
194         {
195             finalGroupName = NEW_GROUP_NAME;
196         }
197 
198         // Create group for addition
199         IDIFGroup transferGroup = new DIFGroupImpl();
200         transferGroup.setID(finalGroupName);
201         transferGroup.setName(finalGroupName);
202         transferGroup.setDescription(DESC);
203         if (!finalGroupName.equals(parentGroup))
204         {
205             transferGroup.setParentGroupID(parentGroup);
206 
207             if (!identityManagerImpl.groupExists(parentGroup))
208             {
209                 createGroup(parentGroup);
210             }
211         }
212 
213         // Add group if it doesn't exist and verify it's existence
214         if (identityManagerImpl.groupExists(finalGroupName))
215         {
216             removeGroup(finalGroupName);
217             assertFalse(identityManagerImpl.groupExists(finalGroupName));
218         }
219         identityManagerImpl.addGroup(transferGroup);
220         assertTrue(identityManagerImpl.groupExists(finalGroupName));
221     }
222 
223     /**
224      * Creates a user and adds it to the identity manager.
225      * 
226      * @throws IdentityManagerException
227      *             if the user can't be created
228      * @throws InternalFrameworkException
229      */
230     protected void createUser() throws IdentityManagerException, InternalFrameworkException
231     {
232         this.createUser(NEW_USER);
233     }
234 
235     /**
236      * Creates a user and adds it to the identity manager.
237      * 
238      * @param userID
239      *            the user ID
240      * @throws IdentityManagerException
241      *             if the user can't be created
242      * @throws InternalFrameworkException
243      */
244     protected void createUser(String userID) throws IdentityManagerException, InternalFrameworkException
245     {
246         // Create user for addition
247         IDIFUser newUser = new DIFUserImpl();
248         newUser.setID(userID);
249         newUser.setName(userID);
250         newUser.setNick(userID);
251         newUser.setEmail(USER_MAIL);
252         newUser.setPassword(USER_PASS);
253         newUser.setProfileID(MAIN_GROUP_NAME);
254 
255         if (!identityManagerImpl.groupExists(MAIN_GROUP_NAME))
256         {
257             createGroup(MAIN_GROUP_NAME);
258         }
259 
260         // Add user if it doesn't exist and verify it's existence
261         if (identityManagerImpl.userExists(userID))
262         {
263             identityManagerImpl.removeUser(userID);
264         }
265 
266         assertFalse(identityManagerImpl.userExists(userID));
267 
268         identityManagerImpl.addUser(newUser);
269         newUser.setAttribute(ATTRIBUTE_KEY, ATTRIBUTE_VALUE);
270 
271         assertTrue(identityManagerImpl.userExists(userID));
272     }
273 
274     /**
275      * Removes the group NEW_GROUP_NAME from the identity manager.
276      * 
277      * @throws IdentityManagerException
278      *             if the user can't be created
279      */
280     protected void removeGroup() throws IdentityManagerException
281     {
282 
283         removeGroup(NEW_GROUP_NAME);
284     }
285 
286     /**
287      * Removes a group from the identity manager.
288      * 
289      * @param groupId
290      *            the group ID to remove
291      * @throws IdentityManagerException
292      */
293     protected void removeGroup(String groupId) throws IdentityManagerException
294     {
295         for (String groupID: identityManagerImpl.getGroupGroups(groupId).keySet())
296         {
297             removeGroup(groupID);
298         }
299 
300         // Remove group and assure it's been removed
301         if (identityManagerImpl.groupExists(groupId))
302         {
303             // First remove any pending test users
304             for (String userID: identityManagerImpl.getGroup(groupId).getUserIDs())
305                 identityManagerImpl.removeUser(userID);
306 
307             identityManagerImpl.removeGroup(groupId);
308         }
309         assertFalse(identityManagerImpl.groupExists(groupId));
310     }
311 
312     /**
313      * Removes a user from the identity manager.
314      * 
315      * @param userId
316      *            the id of the user to remove
317      * @throws IdentityManagerException
318      *             if the user can't be created
319      */
320     protected void removeUser(String userId) throws IdentityManagerException
321     {
322         // Remove user and assure it's been removed
323         identityManagerImpl.removeUser(userId);
324         assertFalse(identityManagerImpl.userExists(userId));
325     }
326 
327     /**
328      * @see junit.framework.TestCase#setUp()
329      */
330     @Override
331     protected void setUp() throws Exception
332     {
333         System.out.print(".");
334         super.setUp();
335     }
336 
337     /**
338      * @see junit.framework.TestCase#tearDown()
339      */
340     @Override
341     public void tearDown()
342     {
343         System.out.println(".");
344         try
345         {
346             if (identityManagerImpl.userExists(NEW_USER))
347                 removeUser(NEW_USER);
348             if (identityManagerImpl.userExists(CHANGED_USER))
349                 removeUser(CHANGED_USER);
350             if (identityManagerImpl.userExists(TEST_USER_1_ID))
351                 removeUser(TEST_USER_1_ID);
352             if (identityManagerImpl.userExists(TEST_USER_2_ID))
353                 removeUser(TEST_USER_2_ID);
354             if (identityManagerImpl.userExists(TEST_USER_3_ID))
355                 removeUser(TEST_USER_3_ID);
356             /*
357              * if (identityManagerImpl.groupExists(NEW_GROUP_NAME)) identityManagerImpl.removeGroup(NEW_GROUP_NAME); if
358              * (identityManagerImpl.groupExists(TEST_GRP_1_ID)) identityManagerImpl.removeGroup(TEST_GRP_1_ID); if
359              * (identityManagerImpl.groupExists(TEST_GRP_2_ID)) identityManagerImpl.removeGroup(TEST_GRP_2_ID); if
360              * (identityManagerImpl.groupExists(TEST_GRP_3_ID)) identityManagerImpl.removeGroup(TEST_GRP_3_ID); if
361              * (identityManagerImpl.groupExists(PARENT_GROUP_ID)) identityManagerImpl.removeGroup(PARENT_GROUP_ID); if
362              * (identityManagerImpl.groupExists(CHILD_GROUP_1_ID)) identityManagerImpl.removeGroup(CHILD_GROUP_1_ID); if
363              * (identityManagerImpl.groupExists(CHILD_GROUP_2_ID)) identityManagerImpl.removeGroup(CHILD_GROUP_2_ID); if
364              * (identityManagerImpl.groupExists(CHANGED_PARENT_GROUP_NAME))
365              * identityManagerImpl.removeGroup(CHANGED_PARENT_GROUP_NAME);
366              */
367 
368             if (identityManagerImpl.groupExists(MAIN_GROUP_NAME))
369                 removeGroup(MAIN_GROUP_NAME);
370 
371         }
372         catch (IdentityManagerException identityManagerException)
373         {
374             identityManagerException.printStackTrace();
375         }
376 
377         identityManagerImpl.resetIdentityManager();
378         identityManagerImpl = null;
379     }
380 
381     /**
382      * Tests if the obtainance of all groups in the system work correctly
383      * 
384      * @throws IdentityManagerException
385      */
386     public void testFindAllGroups() throws IdentityManagerException
387     {
388         IDIFGroup group1 = DIFIoCRegistry.getRegistry().getImplementation(IDIFGroup.class);
389         IDIFGroup group2 = DIFIoCRegistry.getRegistry().getImplementation(IDIFGroup.class);
390         IDIFGroup group3 = DIFIoCRegistry.getRegistry().getImplementation(IDIFGroup.class);
391 
392         int numElements = identityManagerImpl.getAllGroups().size();
393 
394         assertEquals(numElements, identityManagerImpl.countAllGroups());
395 
396         group1.setID(TEST_GRP_1_ID);
397         if (identityManagerImpl.getGroup(TEST_GRP_1_ID) == null)
398         {
399             identityManagerImpl.addGroup(group1);
400             numElements++;
401         }
402 
403         group2.setID(TEST_GRP_2_ID);
404         if (identityManagerImpl.getGroup(TEST_GRP_2_ID) == null)
405         {
406             identityManagerImpl.addGroup(group2);
407             numElements++;
408         }
409 
410         group3.setID(TEST_GRP_3_ID);
411         if (identityManagerImpl.getGroup(TEST_GRP_3_ID) == null)
412         {
413             identityManagerImpl.addGroup(group3);
414             numElements++;
415         }
416 
417         Set<IDIFGroup> allGroups = identityManagerImpl.getAllGroups();
418 
419         assertTrue(allGroups.contains(group1));
420         assertTrue(allGroups.contains(group2));
421         assertTrue(allGroups.contains(group3));
422         assertEquals(allGroups.size(), numElements);
423 
424         // TODO: Waiting for Pagination bug resolution
425         // Pagination pagination = new Pagination(1, numElements);
426         // assertEquals(identityManagerImpl.getGroups(pagination).size(), numElements);
427 
428         // TODO: Waiting for Pagination bug resolution
429         // pagination = new Pagination(1, 2);
430         // assertEquals(identityManagerImpl.getGroups(pagination).size(), 2);
431 
432         // TODO: Waiting for Pagination bug resolution
433         // for (int i = 1; i <= numElements; i++)
434         // {
435         // pagination = new Pagination(i, 1);
436         // assertEquals(identityManagerImpl.getGroups(pagination).size(), 1);
437         // }
438 
439         identityManagerImpl.removeGroup(TEST_GRP_1_ID);
440         numElements--;
441         identityManagerImpl.removeGroup(TEST_GRP_2_ID);
442         numElements--;
443         identityManagerImpl.removeGroup(TEST_GRP_3_ID);
444         numElements--;
445 
446         assertFalse(identityManagerImpl.getAllGroups().contains(group1));
447         assertFalse(identityManagerImpl.getAllGroups().contains(group2));
448         assertFalse(identityManagerImpl.getAllGroups().contains(group3));
449         assertEquals(identityManagerImpl.getAllGroups().size(), numElements);
450     }
451 
452     /**
453      * Tests if the obtainance of all users in the system work correctly
454      * 
455      * @throws IdentityManagerException
456      */
457     public void testFindllUsers() throws IdentityManagerException
458     {
459         createGroup(MAIN_GROUP_NAME);
460 
461         IDIFUser testUser1 = new DIFUserImpl();
462         testUser1.setID(TEST_USER_1_ID);
463         testUser1.setName(TEST_USER_1_ID);
464         testUser1.setNick(TEST_USER_1_ID);
465         testUser1.setEmail(USER_MAIL);
466         testUser1.setPassword(USER_PASS);
467         testUser1.setProfileID(MAIN_GROUP_NAME);
468 
469         IDIFUser testUser2 = new DIFUserImpl();
470         testUser2.setID(TEST_USER_2_ID);
471         testUser2.setName(TEST_USER_2_ID);
472         testUser2.setNick(TEST_USER_2_ID);
473         testUser2.setEmail(USER_MAIL);
474         testUser2.setPassword(USER_PASS);
475         testUser2.setProfileID(MAIN_GROUP_NAME);
476 
477         IDIFUser testUser3 = new DIFUserImpl();
478         testUser3.setID(TEST_USER_3_ID);
479         testUser3.setName(TEST_USER_3_ID);
480         testUser3.setNick(TEST_USER_3_ID);
481         testUser3.setEmail(USER_MAIL);
482         testUser3.setPassword(USER_PASS);
483         testUser3.setProfileID(MAIN_GROUP_NAME);
484 
485         int numElements = identityManagerImpl.getAllUsers().size();
486 
487         assertEquals(numElements, identityManagerImpl.countAllUsers());
488 
489         identityManagerImpl.addUser(testUser1);
490         identityManagerImpl.addUser(testUser2);
491         identityManagerImpl.addUser(testUser3);
492 
493         numElements += 3;
494 
495         assertTrue(identityManagerImpl.getAllUsers().contains(testUser1));
496         assertTrue(identityManagerImpl.getAllUsers().contains(testUser2));
497         assertTrue(identityManagerImpl.getAllUsers().contains(testUser3));
498         assertEquals(identityManagerImpl.getAllUsers().size(), numElements);
499 
500         // TODO: Waiting for Pagination bug resolution
501         // Pagination pagination = new Pagination(1, numElements);
502         // assertEquals(identityManagerImpl.getUsers(pagination).size(), numElements);
503 
504         // TODO: Waiting for Pagination bug resolution
505         // for (int i = 1; i <= numElements; i++)
506         // {
507         // pagination = new Pagination(i, 1);
508         // assertEquals(identityManagerImpl.getUsers(pagination).size(), 1);
509         // }
510 
511         identityManagerImpl.removeUser(TEST_USER_1_ID);
512         identityManagerImpl.removeUser(TEST_USER_2_ID);
513         identityManagerImpl.removeUser(TEST_USER_3_ID);
514 
515         numElements -= 3;
516 
517         assertFalse(identityManagerImpl.getAllUsers().contains(testUser1));
518         assertFalse(identityManagerImpl.getAllUsers().contains(testUser2));
519         assertFalse(identityManagerImpl.getAllUsers().contains(testUser3));
520         assertEquals(identityManagerImpl.getAllUsers().size(), numElements);
521 
522     }
523 
524     /**
525      * Test the find user groups methods
526      */
527     public void testFindUserGroups()
528     {
529         final String GROUP1 = "testFindUserGroupsGroup1";
530         final String GROUP2 = "testFindUserGroupsGroup2";
531         final String GROUP3 = "testFindUserGroupsGroup3";
532         try
533         {
534             createUser();
535 
536             createGroup(GRAND_PARENT_GROUP_ID);
537             createGroup(PARENT_GROUP_ID, GRAND_PARENT_GROUP_ID);
538 
539             createGroup(GROUP1);
540             createGroup(GROUP2);
541             createGroup(GROUP3, PARENT_GROUP_ID);
542 
543             identityManagerImpl.addUserToGroup(NEW_USER, GROUP1);
544             identityManagerImpl.addUserToGroup(NEW_USER, GROUP2);
545             identityManagerImpl.addUserToGroup(NEW_USER, GROUP3);
546 
547             int totalGroups = identityManagerImpl.countAllGroupsOfUser(NEW_USER);
548             assertEquals(totalGroups, 4);
549 
550             totalGroups = identityManagerImpl.countAllGroupsOfUser(NEW_USER, true);
551             assertEquals(totalGroups, 6);
552 
553             totalGroups = identityManagerImpl.countAllGroupsOfUser(NEW_USER, false);
554             assertEquals(totalGroups, 4);
555 
556             Set<String> groupIDs = identityManagerImpl.getUserGroupsIDs(NEW_USER);
557             assertEquals(groupIDs.size(), 4);
558             assertTrue(groupIDs.contains(GROUP1));
559             assertTrue(groupIDs.contains(GROUP2));
560             assertTrue(groupIDs.contains(GROUP3));
561             assertFalse(groupIDs.contains(PARENT_GROUP_ID));
562             assertFalse(groupIDs.contains(GRAND_PARENT_GROUP_ID));
563             assertTrue(groupIDs.contains(MAIN_GROUP_NAME));
564 
565             groupIDs = identityManagerImpl.getUserGroupsIDs(NEW_USER, true);
566             assertEquals(groupIDs.size(), 6);
567             assertTrue(groupIDs.contains(GROUP1));
568             assertTrue(groupIDs.contains(GROUP2));
569             assertTrue(groupIDs.contains(GROUP3));
570             assertTrue(groupIDs.contains(PARENT_GROUP_ID));
571             assertTrue(groupIDs.contains(GRAND_PARENT_GROUP_ID));
572             assertTrue(groupIDs.contains(MAIN_GROUP_NAME));
573 
574             groupIDs = identityManagerImpl.getUserGroupsIDs(NEW_USER, false);
575             assertEquals(groupIDs.size(), 4);
576             assertTrue(groupIDs.contains(GROUP1));
577             assertTrue(groupIDs.contains(GROUP2));
578             assertTrue(groupIDs.contains(GROUP3));
579             assertFalse(groupIDs.contains(PARENT_GROUP_ID));
580             assertFalse(groupIDs.contains(GRAND_PARENT_GROUP_ID));
581             assertTrue(groupIDs.contains(MAIN_GROUP_NAME));
582 
583             Map<String, IDIFGroup> groups = identityManagerImpl.getUserGroups(NEW_USER);
584             assertEquals(groups.size(), 4);
585             assertTrue(groups.containsKey(GROUP1));
586             assertTrue(groups.containsKey(GROUP2));
587             assertTrue(groups.containsKey(GROUP3));
588             assertFalse(groups.containsKey(PARENT_GROUP_ID));
589             assertFalse(groups.containsKey(GRAND_PARENT_GROUP_ID));
590             assertTrue(groups.containsKey(MAIN_GROUP_NAME));
591 
592             groups = identityManagerImpl.getUserGroups(NEW_USER, true);
593             assertEquals(groups.size(), 6);
594             assertTrue(groups.containsKey(GROUP1));
595             assertTrue(groups.containsKey(GROUP2));
596             assertTrue(groups.containsKey(GROUP3));
597             assertTrue(groups.containsKey(PARENT_GROUP_ID));
598             assertTrue(groups.containsKey(GRAND_PARENT_GROUP_ID));
599             assertTrue(groups.containsKey(MAIN_GROUP_NAME));
600 
601             groups = identityManagerImpl.getUserGroups(NEW_USER, false);
602             assertEquals(groups.size(), 4);
603             assertTrue(groups.containsKey(GROUP1));
604             assertTrue(groups.containsKey(GROUP2));
605             assertTrue(groups.containsKey(GROUP3));
606             assertFalse(groups.containsKey(PARENT_GROUP_ID));
607             assertFalse(groups.containsKey(GRAND_PARENT_GROUP_ID));
608             assertTrue(groups.containsKey(MAIN_GROUP_NAME));
609 
610             List<String> groupList = new ArrayList<String>();
611 
612             Pagination page = new Pagination(1, 2);
613             groups = identityManagerImpl.getUserGroupsPagination(NEW_USER, page);
614             assertEquals(groups.size(), 2);
615             groupList.addAll(groups.keySet());
616 
617             page = new Pagination(2, 2);
618             assertEquals(page.getEndRow(), 4);
619             assertEquals(page.getPage(), 2);
620 
621             groups = identityManagerImpl.getUserGroupsPagination(NEW_USER, page);
622 
623             // Only one group exists because the getUserGroupsPagination doesn't consider the profile as a group
624             assertEquals(groups.size(), 1);
625             groupList.addAll(groups.keySet());
626 
627             assertEquals(groupList.size(), 3);
628             assertTrue(groupList.contains(GROUP1));
629             assertTrue(groupList.contains(GROUP2));
630             assertTrue(groupList.contains(GROUP3));
631             assertFalse(groupList.contains(MAIN_GROUP_NAME));
632             assertFalse(groupList.contains(PARENT_GROUP_ID));
633 
634             assertEquals(identityManagerImpl.countAllUsers(GROUP1), 1);
635             assertEquals(identityManagerImpl.countAllUsers(GROUP2), 1);
636             assertEquals(identityManagerImpl.countAllUsers(GROUP3), 1);
637             assertEquals(identityManagerImpl.countAllUsers(MAIN_GROUP_NAME), 0);
638             assertEquals(identityManagerImpl.countAllUsers(PARENT_GROUP_ID), 0);
639         }
640         catch (IdentityManagerException e)
641         {
642             e.printStackTrace();
643             assertFalse(true);
644         }
645         catch (InternalFrameworkException e)
646         {
647             e.printStackTrace();
648             assertFalse(true);
649         }
650     }
651 
652     /**
653      * Tests searching for groups with a given parent.
654      * 
655      * @throws IdentityManagerException
656      *             if the identity manager can't be accessed
657      */
658     public void testGroupGroups() throws IdentityManagerException
659     {
660         createGroup(GRAND_PARENT_GROUP_ID);
661         createGroup(PARENT_GROUP_ID, GRAND_PARENT_GROUP_ID);
662         createGroup(CHILD_GROUP_1_ID, PARENT_GROUP_ID);
663         createGroup(CHILD_GROUP_2_ID, PARENT_GROUP_ID);
664 
665         assertTrue(identityManagerImpl.groupExists(PARENT_GROUP_ID));
666         assertTrue(identityManagerImpl.groupExists(CHILD_GROUP_1_ID));
667         assertTrue(identityManagerImpl.groupExists(CHILD_GROUP_2_ID));
668 
669         Map<String, IDIFGroup> groupGroups = identityManagerImpl.getGroupGroups(GRAND_PARENT_GROUP_ID);
670         assertNotNull(groupGroups);
671         assertEquals(groupGroups.size(), 3);
672         assertFalse(groupGroups.containsKey(GRAND_PARENT_GROUP_ID));
673         assertTrue(groupGroups.containsKey(PARENT_GROUP_ID));
674         assertTrue(groupGroups.containsKey(CHILD_GROUP_1_ID));
675         assertTrue(groupGroups.containsKey(CHILD_GROUP_2_ID));
676         assertEquals(groupGroups.get(CHILD_GROUP_1_ID).getParentGroupID(), PARENT_GROUP_ID);
677         assertEquals(groupGroups.get(CHILD_GROUP_2_ID).getParentGroupID(), PARENT_GROUP_ID);
678 
679         identityManagerImpl.removeGroup(CHILD_GROUP_1_ID);
680         identityManagerImpl.removeGroup(CHILD_GROUP_2_ID);
681         identityManagerImpl.removeGroup(PARENT_GROUP_ID);
682         identityManagerImpl.removeGroup(GRAND_PARENT_GROUP_ID);
683 
684         assertFalse(identityManagerImpl.groupExists(CHILD_GROUP_1_ID));
685         assertFalse(identityManagerImpl.groupExists(CHILD_GROUP_2_ID));
686         assertFalse(identityManagerImpl.groupExists(PARENT_GROUP_ID));
687         assertFalse(identityManagerImpl.groupExists(GRAND_PARENT_GROUP_ID));
688     }
689 
690     /**
691      * Tests the group operations.
692      */
693     public void testGroupOperations()
694     {
695         try
696         {
697             createGroup();
698 
699             // Test group integrity
700             IDIFGroup retrievedGroup = identityManagerImpl.getGroup(NEW_GROUP_NAME);
701             assertEquals(retrievedGroup.getID(), NEW_GROUP_NAME);
702             assertEquals(retrievedGroup.getName(), NEW_GROUP_NAME);
703             assertEquals(retrievedGroup.getDescription(), DESC);
704             assertTrue(retrievedGroup.getParentGroupID().contains(MAIN_GROUP_NAME));
705 
706             createGroup(CHANGED_PARENT_GROUP_NAME);
707 
708             // Set group data for modification
709             IDIFGroup group = new DIFGroupImpl();
710             group.setID(CHANGED_GROUP_NAME);
711             group.setName(CHANGED_GROUP_NAME);
712             group.setDescription(CHANGED_DESC);
713             group.setParentGroupID(CHANGED_PARENT_GROUP_NAME);
714 
715             // Modify group data
716             identityManagerImpl.updateGroup(group);
717             assertTrue(identityManagerImpl.groupExists(NEW_GROUP_NAME));
718 
719             // Test group integrity
720             retrievedGroup = identityManagerImpl.getGroup(CHANGED_GROUP_NAME);
721             assertEquals(retrievedGroup.getID(), CHANGED_GROUP_NAME);
722             assertEquals(retrievedGroup.getName(), CHANGED_GROUP_NAME);
723             assertEquals(retrievedGroup.getDescription(), CHANGED_DESC);
724             assertTrue(retrievedGroup.getParentGroupID().contains(CHANGED_PARENT_GROUP_NAME));
725 
726             removeGroup();
727 
728         }
729         catch (IdentityManagerException e)
730         {
731             e.printStackTrace();
732         }
733     }
734 
735     /**
736      * Tests the group operations forcing failure behavior.
737      * 
738      * @throws IdentityManagerException
739      *             if the identity manager can't be accessed
740      */
741     @SuppressWarnings("unused")
742     public void testGroupOperationsInFailureBehaviour() throws IdentityManagerException
743     {
744         // Create group for addition
745         IDIFGroup transferGroup = new DIFGroupImpl();
746 
747         // Tries to group an user without id...must fail!!
748         try
749         {
750             identityManagerImpl.addGroup(transferGroup);
751         }
752         catch (IdentityManagerException e)
753         {
754             // User must not have been added...
755             assertFalse(identityManagerImpl.groupExists(""));
756         }
757 
758         // Try to fetch data for an inexistent group
759         try
760         {
761             IDIFGroup retrievedGroup = identityManagerImpl.getGroup(BAD_GROUP);
762         }
763         catch (IdentityManagerException identityManagerException)
764         {
765         }
766 
767         // Tries to modify a bad group...must fail!!
768         try
769         {
770             identityManagerImpl.updateGroup(transferGroup);
771         }
772         catch (IdentityManagerException e)
773         {
774             // User must not have been added...
775             assertFalse(identityManagerImpl.groupExists(BAD_GROUP));
776         }
777 
778         // Tries to remove a bad user...must fail!!
779         try
780         {
781             identityManagerImpl.removeGroup(BAD_GROUP);
782         }
783         catch (IdentityManagerException e)
784         {
785             // User must not exist
786             assertFalse(identityManagerImpl.groupExists(BAD_GROUP));
787         }
788     }
789 
790     /**
791      * Tests the illegal operations.
792      * 
793      * @throws InternalFrameworkException
794      */
795     public void testIllegalOperations() throws InternalFrameworkException
796     {
797 
798         IDIFGroup badGroup = null;
799         IDIFUser badUser = null;
800 
801         // Force exception throwing on addGroup
802         try
803         {
804             createGroup();
805             badGroup = new DIFGroupImpl();
806             badGroup.setID(NEW_GROUP_NAME);
807             identityManagerImpl.addGroup(badGroup);
808         }
809         catch (IdentityManagerException e)
810         {
811         }
812         finally
813         {
814             try
815             {
816                 removeGroup();
817             }
818             catch (IdentityManagerException e1)
819             {
820                 e1.printStackTrace();
821             }
822         }
823 
824         // Force exception throwing on addUser
825         try
826         {
827             createUser();
828             badUser = new DIFUserImpl();
829             badUser.setID(NEW_USER);
830             identityManagerImpl.addUser(badUser);
831         }
832         catch (IdentityManagerException e)
833         {
834         }
835         finally
836         {
837             try
838             {
839                 removeUser(NEW_USER);
840             }
841             catch (IdentityManagerException e1)
842             {
843                 e1.printStackTrace();
844             }
845         }
846 
847         // Receive null group
848         try
849         {
850             identityManagerImpl.getGroup(null);
851         }
852         catch (IdentityManagerException e)
853         {
854         }
855 
856         // Force exception throwing on convertFromXXXUserToDIFUser
857         try
858         {
859             final String BAD_USER_ID = "BAD_USER";
860             identityManagerImpl.updateUserAttribute(BAD_USER_ID, AN_ATTRIBUTE_NAME, new Object());
861             assertTrue(false);
862         }
863         catch (IdentityManagerException e)
864         {
865             assertTrue(true);
866         }
867 
868     }
869 
870     /**
871      * Tests the static Identity Manager Initialization
872      */
873     public void testInitialization()
874     {
875         // executions for increasing code coverage purpose
876         identityManagerImpl.getExclusionCharaters();
877         identityManagerImpl.getGroupAttributeName();
878         identityManagerImpl.gatherManagedAttributes();
879         assertFalse(identityManagerImpl.isReadOnly());
880 
881         int managedAttributes = identityManagerImpl.getManagedAttributes().size();
882         identityManagerImpl.addManagedAttributes("dummy1,dummy2");
883         identityManagerImpl.addManagedAttribute("dummy3");
884         assertEquals(identityManagerImpl.getManagedAttributes().size(), managedAttributes + 3);
885 
886         // Only the static implementation have a initialization
887         if (identityManagerImpl instanceof IdentityManagerStaticImpl)
888         {
889             ((IdentityManagerStaticImpl) identityManagerImpl).initializeStaticCustomUsers();
890 
891             try
892             {
893                 IDIFUser user = identityManagerImpl.getUser("pedro");
894                 Map<String, IDIFGroup> groups = user.getGroups();
895                 assertTrue(groups.containsKey("alunos"));
896                 assertTrue(groups.containsKey("docentes"));
897                 assertEquals(user.getAttribute("cd_aluno"), "1");
898                 assertEquals(user.getAttribute("cd_curso"), "2");
899                 assertEquals(user.getAttribute("cd_docente"), "3");
900 
901                 user = identityManagerImpl.getUser("viegas");
902                 groups = user.getGroups();
903                 assertTrue(groups.containsKey("alunos"));
904                 assertTrue(groups.containsKey("docentes"));
905                 assertEquals(user.getAttribute("cd_aluno"), "11");
906                 assertEquals(user.getAttribute("cd_curso"), "12");
907                 assertEquals(user.getAttribute("cd_docente"), "13");
908 
909                 assertNotNull(identityManagerImpl.getGroup("alunos"));
910                 assertNotNull(identityManagerImpl.getGroup("candidatos"));
911             }
912             catch (IdentityManagerException e)
913             {
914                 e.printStackTrace();
915                 assertFalse(true);
916             }
917         }
918 
919     }
920 
921     /**
922      * Not available test condition
923      * 
924      * @throws IdentityManagerException
925      */
926     public void testNotAvailable() throws IdentityManagerException
927     {
928         assertNull(identityManagerImpl.getGroup(N_A));
929     }
930 
931     /**
932      * Tests the identity manager's relation operations.
933      * 
934      * @throws IdentityManagerException
935      *             if the operation can't be executed
936      * @throws InternalFrameworkException
937      */
938     public void testRelationOperations() throws IdentityManagerException, InternalFrameworkException
939     {
940 
941         // Test settings
942         createUser();
943         createGroup(PARENT_GROUP_ID, GRAND_PARENT_GROUP_ID);
944         createGroup(NEW_GROUP_NAME, PARENT_GROUP_ID);
945 
946         // Add user to group
947         identityManagerImpl.addUserToGroup(NEW_USER, NEW_GROUP_NAME);
948         assertTrue(identityManagerImpl.isUserInGroup(NEW_USER, NEW_GROUP_NAME));
949         assertFalse(identityManagerImpl.isUserInGroup(NEW_USER, PARENT_GROUP_ID));
950         assertFalse(identityManagerImpl.isUserInGroup(NEW_USER, GRAND_PARENT_GROUP_ID));
951         assertFalse(identityManagerImpl.isUserInGroup(NEW_USER, "InexistentGroupMustReturnFalse"));
952 
953         assertTrue(identityManagerImpl.isUserInGroup(NEW_USER, NEW_GROUP_NAME, true));
954         assertTrue(identityManagerImpl.isUserInGroup(NEW_USER, PARENT_GROUP_ID, true));
955         assertTrue(identityManagerImpl.isUserInGroup(NEW_USER, GRAND_PARENT_GROUP_ID, true));
956         assertFalse(identityManagerImpl.isUserInGroup(NEW_USER, "InexistentGroupMustReturnFalse", true));
957 
958         assertTrue(identityManagerImpl.isUserInGroup(NEW_USER, NEW_GROUP_NAME, false));
959         assertFalse(identityManagerImpl.isUserInGroup(NEW_USER, PARENT_GROUP_ID, false));
960         assertFalse(identityManagerImpl.isUserInGroup(NEW_USER, GRAND_PARENT_GROUP_ID, false));
961         assertFalse(identityManagerImpl.isUserInGroup(NEW_USER, "InexistentGroupMustReturnFalse", false));
962 
963         // Verify user is in group and group contains user
964         assertTrue(identityManagerImpl.getGroupUsers(NEW_GROUP_NAME).keySet().contains(NEW_USER));
965         assertTrue(identityManagerImpl.getUserIDsInGroup(NEW_GROUP_NAME).contains(NEW_USER));
966         assertTrue(identityManagerImpl.getUserGroups(NEW_USER).keySet().contains(NEW_GROUP_NAME));
967         assertTrue(identityManagerImpl.getUserGroupsIDs(NEW_USER).contains(NEW_GROUP_NAME));
968         assertFalse(identityManagerImpl.getUserGroups(NEW_USER).keySet().contains(PARENT_GROUP_ID));
969         assertFalse(identityManagerImpl.getUserGroupsIDs(NEW_USER).contains(PARENT_GROUP_ID));
970         assertFalse(identityManagerImpl.getUserGroups(NEW_USER).keySet().contains(GRAND_PARENT_GROUP_ID));
971         assertFalse(identityManagerImpl.getUserGroupsIDs(NEW_USER).contains(GRAND_PARENT_GROUP_ID));
972 
973         assertTrue(identityManagerImpl.getUserGroups(NEW_USER, true).keySet().contains(PARENT_GROUP_ID));
974         assertTrue(identityManagerImpl.getUserGroupsIDs(NEW_USER, true).contains(PARENT_GROUP_ID));
975         assertTrue(identityManagerImpl.getUserGroups(NEW_USER, true).keySet().contains(GRAND_PARENT_GROUP_ID));
976         assertTrue(identityManagerImpl.getUserGroupsIDs(NEW_USER, true).contains(GRAND_PARENT_GROUP_ID));
977 
978         // Remove user from group
979         identityManagerImpl.removeUserFromGroup(NEW_USER, NEW_GROUP_NAME);
980         assertFalse(identityManagerImpl.isUserInGroup(NEW_USER, NEW_GROUP_NAME));
981 
982         // Clean test settings
983         removeUser(NEW_USER);
984         removeGroup();
985     }
986 
987     /**
988      * Tests the finders by attributes
989      * 
990      * @throws InternalFrameworkException
991      * @throws IdentityManagerException
992      */
993     public void testUserAttributes() throws IdentityManagerException, InternalFrameworkException
994     {
995         Map<String, String> attribToSearch = new HashMap<String, String>();
996 
997         createUser();
998 
999         attribToSearch.put(identityManagerImpl.getMailAttributeName(), USER_MAIL);
1000         attribToSearch.put(identityManagerImpl.getUserLoginAttributeName(), NEW_USER);
1001         attribToSearch.put(identityManagerImpl.getNameAttributeName(), NEW_USER);
1002         attribToSearch.put(identityManagerImpl.getUserParentGroupAttributeName(), MAIN_GROUP_NAME);
1003         attribToSearch.put(ATTRIBUTE_KEY, ATTRIBUTE_VALUE);
1004         assertEquals(identityManagerImpl.getUsersByAttributes(attribToSearch).size(), 1);
1005         attribToSearch.clear();
1006 
1007         attribToSearch.put(ATTRIBUTE_KEY, ATTRIBUTE_VALUE);
1008         int usersWithAttrib = identityManagerImpl.countUsers(attribToSearch);
1009 
1010         createUser(TEST_USER_1_ID);
1011         createUser(TEST_USER_2_ID);
1012         createUser(TEST_USER_3_ID);
1013 
1014         usersWithAttrib += 3;
1015 
1016         assertEquals(identityManagerImpl.countUsers(attribToSearch), usersWithAttrib);
1017         assertEquals(identityManagerImpl.getUsersByAttribute(ATTRIBUTE_KEY, ATTRIBUTE_VALUE).size(), usersWithAttrib);
1018         assertEquals(identityManagerImpl.getUsersByAttributes(attribToSearch).size(), usersWithAttrib);
1019 
1020         Pagination page = new Pagination(1, 4);
1021         // TODO: Waiting for Pagination bug resolution
1022         // assertEquals(identityManagerImpl.getUsersByAttributes(attribToSearch, page).size(), 4);
1023 
1024         page = new Pagination(1, 2);
1025         assertEquals(identityManagerImpl.getUsersByAttributes(attribToSearch, page).size(), 2);
1026 
1027         page = new Pagination(2, 2);
1028         // TODO: Waiting for Pagination bug resolution
1029         // assertEquals(identityManagerImpl.getUsersByAttributes(attribToSearch, page).size(), 2);
1030 
1031         // Test find user by email
1032         boolean userFound = false;
1033 
1034         for (IDIFUser difUser: identityManagerImpl.getUsersByEmail(USER_MAIL))
1035             if (difUser.getID().equals(NEW_USER) && difUser.getEmail().equals(USER_MAIL))
1036             {
1037                 userFound = true;
1038                 break;
1039             }
1040 
1041         assertTrue(userFound);
1042 
1043         // Test find user by attribute (key)
1044         userFound = false;
1045 
1046         for (IDIFUser difUser: identityManagerImpl.getUsersByAttribute(ATTRIBUTE_KEY, ATTRIBUTE_VALUE))
1047             if (difUser.getID().equals(NEW_USER) && difUser.getAttribute(ATTRIBUTE_KEY).equals(ATTRIBUTE_VALUE))
1048             {
1049                 userFound = true;
1050                 break;
1051             }
1052         assertTrue(userFound);
1053 
1054         // Test find user by several attributes
1055 
1056         removeUser(NEW_USER);
1057     }
1058 
1059     /**
1060      * Tests the user operations.
1061      * 
1062      * @throws IdentityManagerException
1063      *             if the attribute values can't be changed
1064      * @throws InternalFrameworkException
1065      */
1066     @SuppressWarnings("serial")
1067     public void testUserOperations() throws IdentityManagerException, InternalFrameworkException
1068     {
1069         String USER_DEFAULT_PASSWORD = "userDefaultPassword";
1070 
1071         createUser(USER_DEFAULT_PASSWORD);
1072         assertFalse(identityManagerImpl.isIdentityValid(USER_DEFAULT_PASSWORD, "password"));
1073         removeUser(USER_DEFAULT_PASSWORD);
1074 
1075         createUser();
1076 
1077         // User identity MUST be valid! (This is the correct way to validate the password).
1078         assertTrue(identityManagerImpl.isIdentityValid(NEW_USER, USER_PASS));
1079         // Test user integrity
1080         IDIFUser retrievedUser = identityManagerImpl.getUser(NEW_USER);
1081 
1082         /* Test user data integrity */
1083         assertEquals(retrievedUser.getID(), NEW_USER);
1084         assertEquals(retrievedUser.getName(), NEW_USER);
1085         assertEquals(retrievedUser.getNick(), NEW_USER);
1086         assertEquals(retrievedUser.getEmail(), USER_MAIL);
1087         assertEquals(retrievedUser.getProfileID(), MAIN_GROUP_NAME);
1088 
1089         try
1090         {
1091             retrievedUser.setAttribute(AN_ATTRIBUTE_NAME, AN_ATTRIBUTE_VALUE);
1092         }
1093         catch (InternalFrameworkException internalFrameworkException)
1094         {
1095             internalFrameworkException.printStackTrace();
1096         }
1097 
1098         retrievedUser = identityManagerImpl.getUser(retrievedUser.getID());
1099         assertEquals(retrievedUser.getAttribute(AN_ATTRIBUTE_NAME), AN_ATTRIBUTE_VALUE);
1100         assertEquals(retrievedUser.getAttributes().get(AN_ATTRIBUTE_NAME), AN_ATTRIBUTE_VALUE);
1101         assertEquals(retrievedUser.getAttribute(AN_ATTRIBUTE_NAME.toUpperCase()), AN_ATTRIBUTE_VALUE);
1102         assertEquals(retrievedUser.getAttributes().get(AN_ATTRIBUTE_NAME.toUpperCase()), AN_ATTRIBUTE_VALUE);
1103         assertEquals(retrievedUser.getAttribute(AN_ATTRIBUTE_NAME.toLowerCase()), AN_ATTRIBUTE_VALUE);
1104         assertEquals(retrievedUser.getAttributes().get(AN_ATTRIBUTE_NAME.toLowerCase()), AN_ATTRIBUTE_VALUE);
1105 
1106         try
1107         {
1108             retrievedUser.setAttribute(ANOTHER_ATTRIBUTE_NAME, ANOTHER_ATTRIBUTE_VALUE);
1109         }
1110         catch (InternalFrameworkException internalFrameworkException)
1111         {
1112             internalFrameworkException.printStackTrace();
1113         }
1114 
1115         retrievedUser = identityManagerImpl.getUser(retrievedUser.getID());
1116         assertEquals(retrievedUser.getAttribute(ANOTHER_ATTRIBUTE_NAME), ANOTHER_ATTRIBUTE_VALUE);
1117         assertEquals(retrievedUser.getAttributes().get(ANOTHER_ATTRIBUTE_NAME), ANOTHER_ATTRIBUTE_VALUE);
1118         assertEquals(retrievedUser.getAttribute(ANOTHER_ATTRIBUTE_NAME.toUpperCase()), ANOTHER_ATTRIBUTE_VALUE);
1119         assertEquals(retrievedUser.getAttributes().get(ANOTHER_ATTRIBUTE_NAME.toUpperCase()), ANOTHER_ATTRIBUTE_VALUE);
1120         assertEquals(retrievedUser.getAttribute(ANOTHER_ATTRIBUTE_NAME.toLowerCase()), ANOTHER_ATTRIBUTE_VALUE);
1121         assertEquals(retrievedUser.getAttributes().get(ANOTHER_ATTRIBUTE_NAME.toLowerCase()), ANOTHER_ATTRIBUTE_VALUE);
1122 
1123         try
1124         {
1125             retrievedUser.setAttribute(A_BULK_ATTRIBUTE_NAME, A_BULK_ATTRIBUTE_VALUE);
1126         }
1127         catch (InternalFrameworkException internalFrameworkException)
1128         {
1129             internalFrameworkException.printStackTrace();
1130         }
1131 
1132         retrievedUser = identityManagerImpl.getUser(retrievedUser.getID());
1133         assertEquals(retrievedUser.getAttribute(A_BULK_ATTRIBUTE_NAME), A_BULK_ATTRIBUTE_VALUE);
1134         assertEquals(retrievedUser.getAttributes().get(A_BULK_ATTRIBUTE_NAME), A_BULK_ATTRIBUTE_VALUE);
1135         assertEquals(retrievedUser.getAttribute(A_BULK_ATTRIBUTE_NAME.toUpperCase()), A_BULK_ATTRIBUTE_VALUE);
1136         assertEquals(retrievedUser.getAttributes().get(A_BULK_ATTRIBUTE_NAME.toUpperCase()), A_BULK_ATTRIBUTE_VALUE);
1137         assertEquals(retrievedUser.getAttribute(A_BULK_ATTRIBUTE_NAME.toLowerCase()), A_BULK_ATTRIBUTE_VALUE);
1138         assertEquals(retrievedUser.getAttributes().get(A_BULK_ATTRIBUTE_NAME.toLowerCase()), A_BULK_ATTRIBUTE_VALUE);
1139 
1140         try
1141         {
1142             retrievedUser.setAttribute(ANOTHER_BULK_ATTRIBUTE_NAME, ANOTHER_BULK_ATTRIBUTE_VALUE);
1143         }
1144         catch (InternalFrameworkException internalFrameworkException)
1145         {
1146             internalFrameworkException.printStackTrace();
1147         }
1148 
1149         retrievedUser = identityManagerImpl.getUser(retrievedUser.getID());
1150         assertEquals(retrievedUser.getAttribute(ANOTHER_BULK_ATTRIBUTE_NAME), ANOTHER_BULK_ATTRIBUTE_VALUE);
1151         assertEquals(retrievedUser.getAttributes().get(ANOTHER_BULK_ATTRIBUTE_NAME), ANOTHER_BULK_ATTRIBUTE_VALUE);
1152 
1153         /* Removes the attributes one by one */
1154         retrievedUser.removeAttribute(AN_ATTRIBUTE_NAME);
1155         identityManagerImpl.updateUser(retrievedUser, retrievedUser.getID());
1156         retrievedUser = identityManagerImpl.getUser(retrievedUser.getID());
1157         assertNull(retrievedUser.getAttribute(AN_ATTRIBUTE_NAME));
1158         assertEquals(retrievedUser.getAttribute(ANOTHER_ATTRIBUTE_NAME), ANOTHER_ATTRIBUTE_VALUE);
1159         assertEquals(retrievedUser.getAttribute(A_BULK_ATTRIBUTE_NAME), A_BULK_ATTRIBUTE_VALUE);
1160         assertEquals(retrievedUser.getAttribute(ANOTHER_BULK_ATTRIBUTE_NAME), ANOTHER_BULK_ATTRIBUTE_VALUE);
1161 
1162         retrievedUser.removeAttribute(ANOTHER_ATTRIBUTE_NAME);
1163         identityManagerImpl.updateUser(retrievedUser, retrievedUser.getID());
1164         retrievedUser = identityManagerImpl.getUser(retrievedUser.getID());
1165         assertNull(retrievedUser.getAttribute(AN_ATTRIBUTE_NAME));
1166         assertNull(retrievedUser.getAttribute(ANOTHER_ATTRIBUTE_NAME));
1167         assertEquals(retrievedUser.getAttribute(A_BULK_ATTRIBUTE_NAME), A_BULK_ATTRIBUTE_VALUE);
1168         assertEquals(retrievedUser.getAttribute(ANOTHER_BULK_ATTRIBUTE_NAME), ANOTHER_BULK_ATTRIBUTE_VALUE);
1169 
1170         retrievedUser.removeAttribute(A_BULK_ATTRIBUTE_NAME);
1171         identityManagerImpl.updateUser(retrievedUser, retrievedUser.getID());
1172         retrievedUser = identityManagerImpl.getUser(retrievedUser.getID());
1173         assertNull(retrievedUser.getAttribute(AN_ATTRIBUTE_NAME));
1174         assertNull(retrievedUser.getAttribute(ANOTHER_ATTRIBUTE_NAME));
1175         assertNull(retrievedUser.getAttribute(A_BULK_ATTRIBUTE_NAME));
1176         assertEquals(retrievedUser.getAttribute(ANOTHER_BULK_ATTRIBUTE_NAME), ANOTHER_BULK_ATTRIBUTE_VALUE);
1177 
1178         retrievedUser.removeAttribute(ANOTHER_BULK_ATTRIBUTE_NAME);
1179         identityManagerImpl.updateUser(retrievedUser, retrievedUser.getID());
1180         retrievedUser = identityManagerImpl.getUser(retrievedUser.getID());
1181         assertNull(retrievedUser.getAttribute(AN_ATTRIBUTE_NAME));
1182         assertNull(retrievedUser.getAttribute(ANOTHER_ATTRIBUTE_NAME));
1183         assertNull(retrievedUser.getAttribute(A_BULK_ATTRIBUTE_NAME));
1184         assertNull(retrievedUser.getAttribute(ANOTHER_BULK_ATTRIBUTE_NAME));
1185 
1186         /*
1187          * Method 'getPassword()' is undefined for type IDIFUser. Commented for reference.
1188          * assertEquals(user.getPassword(),"passwd");
1189          */
1190 
1191         if (identityManagerImpl.userExists(CHANGED_USER))
1192         {
1193             identityManagerImpl.removeUser(CHANGED_USER);
1194         }
1195         assertFalse(identityManagerImpl.userExists(CHANGED_USER));
1196 
1197         createGroup(CHANGED_PARENT_GROUP_NAME);
1198 
1199         // Set user data for modification- change the user id
1200         IDIFUser user = new DIFUserImpl();
1201         user.setID(CHANGED_USER);
1202         user.setName(CHANGED_USER);
1203         user.setNick(CHANGED_USER);
1204         user.setEmail(CHANGED_MAIL);
1205         user.setPassword(CHANGED_PASS);
1206         user.setProfileID(CHANGED_PARENT_GROUP_NAME);
1207 
1208         // Modify user data
1209         identityManagerImpl.updateUser(user, NEW_USER);
1210         assertTrue(identityManagerImpl.userExists(CHANGED_USER));
1211 
1212         // Update attributes AFTER changing user data (setting attributes requires that the user exists on the
1213         // server).
1214         try
1215         {
1216             user.setAttribute(ATTRIBUTE_KEY, ATTRIBUTE_VALUE);
1217             user.setAttribute(A_BULK_ATTRIBUTE_NAME, A_CHANGED_BULK_ATTRIBUTE_VALUE);
1218             user.setAttribute(ANOTHER_BULK_ATTRIBUTE_NAME, ANOTHER_CHANGED_BULK_PARAMETER_VALUE);
1219             user.setAttribute(AN_ATTRIBUTE_NAME, A_CHANGED_ATTRIBUTE_VALUE);
1220             user.setAttribute(ANOTHER_ATTRIBUTE_NAME, ANOTHER_CHANGED_ATTRIBUTE_VALUE);
1221         }
1222         catch (InternalFrameworkException internalFrameworkException)
1223         {
1224             throw new IdentityManagerException("Could not set attribute!!", internalFrameworkException);
1225         }
1226 
1227         // Force attribute if branch on convertFromDIFUserToXUser
1228         identityManagerImpl.updateUser(user, CHANGED_USER);
1229         // Test user integrity
1230         assertTrue(identityManagerImpl.isIdentityValid(CHANGED_USER, CHANGED_PASS));
1231 
1232         retrievedUser = identityManagerImpl.getUser(CHANGED_USER);
1233         assertEquals(retrievedUser.getID(), CHANGED_USER);
1234         assertEquals(retrievedUser.getName(), CHANGED_USER);
1235         assertEquals(retrievedUser.getNick(), CHANGED_USER);
1236         assertEquals(retrievedUser.getEmail(), CHANGED_MAIL);
1237         assertEquals(retrievedUser.getProfile().getID(), CHANGED_PARENT_GROUP_NAME);
1238 
1239         /* Attributes */
1240         assertEquals(retrievedUser.getAttributes().get(A_BULK_ATTRIBUTE_NAME), A_CHANGED_BULK_ATTRIBUTE_VALUE);
1241         assertEquals(retrievedUser.getAttributes().get(ANOTHER_BULK_ATTRIBUTE_NAME),
1242                 ANOTHER_CHANGED_BULK_PARAMETER_VALUE);
1243         assertEquals(retrievedUser.getAttributes().get(AN_ATTRIBUTE_NAME), A_CHANGED_ATTRIBUTE_VALUE);
1244         assertEquals(retrievedUser.getAttributes().get(ANOTHER_ATTRIBUTE_NAME), ANOTHER_CHANGED_ATTRIBUTE_VALUE);
1245 
1246         // Change attributes (restore old values)
1247         identityManagerImpl.updateUserAttribute(CHANGED_USER, AN_ATTRIBUTE_NAME, AN_ATTRIBUTE_VALUE);
1248         identityManagerImpl.updateUserAttribute(CHANGED_USER, ANOTHER_ATTRIBUTE_NAME, ANOTHER_ATTRIBUTE_VALUE);
1249         identityManagerImpl.updateUserAttribute(CHANGED_USER, A_BULK_ATTRIBUTE_NAME, A_BULK_ATTRIBUTE_VALUE);
1250         identityManagerImpl
1251                 .updateUserAttribute(CHANGED_USER, ANOTHER_BULK_ATTRIBUTE_NAME, ANOTHER_BULK_ATTRIBUTE_VALUE);
1252 
1253         retrievedUser = identityManagerImpl.getUser(CHANGED_USER);
1254         assertEquals(retrievedUser.getAttributes().get(A_BULK_ATTRIBUTE_NAME), A_BULK_ATTRIBUTE_VALUE);
1255         assertEquals(retrievedUser.getAttributes().get(ANOTHER_BULK_ATTRIBUTE_NAME), ANOTHER_BULK_ATTRIBUTE_VALUE);
1256         assertEquals(retrievedUser.getAttributes().get(AN_ATTRIBUTE_NAME), AN_ATTRIBUTE_VALUE);
1257         assertEquals(retrievedUser.getAttributes().get(ANOTHER_ATTRIBUTE_NAME), ANOTHER_ATTRIBUTE_VALUE);
1258 
1259         /* Removes the attributes one by one */
1260         retrievedUser.removeAttribute(AN_ATTRIBUTE_NAME);
1261         identityManagerImpl.updateUser(retrievedUser, retrievedUser.getID());
1262         retrievedUser = identityManagerImpl.getUser(retrievedUser.getID());
1263         assertNull(retrievedUser.getAttribute(AN_ATTRIBUTE_NAME));
1264         assertEquals(retrievedUser.getAttribute(ANOTHER_ATTRIBUTE_NAME), ANOTHER_ATTRIBUTE_VALUE);
1265         assertEquals(retrievedUser.getAttribute(A_BULK_ATTRIBUTE_NAME), A_BULK_ATTRIBUTE_VALUE);
1266         assertEquals(retrievedUser.getAttribute(ANOTHER_BULK_ATTRIBUTE_NAME), ANOTHER_BULK_ATTRIBUTE_VALUE);
1267 
1268         retrievedUser.removeAttribute(ANOTHER_ATTRIBUTE_NAME);
1269         identityManagerImpl.updateUser(retrievedUser, retrievedUser.getID());
1270         retrievedUser = identityManagerImpl.getUser(retrievedUser.getID());
1271         assertNull(retrievedUser.getAttribute(AN_ATTRIBUTE_NAME));
1272         assertNull(retrievedUser.getAttribute(ANOTHER_ATTRIBUTE_NAME));
1273         assertEquals(retrievedUser.getAttribute(A_BULK_ATTRIBUTE_NAME), A_BULK_ATTRIBUTE_VALUE);
1274         assertEquals(retrievedUser.getAttribute(ANOTHER_BULK_ATTRIBUTE_NAME), ANOTHER_BULK_ATTRIBUTE_VALUE);
1275 
1276         retrievedUser.removeAttribute(A_BULK_ATTRIBUTE_NAME);
1277         identityManagerImpl.updateUser(retrievedUser, retrievedUser.getID());
1278         retrievedUser = identityManagerImpl.getUser(retrievedUser.getID());
1279         assertNull(retrievedUser.getAttribute(AN_ATTRIBUTE_NAME));
1280         assertNull(retrievedUser.getAttribute(ANOTHER_ATTRIBUTE_NAME));
1281         assertNull(retrievedUser.getAttribute(A_BULK_ATTRIBUTE_NAME));
1282         assertEquals(retrievedUser.getAttribute(ANOTHER_BULK_ATTRIBUTE_NAME), ANOTHER_BULK_ATTRIBUTE_VALUE);
1283 
1284         retrievedUser.removeAttribute(ANOTHER_BULK_ATTRIBUTE_NAME);
1285         identityManagerImpl.updateUser(retrievedUser, retrievedUser.getID());
1286         retrievedUser = identityManagerImpl.getUser(retrievedUser.getID());
1287         assertNull(retrievedUser.getAttribute(AN_ATTRIBUTE_NAME));
1288         assertNull(retrievedUser.getAttribute(ANOTHER_ATTRIBUTE_NAME));
1289         assertNull(retrievedUser.getAttribute(A_BULK_ATTRIBUTE_NAME));
1290         assertNull(retrievedUser.getAttribute(ANOTHER_BULK_ATTRIBUTE_NAME));
1291 
1292         // Mass attribute change
1293         Map<String, Object> massAttributes = new HashMap<String, Object>() {
1294 
1295             {
1296                 put(A_BULK_ATTRIBUTE_NAME, A_CHANGED_BULK_ATTRIBUTE_VALUE);
1297                 put(ANOTHER_BULK_ATTRIBUTE_NAME, ANOTHER_CHANGED_BULK_PARAMETER_VALUE);
1298                 put(AN_ATTRIBUTE_NAME, A_CHANGED_ATTRIBUTE_VALUE);
1299                 put(ANOTHER_ATTRIBUTE_NAME, ANOTHER_CHANGED_ATTRIBUTE_VALUE);
1300             }
1301         };
1302 
1303         try
1304         {
1305             identityManagerImpl.updateUserAttributes("InexistentUserMustReturnExpcetion", massAttributes);
1306             assertFalse(true);
1307         }
1308         catch (IdentityManagerException e)
1309         {
1310             assertTrue(true);
1311         }
1312         identityManagerImpl.updateUserAttributes(CHANGED_USER, massAttributes);
1313 
1314         retrievedUser = identityManagerImpl.getUser(CHANGED_USER);
1315         assertEquals(retrievedUser.getAttributes().get(A_BULK_ATTRIBUTE_NAME), A_CHANGED_BULK_ATTRIBUTE_VALUE);
1316         assertEquals(retrievedUser.getAttributes().get(ANOTHER_BULK_ATTRIBUTE_NAME),
1317                 ANOTHER_CHANGED_BULK_PARAMETER_VALUE);
1318         assertEquals(retrievedUser.getAttributes().get(AN_ATTRIBUTE_NAME), A_CHANGED_ATTRIBUTE_VALUE);
1319         assertEquals(retrievedUser.getAttributes().get(ANOTHER_ATTRIBUTE_NAME), ANOTHER_CHANGED_ATTRIBUTE_VALUE);
1320 
1321         // Test password changing
1322         assertFalse(identityManagerImpl.isIdentityValid(CHANGED_USER, "WrongPassword"));
1323         identityManagerImpl.changePassword(CHANGED_USER, USER_PASS);
1324         assertTrue(identityManagerImpl.isIdentityValid(CHANGED_USER, USER_PASS));
1325 
1326         removeUser(CHANGED_USER);
1327     }
1328 
1329     /**
1330      * Tests the user operations forcing failure behavior.
1331      * 
1332      * @throws IdentityManagerException
1333      *             if the identity manager can't be accessed
1334      */
1335     @SuppressWarnings("unused")
1336     public void testUserOperationsInFailureBehaviour() throws IdentityManagerException
1337     {
1338         // Create user for addition
1339         IDIFUser transferUser = new DIFUserImpl();
1340 
1341         // Tries to add an user without id...must fail!!
1342         try
1343         {
1344             identityManagerImpl.addUser(transferUser);
1345         }
1346         catch (IdentityManagerException e)
1347         {
1348             // User must not have been added...
1349             assertFalse(identityManagerImpl.userExists(""));
1350         }
1351 
1352         try
1353         {
1354             assertFalse(identityManagerImpl.isIdentityValid(BAD_USER, BAD_PASS));
1355         }
1356         catch (IdentityManagerException e)
1357         {
1358 
1359         }
1360 
1361         // Try to fetch data for an inexistent user
1362         try
1363         {
1364             IDIFUser retrievedUser = identityManagerImpl.getUser(BAD_USER);
1365         }
1366         catch (IdentityManagerException identityManagerException)
1367         {
1368         }
1369 
1370         // Tries to modify a bad user...must fail!!
1371         try
1372         {
1373             identityManagerImpl.updateUser(transferUser, BAD_USER);
1374         }
1375         catch (IdentityManagerException e)
1376         {
1377             // User must not have been added...
1378             assertFalse(identityManagerImpl.userExists(BAD_USER));
1379         }
1380 
1381         // Tries to remove a bad user...must fail!!
1382         try
1383         {
1384             identityManagerImpl.removeUser(CHANGED_USER);
1385         }
1386         catch (IdentityManagerException e)
1387         {
1388             // User must not exist
1389             assertFalse(identityManagerImpl.userExists(NEW_USER));
1390         }
1391     }
1392 }