View Javadoc
1   /*
2    * Copyright (C) 2003-2011 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.social.core.space.spi;
18  
19  import java.io.InputStream;
20  import java.util.*;
21  import java.util.regex.Pattern;
22  
23  import org.apache.commons.lang.ArrayUtils;
24  import org.apache.commons.lang.StringEscapeUtils;
25  
26  import org.exoplatform.commons.utils.ListAccess;
27  import org.exoplatform.portal.mop.navigation.NodeContext;
28  import org.exoplatform.services.log.ExoLogger;
29  import org.exoplatform.portal.mop.navigation.NavigationContext;
30  import org.exoplatform.services.log.Log;
31  import org.exoplatform.services.organization.Group;
32  import org.exoplatform.services.organization.MembershipType;
33  import org.exoplatform.services.organization.OrganizationService;
34  import org.exoplatform.services.organization.User;
35  import org.exoplatform.services.security.MembershipEntry;
36  import org.exoplatform.social.core.activity.model.ExoSocialActivity;
37  import org.exoplatform.social.core.identity.model.Identity;
38  import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
39  import org.exoplatform.social.core.identity.provider.SpaceIdentityProvider;
40  import org.exoplatform.social.core.manager.ActivityManager;
41  import org.exoplatform.social.core.manager.IdentityManager;
42  import org.exoplatform.social.core.model.AvatarAttachment;
43  import org.exoplatform.social.core.service.LinkProvider;
44  import org.exoplatform.social.core.space.*;
45  import org.exoplatform.social.core.space.impl.DefaultSpaceApplicationHandler;
46  import org.exoplatform.social.core.space.model.Space;
47  import org.exoplatform.social.core.storage.IdentityStorageException;
48  import org.exoplatform.social.core.storage.api.IdentityStorage;
49  import org.exoplatform.social.core.storage.impl.StorageUtils;
50  import org.exoplatform.social.core.test.AbstractCoreTest;
51  
52  public class SpaceServiceTest extends AbstractCoreTest {
53    private IdentityStorage identityStorage;
54    private OrganizationService organizationService;
55    protected SpacesAdministrationService spacesAdministrationService;
56    private List<Space> tearDownSpaceList;
57    private List<Identity> tearDownUserList;
58  
59    private final Log       LOG = ExoLogger.getLogger(SpaceServiceTest.class);
60  
61    private Identity demo;
62    private Identity tom;
63    private Identity raul;
64    private Identity ghost;
65    private Identity dragon;
66    private Identity register1;
67    private Identity john;
68    private Identity mary;
69    private Identity harry;
70    private Identity root;
71    private Identity jame;
72    private Identity paul;
73    private Identity hacker;
74    private Identity hearBreaker;
75    private Identity newInvitedUser;
76    private Identity newPendingUser;
77    private Identity user_new;
78    private Identity user_new1;
79    private Identity user_new_dot;
80    private Identity creator;
81    private Identity manager;
82    private Identity member1;
83    private Identity member2;
84    private Identity member3;
85  
86    @Override
87    public void setUp() throws Exception {
88      super.setUp();
89      identityStorage = getContainer().getComponentInstanceOfType(IdentityStorage.class);
90      organizationService = getContainer().getComponentInstanceOfType(OrganizationService.class);
91      spacesAdministrationService = getContainer().getComponentInstanceOfType(SpacesAdministrationService.class);
92      tearDownSpaceList = new ArrayList<Space>();
93      tearDownUserList = new ArrayList<Identity>();
94      
95      user_new = new Identity(OrganizationIdentityProvider.NAME, "user-new");
96      user_new1 = new Identity(OrganizationIdentityProvider.NAME, "user-new.1");
97      user_new_dot = new Identity(OrganizationIdentityProvider.NAME, "user.new");
98      demo = new Identity(OrganizationIdentityProvider.NAME, "demo");
99      tom = new Identity(OrganizationIdentityProvider.NAME, "tom");
100     raul = new Identity(OrganizationIdentityProvider.NAME, "raul");
101     ghost = new Identity(OrganizationIdentityProvider.NAME, "ghost");
102     dragon = new Identity(OrganizationIdentityProvider.NAME, "dragon");
103     register1 = new Identity(OrganizationIdentityProvider.NAME, "register1");
104     mary = new Identity(OrganizationIdentityProvider.NAME, "mary");
105     john = new Identity(OrganizationIdentityProvider.NAME, "john");
106     harry = new Identity(OrganizationIdentityProvider.NAME, "harry");
107     root = new Identity(OrganizationIdentityProvider.NAME, "root");
108     jame = new Identity(OrganizationIdentityProvider.NAME, "jame");
109     paul = new Identity(OrganizationIdentityProvider.NAME, "paul");
110     hacker = new Identity(OrganizationIdentityProvider.NAME, "hacker");
111     hearBreaker = new Identity(OrganizationIdentityProvider.NAME, "hearBreaker");
112     newInvitedUser = new Identity(OrganizationIdentityProvider.NAME, "newInvitedUser");
113     newPendingUser = new Identity(OrganizationIdentityProvider.NAME, "newPendingUser");
114     manager = new Identity(OrganizationIdentityProvider.NAME, "manager");
115     creator = new Identity(OrganizationIdentityProvider.NAME, "creator");
116     member1 = new Identity(OrganizationIdentityProvider.NAME, "member1");
117     member2 = new Identity(OrganizationIdentityProvider.NAME, "member2");
118     member3 = new Identity(OrganizationIdentityProvider.NAME, "member3");
119 
120     identityStorage.saveIdentity(demo);
121     identityStorage.saveIdentity(tom);
122     identityStorage.saveIdentity(raul);
123     identityStorage.saveIdentity(ghost);
124     identityStorage.saveIdentity(dragon);
125     identityStorage.saveIdentity(register1);
126     identityStorage.saveIdentity(mary);
127     identityStorage.saveIdentity(harry);
128     identityStorage.saveIdentity(john);
129     identityStorage.saveIdentity(root);
130     identityStorage.saveIdentity(jame);
131     identityStorage.saveIdentity(paul);
132     identityStorage.saveIdentity(hacker);
133     identityStorage.saveIdentity(hearBreaker);
134     identityStorage.saveIdentity(newInvitedUser);
135     identityStorage.saveIdentity(newPendingUser);
136     identityStorage.saveIdentity(user_new1);
137     identityStorage.saveIdentity(user_new);
138     identityStorage.saveIdentity(user_new_dot);
139     identityStorage.saveIdentity(manager);
140     identityStorage.saveIdentity(creator);
141     identityStorage.saveIdentity(member1);
142     identityStorage.saveIdentity(member2);
143     identityStorage.saveIdentity(member3);
144 
145     StorageUtils.persist();
146 
147     tearDownUserList.add(demo);
148     tearDownUserList.add(tom);
149     tearDownUserList.add(raul);
150     tearDownUserList.add(ghost);
151     tearDownUserList.add(dragon);
152     tearDownUserList.add(register1);
153     tearDownUserList.add(mary);
154     tearDownUserList.add(harry);
155     tearDownUserList.add(john);
156     tearDownUserList.add(root);
157     tearDownUserList.add(jame);
158     tearDownUserList.add(paul);
159     tearDownUserList.add(hacker);
160     tearDownUserList.add(hearBreaker);
161     tearDownUserList.add(newInvitedUser);
162     tearDownUserList.add(newPendingUser);
163     tearDownUserList.add(user_new1);
164     tearDownUserList.add(user_new);
165     tearDownUserList.add(user_new_dot);
166     tearDownUserList.add(manager);
167     tearDownUserList.add(creator);
168     tearDownUserList.add(member1);
169     tearDownUserList.add(member2);
170     tearDownUserList.add(member3);
171   }
172 
173   @Override
174   public void tearDown() throws Exception {
175     end();
176     begin();
177 
178     for (Space space : tearDownSpaceList) {
179       Identity spaceIdentity = identityStorage.findIdentity(SpaceIdentityProvider.NAME, space.getPrettyName());
180       if (spaceIdentity != null) {
181         try {
182         identityStorage.deleteIdentity(spaceIdentity);
183         } catch (IdentityStorageException e) {
184           // It's expected on some identities that could be deleted in tests
185         }
186       }
187       try {
188         spaceService.deleteSpace(space);
189       } catch (Exception e) {
190         // It's expected on some entities that could be deleted in tests
191       }
192     }
193 
194     StorageUtils.persist();
195 
196     for (Identity identity : tearDownUserList) {
197       try {
198         identityStorage.deleteIdentity(identity);
199       } catch (IdentityStorageException e) {
200         // It's expected on some identities that could be deleted in tests
201       }
202     }
203 
204     StorageUtils.persist();
205     super.tearDown();
206   }
207 
208   /**
209    * Test {@link SpaceService#getAllSpaces()}
210    *
211    * @throws Exception
212    */
213   public void testGetAllSpaces() throws Exception {
214     populateData();
215     createMoreSpace("Space2");
216     assertEquals(2, spaceService.getAllSpaces().size());
217   }
218 
219   /**
220    * Test {@link SpaceService#getAllSpacesWithListAccess()}
221    *
222    * @throws Exception
223    * @since 1.2.0-GA
224    */
225   public void testGetAllSpacesWithListAccess() throws Exception {
226     int count = 20;
227     for (int i = 0; i < count; i ++) {
228       this.getSpaceInstance(i);
229     }
230     ListAccess<Space> allSpaces = spaceService.getAllSpacesWithListAccess();
231     assertNotNull("allSpaces must not be null", allSpaces);
232     assertEquals("allSpaces.getSize() must return: " + count, count, allSpaces.getSize());
233     assertEquals("allSpaces.load(0, 1).length must return: 1", 1, allSpaces.load(0, 1).length);
234     assertEquals("allSpaces.load(0, count).length must return: " + count, count, allSpaces.load(0, count).length);
235   }
236 
237   /**
238    * Test {@link SpaceService#getSpaces(String)}
239    *
240    * @throws Exception
241    * @since 1.2.0-GA
242    */
243   public void testGetSpacesByUserId() throws Exception {
244     int count = 20;
245     for (int i = 0; i < count; i ++) {
246       this.getSpaceInstance(i);
247     }
248     List<Space> memberSpaces = spaceService.getSpaces("raul");
249     assertNotNull("memberSpaces must not be null", memberSpaces);
250     assertEquals("memberSpaces.size() must return: " + count, count, memberSpaces.size());
251 
252     memberSpaces = spaceService.getSpaces("ghost");
253     assertNotNull("memberSpaces must not be null", memberSpaces);
254     assertEquals("memberSpaces.size() must return: " + count, count, memberSpaces.size());
255 
256     memberSpaces = spaceService.getSpaces("dragon");
257     assertNotNull("memberSpaces must not be null", memberSpaces);
258     assertEquals("memberSpaces.size() must return: " + count, count, memberSpaces.size());
259 
260     memberSpaces = spaceService.getSpaces("nobody");
261     assertNotNull("memberSpaces must not be null", memberSpaces);
262     assertEquals("memberSpaces.size() must return: " + 0, 0, memberSpaces.size());
263   }
264 
265   /**
266    * Test {@link SpaceService#getSpaceByDisplayName(String)}
267    *
268    * @throws Exception
269    */
270   public void testGetSpaceByDisplayName() throws Exception {
271     Space space = populateData();
272     Space gotSpace1 = spaceService.getSpaceByDisplayName("Space1");
273 
274     assertNotNull("gotSpace1 must not be null", gotSpace1);
275 
276     assertEquals(space.getDisplayName(), gotSpace1.getDisplayName());
277   }
278 
279   public void testGetSpaceByName() throws Exception {
280     int count = 20;
281     for (int i = 0; i < count; i ++) {
282       this.getSpaceInstance(i);
283     }
284     Space foundSpace = spaceService.getSpaceByName("my_space_10");
285     assertNotNull("foundSpace must not be null", foundSpace);
286     assertEquals("foundSpace.getDisplayName() must return: my space 10", "my space 10", foundSpace.getDisplayName());
287     assertEquals("foundSpace.getPrettyName() must return: my_space_10", "my_space_10", foundSpace.getPrettyName());
288 
289     foundSpace = spaceService.getSpaceByName("my_space_0");
290     assertNotNull("foundSpace must not be null", foundSpace);
291     assertEquals("foundSpace.getDisplayName() must return: my space 0", "my space 0", foundSpace.getDisplayName());
292     assertEquals("foundSpace.getPrettyName() must return: my_space_0", "my_space_0", foundSpace.getPrettyName());
293 
294     foundSpace = spaceService.getSpaceByName("my_space_20");
295     assertNull("foundSpace must be null", foundSpace);
296   }
297 
298   /**
299    * Test {@link SpaceService#getSpaceByPrettyName(String)}
300    *
301    * @throws Exception
302    * @since 1.2.0-GA
303    */
304   public void testGetSpaceByPrettyName() throws Exception {
305     int count = 20;
306     for (int i = 0; i < count; i ++) {
307       this.getSpaceInstance(i);
308     }
309     Space foundSpace = spaceService.getSpaceByPrettyName("my_space_10");
310     assertNotNull("foundSpace must not be null", foundSpace);
311     assertEquals("foundSpace.getDisplayName() must return: my space 10", "my space 10", foundSpace.getDisplayName());
312     assertEquals("foundSpace.getPrettyName() must return: my_space_10", "my_space_10", foundSpace.getPrettyName());
313 
314     foundSpace = spaceService.getSpaceByPrettyName("my_space_0");
315     assertNotNull("foundSpace must not be null", foundSpace);
316     assertEquals("foundSpace.getDisplayName() must return: my space 0", "my space 0", foundSpace.getDisplayName());
317     assertEquals("foundSpace.getPrettyName() must return: my_space_0", "my_space_0", foundSpace.getPrettyName());
318 
319     foundSpace = spaceService.getSpaceByPrettyName("my_space_20");
320     assertNull("foundSpace must be null", foundSpace);
321   }
322 
323   /**
324    * Test {@link SpaceService#getSpacesByFirstCharacterOfName(String)}
325    *
326    * @throws Exception
327    */
328   public void testGetSpacesByFirstCharacterOfName() throws Exception {
329     populateData();
330     createMoreSpace("Space2");
331     assertEquals(2, spaceService.getSpacesByFirstCharacterOfName("S").size());
332   }
333 
334   /**
335    * Test {@link SpaceService#getAllSpacesByFilter(SpaceFilter)}
336    *
337    * @throws Exception
338    * @since 1.2.0-GA
339    */
340   public void testGetAllSpacesByFilterWithFirstCharacterOfSpaceName() throws Exception {
341     int count = 20;
342     for (int i = 0; i < count; i ++) {
343       this.getSpaceInstance(i);
344     }
345     ListAccess<Space> foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter('m'));
346     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
347     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
348 
349     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter('M'));
350     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
351     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
352     assertEquals("foundSpaceListAccess.load(0, 1).length must return: 1", 1, foundSpaceListAccess.load(0, 1).length);
353     assertEquals("foundSpaceListAccess.load(0, count).length must return: " + count,
354                  count, foundSpaceListAccess.load(0, count).length);
355     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter('H'));
356     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
357     assertEquals("foundSpaceListAccess.getSize() must return: " + 0, 0, foundSpaceListAccess.getSize());
358 
359     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter('k'));
360     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
361     assertEquals("foundSpaceListAccess.getSize() must return: " + 0, 0, foundSpaceListAccess.getSize());
362 
363     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter('*'));
364     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
365     assertEquals("foundSpaceListAccess.getSize() must return: " + 0, 0, foundSpaceListAccess.getSize());
366   }
367 
368   /**
369    * Test {@link SpaceService#getSpacesBySearchCondition(String)}
370    *
371    * @throws Exception
372    */
373   public void testGetSpacesBySearchCondition() throws Exception {
374     populateData();
375     createMoreSpace("Space2");
376     assertEquals(2, spaceService.getSpacesBySearchCondition("Space").size());
377     assertEquals(1, spaceService.getSpacesBySearchCondition("1").size());
378   }
379 
380   /**
381    * Test {@link SpaceService#getAllSpacesByFilter(SpaceFilter)}
382    *
383    * @throws Exception
384    * @since 1.2.0-GA
385    */
386   public void testGetAllSpacesByFilterWithSpaceNameSearchCondition() throws Exception {
387     int count = 20;
388     for (int i = 0; i < count; i ++) {
389       this.getSpaceInstance(i);
390     }
391 
392     ListAccess<Space> foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("my space"));
393     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
394     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
395     assertEquals("foundSpaceListAccess.load(0, 1).length must return: 1", 1, foundSpaceListAccess.load(0, 1).length);
396     assertEquals("foundSpaceListAccess.load(0, count).length must return: " + count,
397                  count, foundSpaceListAccess.load(0, count).length);
398 
399     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("1"));
400     assertEquals("foundSpaceListAccess.getSize() must return 11", 11, foundSpaceListAccess.getSize());
401     assertEquals("foundSpaceListAccess.load(0, 10).length must return 10",
402                  10, foundSpaceListAccess.load(0, 10).length);
403 
404     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("add new space"));
405     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
406     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
407 
408     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("space"));
409     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
410     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
411 
412     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("*space"));
413     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
414     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
415 
416     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("*space*"));
417     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
418     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
419 
420     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("*a*e*"));
421     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
422     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
423 
424     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("*a*e"));
425     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
426     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
427 
428     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("a*e"));
429     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
430     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
431 
432     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("a*"));
433     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
434     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
435 
436     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("%a%e%"));
437     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
438     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
439 
440     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("%a*e%"));
441     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
442     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
443 
444     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("%a*e*"));
445     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
446     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
447 
448     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("***"));
449     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
450     assertEquals("foundSpaceListAccess.getSize() must return: " + 0, 0, foundSpaceListAccess.getSize());
451 
452     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("%%%%%"));
453     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
454     assertEquals("foundSpaceListAccess.getSize() must return: " + 0, 0, foundSpaceListAccess.getSize());
455 
456     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("new"));
457     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
458     assertEquals("foundSpaceListAccess.getSize() must return: " + count, count, foundSpaceListAccess.getSize());
459     
460     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("<new>new(\"new\")</new>"));
461     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
462     //correct test case : the term  "new new new new" should not match the result
463     assertEquals("foundSpaceListAccess.getSize() must return: " + 0, 0, foundSpaceListAccess.getSize());
464 
465     foundSpaceListAccess = spaceService.getAllSpacesByFilter(new SpaceFilter("what new space add"));
466     assertNotNull("foundSpaceListAccess must not be null", foundSpaceListAccess);
467     assertEquals("foundSpaceListAccess.getSize() must return: " + 0, 0, foundSpaceListAccess.getSize());
468   }
469 
470   /**
471    * Test {@link SpaceService#getSpaceByGroupId(String)}
472    *
473    * @throws Exception
474    * @since 1.2.0-GA
475    */
476   public void testGetSpaceByGroupId() throws Exception {
477     int count = 20;
478     for (int i = 0; i < count; i ++) {
479       this.getSpaceInstance(i);
480     }
481     Space foundSpace = spaceService.getSpaceByGroupId("/space/space0");
482     assertNotNull("foundSpace must not be null", foundSpace);
483     assertEquals("foundSpace.getDisplayName() must return: my space 0", "my space 0", foundSpace.getDisplayName());
484     assertEquals("foundSpace.getGroupId() must return: /space/space0", "/space/space0", foundSpace.getGroupId());
485   }
486 
487 
488   /**
489    * Test {@link SpaceService#getSpaceById(String)}
490    *
491    * @throws Exception
492    */
493   public void testGetSpaceById() throws Exception {
494     Space space = populateData();
495     createMoreSpace("Space2");
496     assertEquals(space.getDisplayName(), spaceService.getSpaceById(space.getId()).getDisplayName());
497   }
498 
499   /**
500    * Test {@link SpaceService#getSpaceByUrl(String)}
501    *
502    * @throws Exception
503    */
504   public void testGetSpaceByUrl() throws Exception {
505     Space space = populateData();
506     assertEquals(space.getDisplayName(), spaceService.getSpaceByUrl("space1").getDisplayName());
507   }
508 
509   /**
510    * Test {@link SpaceService#getEditableSpaces(String)}
511    *
512    * @throws Exception
513    */
514   public void testGetEditableSpaces() throws Exception {
515     populateData();
516     assertEquals(1, spaceService.getEditableSpaces("root").size());
517   }
518 
519   /**
520    * Test {@link SpaceService#getSettingableSpaces(String))}
521    *
522    * @throws Exception
523    * @since 1.2.0-GA
524    */
525   public void testGetSettingableSpaces() throws Exception {
526     int count = 20;
527     for (int i = 0; i < count; i ++) {
528       this.getSpaceInstance(i);
529     }
530     ListAccess<Space> editableSpaceListAccess = spaceService.getSettingableSpaces("demo");
531     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
532     assertEquals("editableSpaceListAccess.getSize() must return: " + count, count, editableSpaceListAccess.getSize());
533     assertEquals("editableSpaceListAccess.load(0, 1).length must return: 1",
534                  1, editableSpaceListAccess.load(0, 1).length);
535     assertEquals("editableSpaceListAccess.load(0, count).length must return: " + count,
536                  count, editableSpaceListAccess.load(0, count).length);
537 
538     editableSpaceListAccess = spaceService.getSettingableSpaces("tom");
539     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
540     assertEquals("editableSpaceListAccess.getSize() must return: " + count, count, editableSpaceListAccess.getSize());
541 
542     editableSpaceListAccess = spaceService.getSettingableSpaces("root");
543     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
544     assertEquals("editableSpaceListAccess.getSize() must return: " + count, count, editableSpaceListAccess.getSize());
545 
546     editableSpaceListAccess = spaceService.getSettingableSpaces("raul");
547     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
548     assertEquals("editableSpaceListAccess.getSize() must return: " + 0, 0, editableSpaceListAccess.getSize());
549 
550     editableSpaceListAccess = spaceService.getSettingableSpaces("ghost");
551     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
552     assertEquals("editableSpaceListAccess.getSize() must return: " + 0, 0, editableSpaceListAccess.getSize());
553   }
554 
555   /**
556    * Test {@link SpaceService#getSettingabledSpacesByFilter(String, SpaceFilter)}
557    *
558    * @throws Exception
559    * @since 1.2.0-GA
560    */
561   public void testGetSettingableSpacesByFilter() throws Exception {
562     int count = 20;
563     for (int i = 0; i < count; i ++) {
564       this.getSpaceInstance(i);
565     }
566     ListAccess<Space> editableSpaceListAccess = spaceService.getSettingabledSpacesByFilter("demo", new SpaceFilter("add"));
567     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
568     assertEquals("editableSpaceListAccess.getSize() must return: " + count, count, editableSpaceListAccess.getSize());
569     assertEquals("editableSpaceListAccess.load(0, 1).length must return: 1",
570                  1, editableSpaceListAccess.load(0, 1).length);
571     assertEquals("editableSpaceListAccess.load(0, count).length must return: " + count,
572                  count, editableSpaceListAccess.load(0, count).length);
573     editableSpaceListAccess = spaceService.getSettingabledSpacesByFilter(demo.getRemoteId(), new SpaceFilter("19"));
574     assertEquals("editableSpaceListAccess.getSize() must return 1", 1, editableSpaceListAccess.getSize());
575     assertEquals("editableSpaceListAccess.load(0, 1).length must return 1",
576                  1, editableSpaceListAccess.load(0, 1).length);
577 
578     editableSpaceListAccess = spaceService.getSettingabledSpacesByFilter("demo", new SpaceFilter("my"));
579     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
580     assertEquals("editableSpaceListAccess.getSize() must return: " + count, count, editableSpaceListAccess.getSize());
581 
582     editableSpaceListAccess = spaceService.getSettingabledSpacesByFilter("demo", new SpaceFilter("new"));
583     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
584     assertEquals("editableSpaceListAccess.getSize() must return: " + count, count, editableSpaceListAccess.getSize());
585 
586     editableSpaceListAccess = spaceService.getSettingabledSpacesByFilter("demo", new SpaceFilter('m'));
587     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
588     assertEquals("editableSpaceListAccess.getSize() must return: " + count, count, editableSpaceListAccess.getSize());
589 
590     editableSpaceListAccess = spaceService.getSettingabledSpacesByFilter("demo", new SpaceFilter('M'));
591     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
592     assertEquals("editableSpaceListAccess.getSize() must return: " + count, count, editableSpaceListAccess.getSize());
593 
594     editableSpaceListAccess = spaceService.getSettingabledSpacesByFilter("demo", new SpaceFilter('k'));
595     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
596     assertEquals("editableSpaceListAccess.getSize() must return: " + 0, 0, editableSpaceListAccess.getSize());
597 
598     editableSpaceListAccess = spaceService.getSettingabledSpacesByFilter("tom", new SpaceFilter("new"));
599     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
600     assertEquals("editableSpaceListAccess.getSize() must return: " + count, count, editableSpaceListAccess.getSize());
601 
602     editableSpaceListAccess = spaceService.getSettingabledSpacesByFilter("root", new SpaceFilter("space"));
603     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
604     assertEquals("editableSpaceListAccess.getSize() must return: " + count, count, editableSpaceListAccess.getSize());
605 
606     editableSpaceListAccess = spaceService.getSettingabledSpacesByFilter("raul", new SpaceFilter("my"));
607     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
608     assertEquals("editableSpaceListAccess.getSize() must return: " + 0, 0, editableSpaceListAccess.getSize());
609 
610     editableSpaceListAccess = spaceService.getSettingabledSpacesByFilter("ghost", new SpaceFilter("space"));
611     assertNotNull("editableSpaceListAccess must not be null", editableSpaceListAccess);
612     assertEquals("editableSpaceListAccess.getSize() must return: " + 0, 0, editableSpaceListAccess.getSize());
613   }
614 
615   /**
616    * Test {@link SpaceService#getInvitedSpaces(String)}
617    *
618    * @throws Exception
619    */
620   public void testGetInvitedSpaces() throws Exception {
621     populateData();
622     assertEquals(0, spaceService.getInvitedSpaces("paul").size());
623     Space space = spaceService.getSpaceByDisplayName("Space1");
624     spaceService.inviteMember(space, "paul");
625     StorageUtils.persist();
626     assertEquals(1, spaceService.getInvitedSpaces("paul").size());
627   }
628 
629   /**
630    * Test {@link SpaceService#getInvitedSpacesWithListAccess(String)}
631    *
632    * @throws Exception
633    * @since 1.2.0-GA
634    */
635   public void testGetInvitedSpacesWithListAccess() throws Exception {
636     int count = 20;
637     for (int i = 0; i < count; i ++) {
638       this.getSpaceInstance(i);
639     }
640     ListAccess<Space> invitedSpaces = spaceService.getInvitedSpacesWithListAccess("register1");
641     assertNotNull("invitedSpaces must not be null", invitedSpaces);
642     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
643     assertEquals("invitedSpaces.load(0, 1).length must return: " + 1, 1, invitedSpaces.load(0, 1).length);
644     assertEquals("invitedSpaces.load(0, count).length must return: " + count,
645                  count, invitedSpaces.load(0, count).length);
646     invitedSpaces = spaceService.getInvitedSpacesWithListAccess("mary");
647     assertNotNull("invitedSpaces must not be null", invitedSpaces);
648     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
649 
650     invitedSpaces = spaceService.getInvitedSpacesWithListAccess("demo");
651     assertNotNull("invitedSpaces must not be null", invitedSpaces);
652     assertEquals("invitedSpaces.getSize() must return: " + 0, 0, invitedSpaces.getSize());
653 
654   }
655 
656   /**
657    * Test {@link SpaceService#getInvitedSpacesByFilter(String, SpaceFilter)}
658    *
659    * @throws Exception
660    * @since 1.2.0-GA
661    */
662   public void testGetInvitedSpacesByFilterWithSpaceNameSearchCondition() throws Exception {
663     int count = 20;
664     for (int i = 0; i < count; i ++) {
665       this.getSpaceInstance(i);
666     }
667     ListAccess<Space> invitedSpaces = spaceService.getInvitedSpacesByFilter("register1", new SpaceFilter("my space"));
668     assertNotNull("invitedSpaces must not be null", invitedSpaces);
669     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
670 
671     invitedSpaces = spaceService.getInvitedSpacesByFilter(register1.getRemoteId(), new SpaceFilter("12"));
672     assertEquals("invitedSpaces.getSize() must return 1", 1, invitedSpaces.getSize());
673     assertEquals("invitedSpaces.load(0, 1).length must return 1", 1, invitedSpaces.load(0, 1).length);
674 
675     invitedSpaces = spaceService.getInvitedSpacesByFilter("mary", new SpaceFilter("my"));
676     assertNotNull("invitedSpaces must not be null", invitedSpaces);
677     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
678     assertEquals("invitedSpaces.load(0, 1).length must return: 1",
679                  1, invitedSpaces.load(0, 1).length);
680     assertEquals("invitedSpaces.load(0, count).length must return: " + count,
681                  count, invitedSpaces.load(0, count).length);
682 
683     invitedSpaces = spaceService.getInvitedSpacesByFilter("mary", new SpaceFilter("*my"));
684     assertNotNull("invitedSpaces must not be null", invitedSpaces);
685     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
686 
687     invitedSpaces = spaceService.getInvitedSpacesByFilter("mary", new SpaceFilter("*my*"));
688     assertNotNull("invitedSpaces must not be null", invitedSpaces);
689     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
690 
691     invitedSpaces = spaceService.getInvitedSpacesByFilter("mary", new SpaceFilter("*my*e*"));
692     assertNotNull("invitedSpaces must not be null", invitedSpaces);
693     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
694 
695     invitedSpaces = spaceService.getInvitedSpacesByFilter("mary", new SpaceFilter("%my%e%"));
696     assertNotNull("invitedSpaces must not be null", invitedSpaces);
697     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
698 
699     invitedSpaces = spaceService.getInvitedSpacesByFilter("mary", new SpaceFilter("%my%e"));
700     assertNotNull("invitedSpaces must not be null", invitedSpaces);
701     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
702 
703     invitedSpaces = spaceService.getInvitedSpacesByFilter("mary", new SpaceFilter("%my*e%"));
704     assertNotNull("invitedSpaces must not be null", invitedSpaces);
705     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
706 
707     invitedSpaces = spaceService.getInvitedSpacesByFilter("mary", new SpaceFilter("%my*e*"));
708     assertNotNull("invitedSpaces must not be null", invitedSpaces);
709     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
710 
711     invitedSpaces = spaceService.getInvitedSpacesByFilter("mary", new SpaceFilter("****"));
712     assertNotNull("invitedSpaces must not be null", invitedSpaces);
713     assertEquals("invitedSpaces.getSize() must return: " + 0, 0, invitedSpaces.getSize());
714 
715     invitedSpaces = spaceService.getInvitedSpacesByFilter("mary", new SpaceFilter("%%%%%"));
716     assertNotNull("invitedSpaces must not be null", invitedSpaces);
717     assertEquals("invitedSpaces.getSize() must return: " + 0, 0, invitedSpaces.getSize());
718 
719     invitedSpaces = spaceService.getInvitedSpacesByFilter("demo", new SpaceFilter("my space"));
720     assertNotNull("invitedSpaces must not be null", invitedSpaces);
721     assertEquals("invitedSpaces.getSize() must return: " + 0, 0, invitedSpaces.getSize());
722 
723     invitedSpaces = spaceService.getInvitedSpacesByFilter("demo", new SpaceFilter("add new"));
724     assertNotNull("invitedSpaces must not be null", invitedSpaces);
725     assertEquals("invitedSpaces.getSize() must return: " + 0, 0, invitedSpaces.getSize());
726 
727     invitedSpaces = spaceService.getInvitedSpacesByFilter("john", new SpaceFilter("space"));
728     assertNotNull("invitedSpaces must not be null", invitedSpaces);
729     assertEquals("invitedSpaces.getSize() must return: " + 0, 0, invitedSpaces.getSize());
730   }
731 
732   /**
733    * Test {@link SpaceService#getInvitedSpacesByFilter(String, SpaceFilter)}
734    *
735    * @throws Exception
736    * @since 1.2.0-GA
737    */
738   public void testGetInvitedSpacesByFilterWithFirstCharacterOfSpaceName() throws Exception {
739     int count = 20;
740     for (int i = 0; i < count; i ++) {
741       this.getSpaceInstance(i);
742     }
743     ListAccess<Space> invitedSpaces = spaceService.getInvitedSpacesByFilter("register1", new SpaceFilter('m'));
744     assertNotNull("invitedSpaces must not be null", invitedSpaces);
745     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
746     assertEquals("invitedSpaces.load(0, 1).length must return: 1", 1, invitedSpaces.load(0, 1).length);
747     assertEquals("invitedSpaces.load(0, count).length must return: " + count,
748                  count, invitedSpaces.load(0, count).length);
749     invitedSpaces = spaceService.getInvitedSpacesByFilter("mary", new SpaceFilter('M'));
750     assertNotNull("invitedSpaces must not be null", invitedSpaces);
751     assertEquals("invitedSpaces.getSize() must return: " + count, count, invitedSpaces.getSize());
752 
753     invitedSpaces = spaceService.getInvitedSpacesByFilter("mary", new SpaceFilter('H'));
754     assertNotNull("invitedSpaces must not be null", invitedSpaces);
755     assertEquals("invitedSpaces.getSize() must return: " + 0, 0, invitedSpaces.getSize());
756 
757     invitedSpaces = spaceService.getInvitedSpacesByFilter("demo", new SpaceFilter('m'));
758     assertNotNull("invitedSpaces must not be null", invitedSpaces);
759     assertEquals("invitedSpaces.getSize() must return: " + 0, 0, invitedSpaces.getSize());
760 
761     invitedSpaces = spaceService.getInvitedSpacesByFilter("john", new SpaceFilter('M'));
762     assertNotNull("invitedSpaces must not be null", invitedSpaces);
763     assertEquals("invitedSpaces.getSize() must return: " + 0, 0, invitedSpaces.getSize());
764   }
765 
766   /**
767    * Test {@link SpaceService#getPublicSpaces(String)}
768    *
769    * @throws Exception
770    */
771   public void testGetPublicSpaces() throws Exception {
772     populateData();
773     assertEquals(0, spaceService.getPublicSpaces("root").size());
774   }
775 
776   /**
777    * Test {@link SpaceService#getPublicSpacesWithListAccess(String)}
778    *
779    * @throws Exception
780    * @since 1.2.0-GA
781    */
782   public void testGetPublicSpacesWithListAccess() throws Exception {
783     int count = 20;
784     for (int i = 0; i < count; i ++) {
785       this.getSpaceInstance(i);
786     }
787     ListAccess<Space> foundSpaces = spaceService.getPublicSpacesWithListAccess("tom");
788     assertNotNull("foundSpaces must not be null", foundSpaces);
789     assertEquals("foundSpaces.getSize() must return: 0", 0, foundSpaces.getSize());
790 
791     foundSpaces = spaceService.getPublicSpacesWithListAccess("hacker");
792     assertNotNull("foundSpaces must not be null", foundSpaces);
793     assertEquals("foundSpaces.getSize() must return: 0", 0, foundSpaces.getSize());
794 
795     foundSpaces = spaceService.getPublicSpacesWithListAccess("mary");
796     assertNotNull("foundSpaces must not be null", foundSpaces);
797     assertEquals("foundSpaces.getSize() must return: 0", 0, foundSpaces.getSize());
798 
799     foundSpaces = spaceService.getPublicSpacesWithListAccess("root");
800     assertNotNull("foundSpaces must not be null", foundSpaces);
801     assertEquals("foundSpaces.getSize() must return: 0", 0, foundSpaces.getSize());
802 
803     foundSpaces = spaceService.getPublicSpacesWithListAccess("nobody");
804     assertNotNull("foundSpaces must not be null", foundSpaces);
805     assertEquals("foundSpaces.getSize() must return: 20", count, foundSpaces.getSize());
806     assertEquals("foundSpaces.load(0, 1).length must return: 1", 1, foundSpaces.load(0, 1).length);
807     assertEquals("foundSpaces.load(0, 20).length must return: 20",
808                  20, foundSpaces.load(0, 20).length);
809     foundSpaces = spaceService.getPublicSpacesWithListAccess("bluray");
810     assertNotNull("foundSpaces must not be null", foundSpaces);
811     assertEquals("foundSpaces.getSize() must return: 20", count, foundSpaces.getSize());
812   }
813 
814 
815   /**
816    * Test {@link SpaceService#getPublicSpacesByFilter(String, SpaceFilter)}
817    *
818    * @throws Exception
819    * @since 1.2.0-GA
820    */
821   public void testGetPublicSpacesByFilterWithSpaceNameSearchCondition() throws Exception {
822     int count = 20;
823     for (int i = 0; i < count; i ++) {
824       this.getSpaceInstance(i);
825     }
826     String nameSpace = "my space";
827     ListAccess<Space> foundSpaces = spaceService.getPublicSpacesByFilter("tom", new SpaceFilter(nameSpace));
828     assertNotNull("foundSpaces must not be null", foundSpaces);
829     assertEquals("foundSpaces.getSize() must return: 0", 0, foundSpaces.getSize());
830 
831     foundSpaces = spaceService.getPublicSpacesByFilter("hearBreaker", new SpaceFilter(nameSpace));
832     assertNotNull("foundSpaces must not be null", foundSpaces);
833     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
834     assertEquals("foundSpaces.load(0, 1).length must return: 1", 1, foundSpaces.load(0, 1).length);
835     assertEquals("foundSpaces.load(0, count).length must return: " + count,
836                  count, foundSpaces.load(0, count).length);
837     foundSpaces = spaceService.getPublicSpacesByFilter("hearBreaker", new SpaceFilter("*m"));
838     assertNotNull("foundSpaces must not be null", foundSpaces);
839     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
840 
841     foundSpaces = spaceService.getPublicSpacesByFilter("hearBreaker", new SpaceFilter("m*"));
842     assertNotNull("foundSpaces must not be null", foundSpaces);
843     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
844 
845     foundSpaces = spaceService.getPublicSpacesByFilter("hearBreaker", new SpaceFilter("*my*"));
846     assertNotNull("foundSpaces must not be null", foundSpaces);
847     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
848 
849     foundSpaces = spaceService.getPublicSpacesByFilter("hearBreaker", new SpaceFilter("*my*e"));
850     assertNotNull("foundSpaces must not be null", foundSpaces);
851     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
852 
853     foundSpaces = spaceService.getPublicSpacesByFilter("hearBreaker", new SpaceFilter("*my*e*"));
854     assertNotNull("foundSpaces must not be null", foundSpaces);
855     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
856 
857     foundSpaces = spaceService.getPublicSpacesByFilter("hearBreaker", new SpaceFilter("%my%e%"));
858     assertNotNull("foundSpaces must not be null", foundSpaces);
859     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
860 
861     foundSpaces = spaceService.getPublicSpacesByFilter("hearBreaker", new SpaceFilter("%my*e%"));
862     assertNotNull("foundSpaces must not be null", foundSpaces);
863     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
864 
865     foundSpaces = spaceService.getPublicSpacesByFilter("hearBreaker", new SpaceFilter("*my%e%"));
866     assertNotNull("foundSpaces must not be null", foundSpaces);
867     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
868 
869     foundSpaces = spaceService.getPublicSpacesByFilter("hearBreaker", new SpaceFilter("***"));
870     assertNotNull("foundSpaces must not be null", foundSpaces);
871     assertEquals("foundSpaces.getSize() must return: " + 0, 0, foundSpaces.getSize());
872 
873     foundSpaces = spaceService.getPublicSpacesByFilter("hearBreaker", new SpaceFilter("%%%"));
874     assertNotNull("foundSpaces must not be null", foundSpaces);
875     assertEquals("foundSpaces.getSize() must return: " + 0, 0, foundSpaces.getSize());
876 
877     nameSpace = "my space 1";
878     foundSpaces = spaceService.getPublicSpacesByFilter("stranger", new SpaceFilter(""));
879     assertNotNull("foundSpaces must not be null", foundSpaces);
880     assertEquals("foundSpaces.getSize() must return: " + 0, 0, foundSpaces.getSize());
881 
882     nameSpace = "my space 20";
883     foundSpaces = spaceService.getPublicSpacesByFilter("hearBreaker", new SpaceFilter(nameSpace));
884     assertNotNull("foundSpaces must not be null", foundSpaces);
885     assertEquals("foundSpaces.getSize() must return: " + 0, 0, foundSpaces.getSize());
886   }
887 
888   /**
889    * Test {@link SpaceService#getPublicSpacesByFilter(String, SpaceFilter)}
890    *
891    * @throws Exception
892    * @since 1.2.0-GA
893    */
894   public void testGetPublicSpacesByFilterWithFirstCharacterOfSpaceName() throws Exception {
895     int count = 10;
896     for (int i = 0; i < count; i ++) {
897       this.getSpaceInstance(i);
898     }
899     ListAccess<Space> foundSpaces = spaceService.getPublicSpacesByFilter("stranger", new SpaceFilter('m'));
900     assertNotNull("foundSpaces must not be null", foundSpaces);
901     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
902 
903     foundSpaces = spaceService.getPublicSpacesByFilter("stranger", new SpaceFilter('M'));
904     assertNotNull("foundSpaces must not be null", foundSpaces);
905     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
906 
907     foundSpaces = spaceService.getPublicSpacesByFilter("root", new SpaceFilter('M'));
908     assertNotNull("foundSpaces must not be null", foundSpaces);
909     assertEquals("foundSpaces.getSize() must return: " + 0, 0, foundSpaces.getSize());
910 
911     foundSpaces = spaceService.getPublicSpacesByFilter("stranger", new SpaceFilter('*'));
912     assertNotNull("foundSpaces must not be null", foundSpaces);
913     assertEquals("foundSpaces.getSize() must return: " + 0, 0, foundSpaces.getSize());
914 
915     foundSpaces = spaceService.getPublicSpacesByFilter("tom", new SpaceFilter('M'));
916     assertNotNull("foundSpaces must not be null", foundSpaces);
917     assertEquals("foundSpaces.getSize() must return: 0", 0, foundSpaces.getSize());
918 
919     foundSpaces = spaceService.getPublicSpacesByFilter("stranger", new SpaceFilter('y'));
920     assertNotNull("foundSpaces must not be null", foundSpaces);
921     assertEquals("foundSpaces.getSize() must return: " + 0, 0, foundSpaces.getSize());
922 
923     foundSpaces = spaceService.getPublicSpacesByFilter("stranger", new SpaceFilter('H'));
924     assertNotNull("foundSpaces must not be null", foundSpaces);
925     assertEquals("foundSpaces.getSize() must return: " + 0, 0, foundSpaces.getSize());
926 
927     ListAccess<Space> johnPublicSpaces = spaceService.getPublicSpacesByFilter("john", new SpaceFilter('m'));
928     assertEquals("johnPublicSpaces.getSize() must return: 10", 10, johnPublicSpaces.getSize());
929     assertEquals("johnPublicSpaces.load(0, 1).length must return: 1", 1, johnPublicSpaces.load(0, 1).length);
930     Space[] johnPublicSpacesArray = johnPublicSpaces.load(0, 10);
931     assertEquals("johnPublicSpaces.load(0, 10).length must return 10", 10, johnPublicSpacesArray.length);
932     assertNotNull("johnPublicSpacesArray[0].getId() must not be null", johnPublicSpacesArray[0].getId());
933     assertNotNull("johnPublicSpacesArray[0].getPrettyName() must not be null",
934                   johnPublicSpacesArray[0].getPrettyName());
935   }
936 
937   /**
938    * Test {@link SpaceService#getAccessibleSpaces(String)}
939    *
940    * @throws Exception
941    * @since 1.2.0-GA
942    */
943   public void testGetAccessibleSpaces() throws Exception {
944     int count = 20;
945     for (int i = 0; i < count; i ++) {
946       this.getSpaceInstance(i);
947     }
948     List<Space> accessibleSpaces = spaceService.getAccessibleSpaces("demo");
949     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
950     assertEquals("accessibleSpaces.size() must return: " + count, count, accessibleSpaces.size());
951 
952     accessibleSpaces = spaceService.getAccessibleSpaces("tom");
953     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
954     assertEquals("accessibleSpaces.size() must return: " + count, count, accessibleSpaces.size());
955 
956     accessibleSpaces = spaceService.getAccessibleSpaces("root");
957     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
958     assertEquals("accessibleSpaces.size() must return: " + 0, 0, accessibleSpaces.size());
959 
960     accessibleSpaces = spaceService.getAccessibleSpaces("dragon");
961     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
962     assertEquals("accessibleSpaces.size() must return: " + count, count, accessibleSpaces.size());
963 
964     accessibleSpaces = spaceService.getAccessibleSpaces("hellgate");
965     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
966     assertEquals("accessibleSpaces.size() must return: " + 0, 0, accessibleSpaces.size());
967   }
968 
969   /**
970    * Test {@link SpaceService#getAccessibleSpacesWithListAccess(String)}
971    *
972    * @throws Exception
973    * @since 1.2.0-GA
974    */
975   public void testGetAccessibleSpacesWithListAccess() throws Exception {
976     int count = 20;
977     for (int i = 0; i < count; i ++) {
978       this.getSpaceInstance(i);
979     }
980     ListAccess<Space> accessibleSpaces = spaceService.getAccessibleSpacesWithListAccess("demo");
981     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
982     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
983     assertEquals("accessibleSpaces.load(0, 1).length must return: 1", 1, accessibleSpaces.load(0, 1).length);
984     assertEquals("accessibleSpaces.load(0, count).length must return: " + count,
985                  count, accessibleSpaces.load(0, count).length);
986 
987     accessibleSpaces = spaceService.getAccessibleSpacesWithListAccess("tom");
988     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
989     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
990 
991     accessibleSpaces = spaceService.getAccessibleSpacesWithListAccess("root");
992     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
993     assertEquals("accessibleSpaces.getSize() must return: " + 0, 0, accessibleSpaces.getSize());
994 
995     accessibleSpaces = spaceService.getAccessibleSpacesWithListAccess("dragon");
996     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
997     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
998 
999     accessibleSpaces = spaceService.getAccessibleSpacesWithListAccess("ghost");
1000     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1001     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1002 
1003     accessibleSpaces = spaceService.getAccessibleSpacesWithListAccess("raul");
1004     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1005     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1006 
1007     accessibleSpaces = spaceService.getAccessibleSpacesWithListAccess("mary");
1008     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1009     assertEquals("accessibleSpaces.getSize() must return: " + 0, 0, accessibleSpaces.getSize());
1010 
1011     accessibleSpaces = spaceService.getAccessibleSpacesWithListAccess("john");
1012     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1013     assertEquals("accessibleSpaces.getSize() must return: " + 0, 0, accessibleSpaces.getSize());
1014   }
1015 
1016   /**
1017    * Test {@link SpaceService#getAccessibleSpacesByFilter(String, SpaceFilter)}
1018    *
1019    * @throws Exception
1020    * @since 1.2.0-GA
1021    */
1022   public void testGetAccessibleSpacesByFilterWithSpaceNameSearchCondition() throws Exception {
1023     int count = 20;
1024     for (int i = 0; i < count; i ++) {
1025       this.getSpaceInstance(i);
1026     }
1027     ListAccess<Space> accessibleSpaces = spaceService.getAccessibleSpacesByFilter("demo", new SpaceFilter("my"));
1028     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1029     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1030     assertEquals("accessibleSpaces.load(0, 1).length must return: 1", 1, accessibleSpaces.load(0, 1).length);
1031     assertEquals("accessibleSpaces.load(0, count).length must return: " + count,
1032                  count, accessibleSpaces.load(0, count).length);
1033 
1034     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("tom", new SpaceFilter("space"));
1035     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1036     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1037 
1038     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter("space"));
1039     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1040     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1041 
1042     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter("*space"));
1043     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1044     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1045 
1046     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter("space*"));
1047     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1048     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1049 
1050     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter("*space*"));
1051     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1052     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1053 
1054     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter("*a*e*"));
1055     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1056     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1057 
1058     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter("%a%e%"));
1059     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1060     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1061 
1062     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter("%a*e%"));
1063     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1064     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1065 
1066     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter("%a*e*"));
1067     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1068     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1069 
1070     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter("*****"));
1071     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1072     assertEquals("accessibleSpaces.getSize() must return: " + 0, 0, accessibleSpaces.getSize());
1073 
1074     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter("%%%%%%%"));
1075     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1076     assertEquals("accessibleSpaces.getSize() must return: " + 0, 0, accessibleSpaces.getSize());
1077 
1078     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter("add new"));
1079     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1080     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1081 
1082     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("dragon", new SpaceFilter("my space"));
1083     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1084     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1085 
1086     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("dragon", new SpaceFilter("add new"));
1087     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1088     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1089 
1090     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("ghost", new SpaceFilter("my space "));
1091     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1092     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1093 
1094     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("hellgate", new SpaceFilter("my space"));
1095     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1096     assertEquals("accessibleSpaces.getSize() must return: " + 0, 0, accessibleSpaces.getSize());
1097   }
1098 
1099   /**
1100    * Test {@link SpaceService#getAccessibleSpacesByFilter(String, SpaceFilter)}
1101    *
1102    * @throws Exception
1103    * @since 1.2.0-GA
1104    */
1105   public void testGetAccessibleSpacesByFilterWithFirstCharacterOfSpaceName() throws Exception {
1106     int count = 20;
1107     for (int i = 0; i < count; i ++) {
1108       this.getSpaceInstance(i);
1109     }
1110     ListAccess<Space> accessibleSpaces = spaceService.getAccessibleSpacesByFilter("demo", new SpaceFilter('m'));
1111     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1112     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1113     assertEquals("accessibleSpaces.load(0, 1).length must return: 1", 1, accessibleSpaces.load(0, 1).length);
1114     assertEquals("accessibleSpaces.load(0, count).length must return: " + count,
1115                  count, accessibleSpaces.load(0, count).length);
1116     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("tom", new SpaceFilter('M'));
1117     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1118     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1119 
1120     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter('M'));
1121     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1122     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1123 
1124     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("root", new SpaceFilter('*'));
1125     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1126     assertEquals("accessibleSpaces.getSize() must return: " + 0, 0, accessibleSpaces.getSize());
1127 
1128     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("tom", new SpaceFilter('h'));
1129     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1130     assertEquals("accessibleSpaces.getSize() must return: " + 0, 0, accessibleSpaces.getSize());
1131 
1132     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("dragon", new SpaceFilter('m'));
1133     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1134     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1135 
1136     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("ghost", new SpaceFilter('M'));
1137     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1138     assertEquals("accessibleSpaces.getSize() must return: " + count, count, accessibleSpaces.getSize());
1139 
1140     accessibleSpaces = spaceService.getAccessibleSpacesByFilter("hellgate", new SpaceFilter('m'));
1141     assertNotNull("accessibleSpaces must not be null", accessibleSpaces);
1142     assertEquals("accessibleSpaces.getSize() must return: " + 0, 0, accessibleSpaces.getSize());
1143   }
1144 
1145   /**
1146    * Test {@link SpaceService#getSpaces(String)}
1147    *
1148    * @throws Exception
1149    * @since 1.2.0-GA
1150    */
1151   public void testGetSpaces() throws Exception {
1152     int count = 20;
1153     for (int i = 0; i < count; i ++) {
1154       this.getSpaceInstance(i);
1155     }
1156 
1157     List<Space> memberSpaceListAccess = spaceService.getSpaces("raul");
1158     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1159     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.size());
1160 
1161     memberSpaceListAccess = spaceService.getSpaces("ghost");
1162     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1163     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.size());
1164 
1165     memberSpaceListAccess = spaceService.getSpaces("dragon");
1166     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1167     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.size());
1168 
1169     memberSpaceListAccess = spaceService.getSpaces("root");
1170     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1171     assertEquals("memberSpaceListAccess.size() must return: " + 0, 0, memberSpaceListAccess.size());
1172   }
1173 
1174   /**
1175    * Test {@link SpaceService#getMemberSpaces(String)}
1176    *
1177    * @throws Exception
1178    * @since 1.2.0-GA
1179    */
1180   public void testGetMemberSpaces() throws Exception {
1181     int count = 20;
1182     for (int i = 0; i < count; i ++) {
1183       this.getSpaceInstance(i);
1184     }
1185 
1186     ListAccess<Space> memberSpaceListAccess = spaceService.getMemberSpaces("raul");
1187     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1188     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.getSize());
1189     assertEquals("memberSpaceListAccess.load(0, 1).length must return: 1",
1190                  1, memberSpaceListAccess.load(0, 1).length);
1191     assertEquals("memberSpaceListAccess.load(0, count).length must return: " + count,
1192                  count, memberSpaceListAccess.load(0, count).length);
1193     memberSpaceListAccess = spaceService.getMemberSpaces("ghost");
1194     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1195     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.getSize());
1196 
1197     memberSpaceListAccess = spaceService.getMemberSpaces("dragon");
1198     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1199     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.getSize());
1200 
1201     memberSpaceListAccess = spaceService.getMemberSpaces("root");
1202     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1203     assertEquals("memberSpaceListAccess.size() must return: " + 0, 0, memberSpaceListAccess.getSize());
1204   }
1205 
1206   /**
1207    * Test {@link SpaceService#getMemberSpacesByFilter(String, SpaceFilter)}
1208    *
1209    * @throws Exception
1210    * @since 1.2.0-GA
1211    */
1212   public void testGetMemberSpacesByFilter() throws Exception {
1213     int count = 20;
1214     for (int i = 0; i < count; i ++) {
1215       this.getSpaceInstance(i);
1216     }
1217 
1218     ListAccess<Space> memberSpaceListAccess = spaceService.getMemberSpacesByFilter("raul", new SpaceFilter("add"));
1219     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1220     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.getSize());
1221     assertEquals("memberSpaceListAccess.load(0, 1).length must return: 1",
1222                  1, memberSpaceListAccess.load(0, 1).length);
1223     assertEquals("memberSpaceListAccess.load(0, count).length must return: " + count,
1224                  count, memberSpaceListAccess.load(0, count).length);
1225 
1226     memberSpaceListAccess = spaceService.getMemberSpacesByFilter("raul", new SpaceFilter("new"));
1227     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1228     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.getSize());
1229 
1230     memberSpaceListAccess = spaceService.getMemberSpacesByFilter("raul", new SpaceFilter("space"));
1231     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1232     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.getSize());
1233 
1234     memberSpaceListAccess = spaceService.getMemberSpacesByFilter("raul", new SpaceFilter("my"));
1235     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1236     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.getSize());
1237 
1238     memberSpaceListAccess = spaceService.getMemberSpacesByFilter("raul", new SpaceFilter('m'));
1239     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1240     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.getSize());
1241 
1242     memberSpaceListAccess = spaceService.getMemberSpacesByFilter("raul", new SpaceFilter('M'));
1243     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1244     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.getSize());
1245 
1246     memberSpaceListAccess = spaceService.getMemberSpacesByFilter("raul", new SpaceFilter('k'));
1247     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1248     assertEquals("memberSpaceListAccess.size() must return: " + 0, 0, memberSpaceListAccess.getSize());
1249 
1250     memberSpaceListAccess = spaceService.getMemberSpacesByFilter("ghost", new SpaceFilter("my"));
1251     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1252     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.getSize());
1253 
1254     memberSpaceListAccess = spaceService.getMemberSpacesByFilter("dragon", new SpaceFilter("space"));
1255     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1256     assertEquals("memberSpaceListAccess.size() must return: " + count, count, memberSpaceListAccess.getSize());
1257 
1258     memberSpaceListAccess = spaceService.getMemberSpacesByFilter("root", new SpaceFilter("my space"));
1259     assertNotNull("memberSpaceListAccess must not be null", memberSpaceListAccess);
1260     assertEquals("memberSpaceListAccess.size() must return: " + 0, 0, memberSpaceListAccess.getSize());
1261   }
1262 
1263   /**
1264    * Test {@link SpaceService#getPendingSpaces(String)}
1265    *
1266    * @throws Exception
1267    */
1268   public void testGetPendingSpaces() throws Exception {
1269     populateData();
1270     Space space = spaceService.getSpaceByDisplayName("Space1");
1271     spaceService.requestJoin(space, "paul");
1272     assertEquals(true, spaceService.isPending(space, "paul"));
1273   }
1274 
1275   /**
1276    * Test {@link SpaceService#getPendingSpacesWithListAccess(String)}
1277    *
1278    * @throws Exception
1279    * @since 1.2.0-GA
1280    */
1281   public void testGetPendingSpacesWithListAccess() throws Exception {
1282     int count = 20;
1283     for (int i = 0; i < count; i ++) {
1284       this.getSpaceInstance(i);
1285     }
1286     ListAccess<Space> foundSpaces = spaceService.getPendingSpacesWithListAccess("jame");
1287     assertNotNull("foundSpaces must not be null", foundSpaces);
1288     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1289     assertEquals("foundSpaces.load(0, 1).length must return: 1",
1290                  1, foundSpaces.load(0, 1).length);
1291     assertEquals("foundSpaces.load(0, count).length must return: " + count,
1292                  count, foundSpaces.load(0, count).length);
1293 
1294     foundSpaces = spaceService.getPendingSpacesWithListAccess("paul");
1295     assertNotNull("foundSpaces must not be null", foundSpaces);
1296     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1297 
1298     foundSpaces = spaceService.getPendingSpacesWithListAccess("hacker");
1299     assertNotNull("foundSpaces must not be null", foundSpaces);
1300     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1301 
1302     foundSpaces = spaceService.getPendingSpacesWithListAccess("ghost");
1303     assertNotNull("foundSpaces must not be null", foundSpaces);
1304     assertEquals("foundSpaces.getSize() must return: " + 0, 0, foundSpaces.getSize());
1305 
1306     foundSpaces = spaceService.getPendingSpacesWithListAccess("hellgate");
1307     assertNotNull("foundSpaces must not be null", foundSpaces);
1308     assertEquals("foundSpaces.getSize() must return: " + 0, 0, foundSpaces.getSize());
1309   }
1310 
1311   /**
1312    * Test {@link SpaceService#getPendingSpacesByFilter(String, SpaceFilter))}
1313    *
1314    * @throws Exception
1315    * @since 1.2.0-GA
1316    */
1317   public void testGetPendingSpacesByFilterWithSpaceNameSearchCondition() throws Exception {
1318     int count = 20;
1319     for (int i = 0; i < count; i ++) {
1320       this.getSpaceInstance(i);
1321     }
1322     String nameSpace = "my space";
1323     ListAccess<Space> foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter(nameSpace));
1324     assertNotNull("foundSpaces must not be null", foundSpaces);
1325     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1326     assertEquals("foundSpaces.load(0, 1).length must return: 1",
1327                  1, foundSpaces.load(0, 1).length);
1328     assertEquals("foundSpaces.load(0, count).length must return: " + count,
1329                  count, foundSpaces.load(0, count).length);
1330 
1331     foundSpaces = spaceService.getPendingSpacesByFilter("paul", new SpaceFilter(nameSpace));
1332     assertNotNull("foundSpaces must not be null", foundSpaces);
1333     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1334 
1335     foundSpaces = spaceService.getPendingSpacesByFilter("hacker", new SpaceFilter("space"));
1336     assertNotNull("foundSpaces must not be null", foundSpaces);
1337     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1338 
1339     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter("add new"));
1340     assertNotNull("foundSpaces must not be null", foundSpaces);
1341     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1342 
1343     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter("add*"));
1344     assertNotNull("foundSpaces must not be null", foundSpaces);
1345     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1346 
1347     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter("*add*"));
1348     assertNotNull("foundSpaces must not be null", foundSpaces);
1349     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1350 
1351     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter("*add"));
1352     assertNotNull("foundSpaces must not be null", foundSpaces);
1353     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1354 
1355     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter("*add*e"));
1356     assertNotNull("foundSpaces must not be null", foundSpaces);
1357     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1358 
1359     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter("*add*e*"));
1360     assertNotNull("foundSpaces must not be null", foundSpaces);
1361     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1362 
1363     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter("%add%e%"));
1364     assertNotNull("foundSpaces must not be null", foundSpaces);
1365     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1366 
1367     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter("%add*e%"));
1368     assertNotNull("foundSpaces must not be null", foundSpaces);
1369     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1370 
1371     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter("%add*e*"));
1372     assertNotNull("foundSpaces must not be null", foundSpaces);
1373     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1374 
1375     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter("no space"));
1376     assertNotNull("foundSpaces must not be null", foundSpaces);
1377     assertEquals("foundSpaces.getSize() must return: 0", 0, foundSpaces.getSize());
1378   }
1379 
1380   /**
1381    * Test {@link SpaceService#getPendingSpacesByFilter(String, SpaceFilter)}
1382    *
1383    * @throws Exception
1384    * @since 1.2.0-GA
1385    */
1386   public void testGetPendingSpacesByFilterWithFirstCharacterOfSpaceName() throws Exception {
1387     int count = 20;
1388     for (int i = 0; i < count; i ++) {
1389       this.getSpaceInstance(i);
1390     }
1391     ListAccess<Space> foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter('m'));
1392     assertNotNull("foundSpaces must not be null", foundSpaces);
1393     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1394     assertEquals("foundSpaces.load(0, 1).length must return: 1",
1395                  1, foundSpaces.load(0, 1).length);
1396     assertEquals("foundSpaces.load(0, count).length must return: " + count,
1397                  count, foundSpaces.load(0, count).length);
1398 
1399     foundSpaces = spaceService.getPendingSpacesByFilter("paul", new SpaceFilter('M'));
1400     assertNotNull("foundSpaces must not be null", foundSpaces);
1401     assertEquals("foundSpaces.getSize() must return: " + count, count, foundSpaces.getSize());
1402 
1403     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter('*'));
1404     assertNotNull("foundSpaces must not be null", foundSpaces);
1405     assertEquals("foundSpaces.getSize() must return: " + 0, 0, foundSpaces.getSize());
1406 
1407     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter('H'));
1408     assertNotNull("foundSpaces must not be null", foundSpaces);
1409     assertEquals("foundSpaces.getSize() must return: 0", 0, foundSpaces.getSize());
1410 
1411     foundSpaces = spaceService.getPendingSpacesByFilter("jame", new SpaceFilter('k'));
1412     assertNotNull("foundSpaces must not be null", foundSpaces);
1413     assertEquals("foundSpaces.getSize() must return: 0", 0, foundSpaces.getSize());
1414   }
1415 
1416   /**
1417    * Test {@link SpaceService#createSpace(Space, String)}
1418    *
1419    * @throws Exception
1420    */
1421   public void testCreateSpace() throws Exception {
1422     populateData();
1423     createMoreSpace("Space2");
1424     ListAccess<Space> spaceListAccess = spaceService.getAllSpacesWithListAccess();
1425     assertNotNull("spaceListAccess must not be null", spaceListAccess);
1426     assertEquals("spaceListAccess.getSize() must return: 2", 2, spaceListAccess.getSize());
1427   }
1428 
1429   /**
1430    * Test {@link SpaceService#createSpace(org.exoplatform.social.core.space.model.Space, String, String)}
1431    *
1432    */
1433    public void testCreateSpaceWithManagersAndMembers() throws SpaceException {
1434      String[] managers = {"manager"};
1435      String[] members = {"member1","member2","member3"};
1436      String creator = "root";
1437      String invitedGroup = "invited";
1438      Space space = new Space();
1439      space.setDisplayName("testSpace");
1440      space.setDescription("Space Description for Testing");
1441      String shortName = SpaceUtils.cleanString(space.getDisplayName());
1442      space.setGroupId("/spaces/" + shortName);
1443      space.setManagers(managers);
1444      space.setMembers(members);
1445      space.setPrettyName(space.getDisplayName());
1446      space.setPriority("3");
1447      space.setRegistration("validation");
1448      space.setTag("Space Tag for Testing");
1449      space.setType("classic");
1450      space.setUrl(shortName);
1451      space.setVisibility("public");
1452      spaceService.createSpace(space,creator,invitedGroup);
1453      tearDownSpaceList.add(space);
1454      // 2 = 1 creator + 1 managers
1455      assertEquals(2,space.getManagers().length);
1456      // 4 = 1 creator + 3 members
1457      assertEquals(4,space.getMembers().length);
1458    }
1459 
1460    /**
1461    * Test {@link SpaceService#createSpace(org.exoplatform.social.core.space.model.Space, String, List)}
1462    *
1463    */
1464    public void testCreateSpaceWithInvitation() throws Exception {
1465      String[] managers = {"manager"};
1466      String creator = "root";
1467      Space spaceCreated = createMoreSpace("invitedSpace");
1468      String[] users = {"member1", "member2"};
1469      spaceCreated.setMembers(users);
1470      spaceService.updateSpace(spaceCreated);
1471      Identity spaceIdentity = getService(IdentityManager.class).getOrCreateIdentity(SpaceIdentityProvider.NAME, "invitedspace", false);
1472      List<Identity> invitedIdentities = new ArrayList<>(Arrays.asList(tom, dragon, hearBreaker, spaceIdentity));
1473      Space space = new Space();
1474      space.setDisplayName("testSpace");
1475      space.setDescription("Space Description for Testing");
1476      String shortName = SpaceUtils.cleanString(space.getDisplayName());
1477      space.setGroupId("/spaces/" + shortName);
1478      space.setManagers(managers);
1479      space.setPrettyName(space.getDisplayName());
1480      space.setPriority("3");
1481      space.setRegistration("validation");
1482      space.setTag("Space Tag for Testing");
1483      space.setType("classic");
1484      space.setUrl(shortName);
1485      space.setVisibility("public");
1486      spaceService.createSpace(space,creator,invitedIdentities);
1487      tearDownSpaceList.add(space);
1488      // 2 = 1 creator + 1 managers
1489      assertEquals(2,space.getManagers().length);
1490      // 5 = member1, member2 from invitedSpace + tom + dragon + hearBreaker
1491      assertEquals(5,space.getInvitedUsers().length);
1492    }
1493 
1494   public void testCreateSpaceExceedingNameLimit () throws RuntimeException{
1495     Space space = new Space();
1496     String spaceDisplayName = "zzz0123456791011121314151617181920012345679101112131415161718192001234567910111213141516171819200123456791011121314151617181920012345679101112131415161718192001234567910111213141516171819200123456791011121314151617181920012345679101112131415161718192001234567910111213141516171819200123456791011121314151617181920";
1497     String shortName = "zzz";
1498     space.setDisplayName(spaceDisplayName);
1499     space.setPrettyName(shortName);
1500     space.setRegistration(Space.OPEN);
1501     space.setDescription("add new space ");
1502     space.setType(DefaultSpaceApplicationHandler.NAME);
1503     space.setVisibility(Space.PUBLIC);
1504     space.setRegistration(Space.VALIDATION);
1505     space.setPriority(Space.INTERMEDIATE_PRIORITY);
1506     String creator = "root";
1507     space.setGroupId("/spaces/" + shortName);
1508     try {
1509       spaceService.createSpace(space, creator);
1510       fail("Should have thrown an RuntimeException because Name length exceeds limits");
1511     }
1512     catch (RuntimeException e){
1513       assertTrue(e.getMessage().contains("space name cannot exceed 200 characters"));
1514     }
1515   }
1516 
1517   public void testCreateSpaceWithInvalidSpaceName() throws RuntimeException{
1518     Space space = new Space();
1519     String spaceDisplayName = "%zzz:^!/<>";
1520     space.setDisplayName(spaceDisplayName);
1521     String creator = "root";
1522     String shortName = "zzz";
1523     space.setDisplayName(spaceDisplayName);
1524     space.setPrettyName(shortName);
1525     space.setRegistration(Space.OPEN);
1526     space.setDescription("add new space ");
1527     space.setType(DefaultSpaceApplicationHandler.NAME);
1528     space.setVisibility(Space.PUBLIC);
1529     space.setRegistration(Space.VALIDATION);
1530     space.setPriority(Space.INTERMEDIATE_PRIORITY);
1531     try {
1532       spaceService.createSpace(space, creator);
1533       fail("Should have thrown an RuntimeException because Name is invalid");
1534     }
1535     catch (RuntimeException e){
1536       assertTrue(e.getMessage().contains("space name can only contain letters, digits or space characters only"));
1537     }
1538   }
1539 
1540   /**
1541    * Test {@link SpaceService#saveSpace(Space, boolean)}
1542    *
1543    * @throws Exception
1544    * @since 1.2.0-GA
1545    */
1546   public void testSaveSpace() throws Exception {
1547     Space space = this.getSpaceInstance(0);
1548     String spaceDisplayName = space.getDisplayName();
1549     String spaceDescription = space.getDescription();
1550     String groupId = space.getGroupId();
1551     Space savedSpace = spaceService.getSpaceByDisplayName(spaceDisplayName);
1552     assertNotNull("savedSpace must not be null", savedSpace);
1553     assertEquals("savedSpace.getDisplayName() must return: " + spaceDisplayName, spaceDisplayName, savedSpace.getDisplayName());
1554     assertEquals("savedSpace.getDescription() must return: " + spaceDescription, spaceDescription, savedSpace.getDescription());
1555     assertEquals("savedSpace.getGroupId() must return: " + groupId, groupId, savedSpace.getGroupId());
1556     assertEquals(null, savedSpace.getAvatarUrl());
1557   }
1558 
1559   /**
1560    * Test {@link SpaceService#renameSpace(Space, String)}
1561    *
1562    * @throws Exception
1563    * @since 1.2.8
1564    */
1565   public void testRenameSpace() throws Exception {
1566     Space space = this.getSpaceInstance(0);
1567     
1568     Identity identity = new Identity(SpaceIdentityProvider.NAME, space.getPrettyName());
1569     identityStorage.saveIdentity(identity);
1570     tearDownUserList.add(identity);
1571     
1572     String newDisplayName = "new display name";
1573     
1574     spaceService.renameSpace(space, newDisplayName);
1575     
1576     Space got = spaceService.getSpaceById(space.getId());
1577     assertEquals(newDisplayName, got.getDisplayName());
1578     
1579     {
1580       newDisplayName = "new display name with super admin";
1581       
1582       //
1583       spaceService.renameSpace(root.getRemoteId(), space, newDisplayName);
1584       
1585       got = spaceService.getSpaceById(space.getId());
1586       assertEquals(newDisplayName, got.getDisplayName());
1587     }
1588     
1589     {
1590       newDisplayName = "new display name with normal admin";
1591       
1592       //
1593       spaceService.renameSpace(mary.getRemoteId(), space, newDisplayName);
1594       
1595       got = spaceService.getSpaceById(space.getId());
1596       assertEquals(newDisplayName, got.getDisplayName());
1597     }
1598     
1599     {
1600       newDisplayName = "new display name with null remoteId";
1601       
1602       //
1603       spaceService.renameSpace(null, space, newDisplayName);
1604       
1605       got = spaceService.getSpaceById(space.getId());
1606       assertEquals(newDisplayName, got.getDisplayName());
1607     }
1608     
1609     Identity savedIdentity = identityStorage.findIdentity(SpaceIdentityProvider.NAME, space.getPrettyName());
1610     assertNotNull(savedIdentity);
1611   }
1612   
1613   /**
1614    * Test {@link SpaceService#saveSpace(Space, boolean)}
1615    *
1616    * @throws Exception
1617    * @since 1.2.0-GA
1618    */
1619   public void testUpdateSpaceAvatar() throws Exception {
1620 
1621     Space space = this.getSpaceInstance(0);
1622     Identity spaceIdentity = new Identity(SpaceIdentityProvider.NAME, space.getPrettyName());
1623     identityStorage.saveIdentity(spaceIdentity);
1624 
1625     tearDownUserList.add(spaceIdentity);
1626 
1627     InputStream inputStream = getClass().getResourceAsStream("/eXo-Social.png");
1628     AvatarAttachment avatarAttachment =
1629         new AvatarAttachment(null, "avatar", "png", inputStream, null, System.currentTimeMillis());
1630     space.setAvatarAttachment(avatarAttachment);
1631 
1632     spaceService.updateSpaceAvatar(space);
1633     spaceService.updateSpace(space);
1634 
1635     Space savedSpace = spaceService.getSpaceById(space.getId());
1636     assertFalse(savedSpace.getAvatarUrl() == null);
1637     String avatarRandomURL = savedSpace.getAvatarUrl();
1638     int indexOfRandomVar = avatarRandomURL.indexOf("/?upd=");
1639 
1640     String avatarURL = null;
1641     if(indexOfRandomVar != -1){
1642       avatarURL = avatarRandomURL.substring(0,indexOfRandomVar);
1643     } else {
1644       avatarURL = avatarRandomURL;
1645   }
1646     assertEquals(LinkProvider.escapeJCRSpecialCharacters(
1647             String.format(
1648               "/rest/jcr/repository/portal-test/production/soc:providers/soc:space/soc:%s/soc:profile/soc:avatar",
1649               space.getPrettyName())
1650             ),avatarURL);
1651 
1652   }
1653 
1654   /**
1655    * Test {@link SpaceService#deleteSpace(Space)}
1656    *
1657    * @throws Exception
1658    * @since 1.2.0-GA
1659    */
1660   public void testDeleteSpace() throws Exception {
1661     Space space = this.getSpaceInstance(0);
1662     String spaceDisplayName = space.getDisplayName();
1663     String spaceDescription = space.getDescription();
1664     String groupId = space.getGroupId();
1665     Space savedSpace = spaceService.getSpaceByDisplayName(spaceDisplayName);
1666     assertNotNull("savedSpace must not be null", savedSpace);
1667     assertEquals("savedSpace.getDisplayName() must return: " + spaceDisplayName, spaceDisplayName, savedSpace.getDisplayName());
1668     assertEquals("savedSpace.getDescription() must return: " + spaceDescription, spaceDescription, savedSpace.getDescription());
1669     assertEquals("savedSpace.getGroupId() must return: " + groupId, groupId, savedSpace.getGroupId());
1670     spaceService.deleteSpace(space);
1671     savedSpace = spaceService.getSpaceByDisplayName(spaceDisplayName);
1672     assertNull("savedSpace must be null", savedSpace);
1673   }
1674 
1675   public void testUpdateSpacePermissions() throws Exception {
1676     Space space = this.getSpaceInstance(0);
1677     space.setEditor("raul");
1678     try {
1679       spaceService.updateSpaceBanner(space);
1680       fail("Space member shouldn't be able to update space banner");
1681     } catch (Exception e) {
1682       // Expected
1683     }
1684     try {
1685       spaceService.updateSpaceAvatar(space);
1686       fail("Space member shouldn't be able to update space avatar");
1687     } catch (Exception e) {
1688       // Expected
1689     }
1690   }
1691 
1692   /**
1693    * Test {@link SpaceService#updateSpace(Space)}
1694    *
1695    * @throws Exception
1696    * @since 1.2.0-GA
1697    */
1698   public void testUpdateSpace() throws Exception {
1699     Space space = this.getSpaceInstance(0);
1700     String spaceDisplayName = space.getDisplayName();
1701     String spaceDescription = space.getDescription();
1702     String groupId = space.getGroupId();
1703     Space savedSpace = spaceService.getSpaceByDisplayName(spaceDisplayName);
1704     assertNotNull("savedSpace must not be null", savedSpace);
1705     assertEquals("savedSpace.getDisplayName() must return: " + spaceDisplayName, spaceDisplayName, savedSpace.getDisplayName());
1706     assertEquals("savedSpace.getDescription() must return: " + spaceDescription, spaceDescription, savedSpace.getDescription());
1707     assertEquals("savedSpace.getGroupId() must return: " + groupId, groupId, savedSpace.getGroupId());
1708 
1709     String updateSpaceDisplayName = "update new space display name";
1710     space.setDisplayName(updateSpaceDisplayName);
1711     space.setPrettyName(space.getDisplayName());
1712     spaceService.updateSpace(space);
1713     StorageUtils.persist();
1714     savedSpace = spaceService.getSpaceByDisplayName(updateSpaceDisplayName);
1715     assertNotNull("savedSpace must not be null", savedSpace);
1716     assertEquals("savedSpace.getDisplayName() must return: " + updateSpaceDisplayName, updateSpaceDisplayName, savedSpace.getDisplayName());
1717     assertEquals("savedSpace.getDescription() must return: " + spaceDescription, spaceDescription, savedSpace.getDescription());
1718     assertEquals("savedSpace.getGroupId() must return: " + groupId, groupId, savedSpace.getGroupId());
1719   }
1720 
1721   /**
1722    * Test {@link SpaceService#addPendingUser(Space, String)}
1723    *
1724    * @throws Exception
1725    * @since 1.2.0-GA
1726    */
1727   public void testAddPendingUser() throws Exception {
1728     Space space = this.getSpaceInstance(0);
1729     int pendingUsersCount = space.getPendingUsers().length;
1730     assertFalse("ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()) must be false",
1731                 ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()));
1732     spaceService.addPendingUser(space, newPendingUser.getRemoteId());
1733     space = spaceService.getSpaceByDisplayName(space.getDisplayName());
1734     assertEquals("space.getPendingUsers().length must return: " + pendingUsersCount + 1, pendingUsersCount + 1, space.getPendingUsers().length);
1735     assertTrue("ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()) must be true",
1736                ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()));
1737   }
1738 
1739   /**
1740    * Test {@link SpaceService#removePendingUser(Space, String)}
1741    *
1742    * @throws Exception
1743    * @since 1.2.0-GA
1744    */
1745   public void testRemovePendingUser() throws Exception {
1746     Space space = this.getSpaceInstance(0);
1747     int pendingUsersCount = space.getPendingUsers().length;
1748     assertFalse("ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()) must be false",
1749                 ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()));
1750     spaceService.addPendingUser(space, newPendingUser.getRemoteId());
1751     space = spaceService.getSpaceByDisplayName(space.getDisplayName());
1752     assertEquals("space.getPendingUsers().length must return: " + pendingUsersCount + 1,
1753                  pendingUsersCount + 1, space.getPendingUsers().length);
1754     assertTrue("ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()) must be true",
1755                ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()));
1756 
1757     spaceService.removePendingUser(space, newPendingUser.getRemoteId());
1758     space = spaceService.getSpaceByDisplayName(space.getDisplayName());
1759     assertEquals("space.getPendingUsers().length must return: " + pendingUsersCount,
1760                  pendingUsersCount, space.getPendingUsers().length);
1761     assertFalse("ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()) must be true",
1762                  ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()));
1763   }
1764 
1765   /**
1766    * Test {@link SpaceService#isPendingUser(Space, String)}
1767    *
1768    * @throws Exception@since 1.2.0-GA
1769    * @since 1.2.0-GA
1770    */
1771   public void testIsPendingUser() throws Exception {
1772     Space space = this.getSpaceInstance(0);
1773     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1774     assertNotNull("savedSpace must not be null", savedSpace);
1775     assertTrue("spaceService.isPendingUser(savedSpace, \"jame\") must return true", spaceService.isPendingUser(savedSpace, "jame"));
1776     assertTrue("spaceService.isPendingUser(savedSpace, \"paul\") must return true", spaceService.isPendingUser(savedSpace, "paul"));
1777     assertTrue("spaceService.isPendingUser(savedSpace, \"hacker\") must return true", spaceService.isPendingUser(savedSpace, "hacker"));
1778     assertFalse("spaceService.isPendingUser(savedSpace, \"newpendinguser\") must return false", spaceService.isPendingUser(savedSpace, "newpendinguser"));
1779   }
1780 
1781   /**
1782    * Test {@link SpaceService#addInvitedUser(Space, String)}
1783    *
1784    * @throws Exception
1785    *
1786    */
1787   public void testAddInvitedUser() throws Exception {
1788     Space space = this.getSpaceInstance(0);
1789     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1790     assertNotNull("savedSpace must not be null", savedSpace);
1791     int invitedUsersCount = savedSpace.getInvitedUsers().length;
1792     assertFalse("ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()) must return false",
1793                 ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()));
1794     spaceService.addInvitedUser(savedSpace, newInvitedUser.getRemoteId());
1795     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1796     assertEquals("savedSpace.getInvitedUsers().length must return: " + invitedUsersCount + 1,
1797                  invitedUsersCount + 1, savedSpace.getInvitedUsers().length);
1798     assertTrue("ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()) must return true",
1799                 ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()));
1800   }
1801 
1802   /**
1803    * Test {@link SpaceService#removeInvitedUser(Space, String)}
1804    *
1805    * @throws Exception
1806    * @since 1.2.0-GA
1807    */
1808   public void testRemoveInvitedUser() throws Exception {
1809     Space space = this.getSpaceInstance(0);
1810     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1811     assertNotNull("savedSpace must not be null", savedSpace);
1812     int invitedUsersCount = savedSpace.getInvitedUsers().length;
1813     assertFalse("ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()) must return false",
1814                 ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()));
1815     spaceService.addInvitedUser(savedSpace, newInvitedUser.getRemoteId());
1816     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1817     assertEquals("savedSpace.getInvitedUsers().length must return: " + invitedUsersCount + 1,
1818                  invitedUsersCount + 1, savedSpace.getInvitedUsers().length);
1819     assertTrue("ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()) must return true",
1820                 ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()));
1821     spaceService.removeInvitedUser(savedSpace, newInvitedUser.getRemoteId());
1822     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1823     assertEquals("savedSpace.getInvitedUsers().length must return: " + invitedUsersCount,
1824                  invitedUsersCount, savedSpace.getInvitedUsers().length);
1825     assertFalse("ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()) must return false",
1826                 ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()));
1827   }
1828 
1829   /**
1830    * Test {@link SpaceService#isInvitedUser(Space, String)}
1831    *
1832    * @throws Exception
1833    * @since 1.2.0-GA
1834    */
1835   public void testIsInvitedUser() throws Exception {
1836     Space space = this.getSpaceInstance(0);
1837     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1838     assertNotNull("savedSpace must not be null", savedSpace);
1839     assertTrue("spaceService.isInvitedUser(savedSpace, \"register1\") must return true", spaceService.isInvitedUser(savedSpace, "register1"));
1840     assertTrue("spaceService.isInvitedUser(savedSpace, \"mary\") must return true", spaceService.isInvitedUser(savedSpace, "mary"));
1841     assertFalse("spaceService.isInvitedUser(savedSpace, \"hacker\") must return false", spaceService.isInvitedUser(savedSpace, "hacker"));
1842     assertFalse("spaceService.isInvitedUser(savedSpace, \"nobody\") must return false", spaceService.isInvitedUser(savedSpace, "nobody"));
1843   }
1844 
1845   /**
1846    * Test {@link SpaceService#setManager(Space, String, boolean)}
1847    *
1848    * @throws Exception
1849    * @since 1.2.0-GA
1850    */
1851   public void testSetManager() throws Exception {
1852     int number = 0;
1853     Space space = new Space();
1854     space.setDisplayName("my space " + number);
1855     space.setPrettyName(space.getDisplayName());
1856     space.setRegistration(Space.OPEN);
1857     space.setDescription("add new space " + number);
1858     space.setType(DefaultSpaceApplicationHandler.NAME);
1859     space.setVisibility(Space.PUBLIC);
1860     space.setRegistration(Space.VALIDATION);
1861     space.setPriority(Space.INTERMEDIATE_PRIORITY);
1862     space.setGroupId("/space/space" + number);
1863     space.setUrl(space.getPrettyName());
1864     String[] spaceManagers = new String[] {"demo", "tom"};
1865     String[] members = new String[] {"raul", "ghost", "dragon"};
1866     String[] invitedUsers = new String[] {"register1", "mary"};
1867     String[] pendingUsers = new String[] {"jame", "paul", "hacker"};
1868     space.setInvitedUsers(invitedUsers);
1869     space.setPendingUsers(pendingUsers);
1870     space.setManagers(spaceManagers);
1871     space.setMembers(members);
1872 
1873     space = this.createSpaceNonInitApps(space, "demo", null);
1874 
1875     //Space space = this.getSpaceInstance(0);
1876     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1877     assertNotNull("savedSpace must not be null", savedSpace);
1878     int managers = savedSpace.getManagers().length;
1879     spaceService.setManager(savedSpace, "demo", true);
1880     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1881     assertEquals("savedSpace.getManagers().length must return: " + managers, managers, savedSpace.getManagers().length);
1882 
1883     spaceService.setManager(savedSpace, "john", true);
1884     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1885     assertEquals("savedSpace.getManagers().length must return: " + managers + 1, managers + 1, savedSpace.getManagers().length);
1886 
1887     spaceService.setManager(savedSpace, "demo", false);
1888     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1889     assertEquals("savedSpace.getManagers().length must return: " + managers, managers, savedSpace.getManagers().length);
1890 
1891     // Wait 3 secs to have activity stored
1892     try {
1893       IdentityManager identityManager = (IdentityManager) getContainer().getComponentInstanceOfType(IdentityManager.class);
1894       ActivityManager activityManager = (ActivityManager) getContainer().getComponentInstanceOfType(ActivityManager.class);
1895       Thread.sleep(3000);
1896       List<ExoSocialActivity> broadCastActivities = activityManager.getActivities(identityManager.getOrCreateIdentity(SpaceIdentityProvider.NAME, savedSpace.getPrettyName(), false), 0, 10);
1897       for (ExoSocialActivity activity : broadCastActivities) {
1898         activityManager.deleteActivity(activity);
1899       }
1900     } catch (InterruptedException e) {
1901       LOG.error(e.getMessage(), e);
1902     }
1903   }
1904 
1905 
1906   /**
1907    * Test {@link SpaceService#isManager(Space, String)}
1908    *
1909    * @throws Exception
1910    * @since 1.2.0-GA
1911    */
1912   public void testIsManager() throws Exception {
1913     Space space = this.getSpaceInstance(0);
1914     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1915     assertNotNull("savedSpace must not be null", savedSpace);
1916     assertTrue("spaceService.isManager(savedSpace, \"demo\") must return true", spaceService.isManager(savedSpace, "demo"));
1917     assertTrue("spaceService.isManager(savedSpace, \"tom\") must return true", spaceService.isManager(savedSpace, "tom"));
1918     assertFalse("spaceService.isManager(savedSpace, \"mary\") must return false", spaceService.isManager(savedSpace, "mary"));
1919     assertFalse("spaceService.isManager(savedSpace, \"john\") must return false", spaceService.isManager(savedSpace, "john"));
1920   }
1921 
1922   /**
1923    * Test {@link SpaceService#isOnlyManager(Space, String)}
1924    *
1925    * @throws Exception
1926    * @since 1.2.0-GA
1927    */
1928   public void testIsOnlyManager() throws Exception {
1929     Space space = this.getSpaceInstance(0);
1930     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1931     assertNotNull("savedSpace must not be null", savedSpace);
1932     assertFalse("spaceService.isOnlyManager(savedSpace, \"tom\") must return false", spaceService.isOnlyManager(savedSpace, "tom"));
1933     assertFalse("spaceService.isOnlyManager(savedSpace, \"demo\") must return false", spaceService.isOnlyManager(savedSpace, "demo"));
1934 
1935     savedSpace.setManagers(new String[] {"demo"});
1936     spaceService.updateSpace(savedSpace);
1937     assertTrue("spaceService.isOnlyManager(savedSpace, \"demo\") must return true", spaceService.isOnlyManager(savedSpace, "demo"));
1938     assertFalse("spaceService.isOnlyManager(savedSpace, \"tom\") must return false", spaceService.isOnlyManager(savedSpace, "tom"));
1939 
1940     savedSpace.setManagers(new String[] {"tom"});
1941     spaceService.updateSpace(savedSpace);
1942     assertFalse("spaceService.isOnlyManager(savedSpace, \"demo\") must return false", spaceService.isOnlyManager(savedSpace, "demo"));
1943     assertTrue("spaceService.isOnlyManager(savedSpace, \"tom\") must return true", spaceService.isOnlyManager(savedSpace, "tom"));
1944   }
1945 
1946   /**
1947    * Test {@link SpaceService#hasSettingPermission(Space, String)}
1948    *
1949    * @throws Exception
1950    * @since 1.2.0-GA
1951    */
1952   public void testHasSettingPermission() throws Exception {
1953     Space space = this.getSpaceInstance(0);
1954     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
1955     assertNotNull("savedSpace must not be null", savedSpace);
1956 
1957     assertTrue("spaceService.hasSettingPermission(savedSpace, \"demo\") must return true", spaceService.hasSettingPermission(savedSpace, "demo"));
1958     assertTrue("spaceService.hasSettingPermission(savedSpace, \"tom\") must return true", spaceService.hasSettingPermission(savedSpace, "tom"));
1959     assertTrue("spaceService.hasSettingPermission(savedSpace, \"root\") must return true", spaceService.hasSettingPermission(savedSpace, "root"));
1960     assertFalse("spaceService.hasSettingPermission(savedSpace, \"mary\") must return false", spaceService.hasSettingPermission(savedSpace, "mary"));
1961     assertFalse("spaceService.hasSettingPermission(savedSpace, \"john\") must return false", spaceService.hasSettingPermission(savedSpace, "john"));
1962   }
1963 
1964   /**
1965    * Test {@link SpaceService#registerSpaceListenerPlugin(org.exoplatform.social.core.space.SpaceListenerPlugin)}
1966    *
1967    * @throws Exception
1968    * @since 1.2.0-GA
1969    */
1970   public void testRegisterSpaceListenerPlugin() throws Exception {
1971     //TODO
1972   }
1973 
1974   /**
1975    * Test {@link SpaceService#unregisterSpaceListenerPlugin(org.exoplatform.social.core.space.SpaceListenerPlugin)}
1976    *
1977    * @throws Exception
1978    * @since 1.2.0-GA
1979    */
1980   public void testUnregisterSpaceListenerPlugin() throws Exception {
1981     //TODO
1982   }
1983 
1984   /**
1985    * Test {@link SpaceService#initApp(Space)}
1986    *
1987    * @throws Exception
1988    * @since 1.2.0-GA
1989    */
1990   public void testInitApp() throws Exception {
1991     //TODO Complete this
1992   }
1993 
1994   /**
1995    * Test {@link SpaceService#initApps(Space)}
1996    *
1997    * @throws Exception
1998    * @since 1.2.0-GA
1999    */
2000   public void testInitApps() throws Exception {
2001     //TODO Complete this
2002   }
2003 
2004   /**
2005    * Test {@link SpaceService#deInitApps(Space)}
2006    *
2007    * @throws Exception
2008    * @since 1.2.0-GA
2009    */
2010   public void testDeInitApps() throws Exception {
2011     //TODO Complete this
2012   }
2013 
2014   /**
2015    * Test {@link SpaceService#addMember(Space, String)}
2016    *
2017    * @throws Exception
2018    * @since 1.2.0-GA
2019    */
2020   public void testAddMember() throws Exception {
2021     int number = 0;
2022     Space space = new Space();
2023     space.setDisplayName("my space " + number);
2024     space.setPrettyName(space.getDisplayName());
2025     space.setRegistration(Space.OPEN);
2026     space.setDescription("add new space " + number);
2027     space.setType(DefaultSpaceApplicationHandler.NAME);
2028     space.setVisibility(Space.PUBLIC);
2029     space.setRegistration(Space.VALIDATION);
2030     space.setPriority(Space.INTERMEDIATE_PRIORITY);
2031     space.setGroupId("/space/space" + number);
2032     space.setUrl(space.getPrettyName());
2033     String[] spaceManagers = new String[] {"demo"};
2034     String[] members = new String[] {};
2035     String[] invitedUsers = new String[] {"register1", "mary"};
2036     String[] pendingUsers = new String[] {"jame", "paul", "hacker"};
2037     space.setInvitedUsers(invitedUsers);
2038     space.setPendingUsers(pendingUsers);
2039     space.setManagers(spaceManagers);
2040     space.setMembers(members);
2041 
2042     space = this.createSpaceNonInitApps(space, "demo", null);
2043 
2044     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2045     assertNotNull("savedSpace must not be null", savedSpace);
2046 
2047     spaceService.addMember(savedSpace, "root");
2048     spaceService.addMember(savedSpace, "mary");
2049     spaceService.addMember(savedSpace, "john");
2050     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2051     assertEquals("savedSpace.getMembers().length must return 4", 4, savedSpace.getMembers().length);
2052     // Wait 3 secs to have activity stored
2053     try {
2054       IdentityManager identityManager = (IdentityManager) getContainer().getComponentInstanceOfType(IdentityManager.class);
2055       ActivityManager activityManager = (ActivityManager) getContainer().getComponentInstanceOfType(ActivityManager.class);
2056       Thread.sleep(3000);
2057       List<ExoSocialActivity> broadCastActivities = activityManager.getActivities(identityManager.getOrCreateIdentity(SpaceIdentityProvider.NAME, savedSpace.getPrettyName(), false), 0, 10);
2058       for (ExoSocialActivity activity : broadCastActivities) {
2059         activityManager.deleteActivity(activity);
2060       }
2061     } catch (InterruptedException e) {
2062       LOG.error(e.getMessage(), e);
2063     }
2064   }
2065   
2066   /**
2067    * Test {@link SpaceService#addMember(Space, String)}
2068    *
2069    * @throws Exception
2070    * @since 1.2.0-GA
2071    */
2072   public void testAddMemberSpecialCharacter() throws Exception {
2073     String reg = "^\\p{L}[\\p{L}\\d\\s._,-]+$";
2074     Pattern pattern = Pattern.compile(reg);
2075     assertTrue(pattern.matcher("user-new.1").matches());
2076     assertTrue(pattern.matcher("user.new").matches());
2077     assertTrue(pattern.matcher("user-new").matches());
2078   
2079     int number = 0;
2080     Space space = new Space();
2081     space.setDisplayName("my space " + number);
2082     space.setPrettyName(space.getDisplayName());
2083     space.setRegistration(Space.OPEN);
2084     space.setDescription("add new space " + number);
2085     space.setType(DefaultSpaceApplicationHandler.NAME);
2086     space.setVisibility(Space.PUBLIC);
2087     space.setRegistration(Space.VALIDATION);
2088     space.setPriority(Space.INTERMEDIATE_PRIORITY);
2089     space.setGroupId("/space/space" + number);
2090     space.setUrl(space.getPrettyName());
2091     String[] spaceManagers = new String[] {"demo"};
2092     String[] members = new String[] {};
2093     String[] invitedUsers = new String[] {"register1", "mary"};
2094     String[] pendingUsers = new String[] {"jame", "paul", "hacker"};
2095     space.setInvitedUsers(invitedUsers);
2096     space.setPendingUsers(pendingUsers);
2097     space.setManagers(spaceManagers);
2098     space.setMembers(members);
2099 
2100     space = this.createSpaceNonInitApps(space, "demo", null);
2101 
2102     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2103     assertNotNull("savedSpace must not be null", savedSpace);
2104 
2105     User user = organizationService.getUserHandler().createUserInstance("user-new.1");
2106     organizationService.getUserHandler().createUser(user, false);
2107     user = organizationService.getUserHandler().createUserInstance("user.new");
2108     organizationService.getUserHandler().createUser(user, false);
2109     user = organizationService.getUserHandler().createUserInstance("user-new");
2110     organizationService.getUserHandler().createUser(user, false);
2111 
2112     spaceService.addMember(savedSpace, "user-new.1");
2113     spaceService.addMember(savedSpace, "user.new");
2114     spaceService.addMember(savedSpace, "user-new");
2115     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2116     assertEquals(4, savedSpace.getMembers().length);
2117   }
2118 
2119   
2120   /**
2121    * Test {@link SpaceService#removeMember(Space, String)}
2122    *
2123    * @throws Exception
2124    * @since 1.2.0-GA
2125    */
2126   public void testRemoveMember() throws Exception {
2127     int number = 0;
2128     Space space = new Space();
2129     space.setDisplayName("my space " + number);
2130     space.setPrettyName(space.getDisplayName());
2131     space.setRegistration(Space.OPEN);
2132     space.setDescription("add new space " + number);
2133     space.setType(DefaultSpaceApplicationHandler.NAME);
2134     space.setVisibility(Space.PUBLIC);
2135     space.setRegistration(Space.VALIDATION);
2136     space.setPriority(Space.INTERMEDIATE_PRIORITY);
2137     space.setGroupId("/space/space" + number);
2138     space.setUrl(space.getPrettyName());
2139     String[] spaceManagers = new String[] {"demo"};
2140     String[] members = new String[] {};
2141     String[] invitedUsers = new String[] {"register1", "mary"};
2142     String[] pendingUsers = new String[] {"jame", "paul", "hacker"};
2143     space.setInvitedUsers(invitedUsers);
2144     space.setPendingUsers(pendingUsers);
2145     space.setManagers(spaceManagers);
2146     space.setMembers(members);
2147 
2148     space = this.createSpaceNonInitApps(space, "demo", null);
2149     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2150     assertNotNull("savedSpace must not be null", savedSpace);
2151 
2152     spaceService.addMember(savedSpace, "root");
2153     spaceService.addMember(savedSpace, "mary");
2154     spaceService.addMember(savedSpace, "john");
2155     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2156     assertEquals("savedSpace.getMembers().length must return 4", 4, savedSpace.getMembers().length);
2157 
2158     spaceService.removeMember(savedSpace, "root");
2159     spaceService.removeMember(savedSpace, "mary");
2160     spaceService.removeMember(savedSpace, "john");
2161     assertEquals("savedSpace.getMembers().length must return 1", 1, savedSpace.getMembers().length);
2162     // Wait 3 secs to have activity stored
2163     try {
2164       IdentityManager identityManager = (IdentityManager) getContainer().getComponentInstanceOfType(IdentityManager.class);
2165       ActivityManager activityManager = (ActivityManager) getContainer().getComponentInstanceOfType(ActivityManager.class);
2166       Thread.sleep(3000);
2167       List<ExoSocialActivity> broadCastActivities = activityManager.getActivities(identityManager.getOrCreateIdentity(SpaceIdentityProvider.NAME, savedSpace.getPrettyName(), false), 0, 10);
2168       for (ExoSocialActivity activity : broadCastActivities) {
2169         activityManager.deleteActivity(activity);
2170       }
2171     } catch (InterruptedException e) {
2172       LOG.error(e.getMessage(), e);
2173     }
2174   }
2175 
2176   /**
2177    * Test {@link SpaceService#getMembers(Space)}
2178    *
2179    * @throws Exception
2180    * @since 1.2.0-GA
2181    */
2182   public void testGetMembers() throws Exception {
2183     Space space = this.getSpaceInstance(0);
2184     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2185     assertNotNull("savedSpace must not be null", savedSpace);
2186     assertEquals("spaceService.getMembers(savedSpace).size() must return: " + savedSpace.getMembers().length, savedSpace.getMembers().length, spaceService.getMembers(savedSpace).size());
2187   }
2188 
2189   /**
2190    * Test {@link SpaceService#setLeader(Space, String, boolean)}
2191    *
2192    * @throws Exception
2193    * @since 1.2.0-GA
2194    */
2195   public void testSetLeader() throws Exception {
2196     int number = 0;
2197     Space space = new Space();
2198     space.setDisplayName("my space " + number);
2199     space.setPrettyName(space.getDisplayName());
2200     space.setRegistration(Space.OPEN);
2201     space.setDescription("add new space " + number);
2202     space.setType(DefaultSpaceApplicationHandler.NAME);
2203     space.setVisibility(Space.PUBLIC);
2204     space.setRegistration(Space.VALIDATION);
2205     space.setPriority(Space.INTERMEDIATE_PRIORITY);
2206     space.setGroupId("/space/space" + number);
2207     space.setUrl(space.getPrettyName());
2208     String[] spaceManagers = new String[] {"demo", "tom"};
2209     String[] members = new String[] {"raul", "ghost", "dragon"};
2210     String[] invitedUsers = new String[] {"register1", "mary"};
2211     String[] pendingUsers = new String[] {"jame", "paul", "hacker"};
2212     space.setInvitedUsers(invitedUsers);
2213     space.setPendingUsers(pendingUsers);
2214     space.setManagers(spaceManagers);
2215     space.setMembers(members);
2216 
2217     space = this.createSpaceNonInitApps(space, "demo", null);
2218 
2219     //Space space = this.getSpaceInstance(0);
2220     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2221     assertNotNull("savedSpace must not be null", savedSpace);
2222     int managers = savedSpace.getManagers().length;
2223     spaceService.setLeader(savedSpace, "demo", true);
2224     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2225     assertEquals("savedSpace.getManagers().length must return: " + managers, managers, savedSpace.getManagers().length);
2226 
2227     spaceService.setLeader(savedSpace, "john", true);
2228     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2229     assertEquals("savedSpace.getManagers().length must return: " + managers + 1, managers + 1, savedSpace.getManagers().length);
2230 
2231     spaceService.setLeader(savedSpace, "demo", false);
2232     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2233     assertEquals("savedSpace.getManagers().length must return: " + managers, managers, savedSpace.getManagers().length);
2234 
2235     // Wait 3 secs to have activity stored
2236     try {
2237       IdentityManager identityManager = (IdentityManager) getContainer().getComponentInstanceOfType(IdentityManager.class);
2238       ActivityManager activityManager = (ActivityManager) getContainer().getComponentInstanceOfType(ActivityManager.class);
2239       Thread.sleep(3000);
2240       List<ExoSocialActivity> broadCastActivities = activityManager.getActivities(identityManager.getOrCreateIdentity(SpaceIdentityProvider.NAME, savedSpace.getPrettyName(), false), 0, 10);
2241       for (ExoSocialActivity activity : broadCastActivities) {
2242         activityManager.deleteActivity(activity);
2243       }
2244     } catch (InterruptedException e) {
2245       LOG.error(e.getMessage(), e);
2246     }
2247   }
2248 
2249   /**
2250    * Test {@link SpaceService#isLeader(Space, String)}
2251    *
2252    * @throws Exception
2253    * @since 1.2.0-GA
2254    */
2255   public void testIsLeader() throws Exception {
2256     Space space = this.getSpaceInstance(0);
2257     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2258     assertNotNull("savedSpace must not be null", savedSpace);
2259     assertTrue("spaceService.isLeader(savedSpace, \"demo\") must return true", spaceService.isLeader(savedSpace, "demo"));
2260     assertTrue("spaceService.isLeader(savedSpace, \"tom\") must return true", spaceService.isLeader(savedSpace, "tom"));
2261     assertFalse("spaceService.isLeader(savedSpace, \"mary\") must return false", spaceService.isLeader(savedSpace, "mary"));
2262     assertFalse("spaceService.isLeader(savedSpace, \"john\") must return false", spaceService.isLeader(savedSpace, "john"));
2263   }
2264 
2265   /**
2266    * Test {@link SpaceService#isOnlyLeader(Space, String)}
2267    *
2268    * @throws Exception
2269    * @since 1.2.0-GA
2270    */
2271   public void testIsOnlyLeader() throws Exception {
2272     Space space = this.getSpaceInstance(0);
2273     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2274     assertNotNull("savedSpace must not be null", savedSpace);
2275     assertFalse("spaceService.isOnlyLeader(savedSpace, \"tom\") must return false", spaceService.isOnlyLeader(savedSpace, "tom"));
2276     assertFalse("spaceService.isOnlyLeader(savedSpace, \"demo\") must return false", spaceService.isOnlyLeader(savedSpace, "demo"));
2277 
2278     savedSpace.setManagers(new String[] {"demo"});
2279     spaceService.updateSpace(savedSpace);
2280     assertTrue("spaceService.isOnlyLeader(savedSpace, \"demo\") must return true", spaceService.isOnlyLeader(savedSpace, "demo"));
2281     assertFalse("spaceService.isOnlyLeader(savedSpace, \"tom\") must return false", spaceService.isOnlyLeader(savedSpace, "tom"));
2282 
2283     savedSpace.setManagers(new String[] {"tom"});
2284     spaceService.updateSpace(savedSpace);
2285     assertFalse("spaceService.isOnlyLeader(savedSpace, \"demo\") must return false", spaceService.isOnlyLeader(savedSpace, "demo"));
2286     assertTrue("spaceService.isOnlyLeader(savedSpace, \"tom\") must return true", spaceService.isOnlyLeader(savedSpace, "tom"));
2287   }
2288 
2289   /**
2290    * Test {@link SpaceService#isMember(Space, String)}
2291    *
2292    * @throws Exception
2293    * @since 1.2.0-GA
2294    */
2295   public void testIsMember() throws Exception {
2296     Space space = this.getSpaceInstance(0);
2297     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2298     assertNotNull("savedSpace must not be null", savedSpace);
2299 
2300     assertTrue("spaceService.isMember(savedSpace, \"raul\") must return true", spaceService.isMember(savedSpace, "raul"));
2301     assertTrue("spaceService.isMember(savedSpace, \"ghost\") must return true", spaceService.isMember(savedSpace, "ghost"));
2302     assertTrue("spaceService.isMember(savedSpace, \"dragon\") must return true", spaceService.isMember(savedSpace, "dragon"));
2303     assertFalse("spaceService.isMember(savedSpace, \"stranger\") must return true", spaceService.isMember(savedSpace, "stranger"));
2304   }
2305 
2306   /**
2307    * Test {@link SpaceService#hasAccessPermission(Space, String)}
2308    *
2309    * @throws Exception
2310    * @since 1.2.0-GA
2311    */
2312   public void testHasAccessPermission() throws Exception {
2313     Space space = this.getSpaceInstance(0);
2314     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2315     assertNotNull("savedSpace must not be null", savedSpace);
2316 
2317     assertTrue("spaceService.hasAccessPermission(savedSpace, \"raul\") must return true", spaceService.hasAccessPermission(savedSpace, "raul"));
2318     assertTrue("spaceService.hasAccessPermission(savedSpace, \"ghost\") must return true", spaceService.hasAccessPermission(savedSpace, "ghost"));
2319     assertTrue("spaceService.hasAccessPermission(savedSpace, \"dragon\") must return true", spaceService.hasAccessPermission(savedSpace, "dragon"));
2320     assertTrue("spaceService.hasAccessPermission(savedSpace, \"tom\") must return true", spaceService.hasAccessPermission(savedSpace, "tom"));
2321     assertTrue("spaceService.hasAccessPermission(savedSpace, \"demo\") must return true", spaceService.hasAccessPermission(savedSpace, "demo"));
2322     assertTrue("spaceService.hasAccessPermission(savedSpace, \"root\") must return true", spaceService.hasAccessPermission(savedSpace, "root"));
2323     assertFalse("spaceService.hasAccessPermission(savedSpace, \"mary\") must return false", spaceService.hasAccessPermission(savedSpace, "mary"));
2324     assertFalse("spaceService.hasAccessPermission(savedSpace, \"john\") must return false", spaceService.hasAccessPermission(savedSpace, "john"));
2325   }
2326 
2327   /**
2328    * Test {@link SpaceService#hasEditPermission(Space, String)}
2329    *
2330    * @throws Exception
2331    * @since 1.2.0-GA
2332    */
2333   public void testHasEditPermission() throws Exception {
2334     Space space = this.getSpaceInstance(0);
2335     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2336     assertNotNull("savedSpace must not be null", savedSpace);
2337 
2338     assertTrue("spaceService.hasEditPermission(savedSpace, \"root\") must return true", spaceService.hasEditPermission(savedSpace, "root"));
2339     assertTrue("spaceService.hasEditPermission(savedSpace, \"demo\") must return true", spaceService.hasEditPermission(savedSpace, "demo"));
2340     assertTrue("spaceService.hasEditPermission(savedSpace, \"tom\") must return true", spaceService.hasEditPermission(savedSpace, "tom"));
2341     assertFalse("spaceService.hasEditPermission(savedSpace, \"mary\") must return false", spaceService.hasEditPermission(savedSpace, "mary"));
2342     assertFalse("spaceService.hasEditPermission(savedSpace, \"john\") must return false", spaceService.hasEditPermission(savedSpace, "john"));
2343     assertFalse("spaceService.hasEditPermission(savedSpace, \"raul\") must return false", spaceService.hasEditPermission(savedSpace, "raul"));
2344     assertFalse("spaceService.hasEditPermission(savedSpace, \"ghost\") must return false", spaceService.hasEditPermission(savedSpace, "ghost"));
2345     assertFalse("spaceService.hasEditPermission(savedSpace, \"dragon\") must return false", spaceService.hasEditPermission(savedSpace, "dragon"));
2346   }
2347 
2348   /**
2349    * Test {@link SpaceService#isInvited(Space, String)}
2350    *
2351    * @throws Exception
2352    * @since 1.2.0-GA
2353    */
2354   public void testIsInvited() throws Exception {
2355     Space space = this.getSpaceInstance(0);
2356     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2357     assertNotNull("savedSpace must not be null", savedSpace);
2358 
2359     assertTrue("spaceService.isInvited(savedSpace, \"register1\") must return true", spaceService.isInvited(savedSpace, "register1"));
2360     assertTrue("spaceService.isInvited(savedSpace, \"mary\") must return true", spaceService.isInvited(savedSpace, "mary"));
2361     assertFalse("spaceService.isInvited(savedSpace, \"demo\") must return false", spaceService.isInvited(savedSpace, "demo"));
2362     assertFalse("spaceService.isInvited(savedSpace, \"john\") must return false", spaceService.isInvited(savedSpace, "john"));
2363   }
2364 
2365   /**
2366    * Test {@link SpaceService#isPending(Space, String)}
2367    *
2368    * @throws Exception
2369    * @since 1.2.0-GA
2370    */
2371   public void testIsPending() throws Exception {
2372     Space space = this.getSpaceInstance(0);
2373     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2374     assertNotNull("savedSpace must not be null", savedSpace);
2375 
2376     assertTrue("spaceService.isPending(savedSpace, \"jame\") must return true", spaceService.isPending(savedSpace, "jame"));
2377     assertTrue("spaceService.isPending(savedSpace, \"paul\") must return true", spaceService.isPending(savedSpace, "paul"));
2378     assertTrue("spaceService.isPending(savedSpace, \"hacker\") must return true", spaceService.isPending(savedSpace, "hacker"));
2379     assertFalse("spaceService.isPending(savedSpace, \"mary\") must return false", spaceService.isPending(savedSpace, "mary"));
2380     assertFalse("spaceService.isPending(savedSpace, \"john\") must return false", spaceService.isPending(savedSpace, "john"));
2381   }
2382 
2383   /**
2384    * Test {@link SpaceService#installApplication(Space, String)}
2385    *
2386    * @throws Exception
2387    * @since 1.2.0-GA
2388    */
2389   public void testInstallApplication() throws Exception {
2390     //TODO Complete this
2391   }
2392 
2393   /**
2394    * Test {@link SpaceService#activateApplication(Space, String)}
2395    *
2396    * @throws Exception
2397    * @since 1.2.0-GA
2398    */
2399   public void testActivateApplication() throws Exception {
2400     startSessionAs("root");
2401     String spaceName = "testSpace";
2402     String creator = "john";
2403     Space space = new Space();
2404     space.setDisplayName(spaceName);
2405     space.setPrettyName(spaceName);
2406     space.setGroupId("/spaces/" + space.getPrettyName());
2407     space.setRegistration(Space.OPEN);
2408     space.setDescription("description of space" + spaceName);
2409     space.setType(DefaultSpaceApplicationHandler.NAME);
2410     space.setVisibility(Space.PRIVATE);
2411     space.setRegistration(Space.OPEN);
2412     space.setPriority(Space.INTERMEDIATE_PRIORITY);
2413     space.setManagers(new String[]{creator});
2414     space.setMembers(new String[]{creator});
2415     space = spaceService.createSpace(space, "root");
2416     tearDownSpaceList.add(space);
2417 
2418     StorageUtils.persist();
2419 
2420     assertTrue(space.getApp().contains("DashboardPortlet"));
2421     spaceService.removeApplication(space, "DashboardPortlet", "Dashboard");
2422     assertFalse(space.getApp().contains("DashboardPortlet"));
2423     spaceService.activateApplication(space, "DashboardPortlet");
2424     assertTrue(space.getApp().contains("DashboardPortlet"));
2425 
2426     NavigationContext navContext = SpaceUtils.getGroupNavigationContext(space.getGroupId());
2427     NodeContext<NodeContext<?>> homeNodeCtx = SpaceUtils.getHomeNodeWithChildren(navContext, space.getUrl());
2428     boolean found = homeNodeCtx.getNodes().stream()
2429             .filter(node -> "dashboard".equals(node.getName()))
2430             .findAny()
2431             .isPresent();
2432     assertTrue(found);
2433   }
2434 
2435   /**
2436    * Test {@link SpaceService#deactivateApplication(Space, String)}
2437    *
2438    * @throws Exception
2439    * @since 1.2.0-GA
2440    */
2441   public void testDeactivateApplication() throws Exception {
2442     //TODO Complete this
2443   }
2444 
2445   /**
2446    * Test {@link SpaceService#removeApplication(Space, String, String)}
2447    *
2448    * @throws Exception
2449    * @since 1.2.0-GA
2450    */
2451   public void testRemoveApplication() throws Exception {
2452     //TODO Complete this
2453   }
2454 
2455   /**
2456    * Test {@link SpaceService#requestJoin(Space, String)}
2457    *
2458    * @throws Exception
2459    * @since 1.2.0-GA
2460    */
2461   public void testRequestJoin() throws Exception {
2462     Space space = this.getSpaceInstance(0);
2463     int pendingUsersCount = space.getPendingUsers().length;
2464     assertFalse("ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()) must be false",
2465                 ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()));
2466     spaceService.requestJoin(space, newPendingUser.getRemoteId());
2467     space = spaceService.getSpaceByDisplayName(space.getDisplayName());
2468     assertEquals("space.getPendingUsers().length must return: " + pendingUsersCount + 1,
2469                  pendingUsersCount + 1, space.getPendingUsers().length);
2470     assertTrue("ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()) must be true",
2471                 ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()));
2472   }
2473 
2474   /**
2475    * Test {@link SpaceService#revokeRequestJoin(Space, String)}
2476    *
2477    * @throws Exception
2478    * @since 1.2.0-GA
2479    */
2480   public void testRevokeRequestJoin() throws Exception {
2481     Space space = this.getSpaceInstance(0);
2482     int pendingUsersCount = space.getPendingUsers().length;
2483     assertFalse("ArrayUtils.contains(space.getPendingUsers(), newPendingUser) must be false",
2484                 ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()));
2485     spaceService.requestJoin(space, newPendingUser.getRemoteId());
2486     space = spaceService.getSpaceByDisplayName(space.getDisplayName());
2487     assertEquals("space.getPendingUsers().length must return: " + pendingUsersCount + 1,
2488                  pendingUsersCount + 1, space.getPendingUsers().length);
2489     assertTrue("ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()) must be true",
2490                ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()));
2491 
2492     spaceService.revokeRequestJoin(space, newPendingUser.getRemoteId());
2493     space = spaceService.getSpaceByDisplayName(space.getDisplayName());
2494     assertEquals("space.getPendingUsers().length must return: " + pendingUsersCount, pendingUsersCount, space.getPendingUsers().length);
2495     assertFalse("ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()) must be true",
2496                 ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()));
2497   }
2498 
2499   /**
2500    * Test {@link SpaceService#inviteMember(Space, String)}
2501    *
2502    * @throws Exception
2503    * @since 1.2.0-GA
2504    */
2505   public void testInviteMember() throws Exception {
2506     Space space = this.getSpaceInstance(0);
2507     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2508     assertNotNull("savedSpace must not be null", savedSpace);
2509     int invitedUsersCount = savedSpace.getInvitedUsers().length;
2510     assertFalse("ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()) must return false",
2511                 ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()));
2512     spaceService.inviteMember(savedSpace, newInvitedUser.getRemoteId());
2513     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2514     assertEquals("savedSpace.getInvitedUsers().length must return: " + invitedUsersCount + 1,
2515                  invitedUsersCount + 1, savedSpace.getInvitedUsers().length);
2516     assertTrue("ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()) must return true",
2517                 ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()));
2518   }
2519 
2520   /**
2521    * Test {@link SpaceService#revokeInvitation(Space, String)}
2522    *
2523    * @throws Exception
2524    * @since 1.2.0-GA
2525    */
2526   public void testRevokeInvitation() throws Exception {
2527     Space space = this.getSpaceInstance(0);
2528     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2529     assertNotNull("savedSpace must not be null", savedSpace);
2530     int invitedUsersCount = savedSpace.getInvitedUsers().length;
2531     assertFalse("ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()) must return false",
2532                 ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()));
2533     spaceService.addInvitedUser(savedSpace, newInvitedUser.getRemoteId());
2534     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2535     assertEquals("savedSpace.getInvitedUsers().length must return: " + invitedUsersCount + 1,
2536                  invitedUsersCount + 1, savedSpace.getInvitedUsers().length);
2537     assertTrue("ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()) must return true",
2538                 ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()));
2539     spaceService.revokeInvitation(savedSpace, newInvitedUser.getRemoteId());
2540     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2541     assertEquals("savedSpace.getInvitedUsers().length must return: " + invitedUsersCount,
2542                  invitedUsersCount, savedSpace.getInvitedUsers().length);
2543     assertFalse("ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()) must return false",
2544                 ArrayUtils.contains(savedSpace.getInvitedUsers(), newInvitedUser.getRemoteId()));
2545   }
2546   
2547   public void testGetVisibleSpacesWithCondition() throws Exception {
2548     Space sp1 = this.createSpace("space test", "demo");
2549     Space sp2 = this.createSpace("space 11", "demo");
2550     
2551     SpaceListAccess list = spaceService.getVisibleSpacesWithListAccess("demo", new SpaceFilter("space test"));
2552     assertEquals(1, list.getSize());
2553     assertEquals(1, list.load(0, 10).length);
2554     list = spaceService.getVisibleSpacesWithListAccess("demo", new SpaceFilter("space 11"));
2555     assertEquals(1, list.getSize());
2556     assertEquals(1, list.load(0, 10).length);
2557     list = spaceService.getVisibleSpacesWithListAccess("demo", new SpaceFilter("space_11"));
2558     assertEquals(1, list.getSize());
2559     assertEquals(1, list.load(0, 10).length);
2560     list = spaceService.getVisibleSpacesWithListAccess("demo", new SpaceFilter("space"));
2561     assertEquals(2, list.getSize());
2562     assertEquals(2, list.load(0, 10).length);
2563   }
2564   
2565   public void testGetVisibleSpacesWithSpecialCharacters() throws Exception {
2566     Space space1 = new Space();
2567     space1.setDisplayName("広いニーズ");
2568     space1.setPrettyName("広いニーズ");
2569     space1.setDescription(StringEscapeUtils.escapeHtml("広いニーズに応えます。"));
2570     space1.setManagers(new String[]{"root"});
2571     space1.setMembers(new String[]{"root","mary"});
2572     space1.setType(DefaultSpaceApplicationHandler.NAME);
2573     space1.setRegistration(Space.OPEN);
2574     createSpaceNonInitApps(space1, "mary", null);
2575     
2576     Space space2 = new Space();
2577     space2.setDisplayName("ça c'est la vie");
2578     space2.setPrettyName("ça c'est la vie");
2579     space2.setManagers(new String[]{"root"});
2580     space2.setMembers(new String[]{"root","mary"});
2581     space2.setType(DefaultSpaceApplicationHandler.NAME);
2582     space2.setRegistration(Space.OPEN);
2583     createSpaceNonInitApps(space2, "mary", null);
2584     
2585     Space space3 = new Space();
2586     space3.setDisplayName("Đây là không gian tiếng Việt");
2587     space3.setPrettyName("Đây là không gian tiếng Việt");
2588     space3.setManagers(new String[]{"root"});
2589     space3.setMembers(new String[]{"root","mary"});
2590     space3.setType(DefaultSpaceApplicationHandler.NAME);
2591     space3.setRegistration(Space.OPEN);
2592     createSpaceNonInitApps(space3, "mary", null);
2593     
2594     SpaceListAccess list = spaceService.getVisibleSpacesWithListAccess("root", new SpaceFilter("広いニーズ"));
2595     assertEquals(1, list.getSize());
2596     assertEquals(1, list.load(0, 10).length);
2597     list = spaceService.getVisibleSpacesWithListAccess("root", new SpaceFilter("広いニーズに応えます。"));
2598     assertEquals(1, list.getSize());
2599     assertEquals(1, list.load(0, 10).length);
2600     list = spaceService.getVisibleSpacesWithListAccess("mary", new SpaceFilter("広いニーズ"));
2601     assertEquals(1, list.getSize());
2602     assertEquals(1, list.load(0, 10).length);
2603     
2604     list = spaceService.getVisibleSpacesWithListAccess("root", new SpaceFilter("ça c'est la vie"));
2605     assertEquals(1, list.getSize());
2606     assertEquals(1, list.load(0, 10).length);
2607     list = spaceService.getVisibleSpacesWithListAccess("mary", new SpaceFilter("ça c'est la vie"));
2608     assertEquals(1, list.getSize());
2609     assertEquals(1, list.load(0, 10).length);
2610     
2611     list = spaceService.getVisibleSpacesWithListAccess("root", new SpaceFilter("Đây là không gian tiếng Việt"));
2612     assertEquals(1, list.getSize());
2613     assertEquals(1, list.load(0, 10).length);
2614     list = spaceService.getVisibleSpacesWithListAccess("mary", new SpaceFilter("Đây là không gian tiếng Việt"));
2615     assertEquals(1, list.getSize());
2616     assertEquals(1, list.load(0, 10).length);
2617   }
2618 
2619   /**
2620    * Test {@link SpaceService#acceptInvitation(Space, String)}
2621    *
2622    * @throws Exception
2623    * @since 1.2.0-GA
2624    */
2625   public void testAcceptInvitation() throws Exception {
2626     int number = 0;
2627     Space space = new Space();
2628     space.setDisplayName("my space " + number);
2629     space.setPrettyName(space.getDisplayName());
2630     space.setRegistration(Space.OPEN);
2631     space.setDescription("add new space " + number);
2632     space.setType(DefaultSpaceApplicationHandler.NAME);
2633     space.setVisibility(Space.PUBLIC);
2634     space.setRegistration(Space.VALIDATION);
2635     space.setPriority(Space.INTERMEDIATE_PRIORITY);
2636     space.setGroupId("/space/space" + number);
2637     space.setUrl(space.getPrettyName());
2638     String[] spaceManagers = new String[] {"demo"};
2639     String[] members = new String[] {};
2640     String[] invitedUsers = new String[] {"register1", "mary"};
2641     String[] pendingUsers = new String[] {"jame", "paul", "hacker"};
2642     space.setInvitedUsers(invitedUsers);
2643     space.setPendingUsers(pendingUsers);
2644     space.setManagers(spaceManagers);
2645     space.setMembers(members);
2646 
2647     space = this.createSpaceNonInitApps(space, "demo", null);
2648 
2649     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2650     assertNotNull("savedSpace must not be null", savedSpace);
2651 
2652     spaceService.acceptInvitation(savedSpace, "root");
2653     spaceService.acceptInvitation(savedSpace, "mary");
2654     spaceService.acceptInvitation(savedSpace, "john");
2655     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2656     assertEquals("savedSpace.getMembers().length must return 4", 4, savedSpace.getMembers().length);
2657     // Wait 3 secs to have activity stored
2658     try {
2659       IdentityManager identityManager = (IdentityManager) getContainer().getComponentInstanceOfType(IdentityManager.class);
2660       ActivityManager activityManager = (ActivityManager) getContainer().getComponentInstanceOfType(ActivityManager.class);
2661       Thread.sleep(3000);
2662       List<ExoSocialActivity> broadCastActivities = activityManager.getActivities(identityManager.getOrCreateIdentity(SpaceIdentityProvider.NAME, savedSpace.getPrettyName(), false), 0, 10);
2663       for (ExoSocialActivity activity : broadCastActivities) {
2664         activityManager.deleteActivity(activity);
2665       }
2666     } catch (InterruptedException e) {
2667       LOG.error(e.getMessage(), e);
2668     }
2669   }
2670 
2671   /**
2672    * Test {@link SpaceService#denyInvitation(Space, String)}
2673    *
2674    * @throws Exception
2675    * @since 1.2.0-GA
2676    */
2677   public void testDenyInvitation() throws Exception {
2678     Space space = this.getSpaceInstance(0);
2679     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2680     assertNotNull("savedSpace must not be null", savedSpace);
2681 
2682     spaceService.denyInvitation(savedSpace, "new member 1");
2683     spaceService.denyInvitation(savedSpace, "new member 2");
2684     spaceService.denyInvitation(savedSpace, "new member 3");
2685     assertEquals("savedSpace.getMembers().length must return 2", 2, savedSpace.getInvitedUsers().length);
2686 
2687     spaceService.denyInvitation(savedSpace, "raul");
2688     spaceService.denyInvitation(savedSpace, "ghost");
2689     spaceService.denyInvitation(savedSpace, "dragon");
2690     assertEquals("savedSpace.getMembers().length must return 2", 2, savedSpace.getInvitedUsers().length);
2691 
2692     spaceService.denyInvitation(savedSpace, "register1");
2693     spaceService.denyInvitation(savedSpace, "mary");
2694     assertEquals("savedSpace.getMembers().length must return 0", 0, savedSpace.getInvitedUsers().length);
2695   }
2696 
2697   /**
2698    * Test {@link SpaceService#validateRequest(Space, String)}
2699    *
2700    * @throws Exception
2701    * @since 1.2.0-GA
2702    */
2703   public void testValidateRequest() throws Exception {
2704     int number = 0;
2705     Space space = new Space();
2706     space.setDisplayName("my space " + number);
2707     space.setPrettyName(space.getDisplayName());
2708     space.setRegistration(Space.OPEN);
2709     space.setDescription("add new space " + number);
2710     space.setType(DefaultSpaceApplicationHandler.NAME);
2711     space.setVisibility(Space.PUBLIC);
2712     space.setRegistration(Space.VALIDATION);
2713     space.setPriority(Space.INTERMEDIATE_PRIORITY);
2714     space.setGroupId("/space/space" + number);
2715     space.setUrl(space.getPrettyName());
2716     String[] spaceManagers = new String[] {"demo"};
2717     String[] members = new String[] {};
2718     String[] invitedUsers = new String[] {"register1", "mary"};
2719     String[] pendingUsers = new String[] {"jame", "paul", "hacker"};
2720     space.setInvitedUsers(invitedUsers);
2721     space.setPendingUsers(pendingUsers);
2722     space.setManagers(spaceManagers);
2723     space.setMembers(members);
2724 
2725     space = this.createSpaceNonInitApps(space, "demo", null);
2726 
2727     Space savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2728     assertNotNull("savedSpace must not be null", savedSpace);
2729 
2730     spaceService.validateRequest(savedSpace, "root");
2731     spaceService.validateRequest(savedSpace, "mary");
2732     spaceService.validateRequest(savedSpace, "john");
2733     savedSpace = spaceService.getSpaceByDisplayName(space.getDisplayName());
2734     assertEquals("savedSpace.getMembers().length must return 4", 4, savedSpace.getMembers().length);
2735     // Wait 3 secs to have activity stored
2736     try {
2737       IdentityManager identityManager = (IdentityManager) getContainer().getComponentInstanceOfType(IdentityManager.class);
2738       ActivityManager activityManager = (ActivityManager) getContainer().getComponentInstanceOfType(ActivityManager.class);
2739       Thread.sleep(3000);
2740       List<ExoSocialActivity> broadCastActivities = activityManager.getActivities(identityManager.getOrCreateIdentity(SpaceIdentityProvider.NAME, savedSpace.getPrettyName(), false), 0, 10);
2741       for (ExoSocialActivity activity : broadCastActivities) {
2742         activityManager.deleteActivity(activity);
2743       }
2744     } catch (InterruptedException e) {
2745       LOG.error(e.getMessage(), e);
2746     }
2747   }
2748 
2749   /**
2750    * Test {@link SpaceService#declineRequest(Space, String)}
2751    *
2752    * @throws Exception
2753    * @since 1.2.0-GA
2754    */
2755   public void testDeclineRequest() throws Exception {
2756     Space space = this.getSpaceInstance(0);
2757     int pendingUsersCount = space.getPendingUsers().length;
2758     assertFalse("ArrayUtils.contains(space.getPendingUsers(), newPendingUser) must be false",
2759                 ArrayUtils.contains(space.getPendingUsers(), newPendingUser.getRemoteId()));
2760     spaceService.addPendingUser(space, newInvitedUser.getRemoteId());
2761     space = spaceService.getSpaceByDisplayName(space.getDisplayName());
2762     assertEquals("space.getPendingUsers().length must return: " + pendingUsersCount + 1,
2763                  pendingUsersCount + 1, space.getPendingUsers().length);
2764     assertTrue("ArrayUtils.contains(space.getPendingUsers(), newInvitedUser.getRemoteId()) must be true",
2765                 ArrayUtils.contains(space.getPendingUsers(), newInvitedUser.getRemoteId()));
2766 
2767     spaceService.declineRequest(space, newInvitedUser.getRemoteId());
2768     space = spaceService.getSpaceByDisplayName(space.getDisplayName());
2769     assertEquals("space.getPendingUsers().length must return: " + pendingUsersCount,
2770                  pendingUsersCount, space.getPendingUsers().length);
2771     assertFalse("ArrayUtils.contains(space.getPendingUsers(), newInvitedUser.getRemoteId()) must be true",
2772                 ArrayUtils.contains(space.getPendingUsers(), newInvitedUser.getRemoteId()));
2773   }
2774 
2775   /**
2776    * Test {@link SpaceService#registerSpaceLifeCycleListener(SpaceLifeCycleListener)}
2777    *
2778    * @throws Exception
2779    * @since 1.2.0-GA
2780    */
2781   public void testRegisterSpaceLifeCybleListener() throws Exception {
2782     //TODO Complete this
2783   }
2784 
2785   /**
2786    * Test {@link SpaceService#unregisterSpaceLifeCycleListener(SpaceLifeCycleListener)}
2787    *
2788    * @throws Exception
2789    * @since 1.2.0-GA
2790    */
2791   public void testUnRegisterSpaceLifeCycleListener() throws Exception {
2792     //TODO Complete this
2793   }
2794 
2795   /**
2796    * Test {@link SpaceService#setPortletsPrefsRequired(org.exoplatform.social.core.application.PortletPreferenceRequiredPlugin)}
2797    *
2798    * @throws Exception
2799    * @since 1.2.0-GA
2800    */
2801   public void testSetPortletsPrefsRequired() throws Exception {
2802     //TODO Complete this
2803   }
2804 
2805   /**
2806    * Test {@link SpaceService#getPortletsPrefsRequired()}
2807    *
2808    * @throws Exception
2809    * @since 1.2.0-GA
2810    */
2811   public void testGetPortletsPrefsRequired() throws Exception {
2812     //TODO Complete this
2813   }
2814 
2815   /**
2816    * Test {@link SpaceService#addSpaceTemplateConfigPlugin(SpaceTemplateConfigPlugin)} (org.exoplatform.social.core.space.spaceTemplateConfigPlugin)}
2817    *
2818    * @throws Exception
2819    * @since 1.2.0-GA
2820    */
2821   public void testAddSpaceTemplateConfigPlugin() throws Exception {
2822     //TODO Complete this
2823   }
2824 
2825   /**
2826    * Test {@link SpaceService}
2827    *
2828    * @throws Exception
2829    * @since 1.2.0-GA
2830    */
2831   public void testGetSpaceTemplateConfigPlugin() throws Exception {
2832     //TODO Complete this
2833   }
2834   
2835   /**
2836    * Test {@link org.exoplatform.social.core.storage.SpaceStorage#getVisibleSpaces(String)}
2837    *
2838    * @throws Exception
2839    * @since 1.2.5-GA
2840    */
2841   public void testGetVisibleSpaces() throws Exception {
2842     int countSpace = 10;
2843     Space []listSpace = new Space[10];
2844     
2845     //there are 6 spaces with visible = 'private'
2846     for (int i = 0; i < countSpace; i ++) {
2847     
2848       if (i < 6)
2849          //[0->5] :: there are 6 spaces with visible = 'private'
2850         listSpace[i] = this.getSpaceInstance(i, Space.PRIVATE, Space.OPEN, "demo");
2851       else
2852         //[6->9]:: there are 4 spaces with visible = 'hidden'
2853         listSpace[i] = this.getSpaceInstance(i, Space.HIDDEN, Space.OPEN, "demo");
2854       
2855       spaceService.saveSpace(listSpace[i], true);
2856       StorageUtils.persist();
2857     }
2858     
2859     //visible with remoteId = 'demo'  return 10 spaces
2860     {
2861       List<Space> visibleAllSpaces = spaceService.getVisibleSpaces("demo", null);
2862       assertNotNull("visibleSpaces must not be  null", visibleAllSpaces);
2863       assertEquals("visibleSpaces() must return: " + countSpace, countSpace, visibleAllSpaces.size());
2864     }
2865     
2866     
2867   }
2868   
2869   
2870   
2871   /**
2872    * Test {@link org.exoplatform.social.core.storage.SpaceStorage#getVisibleSpaces(String)}
2873    *
2874    * @throws Exception
2875    * @since 1.2.5-GA
2876    */
2877   public void testGetVisibleSpacesCloseRegistration() throws Exception {
2878     int countSpace = 10;
2879     Space []listSpace = new Space[10];
2880     
2881     //there are 6 spaces with visible = 'private'
2882     for (int i = 0; i < countSpace; i ++) {
2883     
2884       if (i < 6)
2885          //[0->5] :: there are 6 spaces with visible = 'private'
2886         listSpace[i] = this.getSpaceInstance(i, Space.PRIVATE, Space.CLOSE, "demo");
2887       else
2888         //[6->9]:: there are 4 spaces with visible = 'hidden'
2889         listSpace[i] = this.getSpaceInstance(i, Space.HIDDEN, Space.CLOSE, "demo");
2890       
2891       spaceService.saveSpace(listSpace[i], true);
2892       StorageUtils.persist();
2893     }
2894     
2895     //visible with remoteId = 'demo'  return 10 spaces
2896     {
2897       List<Space> visibleAllSpaces = spaceService.getVisibleSpaces("demo", null);
2898       assertNotNull("visibleSpaces must not be  null", visibleAllSpaces);
2899       assertEquals("visibleSpaces() must return: " + countSpace, countSpace, visibleAllSpaces.size());
2900     }
2901     
2902     //visible with remoteId = 'mary'  return 6 spaces: can see
2903     {
2904       int registrationCloseSpaceCount = 6;
2905       List<Space> registrationCloseSpaces = spaceService.getVisibleSpaces("mary", null);
2906       assertNotNull("registrationCloseSpaces must not be  null", registrationCloseSpaces);
2907       assertEquals("registrationCloseSpaces must return: " + registrationCloseSpaceCount, registrationCloseSpaceCount, registrationCloseSpaces.size());
2908     }
2909     
2910    
2911   }
2912   
2913   /**
2914    * Test {@link org.exoplatform.social.core.storage.SpaceStorage#getVisibleSpaces(String)}
2915    *
2916    * @throws Exception
2917    * @since 1.2.5-GA
2918    */
2919   public void testGetVisibleSpacesInvitedMember() throws Exception {
2920     int countSpace = 10;
2921     Space[] listSpace = new Space[10];
2922     
2923     //there are 6 spaces with visible = 'private'
2924     for (int i = 0; i < countSpace; i ++) {
2925     
2926       if (i < 6)
2927          //[0->5] :: there are 6 spaces with visible = 'private'
2928         listSpace[i] = this.getSpaceInstanceInvitedMember(i, Space.PRIVATE, Space.CLOSE, new String[] {"mary", "hacker"}, "demo");
2929       else
2930         //[6->9]:: there are 4 spaces with visible = 'hidden'
2931         listSpace[i] = this.getSpaceInstance(i, Space.HIDDEN, Space.CLOSE, "demo");
2932       
2933       spaceService.saveSpace(listSpace[i], true);
2934       StorageUtils.persist();
2935     }
2936     
2937     //visible with remoteId = 'demo'  return 10 spaces
2938     {
2939       List<Space> visibleAllSpaces = spaceService.getVisibleSpaces("demo", null);
2940       assertNotNull("visibleSpaces must not be  null", visibleAllSpaces);
2941       assertEquals("visibleSpaces() must return: " + countSpace, countSpace, visibleAllSpaces.size());
2942     }
2943     
2944     //visible with invited = 'mary'  return 6 spaces
2945     {
2946       int invitedSpaceCount1 = 6;
2947       List<Space> invitedSpaces1 = spaceService.getVisibleSpaces("mary", null);
2948       assertNotNull("invitedSpaces must not be  null", invitedSpaces1);
2949       assertEquals("invitedSpaces must return: " + invitedSpaceCount1, invitedSpaceCount1, invitedSpaces1.size());
2950     }
2951     
2952     //visible with invited = 'hacker'  return 6 spaces
2953     {
2954       int invitedSpaceCount1 = 6;
2955       List<Space> invitedSpaces1 = spaceService.getVisibleSpaces("hacker", null);
2956       assertNotNull("invitedSpaces must not be  null", invitedSpaces1);
2957       assertEquals("invitedSpaces must return: " + invitedSpaceCount1, invitedSpaceCount1, invitedSpaces1.size());
2958     }
2959     
2960     //visible with invited = 'paul'  return 6 spaces
2961     {
2962       int invitedSpaceCount2 = 6;
2963       List<Space> invitedSpaces2 = spaceService.getVisibleSpaces("paul", null);
2964       assertNotNull("invitedSpaces must not be  null", invitedSpaces2);
2965       assertEquals("invitedSpaces must return: " + invitedSpaceCount2, invitedSpaceCount2, invitedSpaces2.size());
2966     }
2967   }
2968 
2969   public void testGetLastSpaces() throws Exception {
2970     populateData();
2971     createMoreSpace("Space2");
2972     List<Space> lastSpaces = spaceService.getLastSpaces(1);
2973     assertEquals(1, lastSpaces.size());
2974     Space sp1 = lastSpaces.get(0);
2975     lastSpaces = spaceService.getLastSpaces(1);
2976     assertEquals(1, lastSpaces.size());
2977     assertEquals(sp1, lastSpaces.get(0));
2978     lastSpaces = spaceService.getLastSpaces(5);
2979     assertEquals(2, lastSpaces.size());
2980     assertEquals(sp1, lastSpaces.get(0));
2981     Space newSp1 = createMoreSpace("newSp1");
2982     lastSpaces = spaceService.getLastSpaces(1);
2983     assertEquals(1, lastSpaces.size());
2984     assertEquals(newSp1, lastSpaces.get(0));
2985     lastSpaces = spaceService.getLastSpaces(5);
2986     assertEquals(3, lastSpaces.size());
2987     assertEquals(newSp1, lastSpaces.get(0));
2988     Space newSp2 = createMoreSpace("newSp2");
2989     lastSpaces = spaceService.getLastSpaces(1);
2990     assertEquals(1, lastSpaces.size());
2991     assertEquals(newSp2, lastSpaces.get(0));
2992     lastSpaces = spaceService.getLastSpaces(5);
2993     assertEquals(4, lastSpaces.size());
2994     assertEquals(newSp2, lastSpaces.get(0));
2995     assertEquals(newSp1, lastSpaces.get(1));
2996     spaceService.deleteSpace(newSp1);
2997     lastSpaces = spaceService.getLastSpaces(5);
2998     assertEquals(3, lastSpaces.size());
2999     assertEquals(newSp2, lastSpaces.get(0));
3000     assertFalse(newSp1.equals(lastSpaces.get(1)));
3001     spaceService.deleteSpace(newSp2);
3002     lastSpaces = spaceService.getLastSpaces(5);
3003     assertEquals(2, lastSpaces.size());
3004     assertEquals(sp1, lastSpaces.get(0));
3005   }
3006 
3007   public void testSpacesSuperManager() throws Exception {
3008     Space space = createSpace("spacename1", "root");
3009     space.setVisibility(Space.PUBLIC);
3010     space.setRegistration(Space.OPEN);
3011     spaceService.updateSpace(space);
3012 
3013     space = createSpace("spacename2", "root");
3014     space.setVisibility(Space.PUBLIC);
3015     space.setRegistration(Space.CLOSE);
3016     spaceService.updateSpace(space);
3017 
3018     space = createSpace("spacename3", "root");
3019     space.setVisibility(Space.PRIVATE);
3020     space.setRegistration(Space.OPEN);
3021     spaceService.updateSpace(space);
3022 
3023     space = createSpace("spacename4", "root");
3024     space.setVisibility(Space.PRIVATE);
3025     space.setRegistration(Space.CLOSE);
3026     spaceService.updateSpace(space);
3027 
3028     space = createSpace("spacename5", "root");
3029     space.setVisibility(Space.HIDDEN);
3030     space.setRegistration(Space.OPEN);
3031     spaceService.updateSpace(space);
3032 
3033     space = createSpace("spacename6", "root");
3034     space.setVisibility(Space.HIDDEN);
3035     space.setRegistration(Space.CLOSE);
3036     spaceService.updateSpace(space);
3037 
3038     User user = organizationService.getUserHandler().createUserInstance("user-super-1");
3039     organizationService.getUserHandler().createUser(user, false);
3040     Group group = organizationService.getGroupHandler().createGroupInstance();
3041     group.setGroupName("testgroup");
3042     organizationService.getGroupHandler().addChild(null, group, true);
3043     MembershipType mstype = organizationService.getMembershipTypeHandler().createMembershipTypeInstance();
3044     mstype.setName("mstypetest");
3045     organizationService.getMembershipTypeHandler().createMembershipType(mstype, true);
3046 
3047     organizationService.getMembershipHandler().linkMembership(user, group, mstype, true);
3048 
3049     String userName = user.getUserName();
3050 
3051     assertEquals(6, spaceService.getAllSpacesWithListAccess().getSize());
3052 
3053     assertFalse(spaceService.isSuperManager(userName));
3054     assertFalse(spaceService.hasAccessPermission(space, userName));
3055     assertFalse(spaceService.hasSettingPermission(space, userName));
3056     assertEquals(2, spaceService.getVisibleSpacesWithListAccess(userName, null).getSize());
3057     assertEquals(0, spaceService.getPublicSpacesByFilter(userName, null).getSize());
3058     assertEquals(0, spaceService.getAccessibleSpacesByFilter(userName, null).getSize());
3059     assertEquals(0, spaceService.getSettingableSpaces(userName).getSize());
3060 
3061     spacesAdministrationService.updateSpacesAdministratorsMemberships(Arrays.asList(new MembershipEntry("/testgroup", "mstypetest")));
3062     assertTrue(spaceService.isSuperManager(userName));
3063     assertTrue(spaceService.hasAccessPermission(space, userName));
3064     assertTrue(spaceService.hasSettingPermission(space, userName));
3065     assertEquals(6, spaceService.getVisibleSpacesWithListAccess(userName, null).getSize());
3066 
3067     // number fixed to 0 for super users in SpaceListAccess.getSize()
3068     assertEquals(0, spaceService.getPublicSpacesByFilter(userName, null).getSize());
3069 
3070     assertEquals(6, spaceService.getAccessibleSpacesByFilter(userName, null).getSize());
3071     assertEquals(6, spaceService.getSettingableSpaces(userName).getSize());
3072   }
3073 
3074   private Space populateData() throws Exception {
3075     try {
3076       String spaceDisplayName = "Space1";
3077       Space space1 = new Space();
3078       space1.setApp("Calendar;FileSharing");
3079       space1.setDisplayName(spaceDisplayName);
3080       space1.setPrettyName(space1.getDisplayName());
3081       String shortName = SpaceUtils.cleanString(spaceDisplayName);
3082       space1.setGroupId("/spaces/" + shortName);
3083       space1.setUrl(shortName);
3084       space1.setRegistration("validation");
3085       space1.setDescription("This is my first space for testing");
3086       space1.setType("classic");
3087       space1.setVisibility("public");
3088       space1.setPriority("2");
3089       String[] manager = new String []{"root"};
3090       String[] members = new String []{"demo", "john", "mary", "tom", "harry"};
3091       space1.setManagers(manager);
3092       space1.setMembers(members);
3093 
3094       spaceService.saveSpace(space1, true);
3095       tearDownSpaceList.add(space1);
3096       return space1;
3097       
3098     } finally {
3099       StorageUtils.persist();
3100     }
3101     
3102   }
3103   
3104   private Space createSpace(String spaceName, String creator) throws Exception {
3105     try {
3106     Space space = new Space();
3107     space.setDisplayName(spaceName);
3108     space.setPrettyName(spaceName);
3109     space.setGroupId("/spaces/" + space.getPrettyName());
3110     space.setRegistration(Space.OPEN);
3111     space.setDescription("description of space" + spaceName);
3112     space.setType(DefaultSpaceApplicationHandler.NAME);
3113     space.setVisibility(Space.PRIVATE);
3114     space.setRegistration(Space.OPEN);
3115     space.setPriority(Space.INTERMEDIATE_PRIORITY);
3116     String[] managers = new String[] {creator};
3117     String[] members = new String[] {creator};
3118     space.setManagers(managers);
3119     space.setMembers(members);
3120     spaceService.saveSpace(space, true);
3121     tearDownSpaceList.add(space);
3122     return space;
3123     } finally {
3124       StorageUtils.persist();
3125     }
3126   }
3127 
3128   /**
3129    * Gets an instance of the space.
3130    *
3131    * @param number
3132    * @return
3133    * @throws Exception
3134    * @since 1.2.0-GA
3135    */
3136   private Space getSpaceInstance(int number) throws Exception {
3137     try {
3138       Space space = new Space();
3139       space.setDisplayName("my space " + number);
3140       space.setPrettyName(space.getDisplayName());
3141       space.setRegistration(Space.OPEN);
3142       space.setDescription("add new space " + number);
3143       space.setType(DefaultSpaceApplicationHandler.NAME);
3144       space.setVisibility(Space.PUBLIC);
3145       space.setRegistration(Space.VALIDATION);
3146       space.setPriority(Space.INTERMEDIATE_PRIORITY);
3147       space.setGroupId("/space/space" + number);
3148       String[] managers = new String[] {"demo", "tom"};
3149       String[] members = new String[] {"demo", "raul", "ghost", "dragon"};
3150       String[] invitedUsers = new String[] {"register1", "mary"};
3151       String[] pendingUsers = new String[] {"jame", "paul", "hacker"};
3152       space.setInvitedUsers(invitedUsers);
3153       space.setPendingUsers(pendingUsers);
3154       space.setManagers(managers);
3155       space.setMembers(members);
3156       space.setUrl(space.getPrettyName());
3157       this.spaceService.saveSpace(space, true);
3158       tearDownSpaceList.add(space);
3159       return space;
3160     } finally {
3161       StorageUtils.persist();
3162     }
3163   }
3164   
3165   /**
3166    * Gets an instance of Space.
3167    *
3168    * @param number
3169    * @return an instance of space
3170    */
3171   private Space getSpaceInstance(int number, String visible, String registration, String manager, String...members) {
3172     try {
3173       Space space = new Space();
3174       space.setApp("app");
3175       space.setDisplayName("my space " + number);
3176       space.setPrettyName(space.getDisplayName());
3177       space.setRegistration(registration);
3178       space.setDescription("add new space " + number);
3179       space.setType(DefaultSpaceApplicationHandler.NAME);
3180       space.setVisibility(visible);
3181       space.setPriority(Space.INTERMEDIATE_PRIORITY);
3182       space.setGroupId("/spaces/space" + number);
3183       String[] managers = new String[] {manager};
3184       String[] invitedUsers = new String[] {};
3185       String[] pendingUsers = new String[] {};
3186       space.setInvitedUsers(invitedUsers);
3187       space.setPendingUsers(pendingUsers);
3188       space.setManagers(managers);
3189       space.setMembers(members);
3190       space.setUrl(space.getPrettyName());
3191       tearDownSpaceList.add(space);
3192       return space;
3193     } finally {
3194       StorageUtils.persist();
3195     }
3196     
3197   }
3198   
3199   /**
3200    * Gets an instance of Space.
3201    *
3202    * @param number
3203    * @return an instance of space
3204    */
3205   private Space getSpaceInstanceInvitedMember(int number, String visible, String registration, String[] invitedMember, String manager, String...members) {
3206     try {
3207       Space space = new Space();
3208       space.setApp("app");
3209       space.setDisplayName("my space " + number);
3210       space.setPrettyName(space.getDisplayName());
3211       space.setRegistration(registration);
3212       space.setDescription("add new space " + number);
3213       space.setType(DefaultSpaceApplicationHandler.NAME);
3214       space.setVisibility(visible);
3215       space.setPriority(Space.INTERMEDIATE_PRIORITY);
3216       space.setGroupId("/spaces/space" + number);
3217       space.setUrl(space.getPrettyName());
3218       String[] managers = new String[] {manager};
3219       //String[] invitedUsers = new String[] {invitedMember};
3220       String[] pendingUsers = new String[] {};
3221       space.setInvitedUsers(invitedMember);
3222       space.setPendingUsers(pendingUsers);
3223       space.setManagers(managers);
3224       space.setMembers(members);
3225       tearDownSpaceList.add(space);
3226       return space;
3227     } finally {
3228       StorageUtils.persist();
3229     }
3230   }
3231 
3232   private Space createMoreSpace(String spaceName) throws Exception {
3233     try {
3234       Space space2 = new Space();
3235       space2.setApp("Contact,Forum");
3236       space2.setDisplayName(spaceName);
3237       space2.setPrettyName(space2.getDisplayName());
3238       String shortName = SpaceUtils.cleanString(spaceName);
3239       space2.setGroupId("/spaces/" + shortName );
3240       space2.setUrl(shortName);
3241       space2.setRegistration("open");
3242       space2.setDescription("This is my second space for testing");
3243       space2.setType("classic");
3244       space2.setVisibility("public");
3245       space2.setPriority("2");
3246 
3247       spaceService.saveSpace(space2, true);
3248       tearDownSpaceList.add(space2);
3249       return space2;
3250     } finally {
3251       StorageUtils.persist();
3252     }
3253     
3254   }
3255 }