EDIT: Try to fix server, again

This commit is contained in:
Merel Steenbergen
2019-03-22 18:44:59 +01:00
parent 6323fd06eb
commit e69e08ad15
3 changed files with 56 additions and 2 deletions

View File

@@ -99,6 +99,26 @@ 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

@@ -111,14 +111,27 @@ public class User {
}
}
public String friendsToString(){
String result = "";
for(User u : friends){
result += u.getId() + ", ";
}
result = result.substring(0, result.lastIndexOf(","));
return result;
}
/**
* Returns a human readable object. It's in JSON.
* @return the JSON form of the object.
*/
@Override
public String toString() {
return "User(id=" + this.id + ", name=" + this.name + ", password="
+ this.password + ", veganMeal=" + this.veganMeal + ")";
String result = "User(id=" + this.id + ", name=" + this.name + ", password="
+ this.password + ", veganMeal=" + this.veganMeal + ", friends=[";
result += friendsToString() + "])";
// result += ")";
return result;
}
@Override

View File

@@ -27,6 +27,7 @@ public class UserTest {
assertEquals(user.getPassword(), "password");
assertEquals(user.getVeganMeal(), 3);
assertEquals(user, testUser);
assertEquals(user.getFriends(), new ArrayList<User>());
}
@Test
@@ -100,5 +101,25 @@ public class UserTest {
});
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());
}
}