Merge branch 'AddFriends' into 'master'

Add methods and tests for friends.

See merge request cse1105/2018-2019/oopp-group-43/template!43
This commit is contained in:
Merel Steenbergen
2019-03-26 14:29:14 +00:00
5 changed files with 150 additions and 6 deletions

View File

@@ -94,4 +94,18 @@ public class UserService {
.encode().toUri(), String.class);
return result;
}
@SuppressWarnings("Duplicates")
public String addFriend(String name, String friend) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/addFriend")
.queryParam("name", name)
.queryParam("friend",friend);
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
String result = this.restTemplate.getForObject(builder.build()
.encode().toUri(), String.class);
return result;
}
}

View File

@@ -1,18 +1,17 @@
package greenify.server.data.model;
import greenify.common.ApplicationException;
import greenify.server.InputValidator;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.*;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
@Entity
@@ -21,6 +20,7 @@ import javax.validation.constraints.NotNull;
public class User {
@Id
@NotNull
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@@ -36,6 +36,10 @@ public class User {
@ElementCollection
private Map<String,String> footPrintInputs = new HashMap<>();
@ManyToMany
@JoinColumn
private Collection<User> friends;
public User() {}
/**
@@ -49,6 +53,7 @@ public class User {
this.name = name;
this.password = password;
this.setFootPrintInputs(InputValidator.getDefaultValues());
this.friends = new ArrayList<User>();
}
/**
@@ -103,6 +108,24 @@ public class User {
this.footPrintInputs = footPrintInputs;
}
public ArrayList<User> getFriends() {
return (ArrayList<User>)this.friends;
}
/**
* Adds a friend to the friendslist of the user.
* @param user the friend you want to add.
*/
public void addFriend(User user) {
if (user.equals(this)) {
throw new ApplicationException("Cannot add yourself as a friend");
}
else {
friends.add(user);
System.out.print("Friend added!");
}
}
public void setFootPrint(Float footPrint) {
this.footPrint = footPrint;
}
@@ -118,6 +141,21 @@ public class User {
+ this.password + ")";
}
/**
* Returns the name and score of the friends in JSON. Needed for the leaderboard.
* @return a JSON object of the friendlist with only names and scores.
*/
public String friendsToString(){
String result = "friends=[";
for (User u : friends) {
result += "{name=" + u.getName() + ", footprint=" + u.getFootPrint() + "}, ";
}
if (result.endsWith(", ")) {
return result.substring(0, result.lastIndexOf(",")) + "]";
}
return result + "]";
}
@Override
public boolean equals(Object other) {
if (other instanceof User) {

View File

@@ -59,6 +59,31 @@ public class UserService {
return new UserDto(user.getId(), user.getName());
}
/**
* 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.
* @return a userDTO of the logged in user
*/
public void addFriend(String name, String friend) {
User user = userRepository.findByName(name);
User add = userRepository.findByName(friend);
if (add == null) {
throw new ApplicationException("User does not exist");
}
user.addFriend(add);
}
/**
* Returns the friendlist of the user in JSON.
* @param name the username of the user
* @return a userDTO of the logged in user
*/
public String getLeaderboard(String name) {
User user = userRepository.findByName(name);
return user.friendsToString();
}
/**
* The method sets input value.
* @param name of the user

View File

@@ -2,10 +2,14 @@ package greenify.server.data.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import greenify.common.ApplicationException;
import org.junit.Test;
import java.util.ArrayList;
public class UserTest {
@Test
public void setAndGetTest() {
@@ -78,5 +82,42 @@ public class UserTest {
assertEquals(first, second);
assertEquals(first.hashCode(), second.hashCode());
}
@Test
public void getFriendEmpty() {
User first = new User(1L, "greenify", "password");
User second = new User(1L, "merel", "password");
assertEquals(first.getFriends(), second.getFriends());
assertEquals(first.getFriends(), new ArrayList<User>());
}
@Test
public void addFriendTest() {
User first = new User(1L, "greenify", "password");
User second = new User(1L, "merel", "password");
assertEquals(first.getFriends(), second.getFriends());
first.addFriend(second);
ArrayList<User> test = new ArrayList<User>();
test.add(second);
assertEquals(first.getFriends(), test);
}
@Test
public void addYourselfTest() {
User test = new User(1L, "greenify", "password");
assertEquals(test.getFriends(), new ArrayList<User>());
assertThrows(ApplicationException.class, () -> {
test.addFriend(test);
});
}
@Test
public void friendsToStringTest() {
User first = new User(1L, "greenify", "password");
User second = new User(1L, "merel", "password");
first.addFriend(second);
assertEquals(first.friendsToString(), "friends=[{name=merel, footprint=0.0}]");
}
}

View File

@@ -18,6 +18,8 @@ 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;
@RunWith(SpringRunner.class)
public class UserServiceTest {
@TestConfiguration
@@ -45,6 +47,9 @@ public class UserServiceTest {
User alex = new User(1L, "alex", "password");
when(userRepository.findByName(alex.getName()))
.thenReturn(alex);
User lola = new User(2L, "lola", "password");
when(userRepository.findByName(lola.getName()))
.thenReturn(lola);
}
@Test
@@ -157,4 +162,25 @@ public class UserServiceTest {
userService.loginUser(null, null);
});
}
@Test
public void addFriendTest() {
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);
}
@Test
public void leaderboardTest() {
User alex = userRepository.findByName("alex");
User lola = userRepository.findByName("lola");
userService.addFriend("alex", "lola");
assertEquals(userService.getLeaderboard("alex"), "friends=[{name=lola, footprint=0.0}]");
}
}