ADD:: RemoveFriend method and tests in Server.UserService
This commit is contained in:
@@ -64,21 +64,37 @@ public class UserService {
|
||||
}
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* Adds a friend to the friendlist of the user.
|
||||
* @param name the username of the user
|
||||
* @param friend the name of the friend you want to add.
|
||||
* @throws ApplicationException if the user is not in the database.
|
||||
*/
|
||||
public void addFriend(String name, String friend) {
|
||||
User user = userRepository.findByName(name);
|
||||
User add = userRepository.findByName(friend);
|
||||
if (user == null || add == null ) {
|
||||
if (add == null ) {
|
||||
throw new ApplicationException("User does not exist");
|
||||
}
|
||||
user.addFriend(add);
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a friend from the friendlist of the user.
|
||||
* @param name the username of the user
|
||||
* @param friend the name of the friend you want to remove.
|
||||
* @throws ApplicationException if the user is not in the database.
|
||||
*/
|
||||
public void removeFriend(String name, String friend) {
|
||||
User user = userRepository.findByName(name);
|
||||
User remove = userRepository.findByName(friend);
|
||||
if (remove == null ) {
|
||||
throw new ApplicationException("User does not exist");
|
||||
}
|
||||
user.removeFriend(remove);
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method sets input for a user.
|
||||
* @param name name of the user
|
||||
|
||||
@@ -266,12 +266,25 @@ public class UserServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addFriendNullFriendTest() {
|
||||
assertThrows(ApplicationException.class, () -> userService.addFriend("alex", null));
|
||||
public void removeFriendTest() {
|
||||
User alex = userRepository.findByName("alex");
|
||||
User lola = userRepository.findByName("lola");
|
||||
assertEquals(lola.getFriends(), alex.getFriends());
|
||||
userService.addFriend("alex", "lola");
|
||||
ArrayList<User> test = new ArrayList<User>();
|
||||
test.add(lola);
|
||||
assertEquals(alex.getFriends(), test);
|
||||
userService.removeFriend("alex", "lola");
|
||||
assertEquals(lola.getFriends(), alex.getFriends());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addFriendNullUserTest() {
|
||||
assertThrows(ApplicationException.class, () -> userService.addFriend(null, "password"));
|
||||
public void removeFriendNullTest() {
|
||||
assertThrows(ApplicationException.class, () -> userService.removeFriend("alex", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addFriendNullFriendTest() {
|
||||
assertThrows(ApplicationException.class, () -> userService.addFriend("alex", null));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user