ADD:: RemoveFriend method and tests in Server.UserService

This commit is contained in:
Merel Steenbergen
2019-04-01 16:43:10 +02:00
parent 52291ac10f
commit 6d37b99c14
2 changed files with 35 additions and 6 deletions

View File

@@ -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

View File

@@ -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));
}
}