View Javadoc
1   /*
2    * Copyright (C) 2003-2010 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (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 General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.social.core.manager;
18  
19  import java.io.InputStream;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Map.Entry;
25  
26  import org.exoplatform.commons.utils.ListAccess;
27  import org.exoplatform.social.core.identity.model.Identity;
28  import org.exoplatform.social.core.identity.model.Profile;
29  import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
30  import org.exoplatform.social.core.model.AvatarAttachment;
31  import org.exoplatform.social.core.relationship.model.Relationship;
32  import org.exoplatform.social.core.relationship.model.Relationship.Type;
33  import org.exoplatform.social.core.test.AbstractCoreTest;
34  
35  /**
36   * Unit Tests for {@link RelationshipManager}
37   *
38   */
39  public class RelationshipManagerTest extends AbstractCoreTest {
40    private RelationshipManager relationshipManager;
41    private IdentityManager identityManager;
42  
43    private Identity rootIdentity,
44                     johnIdentity,
45                     maryIdentity,
46                     demoIdentity,
47                     ghostIdentity,
48                     paulIdentity;
49  
50    private List<Relationship> tearDownRelationshipList;
51  
52    @Override
53    protected void setUp() throws Exception {
54      super.setUp();
55      tearDownRelationshipList = new ArrayList<Relationship>();
56      relationshipManager = (RelationshipManager) getContainer().getComponentInstanceOfType(RelationshipManager.class);
57      identityManager = (IdentityManager) getContainer().getComponentInstanceOfType(IdentityManager.class);
58      assertNotNull("relationshipManager must not be null", relationshipManager);
59      assertNotNull("identityManager must not be null", identityManager);
60      rootIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, "root");
61      johnIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, "john");
62      maryIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, "mary");
63      demoIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, "demo");
64      ghostIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, "ghost");
65      paulIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, "paul");
66    }
67  
68    @Override
69    protected void tearDown() throws Exception {
70      for (Relationship relationship : tearDownRelationshipList) {
71        relationshipManager.remove(relationship);
72      }
73  
74      identityManager.deleteIdentity(rootIdentity);
75      identityManager.deleteIdentity(johnIdentity);
76      identityManager.deleteIdentity(maryIdentity);
77      identityManager.deleteIdentity(demoIdentity);
78      identityManager.deleteIdentity(ghostIdentity);
79      identityManager.deleteIdentity(paulIdentity);
80  
81      super.tearDown();
82    }
83  
84    /**
85     * Test {@link RelationshipManager#getAll(Identity)}
86     * 
87     * @throws Exception
88     */
89    public void testGetAll() throws Exception {
90      relationshipManager.invite(johnIdentity, demoIdentity);
91      List<Relationship> senderRelationships = relationshipManager.getAll(johnIdentity);
92      List<Relationship> receiverRelationships = relationshipManager.getAll(demoIdentity);
93  
94      assertEquals(1, senderRelationships.size());
95      assertEquals(1, receiverRelationships.size());
96  
97      tearDownRelationshipList.addAll(senderRelationships);
98    }
99  
100   /**
101    * Test {@link RelationshipManager#getAll(Identity, List)}
102    * 
103    * @throws Exception
104    * @since 1.2.0-Beta3
105    */
106   public void testGetAllWithListIdentities() throws Exception {
107     List<Identity> listIdentities = new ArrayList<Identity>();
108     listIdentities.add(rootIdentity);
109     listIdentities.add(demoIdentity);
110     listIdentities.add(johnIdentity);
111     listIdentities.add(maryIdentity);
112     
113     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
114     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
115     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
116     
117     List<Relationship> rootRelationships = relationshipManager.getAll(rootIdentity, listIdentities);
118     assertNotNull("rootRelationships must not be null", rootRelationships);
119     assertEquals("rootRelationships.size() must return: 3", 3, rootRelationships.size());
120     
121     List<Relationship> maryRelationships = relationshipManager.getAll(maryIdentity, listIdentities);
122     assertNotNull("maryRelationships must not be null", maryRelationships);
123     assertEquals("maryRelationships.size() mut return: 1", 1, maryRelationships.size());
124     
125     List<Relationship> johnRelationships = relationshipManager.getAll(johnIdentity, listIdentities);
126     assertNotNull("johnRelationships must not be null", johnRelationships);
127     assertEquals("johnRelationships.size() mut return: 1", 1, johnRelationships.size());
128     
129     tearDownRelationshipList.add(rootToDemoRelationship);
130     tearDownRelationshipList.add(maryToRootRelationship);
131     tearDownRelationshipList.add(rootToJohnRelationship);
132   }
133   
134   /**
135    * Test {@link RelationshipManager#getAll(Identity, Type, List)}
136    * 
137    * @throws Exception
138    * @since 1.2.0-Beta3
139    */
140   public void testGetAllWithTypeAndListIdentities() throws Exception {
141     List<Identity> listIdentities = new ArrayList<Identity>();
142     listIdentities.add(rootIdentity);
143     listIdentities.add(demoIdentity);
144     listIdentities.add(johnIdentity);
145     listIdentities.add(maryIdentity);
146     
147     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
148     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
149     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
150     
151     List<Relationship> rootPendingRelationships = relationshipManager.getAll(rootIdentity, Relationship.Type.PENDING, listIdentities);
152     assertNotNull("rootPendingRelationships must not be null", rootPendingRelationships);
153     assertEquals("rootPendingRelationships.size() must return: 3", 3, rootPendingRelationships.size());
154     
155     List<Relationship> maryPendingRelationships = relationshipManager.getAll(maryIdentity, Relationship.Type.PENDING, listIdentities);
156     assertNotNull("maryPendingRelationships must not be null", maryPendingRelationships);
157     assertEquals("maryPendingRelationships.size() mut return: 1", 1, maryPendingRelationships.size());
158     
159     List<Relationship> johnPendingRelationships = relationshipManager.getAll(maryIdentity, Relationship.Type.PENDING, listIdentities);
160     assertNotNull("johnPendingRelationships must not be null", johnPendingRelationships);
161     assertEquals("johnPendingRelationships.size() mut return: 1", 1, johnPendingRelationships.size());
162     
163     relationshipManager.confirm(demoIdentity, rootIdentity);
164     
165     List<Relationship> rootConfirmedRelationships = relationshipManager.getAll(rootIdentity, Relationship.Type.CONFIRMED, listIdentities);
166     assertNotNull("rootConfirmedRelationships must not be null", rootConfirmedRelationships);
167     assertEquals("rootConfirmedRelationships.size() must return: 1", 1, rootConfirmedRelationships.size());
168     
169     tearDownRelationshipList.add(rootToDemoRelationship);
170     tearDownRelationshipList.add(maryToRootRelationship);
171     tearDownRelationshipList.add(rootToJohnRelationship);
172   }
173   
174   /**
175    * Test {@link RelationshipManager#get(Identity, Identity)}
176    * 
177    * @throws Exception
178    * @since 1.2.0-Beta3
179    */
180   public void testGet() throws Exception {
181     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
182     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
183     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
184     
185     rootToDemoRelationship = relationshipManager.get(rootIdentity, demoIdentity);
186     assertNotNull("rootToDemoRelationship must not be null", rootToDemoRelationship);
187     assertEquals("rootToDemoRelationship.getSender() must return: " + rootIdentity, rootIdentity, rootToDemoRelationship.getSender());
188     assertEquals("rootToDemoRelationship.getReceiver() must return: " + demoIdentity, demoIdentity, rootToDemoRelationship.getReceiver());
189     assertEquals("rootToDemoRelationship.getStatus() must return: " + Relationship.Type.PENDING, 
190                  Relationship.Type.PENDING, rootToDemoRelationship.getStatus());
191     
192     relationshipManager.confirm(johnIdentity, rootIdentity);
193     rootToJohnRelationship = relationshipManager.get(johnIdentity, rootIdentity);
194     assertEquals("rootToJohnRelationship.getStatus() must return: ", Relationship.Type.CONFIRMED, 
195                  rootToJohnRelationship.getStatus());
196     
197     tearDownRelationshipList.add(rootToDemoRelationship);
198     tearDownRelationshipList.add(maryToRootRelationship);
199     tearDownRelationshipList.add(rootToJohnRelationship);
200   }
201   
202   /**
203    * Test {@link RelationshipManager#get(String)}
204    * 
205    * @throws Exception
206    * @since 1.2.0-Beta3
207    */
208   public void testGetWithRelationshipId() throws Exception {
209     Relationship relationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
210     String relationshipId = relationship.getId();
211     
212     relationshipManager.confirm(johnIdentity, rootIdentity);
213     relationship = relationshipManager.get(relationship.getId());
214     assertNotNull("relationship must not be null", relationship);
215     assertEquals("relationship.getStatus() must return: " + Relationship.Type.CONFIRMED, Relationship.Type.CONFIRMED, relationship.getStatus());
216     
217     relationshipManager.delete(relationship);
218 
219     relationship = relationshipManager.get(rootIdentity, johnIdentity);
220     assertNull("relationship must be null", relationship);
221     
222     relationship = relationshipManager.get(relationshipId);
223     assertNull("relationship must be null", relationship);
224   }
225   
226   /**
227    * Test {@link RelationshipManager#update(Relationship)}
228    * 
229    * @throws Exception
230    * @since 1.2.0-Beta3
231    */
232   public void testUpdate() throws Exception {
233     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
234     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
235     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
236     
237     assertEquals("rootToDemoRelationship.getStatus() must return: " + Relationship.Type.PENDING,
238                  Relationship.Type.PENDING, rootToDemoRelationship.getStatus());
239     rootToDemoRelationship.setStatus(Relationship.Type.CONFIRMED);
240     relationshipManager.update(rootToDemoRelationship);
241     rootToDemoRelationship = relationshipManager.get(rootIdentity, demoIdentity);
242     assertEquals("rootToDemoRelationship.getStatus() must return: " + Relationship.Type.CONFIRMED,
243                  Relationship.Type.CONFIRMED, rootToDemoRelationship.getStatus());
244     
245     assertEquals("maryToRootRelationship.getStatus() must return: " + Relationship.Type.PENDING,
246                  Relationship.Type.PENDING, maryToRootRelationship.getStatus());
247     maryToRootRelationship.setStatus(Relationship.Type.IGNORED);
248     relationshipManager.update(maryToRootRelationship);
249     maryToRootRelationship = relationshipManager.get(maryIdentity, rootIdentity);
250     assertEquals("maryToRootRelationship.getStatus() must return: " + Relationship.Type.IGNORED,
251                  Relationship.Type.IGNORED, maryToRootRelationship.getStatus());
252     
253     tearDownRelationshipList.add(rootToDemoRelationship);
254     tearDownRelationshipList.add(maryToRootRelationship);
255     tearDownRelationshipList.add(rootToJohnRelationship);
256   }
257   
258   /**
259    * Test {@link RelationshipManager#inviteToConnect(Identity, Identity)}
260    * 
261    * @throws Exception
262    * @since 1.2.0-Beta3
263    */
264   public void testInviteToConnect() throws Exception {
265     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
266     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
267     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
268     
269     rootToDemoRelationship = relationshipManager.get(rootIdentity, demoIdentity);
270     assertNotNull("rootToDemoRelationship must not be null", rootToDemoRelationship);
271     assertEquals("rootToDemoRelationship.getStatus() must return: " + Relationship.Type.PENDING,
272                  Relationship.Type.PENDING, rootToDemoRelationship.getStatus());
273     
274     maryToRootRelationship = relationshipManager.get(maryIdentity, rootIdentity);
275     assertNotNull("maryToRootRelationship must not be null", maryToRootRelationship);
276     assertEquals("maryToRootRelationship.getStatus() must return: " + Relationship.Type.PENDING,
277                  Relationship.Type.PENDING, maryToRootRelationship.getStatus());
278     
279     rootToJohnRelationship = relationshipManager.get(johnIdentity, rootIdentity);
280     assertNotNull("rootToJohnRelationship must not be null", rootToJohnRelationship);
281     assertEquals("rootToJohnRelationship.getStatus() must return: " + Relationship.Type.PENDING,
282                  Relationship.Type.PENDING, rootToJohnRelationship.getStatus());
283     
284     tearDownRelationshipList.add(rootToDemoRelationship);
285     tearDownRelationshipList.add(maryToRootRelationship);
286     tearDownRelationshipList.add(rootToJohnRelationship);
287   }
288   
289   /**
290    * Test {@link RelationshipManager#inviteToConnect(Identity, Identity)}
291    *
292    * @throws Exception
293    * @since 1.2.0-Beta3
294   */
295   public void testDupdicateInviteToConnect() throws Exception {
296     Relationship relationship1 = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
297     Relationship relationship2 = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
298     assertEquals("relationShip1 and relationShip2 must be the same",relationship1.getId(), relationship2.getId());
299     tearDownRelationshipList.add(relationship1);
300   }
301   
302   /**
303    * Test {@link RelationshipManager#inviteToConnect(Identity, Identity)}
304    *
305    * @throws Exception
306    * @since 1.2.0-Beta3
307    */
308   public void testDupdicateInviteToConnectWithConfirmedRelationShip() throws Exception {
309     Relationship relationship1 = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
310     assertEquals("RelationShip status must be PENDING",Relationship.Type.PENDING, relationship1.getStatus());
311     relationshipManager.confirm(rootIdentity, demoIdentity);
312     relationship1 = relationshipManager.get(rootIdentity, demoIdentity);
313     assertEquals("RelationShip status must be CONFIRMED",Relationship.Type.CONFIRMED, relationship1.getStatus());
314     Relationship relationship2 = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
315     assertEquals("RelationShip status must be CONFIRMED",Relationship.Type.CONFIRMED, relationship2.getStatus());
316     
317     assertEquals("relationShip1 and relationShip2 must be the same",relationship1.getId(), relationship2.getId());
318     
319     tearDownRelationshipList.add(relationship1);
320   }
321   
322   /**
323    * Test {@link RelationshipManager#confirm(Identity, Identity)}
324    * 
325    * @throws Exception
326    * @since 1.2.0-Beta3
327    */
328   public void testConfirmWithIdentity() throws Exception {
329     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
330     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
331     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
332     
333     rootToJohnRelationship = relationshipManager.get(rootToJohnRelationship.getId());
334     
335     relationshipManager.confirm(rootIdentity, demoIdentity);
336     rootToDemoRelationship = relationshipManager.get(rootIdentity, demoIdentity);
337     assertNotNull("rootToDemoRelationship must not be null", rootToDemoRelationship);
338     assertEquals("rootToDemoRelationship.getStatus() must return: " + Relationship.Type.CONFIRMED,
339                  Relationship.Type.CONFIRMED, rootToDemoRelationship.getStatus());
340     
341     relationshipManager.confirm(maryIdentity, rootIdentity);
342     maryToRootRelationship = relationshipManager.get(maryIdentity, rootIdentity);
343     assertNotNull("maryToRootRelationship must not be null", maryToRootRelationship);
344     assertEquals("maryToRootRelationship.getStatus() must return: " + Relationship.Type.CONFIRMED,
345                  Relationship.Type.CONFIRMED, maryToRootRelationship.getStatus());
346     
347     relationshipManager.confirm(rootIdentity, johnIdentity);
348     rootToJohnRelationship = relationshipManager.get(johnIdentity, rootIdentity);
349     assertNotNull("rootToJohnRelationship must not be null", rootToJohnRelationship);
350     assertEquals("rootToJohnRelationship.getStatus() must return: " + Relationship.Type.CONFIRMED,
351                  Relationship.Type.CONFIRMED, rootToJohnRelationship.getStatus());
352     
353     tearDownRelationshipList.add(rootToDemoRelationship);
354     tearDownRelationshipList.add(maryToRootRelationship);
355     tearDownRelationshipList.add(rootToJohnRelationship);
356   }
357   
358   /**
359    * Test {@link RelationshipManager#deny(Identity, Identity)}
360    * 
361    * @throws Exception
362    * @since 1.2.0-Beta3
363    */
364   public void testDeny() throws Exception {
365     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
366     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
367     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
368     
369     relationshipManager.confirm(johnIdentity, rootIdentity);
370     relationshipManager.deny(johnIdentity, rootIdentity);
371     assertNull(relationshipManager.get(rootToJohnRelationship.getId()));
372     
373     relationshipManager.deny(demoIdentity, rootIdentity);
374     rootToDemoRelationship = relationshipManager.get(rootIdentity, demoIdentity);
375     assertNull("rootToDemoRelationship must be null", rootToDemoRelationship);
376     
377     relationshipManager.deny(maryIdentity, rootIdentity);
378     maryToRootRelationship = relationshipManager.get(maryIdentity, rootIdentity);
379     assertNull("maryToRootRelationship must be null", maryToRootRelationship);
380     
381     relationshipManager.deny(rootIdentity, johnIdentity);
382     rootToJohnRelationship = relationshipManager.get(rootIdentity, johnIdentity);
383     assertNull("rootToJohnRelationship must be null", rootToJohnRelationship);
384   }
385   
386   /**
387    * Test {@link RelationshipManager#ignore(Identity, Identity)}
388    * 
389    * @throws Exception
390    * @since 1.2.0-Beta3
391    */
392   public void testIgnore() throws Exception {
393     Relationship relationship = relationshipManager.get(rootIdentity, demoIdentity);
394     assertNull(relationship);
395 
396     //
397     relationshipManager.ignore(rootIdentity, demoIdentity);
398     relationship = relationshipManager.get(rootIdentity, demoIdentity);
399     assertNotNull(relationship);
400     assertEquals(rootIdentity, relationship.getSender());
401     assertEquals(demoIdentity, relationship.getReceiver());
402     assertEquals(Relationship.Type.IGNORED, relationship.getStatus());
403 
404     relationship = relationshipManager.get(demoIdentity, rootIdentity);
405     assertNotNull(relationship);
406     assertEquals(rootIdentity, relationship.getSender());
407     assertEquals(demoIdentity, relationship.getReceiver());
408     assertEquals(Relationship.Type.IGNORED, relationship.getStatus());
409 
410     //
411     relationshipManager.ignore(demoIdentity, rootIdentity);
412     relationship = relationshipManager.get(demoIdentity, rootIdentity);
413     assertNotNull(relationship);
414     assertEquals(rootIdentity, relationship.getSender());
415     assertEquals(demoIdentity, relationship.getReceiver());
416     assertEquals(Relationship.Type.IGNORED, relationship.getStatus());
417     
418     relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
419     relationship = relationshipManager.get(rootIdentity, demoIdentity);
420     assertNotNull(relationship);
421     assertEquals(Relationship.Type.PENDING, relationship.getStatus());
422 
423     // Second ignore will not make any exception.
424     relationshipManager.deny(rootIdentity, demoIdentity);
425     relationship = relationshipManager.get(rootIdentity, demoIdentity);
426     assertNotNull(relationship);
427     assertEquals(Relationship.Type.IGNORED, relationship.getStatus());
428 
429     relationshipManager.ignore(rootIdentity, demoIdentity);
430     relationship = relationshipManager.get(rootIdentity, demoIdentity);
431     assertNotNull(relationship);
432     assertEquals(Relationship.Type.IGNORED, relationship.getStatus());
433   }
434 
435   /**
436    * Test {@link RelationshipManager#getIncomingWithListAccess(Identity)}
437    * 
438    * @throws Exception
439    * @since 1.2.0-Beta3
440    */
441   public void testGetIncomingWithListAccess() throws Exception {
442     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
443     Relationship maryToDemoRelationship = relationshipManager.inviteToConnect(maryIdentity, demoIdentity);
444     Relationship johnToDemoRelationship = relationshipManager.inviteToConnect(johnIdentity, demoIdentity);
445     
446     ListAccess<Identity> demoIncoming = relationshipManager.getIncomingWithListAccess(demoIdentity);
447     assertNotNull("demoIncoming must not be null", demoIncoming);
448     assertEquals("demoIncoming.getSize() must return: 3", 3, demoIncoming.getSize());
449     
450     ListAccess<Identity> rootIncoming = relationshipManager.getIncomingWithListAccess(rootIdentity);
451     assertNotNull("rootIncoming must not be null", rootIncoming);
452     assertEquals("rootIncoming.getSize() must return: 0", 0, rootIncoming.getSize());
453     
454     ListAccess<Identity> maryIncoming = relationshipManager.getIncomingWithListAccess(maryIdentity);
455     assertNotNull("maryIncoming must not be null", maryIncoming);
456     assertEquals("maryIncoming.getSize() must return: 0", 0, maryIncoming.getSize());
457     
458     tearDownRelationshipList.add(rootToDemoRelationship);
459     tearDownRelationshipList.add(maryToDemoRelationship);
460     tearDownRelationshipList.add(johnToDemoRelationship);
461   }
462   
463   /**
464    * Test {@link RelationshipManager#getOutgoing(Identity)}
465    * 
466    * @throws Exception
467    * @since 1.2.0-Beta3
468    */
469   public void testGetOutgoing() throws Exception {
470     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
471     Relationship rootToMaryRelationship = relationshipManager.inviteToConnect(rootIdentity, maryIdentity);
472     Relationship maryToDemoRelationship = relationshipManager.inviteToConnect(maryIdentity, demoIdentity);
473     Relationship demoToJohnRelationship = relationshipManager.inviteToConnect(demoIdentity, johnIdentity);
474     
475     ListAccess<Identity> rootOutgoing = relationshipManager.getOutgoing(rootIdentity);
476     assertNotNull("rootOutgoing must not be null", rootOutgoing);
477     assertEquals("rootOutgoing.getSize() must return: 2", 2, rootOutgoing.getSize());
478     
479     ListAccess<Identity> maryOutgoing = relationshipManager.getOutgoing(maryIdentity);
480     assertNotNull("maryOutgoing must not be null", maryOutgoing);
481     assertEquals("maryOutgoing.getSize() must return: 1", 1, maryOutgoing.getSize());
482     
483     ListAccess<Identity> demoOutgoing = relationshipManager.getOutgoing(demoIdentity);
484     assertNotNull("demoOutgoing must not be null", demoOutgoing);
485     assertEquals("demoOutgoing.getSize() must return: 1", 1, demoOutgoing.getSize());
486     
487     tearDownRelationshipList.add(rootToDemoRelationship);
488     tearDownRelationshipList.add(maryToDemoRelationship);
489     tearDownRelationshipList.add(demoToJohnRelationship);
490     tearDownRelationshipList.add(rootToMaryRelationship);
491   }
492   
493   /**
494    * Test {@link RelationshipManager#getStatus(Identity, Identity)}
495    * 
496    * @throws Exception
497    * @since 1.2.0-Beta3
498    */
499   public void testGetStatus() throws Exception {
500     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
501     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
502     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
503     
504     rootToDemoRelationship = relationshipManager.get(rootIdentity, demoIdentity);
505     assertNotNull("rootToDemoRelationship must not be null", rootToDemoRelationship);
506     assertEquals("rootToDemoRelationship.getStatus() must return: " + Relationship.Type.PENDING,
507                  Relationship.Type.PENDING, rootToDemoRelationship.getStatus());
508     assertEquals("relationshipManager.getStatus(rootIdentity, demoIdentity) must return: " + 
509                  Relationship.Type.PENDING, Relationship.Type.PENDING, 
510                  relationshipManager.getStatus(rootIdentity, demoIdentity));
511     
512     maryToRootRelationship = relationshipManager.get(maryIdentity, rootIdentity);
513     assertNotNull("maryToRootRelationship must not be null", maryToRootRelationship);
514     assertEquals("maryToRootRelationship.getStatus() must return: " + Relationship.Type.PENDING,
515                  Relationship.Type.PENDING, maryToRootRelationship.getStatus());
516     assertEquals("relationshipManager.getStatus(maryIdentity, rootIdentity) must return: " + 
517                  Relationship.Type.PENDING, Relationship.Type.PENDING,
518                  relationshipManager.getStatus(maryIdentity, rootIdentity));
519     
520     rootToJohnRelationship = relationshipManager.get(johnIdentity, rootIdentity);
521     assertNotNull("rootToJohnRelationship must not be null", rootToJohnRelationship);
522     assertEquals("rootToJohnRelationship.getStatus() must return: " + Relationship.Type.PENDING,
523                  Relationship.Type.PENDING, rootToJohnRelationship.getStatus());
524     assertEquals("relationshipManager.getStatus(rootIdentity, johnIdentity) must return: " +
525                  Relationship.Type.PENDING, Relationship.Type.PENDING,
526                  relationshipManager.getStatus(rootIdentity, johnIdentity));
527     
528     tearDownRelationshipList.add(rootToDemoRelationship);
529     tearDownRelationshipList.add(maryToRootRelationship);
530     tearDownRelationshipList.add(rootToJohnRelationship);
531   }
532   
533   /**
534    * Test {@link RelationshipManager#getAllWithListAccess(Identity)}
535    * 
536    * @throws Exception
537    * @since 1.2.0-Beta3
538    */
539   public void testGetAllWithListAccess() throws Exception {
540     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
541     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
542     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
543     
544     ListAccess<Identity> rootRelationships = relationshipManager.getAllWithListAccess(rootIdentity);
545     assertNotNull("rootRelationships must not be null", rootRelationships);
546     assertEquals("rootRelationships.getSize() must return: 3", 3, rootRelationships.getSize());
547     
548     ListAccess<Identity> demoRelationships = relationshipManager.getAllWithListAccess(demoIdentity);
549     assertNotNull("demoRelationships must not be null", demoRelationships);
550     assertEquals("demoRelationships.getSize() must return: 1", 1, demoRelationships.getSize());
551     
552     ListAccess<Identity> johnRelationships = relationshipManager.getAllWithListAccess(johnIdentity);
553     assertNotNull("johnRelationships must not be null", johnRelationships);
554     assertEquals("johnRelationships.getSize() must return: 1", 1, johnRelationships.getSize());
555     
556     tearDownRelationshipList.add(rootToDemoRelationship);
557     tearDownRelationshipList.add(maryToRootRelationship);
558     tearDownRelationshipList.add(rootToJohnRelationship);
559   }
560   
561   /**
562    * Test {@link RelationshipManager#getRelationshipById(String)}
563    * 
564    * @throws Exception
565    * @since 1.2.0-Beta3
566    */
567   public void testGetRelationshipById() throws Exception {
568     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
569     
570     rootToDemoRelationship = relationshipManager.getRelationshipById(rootToDemoRelationship.getId());
571     assertNotNull("rootToDemoRelationship must not be null", rootToDemoRelationship);
572     assertEquals("rootToDemoRelationship.getSender() must return: " + rootIdentity, rootIdentity, rootToDemoRelationship.getSender());
573     assertEquals("rootToDemoRelationship.getReceiver() must return: " + demoIdentity, demoIdentity, rootToDemoRelationship.getReceiver());
574     
575     tearDownRelationshipList.add(rootToDemoRelationship);
576   }
577   
578   /**
579    * Test {@link RelationshipManager#deny(Relationship)}
580    * 
581    * @throws Exception
582    * @since 1.2.0-Beta3
583    */
584   public void testDenyWithRelationship() throws Exception {
585     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
586     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
587     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
588     
589     relationshipManager.deny(rootToDemoRelationship);
590     rootToDemoRelationship = relationshipManager.get(rootIdentity, demoIdentity);
591     assertNull("rootToDemoRelationship must be null", rootToDemoRelationship);
592     
593     relationshipManager.deny(maryToRootRelationship);
594     maryToRootRelationship = relationshipManager.get(maryIdentity, rootIdentity);
595     assertNull("maryToRootRelationship must be null", maryToRootRelationship);
596     
597     relationshipManager.deny(rootToJohnRelationship);
598     rootToJohnRelationship = relationshipManager.get(rootIdentity, johnIdentity);
599     assertNull("rootToJohnRelationship must be null", rootToJohnRelationship);
600   }
601 
602   /**
603    * Test {@link RelationshipManager#getPendingRelationships(Identity)}
604    * 
605    * @throws Exception
606    * @since 1.2.0-Beta3
607    */
608   public void testGetPendingRelationships() throws Exception {
609     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
610     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
611     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
612     
613     List<Relationship> rootPendingRelationships = relationshipManager.getPendingRelationships(rootIdentity);
614     assertNotNull("rootPendingRelationships must not be null", rootPendingRelationships);
615     assertEquals("rootPendingRelationships.size() must return: 2", 2, rootPendingRelationships.size());
616     
617     List<Relationship> maryPendingRelationships = relationshipManager.getPendingRelationships(maryIdentity);
618     assertNotNull("maryPendingRelationships must not be null", maryPendingRelationships);
619     assertEquals("maryPendingRelationships.size() must return: 1", 1, maryPendingRelationships.size());
620     
621     List<Relationship> demoPendingRelationships = relationshipManager.getPendingRelationships(demoIdentity);
622     assertNotNull("demoPendingRelationships must not be null", demoPendingRelationships);
623     assertEquals("demoPendingRelationships.size() must return: 0", 0, demoPendingRelationships.size());
624     
625     List<Relationship> johnPendingRelationships = relationshipManager.getPendingRelationships(johnIdentity);
626     assertNotNull("johnPendingRelationships must not be null", johnPendingRelationships);
627     assertEquals("johnPendingRelationships.size() must return: 0", 0, johnPendingRelationships.size());
628     
629     tearDownRelationshipList.add(rootToDemoRelationship);
630     tearDownRelationshipList.add(maryToRootRelationship);
631     tearDownRelationshipList.add(rootToJohnRelationship);
632   }
633   
634   /**
635    * Test {@link RelationshipManager#getPendingRelationships(Identity, boolean)}
636    * 
637    * @throws Exception
638    * @since 1.2.0-Beta3
639    */
640   public void testGetPendingRelationshipWithSentOrReceived() throws Exception {
641     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
642     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
643     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
644     
645     List<Relationship> rootPendingRelationships = relationshipManager.getPendingRelationships(rootIdentity, true);
646     assertNotNull("rootPendingRelationships must not be null", rootPendingRelationships);
647     assertEquals("rootPendingRelationships.size() must return: 3", 3, rootPendingRelationships.size());
648     
649     List<Relationship> maryPendingRelationships = relationshipManager.getPendingRelationships(maryIdentity, true);
650     assertNotNull("maryPendingRelationships must not be null", maryPendingRelationships);
651     assertEquals("maryPendingRelationships.size() must return: 1", 1, maryPendingRelationships.size());
652     
653     List<Relationship> demoPendingRelationships = relationshipManager.getPendingRelationships(demoIdentity, true);
654     assertNotNull("demoPendingRelationships must not be null", demoPendingRelationships);
655     assertEquals("demoPendingRelationships.size() must return: 1", 1, demoPendingRelationships.size());
656     
657     List<Relationship> johnPendingRelationships = relationshipManager.getPendingRelationships(johnIdentity, true);
658     assertNotNull("johnPendingRelationships must not be null", johnPendingRelationships);
659     assertEquals("johnPendingRelationships.size() must return: 1", 1, johnPendingRelationships.size());
660     
661     tearDownRelationshipList.add(rootToDemoRelationship);
662     tearDownRelationshipList.add(maryToRootRelationship);
663     tearDownRelationshipList.add(rootToJohnRelationship);
664   }
665   
666   /**
667    * Test {@link RelationshipManager#getPendingRelationships(Identity, List, boolean)}
668    * 
669    * @throws Exception
670    * @since 1.2.0-Beta3
671    */
672   public void testGetPendingRealtionshipWithListIdentities() throws Exception {
673     List<Identity> identities = new ArrayList<Identity> ();
674     identities.add(rootIdentity);
675     identities.add(demoIdentity);
676     identities.add(johnIdentity);
677     identities.add(maryIdentity);
678     
679     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
680     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
681     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
682     
683     List<Relationship> rootPendingRelationships = relationshipManager.getPendingRelationships(rootIdentity, identities, true);
684     assertNotNull("rootPendingRelationships must not be null", rootPendingRelationships);
685     assertEquals("rootPendingRelationships.size() must return: 3", 3, rootPendingRelationships.size());
686     
687     List<Relationship> maryPendingRelationships = relationshipManager.getPendingRelationships(maryIdentity, identities, true);
688     assertNotNull("maryPendingRelationships must not be null", maryPendingRelationships);
689     assertEquals("maryPendingRelationships.size() must return: 1", 1, maryPendingRelationships.size());
690     
691     List<Relationship> demoPendingRelationships = relationshipManager.getPendingRelationships(demoIdentity, identities, true);
692     assertNotNull("demoPendingRelationships must not be null", demoPendingRelationships);
693     assertEquals("demoPendingRelationships.size() must return: 1", 1, demoPendingRelationships.size());
694     
695     List<Relationship> johnPendingRelationships = relationshipManager.getPendingRelationships(johnIdentity, identities, true);
696     assertNotNull("johnPendingRelationships must not be null", johnPendingRelationships);
697     assertEquals("johnPendingRelationships.size() must return: 1", 1, johnPendingRelationships.size());
698     
699     tearDownRelationshipList.add(rootToDemoRelationship);
700     tearDownRelationshipList.add(maryToRootRelationship);
701     tearDownRelationshipList.add(rootToJohnRelationship);
702   }
703   
704   /**
705    * Test {@link RelationshipManager#getContacts(Identity, List)}
706    * 
707    * @throws Exception
708    * @since 1.2.0-Beta3
709    */
710   public void testGetContactsWithListIdentities() throws Exception {
711     List<Identity> identities = new ArrayList<Identity> ();
712     identities.add(rootIdentity);
713     identities.add(demoIdentity);
714     identities.add(johnIdentity);
715     identities.add(maryIdentity);
716     
717     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
718     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
719     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
720     
721     relationshipManager.confirm(rootIdentity, demoIdentity);
722     relationshipManager.confirm(maryIdentity, rootIdentity);
723     relationshipManager.confirm(rootIdentity, johnIdentity);
724     
725     List<Relationship> rootContacts = relationshipManager.getContacts(rootIdentity, identities);
726     assertNotNull("rootContacts must not be null", rootContacts);
727     assertEquals("rootContacts.size() must return: 3", 3, rootContacts.size());
728     
729     List<Relationship> demoContacts = relationshipManager.getContacts(demoIdentity, identities);
730     assertNotNull("demoContacts must not be null", demoContacts);
731     assertEquals("demoContacts.size() must return: 1", 1, demoContacts.size());
732     
733     List<Relationship> maryContacts = relationshipManager.getContacts(maryIdentity, identities);
734     assertNotNull("maryContacts must not be null", maryContacts);
735     assertEquals("maryContacts.size() must return: 1", 1, maryContacts.size());
736     
737     List<Relationship> johnContacts = relationshipManager.getContacts(johnIdentity, identities);
738     assertNotNull("johnContacts must not be null", johnContacts);
739     assertEquals("johnContacts.size() must return: 1", 1, johnContacts.size());
740     
741     tearDownRelationshipList.add(rootToDemoRelationship);
742     tearDownRelationshipList.add(maryToRootRelationship);
743     tearDownRelationshipList.add(rootToJohnRelationship);
744   }
745   
746   /**
747    * Test {@link RelationshipManager#getContacts(Identity)}
748    * 
749    * @throws Exception
750    * @since 1.2.0-Beta3
751    */
752   public void testGetContacts() throws Exception {
753     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
754     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
755     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
756     
757     relationshipManager.confirm(rootIdentity, demoIdentity);
758     relationshipManager.confirm(maryIdentity, rootIdentity);
759     relationshipManager.confirm(rootIdentity, johnIdentity);
760     
761     List<Relationship> rootContacts = relationshipManager.getContacts(rootIdentity);
762     assertNotNull("rootContacts must not be null", rootContacts);
763     assertEquals("rootContacts.size() must return: 3", 3, rootContacts.size());
764     
765     List<Relationship> demoContacts = relationshipManager.getContacts(demoIdentity);
766     assertNotNull("demoContacts must not be null", demoContacts);
767     assertEquals("demoContacts.size() must return: 1", 1, demoContacts.size());
768     
769     List<Relationship> maryContacts = relationshipManager.getContacts(maryIdentity);
770     assertNotNull("maryContacts must not be null", maryContacts);
771     assertEquals("maryContacts.size() must return: 1", 1, maryContacts.size());
772     
773     List<Relationship> johnContacts = relationshipManager.getContacts(johnIdentity);
774     assertNotNull("johnContacts must not be null", johnContacts);
775     assertEquals("johnContacts.size() must return: 1", 1, johnContacts.size());
776     
777     tearDownRelationshipList.add(rootToDemoRelationship);
778     tearDownRelationshipList.add(maryToRootRelationship);
779     tearDownRelationshipList.add(rootToJohnRelationship);
780   }
781   
782   /**
783    * Test {@link RelationshipManager#getAllRelationships(Identity)}
784    * 
785    * @throws Exception
786    * @since 1.2.0-Beta3
787    */
788   public void testGetAllRelationships() throws Exception {
789     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
790     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
791     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
792     
793     List<Relationship> rootRelationships = relationshipManager.getAllRelationships(rootIdentity);
794     assertNotNull("rootRelationships must not be null", rootRelationships);
795     assertEquals("rootRelationships.size() must return: 3", 3, rootRelationships.size());
796     
797     List<Relationship> maryRelationships = relationshipManager.getAllRelationships(maryIdentity);
798     assertNotNull("maryRelationships must not be null", maryRelationships);
799     assertEquals("maryRelationships.size() must return: 1", 1, maryRelationships.size());
800     
801     List<Relationship> demoRelationships = relationshipManager.getAllRelationships(demoIdentity);
802     assertNotNull("demoRelationships must not be null", demoRelationships);
803     assertEquals("demoRelationships.size() must return: 1", 1, demoRelationships.size());
804     
805     List<Relationship> johnRelationships = relationshipManager.getAllRelationships(johnIdentity);
806     assertNotNull("johnRelationships must not be null", johnRelationships);
807     assertEquals("johnRelationships.size() must return: 1", 1, johnRelationships.size());
808     
809     tearDownRelationshipList.add(rootToDemoRelationship);
810     tearDownRelationshipList.add(maryToRootRelationship);
811     tearDownRelationshipList.add(rootToJohnRelationship);
812   }
813   
814   /**
815    * Test {@link RelationshipManager#getRelationshipsByIdentityId(String)}
816    * 
817    * @throws Exception
818    * @since 1.2.0-Beta3
819    */
820   public void testGetRelationshipsByIdentityId() throws Exception {
821     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
822     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
823     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
824     
825     List<Relationship> rootRelationships = relationshipManager.getRelationshipsByIdentityId(rootIdentity.getId());
826     assertNotNull("rootRelationships must not be null", rootRelationships);
827     assertEquals("rootRelationships.size() must return: 3", 3, rootRelationships.size());
828     
829     tearDownRelationshipList.add(rootToDemoRelationship);
830     tearDownRelationshipList.add(maryToRootRelationship);
831     tearDownRelationshipList.add(rootToJohnRelationship);
832   }
833   
834   /**
835    * Test {@link RelationshipManager#getIdentities(Identity)}
836    * 
837    * @throws Exception
838    * @since 1.2.0-Beta3
839    */
840   public void testGetIdentities() throws Exception {
841     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
842     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
843     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
844     
845     relationshipManager.confirm(rootIdentity, demoIdentity);
846     relationshipManager.confirm(maryIdentity, rootIdentity);
847     relationshipManager.confirm(rootIdentity, johnIdentity);
848     
849     List<Identity> rootConnections = relationshipManager.getIdentities(rootIdentity);
850     assertNotNull("rootConnections must not be null", rootConnections);
851     assertEquals("rootConnections.size() must return: 3", 3, rootConnections.size());
852     
853     tearDownRelationshipList.add(rootToDemoRelationship);
854     tearDownRelationshipList.add(maryToRootRelationship);
855     tearDownRelationshipList.add(rootToJohnRelationship);
856   }
857   
858   /**
859    * Test {@link RelationshipManager#create(Identity, Identity)}
860    * 
861    * @throws Exception
862    * @since 1.2.0-Beta3
863    */
864   public void testCreate() throws Exception {
865     Relationship demoToJohnRelationship = relationshipManager.create(demoIdentity, johnIdentity);
866     assertNotNull("demoToJohnRelationship must not be null", demoToJohnRelationship);
867     assertEquals("demoToJohnRelationship.getSender() must return: " + demoIdentity, demoIdentity, demoToJohnRelationship.getSender());
868     assertEquals("demoToJohnRelationship.getReceiver() must return: " + johnIdentity, johnIdentity, demoToJohnRelationship.getReceiver());
869     assertEquals("demoToJohnRelationship.getStatus() must return: " + Relationship.Type.PENDING, Relationship.Type.PENDING, demoToJohnRelationship.getStatus());
870   }
871   
872   /**
873    * Test {@link RelationshipManager#getRelationship(Identity, Identity)}
874    * 
875    * @throws Exception
876    * @since 1.2.0-Beta3
877    */
878   public void testGetRelationship() throws Exception {
879     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
880     
881     rootToDemoRelationship = relationshipManager.getRelationship(rootIdentity, demoIdentity);
882     assertNotNull("rootToDemoRelationship must not be null", rootToDemoRelationship);
883     assertEquals("rootToDemoRelationship.getStatus() must return: " + Relationship.Type.PENDING, Relationship.Type.PENDING, rootToDemoRelationship.getStatus());
884     
885     relationshipManager.confirm(rootIdentity, demoIdentity);
886     
887     rootToDemoRelationship = relationshipManager.getRelationship(rootIdentity, demoIdentity);
888     assertNotNull("rootToDemoRelationship must not be null", rootToDemoRelationship);
889     assertEquals("rootToDemoRelationship.getStatus() must return: " + Relationship.Type.CONFIRMED, Relationship.Type.CONFIRMED, rootToDemoRelationship.getStatus());
890     
891     tearDownRelationshipList.add(rootToDemoRelationship);
892   }
893   
894   /**
895    * Test {@link RelationshipManager#findRelationships(Identity, Type)}
896    * 
897    * @throws Exception
898    * @since 1.2.0-Beta3
899    */
900   public void testFindRelationships() throws Exception {
901     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
902     Relationship maryToRootRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
903     Relationship rootToJohnRelationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
904     
905     List<Identity> rootRelationships = relationshipManager.findRelationships(rootIdentity, Relationship.Type.PENDING);
906     assertNotNull("rootRelationships must not be null", rootRelationships);
907     assertEquals("rootRelationships.size() must return: 3", 3, rootRelationships.size());
908     
909     relationshipManager.confirm(rootIdentity, demoIdentity);
910     
911     rootRelationships = relationshipManager.findRelationships(rootIdentity, Relationship.Type.CONFIRMED);
912     assertNotNull("rootRelationships must not be null", rootRelationships);
913     assertEquals("rootRelationships.size() must return: 1", 1, rootRelationships.size());
914     
915     tearDownRelationshipList.add(rootToDemoRelationship);
916     tearDownRelationshipList.add(maryToRootRelationship);
917     tearDownRelationshipList.add(rootToJohnRelationship);
918   }
919   
920   /**
921    * Test {@link RelationshipManager#getRelationshipStatus(Relationship, Identity)}
922    * 
923    * @throws Exception
924    * @since 1.2.0-Beta3
925    */
926   public void testGetRelationshipStatus() throws Exception {
927     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
928     assertEquals("relationshipManager.getRelationshipStatus(rootToDemoRelationship, rootIdentity) must return: "
929                  + Relationship.Type.PENDING, Relationship.Type.PENDING
930                  , relationshipManager.getRelationshipStatus(rootToDemoRelationship, rootIdentity));
931     
932     relationshipManager.confirm(rootIdentity, demoIdentity);
933     rootToDemoRelationship = relationshipManager.get(rootIdentity, demoIdentity);
934     assertEquals("relationshipManager.getRelationshipStatus(rootToDemoRelationship, rootIdentity) must return: "
935                  + Relationship.Type.PENDING, Relationship.Type.CONFIRMED
936                  , relationshipManager.getRelationshipStatus(rootToDemoRelationship, rootIdentity));
937     
938     tearDownRelationshipList.add(rootToDemoRelationship);
939   }
940   
941   /**
942    * Test {@link RelationshipManager#getConnectionStatus(Identity, Identity)}
943    * 
944    * @throws Exception
945    * @since 1.2.0-Beta3
946    */
947   public void testGetConnectionStatus() throws Exception {
948     Relationship rootToDemoRelationship = relationshipManager.inviteToConnect(rootIdentity, demoIdentity);
949     
950     assertEquals("relationshipManager.getConnectionStatus(rootIdentity, demoIdentity) must return: " + 
951                  Relationship.Type.PENDING, Relationship.Type.PENDING, 
952                  relationshipManager.getConnectionStatus(rootIdentity, demoIdentity));
953     
954     relationshipManager.confirm(rootIdentity, demoIdentity);
955     assertEquals("relationshipManager.getConnectionStatus(rootIdentity, demoIdentity) must return: " + 
956                  Relationship.Type.CONFIRMED, Relationship.Type.CONFIRMED, 
957                  relationshipManager.getConnectionStatus(rootIdentity, demoIdentity));
958     
959     tearDownRelationshipList.add(rootToDemoRelationship);
960   }
961   
962   /**
963    * Test {@link RelationshipManager#invite(Identity, Identity) and RelationshipManager#get(String)}
964    *
965    * @throws Exception
966    */
967   public void testIntiveAndGetByRelationshipId() throws Exception {
968     Relationship invitedRelationship = relationshipManager.invite(johnIdentity, maryIdentity);
969     
970     Relationship foundRelationship = relationshipManager.get(invitedRelationship.getId());
971     assertNotNull("foundRelationship must not be null", foundRelationship);
972     assertNotNull("foundRelationship.getId() must not be null", foundRelationship.getId());
973     assertEquals(foundRelationship.getId(), invitedRelationship.getId());
974 
975     tearDownRelationshipList.add(invitedRelationship);
976   }
977 
978   /**
979    * Test {@link RelationshipManager#getPending(Identity)}
980    *
981    * @throws Exception
982    */
983   public void testGetPendingWithIdentity() throws Exception {
984     Relationship johnDemoRelationship = relationshipManager.invite(johnIdentity, demoIdentity);
985     Relationship johnMaryRelationship = relationshipManager.invite(johnIdentity, maryIdentity);
986     Relationship johnRootRelationship = relationshipManager.invite(johnIdentity, rootIdentity);
987 
988     List<Relationship> foundListRelationships = relationshipManager.getPending(johnIdentity);
989     assertNotNull("foundListRelationships must not be null", foundListRelationships);
990     assertEquals(3, foundListRelationships.size());
991 
992     tearDownRelationshipList.add(johnDemoRelationship);
993     tearDownRelationshipList.add(johnMaryRelationship);
994     tearDownRelationshipList.add(johnRootRelationship);
995   }
996 
997   /**
998    * Test {@link RelationshipManager#getPending(Identity) and RelationshipManager#getIncoming(Identity)}
999    *
1000    * @throws Exception
1001    */
1002   public void testGetPendingAndIncoming() throws Exception {
1003     Relationship johnDemoRelationship = relationshipManager.invite(johnIdentity, demoIdentity);
1004     Relationship johnMaryRelationship = relationshipManager.invite(johnIdentity, maryIdentity);
1005     Relationship johnRootRelationship = relationshipManager.invite(johnIdentity, rootIdentity);
1006 
1007     List<Relationship> listPendingRelationship = relationshipManager.getPending(johnIdentity);
1008     assertNotNull("listRelationshipConfirm must not be null", listPendingRelationship);
1009     assertEquals(3, listPendingRelationship.size());
1010 
1011     List<Relationship> listMaryRequireValidationRelationship = relationshipManager.getIncoming(maryIdentity);
1012     assertEquals(1, listMaryRequireValidationRelationship.size());
1013 
1014     tearDownRelationshipList.add(johnDemoRelationship);
1015     tearDownRelationshipList.add(johnMaryRelationship);
1016     tearDownRelationshipList.add(johnRootRelationship);
1017   }
1018 
1019   /**
1020    * Test {@link RelationshipManager#getPending(Identity) and RelationshipManager#getIncoming(Identity, List)}
1021    *
1022    * @throws Exception
1023    */
1024   public void testGetPendingAndIncomingWithListIdentities() throws Exception {
1025     Relationship johnDemoRelationship = relationshipManager.invite(johnIdentity, demoIdentity);
1026     Relationship johnMaryRelationship = relationshipManager.invite(johnIdentity, maryIdentity);
1027     Relationship johnRootRelationship = relationshipManager.invite(johnIdentity, rootIdentity);
1028     Relationship maryDemoRelationship = relationshipManager.invite(maryIdentity, demoIdentity);
1029 
1030     List<Identity> listIdentities = new ArrayList<Identity>();
1031     listIdentities.add(demoIdentity);
1032     listIdentities.add(maryIdentity);
1033     listIdentities.add(johnIdentity);
1034     listIdentities.add(rootIdentity);
1035 
1036     List<Relationship> listRelationshipConfirm = relationshipManager.getPending(johnIdentity, listIdentities);
1037     assertEquals(3, listRelationshipConfirm.size());
1038 
1039     List<Relationship> listRelationshipNotConfirm = relationshipManager.getIncoming(demoIdentity, listIdentities);
1040     assertEquals(2, listRelationshipNotConfirm.size());
1041 
1042     tearDownRelationshipList.add(johnDemoRelationship);
1043     tearDownRelationshipList.add(johnMaryRelationship);
1044     tearDownRelationshipList.add(johnRootRelationship);
1045     tearDownRelationshipList.add(maryDemoRelationship);
1046   }
1047 
1048   /**
1049    * Test {@link RelationshipManager#getConfirmed(Identity)}
1050    *
1051    * @throws Exception
1052    */
1053   public void testGetConfirmedWithIdentity() throws Exception {
1054     List<Relationship> johnContacts = relationshipManager.getConfirmed(johnIdentity);
1055     assertNotNull("johnContacts must not be null", johnContacts);
1056     assertEquals("johnContacts.size() must be 0", 0, johnContacts.size());
1057 
1058     Relationship johnDemoRelationship = relationshipManager.invite(johnIdentity, demoIdentity);
1059     Relationship johnMaryRelationship = relationshipManager.invite(johnIdentity, maryIdentity);
1060     Relationship johnRootRelationship = relationshipManager.invite(johnIdentity, rootIdentity);
1061 
1062     relationshipManager.confirm(johnDemoRelationship);
1063     relationshipManager.confirm(johnMaryRelationship);
1064     relationshipManager.confirm(johnRootRelationship);
1065 
1066     List<Relationship> contactsList = relationshipManager.getConfirmed(johnIdentity);
1067     assertEquals(3, contactsList.size());
1068 
1069     tearDownRelationshipList.add(johnDemoRelationship);
1070     tearDownRelationshipList.add(johnMaryRelationship);
1071     tearDownRelationshipList.add(johnRootRelationship);
1072   }
1073 
1074   /**
1075    * Test {@link RelationshipManager#getConfirmed(Identity, List)}
1076    *
1077    * @throws Exception
1078    */
1079   public void testGetConfirmedWithIdentityAndListIdentity() throws Exception {
1080     Relationship johnDemoRelationship = relationshipManager.invite(johnIdentity, demoIdentity);
1081     Relationship johnMaryRelationship = relationshipManager.invite(johnIdentity, maryIdentity);
1082     Relationship johnRootRelationship = relationshipManager.invite(johnIdentity, rootIdentity);
1083 
1084     relationshipManager.confirm(johnDemoRelationship);
1085     relationshipManager.confirm(johnMaryRelationship);
1086     relationshipManager.confirm(johnRootRelationship);
1087 
1088     List<Identity> listIdentities = new ArrayList<Identity>();
1089     listIdentities.add(demoIdentity);
1090     listIdentities.add(maryIdentity);
1091     listIdentities.add(johnIdentity);
1092     listIdentities.add(rootIdentity);
1093 
1094     List<Relationship> contactsList = relationshipManager.getConfirmed(johnIdentity, listIdentities);
1095     assertEquals(3, contactsList.size());
1096     tearDownRelationshipList.add(johnDemoRelationship);
1097     tearDownRelationshipList.add(johnMaryRelationship);
1098     tearDownRelationshipList.add(johnRootRelationship);
1099   }
1100 
1101   /**
1102    * Test {@link RelationshipManager#save(Relationship)}
1103    *
1104    * @throws Exception
1105    */
1106   public void testSave() throws Exception {
1107     Relationship testRelationship = new Relationship(johnIdentity, demoIdentity, Type.PENDING);
1108     relationshipManager.save(testRelationship);
1109     assertNotNull("testRelationship.getId() must not be null", testRelationship.getId());
1110 
1111     tearDownRelationshipList.add(testRelationship);
1112   }
1113 
1114   /**
1115    *
1116    * @throws Exception
1117    */
1118   /*
1119   public void testGetManyRelationshipsByIdentityId() throws Exception {
1120     String providerId = OrganizationIdentityProvider.NAME;
1121 
1122     Identity sender = identityManager.getOrCreateIdentity(providerId,"john");
1123     identityManager.saveIdentity(sender);
1124     assertNotNull(sender.getId());
1125 
1126     Identity receiver = identityManager.getOrCreateIdentity(providerId,"mary");
1127     assertNotNull(receiver.getId());
1128 
1129     relationshipManager.invite(sender, receiver);
1130 
1131     List<Relationship> senderRelationships = relationshipManager.getAllRelationships(sender);
1132     List<Relationship> receiverRelationships = relationshipManager.getAllRelationships(receiver);
1133 
1134     assertEquals(total, senderRelationships.size());
1135     assertEquals(total, receiverRelationships.size());
1136   }
1137 */
1138 
1139   /**
1140    * Test {@link RelationshipManager#invite(Identity, Identity)}
1141    * 
1142    * @throws Exception
1143    */
1144   public void testInviteRelationship() throws Exception {
1145     Relationship relationship = relationshipManager.invite(johnIdentity, maryIdentity);
1146     assertNotNull(relationship.getId());
1147     assertEquals(Relationship.Type.PENDING, relationship.getStatus());
1148 
1149     List<Relationship> senderRelationships = relationshipManager.getAll(johnIdentity);
1150     List<Relationship> receiverRelationships = relationshipManager.getAll(maryIdentity);
1151 
1152     assertEquals(1, senderRelationships.size());
1153     assertEquals(1, receiverRelationships.size());
1154 
1155     tearDownRelationshipList.addAll(senderRelationships);
1156   }
1157 
1158   /**
1159    * Test {@link RelationshipManager#confirm(Relationship)}
1160    *
1161    * @throws Exception
1162    */
1163   public void testConfirm() throws Exception {
1164     Relationship relationship = relationshipManager.invite(johnIdentity, demoIdentity);
1165     relationshipManager.confirm(relationship);
1166     relationship = relationshipManager.get(johnIdentity, demoIdentity);
1167     assertNotNull(relationship.getId());
1168     assertEquals(Relationship.Type.CONFIRMED, relationship.getStatus());
1169 
1170     List<Relationship> senderRelationships = relationshipManager.getAll(johnIdentity);
1171     List<Relationship> receiverRelationships = relationshipManager.getAll(demoIdentity);
1172 
1173     assertEquals(1, senderRelationships.size());
1174     assertEquals(1, receiverRelationships.size());
1175 
1176     tearDownRelationshipList.addAll(senderRelationships);
1177   }
1178 
1179   /**
1180    * Test {@link RelationshipManager#delete(Relationship)}
1181    *
1182    * @throws Exception
1183    * @since 1.2.0-Beta3
1184    */
1185   public void testDelete() throws Exception {
1186     Relationship relationship = relationshipManager.inviteToConnect(rootIdentity, johnIdentity);
1187     relationshipManager.confirm(johnIdentity, rootIdentity);
1188     relationshipManager.delete(relationship);
1189 
1190     relationship = relationshipManager.get(rootIdentity, johnIdentity);
1191     assertNull("relationship must be null", relationship);
1192   }
1193   
1194   /**
1195    * Test {@link RelationshipManager#remove(Relationship)}
1196    *
1197    * @throws Exception
1198    */
1199   public void testRemove() throws Exception {
1200     Relationship relationship = relationshipManager.invite(rootIdentity, johnIdentity);
1201     relationshipManager.confirm(relationship);
1202     relationshipManager.remove(relationship);
1203 
1204     relationship = relationshipManager.get(rootIdentity, johnIdentity);
1205     assertNull("relationship must be null", relationship);
1206   }
1207 
1208 // TODO: Skip this, will be implement later
1209 //  /**
1210 //   *
1211 //   * @throws Exception
1212 //   */
1213 //  public void testIgnore() throws Exception {
1214 //
1215 //    Relationship relationship = relationshipManager.invite(johnIdentity, rootIdentity);
1216 //    relationshipManager.ignore(relationship);
1217 //    assertNotNull(relationship.getId());
1218 //    assertEquals(Relationship.Type.IGNORED, relationship.getStatus());
1219 //
1220 //    List<Relationship> senderRelationships = relationshipManager.getAll(johnIdentity);
1221 //    List<Relationship> receiverRelationships = relationshipManager.getAll(rootIdentity);
1222 //
1223 //    assertEquals(1, senderRelationships.size());
1224 //    assertEquals(1, receiverRelationships.size());
1225 //
1226 //    tearDownRelationshipList.addAll(senderRelationships);
1227 //  }
1228 
1229   /**
1230    * Test {@link RelationshipManager#getPending(Identity)}
1231    * 
1232    * @throws Exception
1233    */
1234   public void testGetPending() throws Exception {
1235     Relationship rootDemo = relationshipManager.invite(rootIdentity, demoIdentity);
1236     assertNotNull("rootDemo.getId() must not be null", rootDemo.getId());
1237     Relationship rootJohn = relationshipManager.invite(rootIdentity, johnIdentity);
1238     assertNotNull("rootJohn.getId() must not be null", rootJohn.getId());
1239     Relationship rootMary = relationshipManager.invite(rootIdentity, maryIdentity);
1240     assertNotNull("rootMary.getId() must not be null", rootMary.getId());
1241     Relationship demoMary = relationshipManager.invite(demoIdentity, maryIdentity);
1242     assertNotNull("demoMary.getId() must not be null", demoMary.getId());
1243     Relationship demoJohn = relationshipManager.invite(demoIdentity, johnIdentity);
1244     assertNotNull("demoJohn.getId() must not be null", demoJohn.getId());
1245     Relationship johnDemo = relationshipManager.invite(johnIdentity, demoIdentity);
1246     assertNotNull("johnDemo.getId() must not be null", johnDemo.getId());
1247 
1248     List<Relationship> rootRelationships = relationshipManager.getPending(rootIdentity);
1249     List<Relationship> demoRelationships = relationshipManager.getPending(demoIdentity);
1250     List<Relationship> johnRelationships = relationshipManager.getPending(johnIdentity);
1251 
1252     assertEquals(3, rootRelationships.size());
1253     assertEquals(2, demoRelationships.size());
1254     assertEquals(0, johnRelationships.size());
1255 
1256     tearDownRelationshipList.add(rootDemo);
1257     tearDownRelationshipList.add(rootJohn);
1258     tearDownRelationshipList.add(rootMary);
1259     tearDownRelationshipList.add(demoMary);
1260     tearDownRelationshipList.add(demoJohn);
1261   }
1262 
1263   /**
1264    * Test relationship with caching.
1265    * 
1266    * @throws Exception
1267    */
1268   public void testSavedCached() throws Exception {
1269     Relationship rootDemo = relationshipManager.get(rootIdentity, demoIdentity);
1270     assertNull("rootDemo must be null", rootDemo);
1271     Relationship rootDemo2 = relationshipManager.get(demoIdentity, rootIdentity);
1272     assertNull("rootDemo must be null", rootDemo2);
1273     Relationship.Type rootDemoStatus = relationshipManager.getStatus(demoIdentity, rootIdentity);
1274     assertNull("rootDemoStatus must be null",rootDemoStatus);
1275     rootDemo = relationshipManager.invite(rootIdentity, demoIdentity);
1276     assertNotNull("rootDemo.getId() must not be null", rootDemo.getId());
1277     assertEquals(rootDemo.getStatus(), Relationship.Type.PENDING);
1278     tearDownRelationshipList.add(rootDemo);
1279 
1280     Relationship rootMary = relationshipManager.get(rootIdentity, maryIdentity);
1281     Relationship.Type rootMaryStatus = relationshipManager.getStatus(maryIdentity, rootIdentity);
1282     assertNull("rootMary must be null", rootMary);
1283     assertNull("rootMaryStatus must be null", rootMaryStatus);
1284     rootMary = relationshipManager.invite(rootIdentity, maryIdentity);
1285     assertNotNull("rootMary.getId() must not be null", rootMary.getId());
1286     assertEquals(Relationship.Type.PENDING, rootMary.getStatus());
1287     tearDownRelationshipList.add(rootMary);
1288 
1289     Relationship rootJohn = relationshipManager.get(rootIdentity, johnIdentity);
1290     assertNull("rootJohn must be null", rootJohn);
1291     assertNull("rootMaryStatus must be null", rootMaryStatus);
1292     rootJohn = relationshipManager.invite(rootIdentity, johnIdentity);
1293     assertNotNull("rootJohn.getId() must not be null", rootJohn.getId());
1294     assertEquals(Relationship.Type.PENDING, rootJohn.getStatus());
1295     tearDownRelationshipList.add(rootJohn);
1296 
1297     Relationship demoMary = relationshipManager.get(demoIdentity, maryIdentity);
1298     Relationship.Type demoMaryStatus = relationshipManager.getStatus(maryIdentity, demoIdentity);
1299     assertNull("demoMary must be null", demoMary);
1300     assertNull("demoMaryStatus must be null", demoMaryStatus);
1301     demoMary = relationshipManager.invite(demoIdentity, maryIdentity);
1302     assertNotNull("demoMary.getId() must not be null", demoMary.getId());
1303     assertEquals(Relationship.Type.PENDING, demoMary.getStatus());
1304     tearDownRelationshipList.add(demoMary);
1305   }
1306   
1307 
1308   /**
1309    * Tests getting connections of one identity with list access.
1310    * 
1311    * @throws Exception
1312    */
1313   public void testGetConnections() throws Exception {
1314      Relationship johnDemoRelationship = relationshipManager.inviteToConnect(johnIdentity, demoIdentity);
1315      Relationship johnMaryRelationship = relationshipManager.inviteToConnect(johnIdentity, maryIdentity);
1316      Relationship johnRootRelationship = relationshipManager.inviteToConnect(johnIdentity, rootIdentity);
1317 
1318      relationshipManager.confirm(demoIdentity, johnIdentity);
1319      relationshipManager.confirm(maryIdentity, johnIdentity);
1320      relationshipManager.confirm(rootIdentity, johnIdentity);
1321 
1322      ListAccess<Identity> contactsList = relationshipManager.getConnections(johnIdentity);
1323      assertEquals(3, contactsList.getSize());
1324 
1325      tearDownRelationshipList.add(johnDemoRelationship);
1326      tearDownRelationshipList.add(johnMaryRelationship);
1327      tearDownRelationshipList.add(johnRootRelationship);
1328   }
1329 
1330   public void testOldGetSuggestions() throws Exception {
1331     Relationship maryToGhostRelationship = relationshipManager.inviteToConnect(ghostIdentity, maryIdentity);
1332     Relationship ghostToJohnRelationship = relationshipManager.inviteToConnect(ghostIdentity, johnIdentity);
1333     Relationship maryToDemoRelationship = relationshipManager.inviteToConnect(demoIdentity, maryIdentity);
1334 
1335     Map<Identity, Integer> suggestions = relationshipManager.getSuggestions(ghostIdentity, 0, 10);
1336     // The relationships must be confirmed first
1337     assertTrue(suggestions.isEmpty());
1338     relationshipManager.confirm(ghostIdentity, maryIdentity);
1339     relationshipManager.confirm(ghostIdentity, johnIdentity);
1340     relationshipManager.confirm(demoIdentity, maryIdentity);
1341     suggestions = relationshipManager.getSuggestions(ghostIdentity, 0, 10);
1342     Object[] objs = suggestions.entrySet().toArray();
1343 
1344     Entry<Identity, Integer> first = (Entry<Identity, Integer>) objs[0];
1345 
1346     assertEquals(1, first.getValue().intValue());
1347     assertEquals(demoIdentity.getRemoteId(), first.getKey().getRemoteId());
1348 
1349     //increase common users
1350     Relationship johnToDemoRelationship = relationshipManager.inviteToConnect(demoIdentity, johnIdentity);
1351     Relationship paulToDemoRelationship = relationshipManager.inviteToConnect(paulIdentity, maryIdentity);
1352     suggestions = relationshipManager.getSuggestions(ghostIdentity, 0, 10);
1353     assertEquals(1, suggestions.size());
1354     relationshipManager.confirm(demoIdentity, johnIdentity);
1355     relationshipManager.confirm(paulIdentity, maryIdentity);
1356 
1357     suggestions = relationshipManager.getSuggestions(ghostIdentity, 0, 10);
1358     objs = suggestions.entrySet().toArray();
1359     first = (Entry<Identity, Integer>) objs[0];
1360     Entry<Identity, Integer> second = (Entry<Identity, Integer>) objs[1];
1361 
1362     assertEquals(demoIdentity.getRemoteId(), first.getKey().getRemoteId());
1363     assertEquals(paulIdentity.getRemoteId(), second.getKey().getRemoteId());
1364     assertEquals(2, first.getValue().intValue());
1365     assertEquals(demoIdentity.getRemoteId(), first.getKey().getRemoteId());
1366     assertEquals(1, second.getValue().intValue());
1367     assertEquals(paulIdentity.getRemoteId(), second.getKey().getRemoteId());
1368 
1369     //test with offset > 0
1370     suggestions = relationshipManager.getSuggestions(ghostIdentity, 1, 10);
1371 
1372     objs = suggestions.entrySet().toArray();
1373     first = (Entry<Identity, Integer>) objs[0];
1374 
1375     assertEquals(1, first.getValue().intValue());
1376     assertEquals(paulIdentity.getRemoteId(), first.getKey().getRemoteId());
1377 
1378     tearDownRelationshipList.add(maryToDemoRelationship);
1379     tearDownRelationshipList.add(johnToDemoRelationship);
1380     tearDownRelationshipList.add(maryToGhostRelationship);
1381     tearDownRelationshipList.add(ghostToJohnRelationship);
1382     tearDownRelationshipList.add(paulToDemoRelationship);
1383   }
1384 
1385   public void testGetSuggestions() throws Exception {
1386     Relationship maryToGhostRelationship = relationshipManager.inviteToConnect(ghostIdentity, maryIdentity);
1387     Relationship ghostToJohnRelationship = relationshipManager.inviteToConnect(ghostIdentity, johnIdentity);
1388     Relationship maryToDemoRelationship = relationshipManager.inviteToConnect(demoIdentity, maryIdentity);
1389 
1390     Map<Identity, Integer> suggestions = relationshipManager.getSuggestions(ghostIdentity, -1, -1, 10); 
1391     // The relationships must be confirmed first
1392     assertTrue(suggestions.isEmpty());
1393     relationshipManager.confirm(ghostIdentity, maryIdentity);
1394     relationshipManager.confirm(ghostIdentity, johnIdentity);
1395     relationshipManager.confirm(demoIdentity, maryIdentity);
1396     suggestions = relationshipManager.getSuggestions(ghostIdentity, -1, -1, 10); 
1397     assertEquals(1, suggestions.size());
1398     Object[] objs = suggestions.entrySet().toArray();
1399     
1400     Entry<Identity, Integer> first = (Entry<Identity, Integer>) objs[0];
1401 
1402     assertEquals(1, first.getValue().intValue());
1403     assertEquals(demoIdentity.getRemoteId(), first.getKey().getRemoteId());
1404 
1405     //increase common users
1406     Relationship johnToDemoRelationship = relationshipManager.inviteToConnect(demoIdentity, johnIdentity);
1407     Relationship paulToDemoRelationship = relationshipManager.inviteToConnect(paulIdentity, maryIdentity);
1408     suggestions = relationshipManager.getSuggestions(ghostIdentity, -1, -1, 10); 
1409     assertEquals(1, suggestions.size());
1410     relationshipManager.confirm(demoIdentity, johnIdentity);
1411     relationshipManager.confirm(paulIdentity, maryIdentity);
1412 
1413     suggestions = relationshipManager.getSuggestions(ghostIdentity, -1, -1, 10); 
1414     assertEquals(2, suggestions.size());
1415     objs = suggestions.entrySet().toArray();
1416     first = (Entry<Identity, Integer>) objs[0];
1417     Entry<Identity, Integer> second = (Entry<Identity, Integer>) objs[1];
1418     
1419     assertEquals(demoIdentity.getRemoteId(), first.getKey().getRemoteId());
1420     assertEquals(paulIdentity.getRemoteId(), second.getKey().getRemoteId());
1421     assertEquals(2, first.getValue().intValue());
1422     assertEquals(demoIdentity.getRemoteId(), first.getKey().getRemoteId());
1423     assertEquals(1, second.getValue().intValue());
1424     assertEquals(paulIdentity.getRemoteId(), second.getKey().getRemoteId());
1425 
1426     relationshipManager.delete(paulToDemoRelationship);
1427     suggestions = relationshipManager.getSuggestions(ghostIdentity, -1, -1, 10); 
1428     assertEquals(1, suggestions.size());
1429 
1430     tearDownRelationshipList.add(maryToDemoRelationship);
1431     tearDownRelationshipList.add(johnToDemoRelationship);
1432     tearDownRelationshipList.add(maryToGhostRelationship);
1433     tearDownRelationshipList.add(ghostToJohnRelationship);
1434   }
1435 
1436   public void testGetSuggestionsWithParams() throws Exception {
1437     Relationship maryToGhostRelationship = relationshipManager.inviteToConnect(ghostIdentity, maryIdentity);
1438     Relationship maryToDemoRelationship = relationshipManager.inviteToConnect(demoIdentity, maryIdentity);
1439     Relationship paulToMaryRelationship = relationshipManager.inviteToConnect(paulIdentity, maryIdentity);
1440     Relationship johnToMaryRelationship = relationshipManager.inviteToConnect(maryIdentity, johnIdentity);
1441     Relationship rootToMaryRelationship = relationshipManager.inviteToConnect(maryIdentity, rootIdentity);
1442 
1443     Map<Identity, Integer> suggestions = relationshipManager.getSuggestions(ghostIdentity, -1, -1, 10); 
1444     // The relationships must be confirmed first
1445     assertTrue(suggestions.isEmpty());
1446     suggestions = relationshipManager.getSuggestions(ghostIdentity, 10, 10, 10);
1447     assertTrue(suggestions.isEmpty());
1448     suggestions = relationshipManager.getSuggestions(ghostIdentity, 10, 10, 10);
1449     assertTrue(suggestions.isEmpty());
1450     relationshipManager.confirm(ghostIdentity, maryIdentity);
1451     relationshipManager.confirm(paulIdentity, maryIdentity);
1452     relationshipManager.confirm(demoIdentity, maryIdentity);
1453     relationshipManager.confirm(maryIdentity, johnIdentity);
1454     relationshipManager.confirm(maryIdentity, rootIdentity);
1455 
1456     suggestions = relationshipManager.getSuggestions(ghostIdentity, -1, -1, 10);
1457     assertFalse(suggestions.containsKey(ghostIdentity));
1458     assertEquals(4, suggestions.size());
1459     suggestions = relationshipManager.getSuggestions(ghostIdentity, -1, -1, 10);
1460     assertFalse(suggestions.containsKey(ghostIdentity));
1461     assertEquals(4, suggestions.size());
1462     suggestions = relationshipManager.getSuggestions(ghostIdentity, 2, 2, 10);
1463     assertFalse(suggestions.containsKey(ghostIdentity));
1464     // 1 or 2 depending on the connections loaded, if there is ghostIdentity, it will be one
1465     // otherwise it will be 2
1466     assertTrue(suggestions.size() > 0 && suggestions.size() <= 2);
1467     suggestions = relationshipManager.getSuggestions(ghostIdentity, 2, 3, 10);
1468     assertFalse(suggestions.containsKey(ghostIdentity));
1469     // 1 or 2 depending on the connections loaded, if there is ghostIdentity, it will be one
1470     // otherwise it will be 2
1471     assertTrue(suggestions.size() > 0 && suggestions.size() <= 2);
1472     suggestions = relationshipManager.getSuggestions(ghostIdentity, 2, 3, 10);
1473     assertFalse(suggestions.containsKey(ghostIdentity));
1474     // 1 or 2 depending on the connections loaded, if there is ghostIdentity, it will be one
1475     // otherwise it will be 2
1476     assertTrue(suggestions.size() > 0 && suggestions.size() <= 2);
1477 
1478     suggestions = relationshipManager.getSuggestions(ghostIdentity, 10, 10, 2);
1479     assertFalse(suggestions.containsKey(ghostIdentity));
1480     assertEquals(2, suggestions.size());
1481 
1482     suggestions = relationshipManager.getSuggestions(ghostIdentity, 10, 2, 2);
1483     assertFalse(suggestions.containsKey(ghostIdentity));
1484     assertEquals(2, suggestions.size());
1485 
1486     tearDownRelationshipList.add(maryToGhostRelationship);
1487     tearDownRelationshipList.add(maryToDemoRelationship);
1488     tearDownRelationshipList.add(paulToMaryRelationship);
1489     tearDownRelationshipList.add(johnToMaryRelationship);
1490     tearDownRelationshipList.add(rootToMaryRelationship);
1491   }
1492   
1493   public void testGetLastConnections() throws Exception {
1494     Relationship maryToGhostRelationship = relationshipManager.inviteToConnect(ghostIdentity, maryIdentity);
1495     relationshipManager.confirm(maryIdentity, ghostIdentity);
1496     Relationship maryToDemoRelationship = relationshipManager.inviteToConnect(demoIdentity, maryIdentity);
1497     relationshipManager.confirm(maryIdentity, demoIdentity);
1498     Relationship paulToMaryRelationship = relationshipManager.inviteToConnect(paulIdentity, maryIdentity);
1499     relationshipManager.confirm(maryIdentity, paulIdentity);
1500     
1501     List<Identity> identities = relationshipManager.getLastConnections(maryIdentity, 10);
1502     assertEquals(3, identities.size());
1503     assertEquals(paulIdentity.getRemoteId(), identities.get(0).getRemoteId());
1504     assertEquals(demoIdentity.getRemoteId(), identities.get(1).getRemoteId());
1505     assertEquals(ghostIdentity.getRemoteId(), identities.get(2).getRemoteId());
1506     
1507     Relationship johnToMaryRelationship = relationshipManager.inviteToConnect(maryIdentity, johnIdentity);
1508     relationshipManager.confirm(johnIdentity, maryIdentity);
1509     identities = relationshipManager.getLastConnections(maryIdentity, 10);
1510     assertEquals(4, identities.size());
1511     assertEquals(johnIdentity.getRemoteId(), identities.get(0).getRemoteId());
1512     
1513     tearDownRelationshipList.add(maryToGhostRelationship);
1514     tearDownRelationshipList.add(maryToDemoRelationship);
1515     tearDownRelationshipList.add(paulToMaryRelationship);
1516     tearDownRelationshipList.add(johnToMaryRelationship); 
1517   }
1518 
1519   public void testGetRelationshipByStatus() throws Exception {
1520     Relationship maryToGhostRelationship = relationshipManager.inviteToConnect(ghostIdentity, maryIdentity);
1521     Relationship ghostToJohnRelationship = relationshipManager.inviteToConnect(ghostIdentity, johnIdentity);
1522     Relationship maryToDemoRelationship = relationshipManager.inviteToConnect(demoIdentity, maryIdentity);
1523     
1524     //check all relationships of ghost
1525     List<Relationship>  list = relationshipManager.getRelationshipsByStatus(ghostIdentity, Relationship.Type.ALL, 0, 10);
1526     assertEquals(2, list.size());
1527     list = relationshipManager.getRelationshipsByStatus(ghostIdentity, Relationship.Type.PENDING, 0, 10);
1528     assertEquals(2, list.size());
1529     list = relationshipManager.getRelationshipsByStatus(ghostIdentity, Relationship.Type.CONFIRMED, 0, 10);
1530     assertEquals(0, list.size());
1531     
1532     relationshipManager.confirm(maryIdentity, ghostIdentity);
1533     relationshipManager.confirm(johnIdentity, ghostIdentity);
1534     
1535     //check all relationships of ghost
1536     list = relationshipManager.getRelationshipsByStatus(ghostIdentity, Relationship.Type.ALL, 0, 10);
1537     assertEquals(2, list.size());
1538     list = relationshipManager.getRelationshipsByStatus(ghostIdentity, Relationship.Type.PENDING, 0, 10);
1539     assertEquals(0, list.size());
1540     list = relationshipManager.getRelationshipsByStatus(ghostIdentity, Relationship.Type.CONFIRMED, 0, 10);
1541     assertEquals(2, list.size());
1542     
1543     assertEquals(1, relationshipManager.getRelationshipsCountByStatus(demoIdentity, Relationship.Type.ALL));
1544     
1545     tearDownRelationshipList.add(maryToGhostRelationship);
1546     tearDownRelationshipList.add(maryToDemoRelationship);
1547     tearDownRelationshipList.add(ghostToJohnRelationship);
1548   }
1549 }