FriendshipService { public boolean followUser(String usernameToFollow) { log.debug("Following user : {}", usernameToFollow); User currentUser = authenticationService.getCurrentUser(); String domain = DomainUtil.getDomainFromLogin(currentUser.getLogin()); String loginToFollow = DomainUtil.getLoginFromUsernameAndDomain(usernameToFollow, domain); User followedUser = userRepository.findUserByLogin(loginToFollow); if (followedUser != null && !followedUser.equals(currentUser)) { if (counterRepository.getFriendsCounter(currentUser.getLogin()) > 0) { for (String alreadyFollowingTest : friendRepository.findFriendsForUser(currentUser.getLogin())) { if (alreadyFollowingTest.equals(loginToFollow)) { log.debug("User {} already follows user {}", currentUser.getLogin(), followedUser.getLogin()); return false; } } } friendRepository.addFriend(currentUser.getLogin(), followedUser.getLogin()); counterRepository.incrementFriendsCounter(currentUser.getLogin()); followerRepository.addFollower(followedUser.getLogin(), currentUser.getLogin()); counterRepository.incrementFollowersCounter(followedUser.getLogin()); MentionFriend mentionFriend = statusRepository.createMentionFriend(followedUser.getLogin(), currentUser.getLogin()); mentionlineRepository.addStatusToMentionline(mentionFriend.getLogin(), mentionFriend.getStatusId()); log.debug("User {} now follows user {} ", currentUser.getLogin(), followedUser.getLogin()); return true; } else { log.debug("Followed user does not exist : " + loginToFollow); return false; } }  boolean followUser(String usernameToFollow); boolean unfollowUser(String usernameToUnfollow); boolean unfollowUser(User currentUser, User userToUnfollow); List<String> getFriendIdsForUser(String login); Collection<String> getFollowerIdsForUser(String login); Collection<User> getFriendsForUser(String username); Collection<User> getFollowersForUser(String username); boolean isFollowed(String userLogin); boolean isFollowing(String userLogin);  }
@Test public void shouldNotFollowUserBecauseUserDoesNotExist() { mockAuthentication("userWhoWantToFollow@ippon.fr"); assertFalse(friendshipService.followUser("unknownUser")); User userWhoFollow = userService.getUserByUsername("userWhoWantToFollow"); assertThat(userWhoFollow.getFriendsCount(), is(0L)); }
@Test public void shouldNotFollowUserBecauseUserIsAlreadyFollowed() throws Exception { mockAuthentication("userWhoFollow@ippon.fr"); assertFalse(friendshipService.followUser("userWhoIsFollowed")); User userWhoFollow = userService.getUserByUsername("userWhoFollow"); assertThat(userWhoFollow.getFriendsCount(), is(1L)); assertThat(userWhoFollow.getFollowersCount(), is(0L)); User userWhoIsFollowed = userService.getUserByUsername("userWhoIsFollowed"); assertThat(userWhoIsFollowed.getFriendsCount(), is(0L)); assertThat(userWhoIsFollowed.getFollowersCount(), is(1L)); }
@Test public void shouldNotFollowUserBecauseSameUser() throws Exception { mockAuthentication("userWhoWantToFollow@ippon.fr"); User userWhoFollow = userService.getUserByUsername("userWhoWantToFollow"); assertThat(userWhoFollow.getFriendsCount(), is(0L)); assertThat(userWhoFollow.getFollowersCount(), is(0L)); assertFalse(friendshipService.followUser("userWhoWantToFollow")); userWhoFollow = userService.getUserByUsername("userWhoWantToFollow"); assertThat(userWhoFollow.getFriendsCount(), is(0L)); assertThat(userWhoFollow.getFollowersCount(), is(0L)); }