KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > organization > ldap > test > TestLDAPOrganizationService


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.organization.ldap.test;
6
7 import java.util.Collection JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import net.sf.hibernate.Transaction;
10 import org.exoplatform.commons.utils.PageList;
11 import org.exoplatform.container.PortalContainer;
12 import org.exoplatform.services.database.HibernateService;
13 import org.exoplatform.services.ldap.LDAPService;
14 import org.exoplatform.services.ldap.LDAPServiceContainer;
15 import org.exoplatform.services.log.LogService;
16 import org.exoplatform.services.log.LogUtil;
17 import org.exoplatform.services.organization.*;
18 import org.exoplatform.services.organization.impl.GroupImpl;
19 import org.exoplatform.test.BasicTestCase;
20 /**
21  * Created by the eXo platform team
22  * User: Daniel Summer
23  * Date: 28 august 2004
24  *
25  */

26 public class TestLDAPOrganizationService extends BasicTestCase {
27
28     private static OrganizationService service;
29     private static HibernateService hservice_;
30     private static LDAPServiceContainer serviceContainer;
31     private static LDAPService lservice_;
32
33     public TestLDAPOrganizationService(String JavaDoc s) {
34         super(s);
35     }
36
37     protected String JavaDoc getDescription() {
38         return "test ldap organization service";
39     }
40
41     public void setUp() throws Exception JavaDoc {
42         if (service == null) {
43             PortalContainer manager = PortalContainer.getInstance();
44             service =
45                 (OrganizationService) manager.getComponentInstanceOfType(
46                     OrganizationService.class);
47             LogUtil.setLevel("org.exoplatform.services.organization", LogService.DEBUG,true);
48             LogUtil.setLevel("org.exoplatform.services.database", LogService.DEBUG, true);
49             hservice_ =
50                 (HibernateService) manager.getComponentInstanceOfType(
51                     HibernateService.class);
52             serviceContainer =
53                 (LDAPServiceContainer) manager.getComponentInstanceOfType(
54                     LDAPServiceContainer.class);
55             lservice_ = serviceContainer.createLDAPService();
56
57         }
58     }
59
60     public void tearDown() throws Exception JavaDoc {
61
62         hservice_.closeSession();
63         serviceContainer.closeLDAPService(lservice_);
64
65     }
66
67     public void testUser() throws Exception JavaDoc {
68         net.sf.hibernate.Session session = hservice_.openSession();
69         String JavaDoc USER = "test";
70         Transaction tx = session.beginTransaction();
71         User user = service.createUserInstance();
72         user.setUserName(USER);
73         user.setPassword("exo");
74         user.setFirstName("Exo");
75         user.setLastName("Platform");
76         user.setEmail("exo@exoportal.org");
77         service.createUser(user);
78         tx.commit();
79         User u = service.findUserByName(USER);
80         assertEquals(u.getFirstName(), "Exo");
81         UserProfile up = service.findUserProfileByName(USER);
82         assertTrue("Expect user profile is found: ", up != null);
83
84         Query query = new Query();
85         PageList users = service.findUsers(query);
86         assertTrue("Expect 1 user found ", users.getAvailable() >= 1);
87
88         u.setFirstName("Exo(Update)");
89         service.saveUser(u);
90         up.getUserInfoMap().put("user.gender", "male");
91         service.saveUserProfile(up);
92         up = service.findUserProfileByName(USER);
93         String JavaDoc male = (String JavaDoc) up.getUserInfoMap().get("user.gender");
94         assertEquals("Expect user profile is updated: ", "male", male);
95
96         PageList piterator = service.getUserPageList(10);
97         assertTrue(piterator.currentPage().size() > 1);
98
99         service.removeUser(USER);
100     }
101
102     public void testGroup() throws Exception JavaDoc {
103         Group groupParent = service.createGroupInstance();
104         groupParent.setGroupName("GroupParent");
105         service.createGroup(groupParent);
106         assertTrue(((GroupImpl) groupParent).getId() != null);
107
108         Group groupChild = service.createGroupInstance();
109         groupChild.setGroupName("GroupChild");
110
111         service.addChild(groupParent, groupChild);
112
113         groupParent = service.findGroupById(groupParent.getId());
114         assertEquals(groupParent.getGroupName(), "GroupParent");
115
116         groupChild = service.findGroupById(groupChild.getId());
117         assertEquals(groupChild.getParentId(), groupParent.getId());
118
119         groupChild.setLabel("GroupRenamed");
120         service.saveGroup(groupChild);
121         assertEquals(
122             service.findGroupById(groupChild.getId()).getLabel(),
123             "GroupRenamed");
124
125         service.removeGroup(groupChild);
126         assertNotNull(service.findGroupById(groupParent.getId()));
127
128         service.addChild(groupParent, groupChild);
129         Collection JavaDoc groups = service.findGroups(groupParent);
130         assertEquals(1, groups.size());
131
132         service.removeGroup(groupParent);
133         assertNull(service.findGroupById(groupParent.getId()));
134         assertNull(service.findGroupById(groupChild.getId()));
135     }
136
137     public void testMembership() throws Exception JavaDoc {
138         MembershipType mt = service.createMembershipTypeInstance();
139         mt.setName("Hierarchical");
140         mt.setDescription("This is a test");
141         mt.setOwner("exo");
142         service.createMembershipType(mt);
143         mt = service.findMembershipType("Hierarchical");
144         assertTrue("Membership is found", mt != null);
145         mt.setDescription("This is a test (update)");
146         service.saveMembershipType(mt);
147         mt = service.findMembershipType("Hierarchical");
148         assertTrue("Membership is found", mt != null);
149
150         User user = service.createUserInstance();
151         user.setUserName("benj");
152         user.setPassword("benj");
153         user.setFirstName("benjamin");
154         user.setLastName("mestrallet");
155         user.setEmail("benjmestrallet@users.sourceforge.net");
156         service.createUser(user);
157
158         Group group = service.createGroupInstance();
159         group.setGroupName("Group");
160         service.createGroup(group);
161
162         Membership membership = service.createMembershipInstance();
163         membership.setMembershipType("Hierarchical");
164         //service.createMembership(membership);
165
//assertTrue(((MembershipImpl) membership).getId() != null);
166
service.linkMembership("benj", group, membership);
167         Collection JavaDoc groups =
168             service.findGroupByMembership("benj", "Hierarchical");
169         assertEquals("expect 1 group ", 1, groups.size());
170
171         Collection JavaDoc c =
172             service.findMembershipsByUserAndGroup("benj", group.getId());
173         assertTrue(c.size() > 0);
174
175         mt = service.removeMembershipType("Hierarchical");
176         mt = service.findMembershipType("Hierarchical");
177         assertTrue("Membership is not found", mt == null);
178         //test cascade delete
179
Collection JavaDoc memberships =
180             service.findMembershipsByUserAndGroup("benj", group.getId());
181         assertTrue(memberships.size() == 0);
182
183         membership = service.createMembershipInstance();
184         membership.setMembershipType("Hierarchical");
185         //service.createMembership(membership);
186
service.linkMembership("benj", group, membership);
187
188         groups = service.findGroupsOfUser("benj");
189         assertEquals("expect 2 group: ", 2, groups.size());
190         groups = service.findGroupByMembership("benj", "Hierarchical");
191         Iterator JavaDoc i = groups.iterator();
192         while (i.hasNext()) {
193             Group g = (Group) i.next();
194             System.out.println("-----------------------" + group.getId());
195         }
196         assertEquals("expect one group: ", 1, groups.size());
197         group = (Group) groups.iterator().next();
198         assertEquals(
199             "find group with group name 'Group'",
200             "Group",
201             group.getGroupName());
202
203         service.removeGroup(group);
204         memberships = service.findMembershipsByUserAndGroup("benj", "Group");
205         assertTrue(memberships.size() == 0);
206         //need to delete the roles belong to the user as well
207
service.removeUser("benj");
208         memberships = service.findMembershipsByUser("benj");
209         assertEquals("Check system role: ", 0, memberships.size());
210     }
211 }
Popular Tags