Merge branch 'Feature/delete_a_friend' into 'master'

Feature/remove a friend

See merge request cse1105/2018-2019/oopp-group-43/template!60
This commit is contained in:
Merel Steenbergen
2019-04-03 14:29:36 +00:00
9 changed files with 126 additions and 8 deletions

View File

@@ -230,6 +230,24 @@ public class UserService {
.encode().toUri(), String.class);
}
/**
* Removes a friend from the friendslist of the user.
* @param name the username of the current user.
* @param friend the username of the friend you want to remove.
*/
@SuppressWarnings("Duplicates")
public void removeFriend(String name, String friend) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/removeFriend")
.queryParam("name", name)
.queryParam("friend",friend);
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
ResponseEntity<String> authenticateResponse = this.restTemplate.getForEntity(builder.build()
.encode().toUri(), String.class);
}
/**
* Gets the footprint inputs of the user.
* @param name the username of the current user.

View File

@@ -147,6 +147,14 @@ public class UserServiceTest {
userService.addFriend("Eric", "Ceren");
Mockito.verify(userService).addFriend("Eric", "Ceren");
}
@Test
public void removeFriendTest() throws Exception {
userService.addFriend("Eric", "Ceren");
Mockito.verify(userService).addFriend("Eric", "Ceren");
userService.removeFriend("Eric", "Ceren");
Mockito.verify(userService).removeFriend("Eric", "Ceren");
}
}

View File

@@ -208,6 +208,19 @@ public class User {
}
}
/**
* Removes a friend from the friendslist of the user.
* @param user the friend you want to remove.
*/
public void removeFriend(User user) {
if (!friends.contains(user)) {
throw new ApplicationException("This user is not your friend!");
} else {
friends.remove(user);
System.out.print("Friend removed");
}
}
/**
* This method gets a human readable (JSON) object.
* @return the JSON form of the object.

View File

@@ -155,13 +155,25 @@ public class UserController {
}
/**
* This method adds friend for a user.
* This method adds a friend to a user.
* @param name name of the user
*
* @param friend the name of the user you want to add as a friend.
*/
@RequestMapping("/addFriend")
public void addFriend(@RequestParam(value = "name") String name,
@RequestParam(value = "friend") String friend) {
userService.addFriend(name, friend);
}
/**
* This method removes a friend from a user.
* @param name name of the user
* @param friend name of the friend you want to remove
*/
@RequestMapping("/removeFriend")
public void removeFriend(@RequestParam(value = "name") String name,
@RequestParam(value = "friend") String friend) {
userService.removeFriend(name, friend);
}
}

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

@@ -125,6 +125,27 @@ public class UserTest {
});
}
@Test
public void removeFriendValidTest() {
User test = new User(1L, "greenify", "password");
List<User> friendList = new ArrayList<>();
friendList.add(test);
User user = new User(1L, "green", "pass");
user.setFriends(friendList);
assertEquals(user.getFriends(), friendList);
user.removeFriend(test);
assertEquals(user.getFriends(), new ArrayList<User>());
}
@Test
public void removeFriendInvalidTest() {
User user = new User(1L, "greenify", "password");
User test = new User(2L, "user", "pass");
assertThrows(ApplicationException.class, () -> {
user.removeFriend(test);
});
}
@Test
public void setFriendTest() {
List<User> friends = new ArrayList<User>();

View File

@@ -113,6 +113,23 @@ public class UserControllerTest {
assertEquals("merel", arg2Captor.getValue());
}
@Test
public void removeFriendTest() throws Exception {
ArgumentCaptor<String> arg1Captor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> arg2Captor = ArgumentCaptor.forClass(String.class);
mvc.perform(get("/removeFriend")
.param("name", "ceren")
.param("friend", "merel")
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
verify(userService, times(1)).
removeFriend(arg1Captor.capture(), arg2Captor.capture());
assertEquals("ceren", arg1Captor.getValue());
assertEquals("merel", arg2Captor.getValue());
}
@Test
public void getInputMapTest() throws Exception {
ArgumentCaptor<String> arg1Captor = ArgumentCaptor.forClass(String.class);

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