@Test public void weightedFriendRecommendation() throws Exception { deleteSocialGraph(); Person a = personRepository.createPerson("a"); Person b = personRepository.createPerson("b"); Person c = personRepository.createPerson("c"); Person d = personRepository.createPerson("d"); Person e = personRepository.createPerson("e"); Person f = personRepository.createPerson("f"); // A is friends with B,C and D a.addFriend(b); a.addFriend(c); a.addFriend(d); // E is only friend with B e.addFriend(b); // F is friend with B, C, D f.addFriend(b); f.addFriend(c); f.addFriend(d); ArrayList<Person> recommendations = fromIterableToArrayList(a.getFriendRecommendation(2).iterator()); // Recommend F to A assertThat(recommendations.get(0), equalTo(f)); // Recommend E to A assertThat(recommendations.get(1), equalTo(e)); }
private static void setupFriendsBetweenPeople(int maxNrOfFriendsEach) { for (Person person : personRepository.getAllPersons()) { int nrOfFriends = r.nextInt(maxNrOfFriendsEach) + 1; for (int j = 0; j < nrOfFriends; j++) { person.addFriend(getRandomPerson()); } } }
@Test public void singleFriendRecommendation() throws Exception { deleteSocialGraph(); Person a = personRepository.createPerson("a"); Person b = personRepository.createPerson("b"); Person c = personRepository.createPerson("c"); Person d = personRepository.createPerson("d"); Person e = personRepository.createPerson("e"); // A is friends with B,C and D a.addFriend(b); a.addFriend(c); a.addFriend(d); // E is also friend with B, C and D e.addFriend(b); e.addFriend(c); e.addFriend(d); Person recommendation = IteratorUtil.single(a.getFriendRecommendation(1).iterator()); assertThat(recommendation, equalTo(e)); }
@Test public void removingOneFriendIsHandledCleanly() { Person person1 = personRepository.getPersonByName("person#1"); Person person2 = personRepository.getPersonByName("person#2"); person1.addFriend(person2); int noOfFriends = person1.getNrOfFriends(); person1.removeFriend(person2); int noOfFriendsAfterChange = person1.getNrOfFriends(); assertThat(noOfFriends, equalTo(noOfFriendsAfterChange + 1)); }
@Test public void getPathBetweenFriends() throws Exception { deleteSocialGraph(); Person start = personRepository.createPerson("start"); start.setLongTermCtx(LongTermCtxTypes.WORK, "company"); Person middleMan1 = personRepository.createPerson("middle1"); middleMan1.setLongTermCtx(LongTermCtxTypes.WORK, "company"); Person middleMan2 = personRepository.createPerson("middle2"); middleMan2.setLongTermCtx(LongTermCtxTypes.WORK, "company"); Person endMan = personRepository.createPerson("endMan"); endMan.setLongTermCtx(LongTermCtxTypes.WORK, "company"); // Start -> middleMan1 -> middleMan2 -> endMan start.addFriend(middleMan1); middleMan1.addFriend(middleMan2); middleMan2.addFriend(endMan); Iterable<Person> path = start.getShortestPathTo(endMan, 4); assertPathIs(path, start, middleMan1, middleMan2, endMan); // assertThat( path, matchesPathByProperty(Person.NAME, "start", "middle1", "middle2", // "endMan")); }