Merge branch 'AddFriends' into 'master'

Add basic friends

See merge request cse1105/2018-2019/oopp-group-43/template!37
This commit is contained in:
Mika Wauben
2019-03-23 12:09:33 +00:00
6 changed files with 167 additions and 12 deletions

View File

@@ -99,6 +99,26 @@ public class UserService {
return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDto.class); 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") @RequestMapping("/userData")
public int getVeganData(@RequestParam(value = "veganMeal") int veganMeal) { public int getVeganData(@RequestParam(value = "veganMeal") int veganMeal) {
return veganMeal; return veganMeal;

View File

@@ -1,7 +1,11 @@
package greenify.server.data.model; package greenify.server.data.model;
import greenify.common.ApplicationException;
import greenify.server.Application;
import lombok.Data; import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
@@ -27,6 +31,8 @@ public class User {
private int veganMeal; private int veganMeal;
private ArrayList<User> friends;
public User() {} public User() {}
/** /**
@@ -41,6 +47,7 @@ public class User {
this.name = name; this.name = name;
this.password = password; this.password = password;
this.veganMeal = veganMeal; this.veganMeal = veganMeal;
this.friends = new ArrayList<User>();
} }
/** /**
@@ -91,6 +98,29 @@ public class User {
this.veganMeal = veganMeal; 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. * Returns a human readable object. It's in JSON.
@@ -98,8 +128,12 @@ public class User {
*/ */
@Override @Override
public String toString() { public String toString() {
return "User(id=" + this.id + ", name=" + this.name + ", password=" String result = "User(id=" + this.id + ", name=" + this.name + ", password="
+ this.password + ", veganMeal=" + this.veganMeal + ")"; + this.password + ", veganMeal=" + this.veganMeal + ", friends=[";
result += friendsToString() + "])";
// result += ")";
return result;
} }
@Override @Override

View File

@@ -32,8 +32,6 @@ public class UserController {
return userService.loginUser(name, password); return userService.loginUser(name, password);
} }
/** /**
* adds a vegetarian meal to the user. * adds a vegetarian meal to the user.
* @param id the id of the user * @param id the id of the user
@@ -46,4 +44,18 @@ public class UserController {
//addVeganMeal method of the userService //addVeganMeal method of the userService
userService.addVeganMeal(id, name); 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,6 +79,20 @@ public class UserService {
+ ", name=" + user.getName() + ")"); + ", 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") @GetMapping(path = "/all")
@ResponseBody @ResponseBody
public Iterable<User> getAllUsers() { public Iterable<User> getAllUsers() {

View File

@@ -1,11 +1,18 @@
package greenify.server.data.model; package greenify.server.data.model;
import static greenify.server.data.model.User.*;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; 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 static org.junit.jupiter.api.Assertions.assertTrue;
import greenify.common.ApplicationException;
import greenify.server.Application;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList;
public class UserTest { public class UserTest {
@Test @Test
public void setAndGetTest() { public void setAndGetTest() {
@@ -20,12 +27,20 @@ public class UserTest {
assertEquals(user.getPassword(), "password"); assertEquals(user.getPassword(), "password");
assertEquals(user.getVeganMeal(), 3); assertEquals(user.getVeganMeal(), 3);
assertEquals(user, testUser); assertEquals(user, testUser);
assertEquals(user.getFriends(), new ArrayList<User>());
} }
@Test @Test
public void toStringTest() { public void toStringTest() {
User user = new User(1L, "greenify", "password", 3); User user = new User(1L, "greenify", "password", 3);
assertEquals("User(id=1, name=greenify, password=password, veganMeal=3)", user.toString()); 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);
} }
@Test @Test
@@ -46,6 +61,12 @@ public class UserTest {
assertFalse(first.equals(second)); assertFalse(first.equals(second));
} }
@Test
public void sameEqualsTest(){
User user = new User(6l, "Merel", "password", 0);
assertEquals(user, user);
}
@Test @Test
public void instanceOfTest() { public void instanceOfTest() {
User first = new User(); User first = new User();
@@ -60,5 +81,45 @@ public class UserTest {
assertEquals(first, second); assertEquals(first, second);
assertEquals(first.hashCode(), second.hashCode()); 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,6 +18,9 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
public class UserServiceTest { public class UserServiceTest {
@TestConfiguration @TestConfiguration
@@ -39,14 +42,17 @@ public class UserServiceTest {
*/ */
@Before @Before
public void setUp() { public void setUp() {
User alex = new User(1L, "alex", "password", 0); User user = new User(1L, "user", "password", 0);
when(userRepository.findByName(alex.getName())) when(userRepository.findByName(user.getName()))
.thenReturn(alex); .thenReturn(user);
User friend = new User(2L, "friend", "password", 0);
when(userRepository.findByName(friend.getName()))
.thenReturn(friend);
} }
@Test @Test
public void validLoginTest() { public void validLoginTest() {
String name = "alex"; String name = "user";
String password = "password"; String password = "password";
UserDto found = userService.loginUser(name, password); UserDto found = userService.loginUser(name, password);
assertEquals(found.getName(), name); assertEquals(found.getName(), name);
@@ -54,8 +60,8 @@ public class UserServiceTest {
@Test @Test
public void userRegisterTest() { public void userRegisterTest() {
User user = new User(1L, "name", "password", 0); User test = new User(1L, "name", "password", 0);
UserDto registered = userService.registerUser(user.getName(), user.getPassword()); UserDto registered = userService.registerUser(test.getName(), test.getPassword());
assertEquals(registered.getName(), "name"); assertEquals(registered.getName(), "name");
} }
@@ -66,9 +72,17 @@ public class UserServiceTest {
@Test @Test
public void invalidLoginTest() { public void invalidLoginTest() {
User user = null;
assertThrows(ApplicationException.class, () -> { assertThrows(ApplicationException.class, () -> {
userService.loginUser(null, null); 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);
}
} }