Revert "Merge branch 'AddFriends' into 'master'"

This reverts merge request !37
This commit is contained in:
Merel Steenbergen
2019-03-23 17:31:00 +00:00
parent 5b3896d310
commit e494b5294d
6 changed files with 12 additions and 167 deletions

View File

@@ -99,26 +99,6 @@ public class UserService {
return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDto.class);
}
/**
* a user adds vegan meal.
* @param id the id of the user
* @param name the username of the user
* @return a userDTO
*/
@SuppressWarnings("Duplicates")
public UserDto addFriend(Long id, String name, String friend) {
//this method is almost the same as the registerUser one, but with a different link
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/addFriend")
.queryParam("id", id)
.queryParam("name", name)
.queryParam("friend", friend);
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDto.class);
}
@RequestMapping("/userData")
public int getVeganData(@RequestParam(value = "veganMeal") int veganMeal) {
return veganMeal;

View File

@@ -1,11 +1,7 @@
package greenify.server.data.model;
import greenify.common.ApplicationException;
import greenify.server.Application;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@@ -31,8 +27,6 @@ public class User {
private int veganMeal;
private ArrayList<User> friends;
public User() {}
/**
@@ -47,7 +41,6 @@ public class User {
this.name = name;
this.password = password;
this.veganMeal = veganMeal;
this.friends = new ArrayList<User>();
}
/**
@@ -98,29 +91,6 @@ public class User {
this.veganMeal = veganMeal;
}
public List<User> getFriends(){
return this.friends;
}
public void addFriend(User user){
if(!user.equals(this)) {
friends.add(user);
}
else {
throw new ApplicationException("Cannnot add yourself as a friend");
}
}
public String friendsToString(){
String result = "";
for(User u : friends){
result += u.getName() + ", ";
}
if(result.endsWith(", ")){
result = result.substring(0, result.lastIndexOf(","));
}
return result;
}
/**
* Returns a human readable object. It's in JSON.
@@ -128,12 +98,8 @@ public class User {
*/
@Override
public String toString() {
String result = "User(id=" + this.id + ", name=" + this.name + ", password="
+ this.password + ", veganMeal=" + this.veganMeal + ", friends=[";
result += friendsToString() + "])";
// result += ")";
return result;
return "User(id=" + this.id + ", name=" + this.name + ", password="
+ this.password + ", veganMeal=" + this.veganMeal + ")";
}
@Override

View File

@@ -32,6 +32,8 @@ public class UserController {
return userService.loginUser(name, password);
}
/**
* adds a vegetarian meal to the user.
* @param id the id of the user
@@ -44,18 +46,4 @@ public class UserController {
//addVeganMeal method of the userService
userService.addVeganMeal(id, name);
}
/**
* adds a friend to the user.
* @param id the id of the user
* @param name thr username of the user
*/
@RequestMapping("/addFriend")
public void addVeganMeal(@RequestParam(value = "id") Long id,
@RequestParam(value = "name") String name,
@RequestParam(value = "friend") String friend) {
//here the requestParams are the id and name of the user and the name of the friend,
// because that is needed for the addFriendmethod of the userService
userService.addFriend(id, name, friend);
}
}

View File

@@ -79,20 +79,6 @@ public class UserService {
+ ", name=" + user.getName() + ")");
}
/**
* add vegan meal to the user.
* @param id the id of the user
* @param name the name of the user
*/
public void addFriend(Long id, String name, String friend) {
User user = userRepository.findByName(name);
User add = userRepository.findByName(friend);
user.addFriend(add);
userRepository.save(user);
logger.info("Added friend to user(id=" + user.getId()
+ ", name=" + user.getName() + ")");
}
@GetMapping(path = "/all")
@ResponseBody
public Iterable<User> getAllUsers() {

View File

@@ -1,18 +1,11 @@
package greenify.server.data.model;
import static greenify.server.data.model.User.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import greenify.common.ApplicationException;
import greenify.server.Application;
import org.junit.Test;
import java.util.ArrayList;
public class UserTest {
@Test
public void setAndGetTest() {
@@ -27,20 +20,12 @@ public class UserTest {
assertEquals(user.getPassword(), "password");
assertEquals(user.getVeganMeal(), 3);
assertEquals(user, testUser);
assertEquals(user.getFriends(), new ArrayList<User>());
}
@Test
public void toStringTest() {
User user = new User(1L, "greenify", "password", 3);
assertEquals("User(id=1, name=greenify, password=password, veganMeal=3, friends=[])", user.toString());
}
@Test
public void equalsNullTest(){
User first = new User(1L, "greenify", "password", 0);
User second = null;
assertNotEquals(first, second);
assertEquals("User(id=1, name=greenify, password=password, veganMeal=3)", user.toString());
}
@Test
@@ -61,12 +46,6 @@ public class UserTest {
assertFalse(first.equals(second));
}
@Test
public void sameEqualsTest(){
User user = new User(6l, "Merel", "password", 0);
assertEquals(user, user);
}
@Test
public void instanceOfTest() {
User first = new User();
@@ -81,45 +60,5 @@ public class UserTest {
assertEquals(first, second);
assertEquals(first.hashCode(), second.hashCode());
}
@Test
public void addFriendTest(){
User user = new User(1l, "user", "friends", 0);
User friend = new User(2l, "friend", "friends", 0);
assertEquals(user.getFriends(), new ArrayList<User>());
user.addFriend(friend);
ArrayList<User> list = new ArrayList<User>();
list.add(friend);
assertEquals(user.getFriends(), list);
}
@Test
public void addYourselfTest(){
User user = new User(1l, "user", "friends", 0);
assertThrows(ApplicationException.class, () -> {
user.addFriend(user);
});
assertEquals(user.getFriends(), new ArrayList<User>());
}
@Test
public void JsonTest(){
User user = new User(1l, "user", "friends", 0);
User friend = new User(2l, "friend", "friends", 0);
assertEquals(user.getFriends(), new ArrayList<User>());
user.addFriend(friend);
ArrayList<User> list = new ArrayList<User>();
list.add(friend);
assertEquals(user.getFriends(), list);
System.out.println(user.toString());
}
@Test
public void friendsToStringTest(){
User user = new User(4l, "user", "pass", 0);
User friend = new User (5l, "friend", "pass", 0);
user.addFriend(friend);
System.out.println(user.friendsToString());
}
}

View File

@@ -18,9 +18,6 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
public class UserServiceTest {
@TestConfiguration
@@ -42,17 +39,14 @@ public class UserServiceTest {
*/
@Before
public void setUp() {
User user = new User(1L, "user", "password", 0);
when(userRepository.findByName(user.getName()))
.thenReturn(user);
User friend = new User(2L, "friend", "password", 0);
when(userRepository.findByName(friend.getName()))
.thenReturn(friend);
User alex = new User(1L, "alex", "password", 0);
when(userRepository.findByName(alex.getName()))
.thenReturn(alex);
}
@Test
public void validLoginTest() {
String name = "user";
String name = "alex";
String password = "password";
UserDto found = userService.loginUser(name, password);
assertEquals(found.getName(), name);
@@ -60,8 +54,8 @@ public class UserServiceTest {
@Test
public void userRegisterTest() {
User test = new User(1L, "name", "password", 0);
UserDto registered = userService.registerUser(test.getName(), test.getPassword());
User user = new User(1L, "name", "password", 0);
UserDto registered = userService.registerUser(user.getName(), user.getPassword());
assertEquals(registered.getName(), "name");
}
@@ -72,17 +66,9 @@ public class UserServiceTest {
@Test
public void invalidLoginTest() {
User user = null;
assertThrows(ApplicationException.class, () -> {
userService.loginUser(null, null);
});
}
@Test
public void addFriendTest() {
userService.addFriend(1L,"user", "friend");
List<User> test = new ArrayList<User>();
test.add(userRepository.findByName("friend"));
assertEquals(userRepository.findByName("user").getFriends(), test);
}
}