private Person getRandomPersonWithFriends() { Person p; do { p = getRandomPerson(); } while (p.getNrOfFriends() == 0); return p; }
@Test public void shouldReturnTheCorrectPersonFromAnyStatusUpdate() throws Exception { Person person = getRandomPerson(); person.addContextStatus("Foo", null, null); person.addContextStatus("Bar", null, null); person.addContextStatus("Baz", null, null); for (ContextUpdates status : person.getStatus()) { assertThat(status.getPerson(), equalTo(person)); System.out.println("Person: " + status.getPerson() + " Status: " + status.getStatusText()); } // for ( Node node : index.query( "name", "*e*" ) ) // { // // This will return // Person p = new Person(node); // System.out.println(p.getName()); // System.out.println(p.getCompany()); // } for (Node node : indexStatus.query(ShortTermCtxTypes.STATUS, "*o*")) { // This will return ContextUpdates s = new ContextUpdates(node); System.out.println(s.getStatusText()); } }
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 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)); }
@Test public void friendsOfFriendsWorks() throws Exception { Person person = getRandomPerson(); Person friend = getRandomFriendOf(person); for (Person friendOfFriend : friend.getFriends()) { if (!friendOfFriend.equals(person)) { // You can't be friends with yourself. // assertThat( person.getFriendsOfFriends(), hasItems( friendOfFriend ) ); } } }
@Test public void addStatusAndRetrieveIt() throws Exception { for (int i = 0; i < 20; i++) { Person person = getRandomPerson(); person.addContextStatus("Testing!", "School", null); ContextUpdates update = person.getStatus().iterator().next(); assertThat(update, CoreMatchers.<Object>notNullValue()); assertThat(update.getStatusText(), equalTo("Testing!")); assertThat(update.getPerson(), equalTo(person)); System.out.println("Person: " + person.getName() + " Status: " + update.getStatusText()); } }
@Test public void retrieveStatusUpdatesInDateOrder() throws Exception { Person person = getRandomPersonWithFriends(); int numberOfStatuses = 20; for (int i = 0; i < numberOfStatuses; i++) { Person friend = getRandomFriendOf(person); friend.addContextStatus("Dum-deli-dum...", "location", null); } ArrayList<ContextUpdates> updates = fromIterableToArrayList(person.friendStatuses()); assertThat(updates.size(), equalTo(numberOfStatuses)); assertUpdatesAreSortedByDate(updates); }
@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 multipleStatusesComeOutInTheRightOrder() throws Exception { ArrayList<String> statuses = new ArrayList<String>(); statuses.add("Test1"); statuses.add("Test2"); statuses.add("Test3"); Person person = getRandomPerson(); for (String status : statuses) { person.addContextStatus(status, "location", null); } int i = statuses.size(); for (ContextUpdates update : person.getStatus()) { i--; assertThat(update.getStatusText(), equalTo(statuses.get(i))); System.out.println("Person: " + person.getName() + " Status: " + update.getStatusText()); } }
@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)); }
private Person getRandomFriendOf(Person p) { ArrayList<Person> friends = new ArrayList<Person>(); IteratorUtil.addToCollection(p.getFriends().iterator(), friends); return friends.get(r.nextInt(friends.size())); }
@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")); }