diff --git a/src/Client/src/main/java/greenify/client/rest/UserService.java b/src/Client/src/main/java/greenify/client/rest/UserService.java index c993e2d..ba726e5 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -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; + } } diff --git a/src/Server/src/main/java/greenify/server/data/model/User.java b/src/Server/src/main/java/greenify/server/data/model/User.java index 39f385b..fd916be 100644 --- a/src/Server/src/main/java/greenify/server/data/model/User.java +++ b/src/Server/src/main/java/greenify/server/data/model/User.java @@ -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 footPrintInputs = new HashMap<>(); + @ManyToMany + @JoinColumn + private Collection friends; + public User() {} /** @@ -49,6 +53,7 @@ public class User { this.name = name; this.password = password; this.setFootPrintInputs(InputValidator.getDefaultValues()); + this.friends = new ArrayList(); } /** @@ -103,6 +108,24 @@ public class User { this.footPrintInputs = footPrintInputs; } + public ArrayList getFriends() { + return (ArrayList)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) { diff --git a/src/Server/src/main/java/greenify/server/service/UserService.java b/src/Server/src/main/java/greenify/server/service/UserService.java index 61ff0d9..4449367 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -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 diff --git a/src/Server/src/test/java/greenify/server/data/model/UserTest.java b/src/Server/src/test/java/greenify/server/data/model/UserTest.java index 3a27fb8..dd22b40 100644 --- a/src/Server/src/test/java/greenify/server/data/model/UserTest.java +++ b/src/Server/src/test/java/greenify/server/data/model/UserTest.java @@ -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()); + } + + @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 test = new ArrayList(); + test.add(second); + assertEquals(first.getFriends(), test); + } + + @Test + public void addYourselfTest() { + User test = new User(1L, "greenify", "password"); + assertEquals(test.getFriends(), new ArrayList()); + 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}]"); + } } diff --git a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java index bf06ce8..5c07c35 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -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 test = new ArrayList(); + 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}]"); + + } + }